diff options
Diffstat (limited to 'tests/string_test.cpp')
-rw-r--r-- | tests/string_test.cpp | 53 |
1 files changed, 40 insertions, 13 deletions
diff --git a/tests/string_test.cpp b/tests/string_test.cpp index 8ec2928cc..ad0040aa9 100644 --- a/tests/string_test.cpp +++ b/tests/string_test.cpp @@ -14,12 +14,14 @@ * limitations under the License. */ -#include <gtest/gtest.h> +#define _GNU_SOURCE 1 + +#include <string.h> #include <errno.h> +#include <gtest/gtest.h> #include <malloc.h> #include <math.h> -#include <string.h> #include "buffer_tests.h" @@ -72,28 +74,34 @@ TEST(string, strerror_concurrent) { #endif // __BIONIC__ } -TEST(string, strerror_r) { -#if defined(__BIONIC__) // glibc's strerror_r doesn't even have the same signature as the POSIX one. +TEST(string, gnu_strerror_r) { char buf[256]; + // Note that glibc doesn't necessarily write into the buffer. + // Valid. - ASSERT_EQ(0, strerror_r(0, buf, sizeof(buf))); + ASSERT_STREQ("Success", strerror_r(0, buf, sizeof(buf))); +#if defined(__BIONIC__) ASSERT_STREQ("Success", buf); - ASSERT_EQ(0, strerror_r(1, buf, sizeof(buf))); +#endif + ASSERT_STREQ("Operation not permitted", strerror_r(1, buf, sizeof(buf))); +#if defined(__BIONIC__) ASSERT_STREQ("Operation not permitted", buf); +#endif // Invalid. - ASSERT_EQ(0, strerror_r(-1, buf, sizeof(buf))); + ASSERT_STREQ("Unknown error -1", strerror_r(-1, buf, sizeof(buf))); ASSERT_STREQ("Unknown error -1", buf); - ASSERT_EQ(0, strerror_r(1234, buf, sizeof(buf))); + ASSERT_STREQ("Unknown error 1234", strerror_r(1234, buf, sizeof(buf))); ASSERT_STREQ("Unknown error 1234", buf); // Buffer too small. - ASSERT_EQ(-1, strerror_r(0, buf, 2)); - ASSERT_EQ(ERANGE, errno); -#else // __BIONIC__ - GTEST_LOG_(INFO) << "This test does nothing.\n"; -#endif // __BIONIC__ + errno = 0; + memset(buf, 0, sizeof(buf)); + ASSERT_EQ(buf, strerror_r(4567, buf, 2)); + ASSERT_STREQ("U", buf); + // The GNU strerror_r doesn't set errno (the POSIX one sets it to ERANGE). + ASSERT_EQ(0, errno); } TEST(string, strsignal) { @@ -1274,3 +1282,22 @@ TEST(string, strchr_align) { TEST(string, strchr_overread) { RunSingleBufferOverreadTest(DoStrchrTest); } + +static void TestBasename(const char* in, const char* expected_out) { + errno = 0; + const char* out = basename(in); + ASSERT_STREQ(expected_out, out) << in; + ASSERT_EQ(0, errno) << in; +} + +TEST(string, __gnu_basename) { + TestBasename("", ""); + TestBasename("/usr/lib", "lib"); + TestBasename("/usr/", ""); + TestBasename("usr", "usr"); + TestBasename("/", ""); + TestBasename(".", "."); + TestBasename("..", ".."); + TestBasename("///", ""); + TestBasename("//usr//lib//", ""); +} |