From 7cebf835f310650f67b254295a685918681656fc Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 12 Aug 2020 14:25:41 -0700 Subject: 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 --- tests/stdlib_test.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'tests/stdlib_test.cpp') 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::is_signed) { // Minimum (such as -128). std::string min{std::to_string(std::numeric_limits::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)); -- cgit v1.2.3