diff options
author | Elliott Hughes <enh@google.com> | 2020-08-12 14:25:41 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2020-08-12 15:52:14 -0700 |
commit | 7cebf835f310650f67b254295a685918681656fc (patch) | |
tree | daf485447fccb691750149c465b2c629adf6e305 /tests/stdlib_test.cpp | |
parent | a4110def5cec22306313a34fff64b5810c9b1792 (diff) |
Various coverage improvements.
Mostly from extra test cases, but also:
* Move the fgets size < 0 assertion into fgets.
* Use ELF aliases for strtoq/strtouq rather than duplicating code.
* Don't check uname() succeeded, since it can't fail.
Test: treehugger
Change-Id: I2e6b3b88b0a3eb16bd68be68b9bc9f40d8043291
Diffstat (limited to 'tests/stdlib_test.cpp')
-rw-r--r-- | tests/stdlib_test.cpp | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/tests/stdlib_test.cpp b/tests/stdlib_test.cpp index 3f1ec866d..c7b2ad89a 100644 --- a/tests/stdlib_test.cpp +++ b/tests/stdlib_test.cpp @@ -800,10 +800,25 @@ static void CheckStrToInt(T fn(const char* s, char** end, int base)) { ASSERT_EQ(T(0), fn("123", &end_p, 37)); ASSERT_EQ(EINVAL, errno); + // Both leading + or - are always allowed (even for the strtou* family). + ASSERT_EQ(T(-123), fn("-123", &end_p, 10)); + ASSERT_EQ(T(123), fn("+123", &end_p, 10)); + // If we see "0x" *not* followed by a hex digit, we shouldn't swallow the 'x'. ASSERT_EQ(T(0), fn("0xy", &end_p, 16)); ASSERT_EQ('x', *end_p); + // Hexadecimal (both the 0x and the digits) is case-insensitive. + ASSERT_EQ(T(0xab), fn("0xab", &end_p, 0)); + ASSERT_EQ(T(0xab), fn("0Xab", &end_p, 0)); + ASSERT_EQ(T(0xab), fn("0xAB", &end_p, 0)); + ASSERT_EQ(T(0xab), fn("0XAB", &end_p, 0)); + ASSERT_EQ(T(0xab), fn("0xAb", &end_p, 0)); + ASSERT_EQ(T(0xab), fn("0XAb", &end_p, 0)); + + // Octal lives! (Sadly.) + ASSERT_EQ(T(0666), fn("0666", &end_p, 0)); + if (std::numeric_limits<T>::is_signed) { // Minimum (such as -128). std::string min{std::to_string(std::numeric_limits<T>::min())}; @@ -878,6 +893,18 @@ TEST(stdlib, strtoumax_smoke) { CheckStrToInt(strtoumax); } +TEST(stdlib, atoi) { + // Implemented using strtol in bionic, so extensive testing unnecessary. + ASSERT_EQ(123, atoi("123four")); + ASSERT_EQ(0, atoi("hello")); +} + +TEST(stdlib, atol) { + // Implemented using strtol in bionic, so extensive testing unnecessary. + ASSERT_EQ(123L, atol("123four")); + ASSERT_EQ(0L, atol("hello")); +} + TEST(stdlib, abs) { ASSERT_EQ(INT_MAX, abs(-INT_MAX)); ASSERT_EQ(INT_MAX, abs(INT_MAX)); |