John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #include "aos/libc/dirname.h" |
Brian Silverman | af78486 | 2014-05-13 08:14:55 -0700 | [diff] [blame] | 2 | |
| 3 | #include <libgen.h> |
| 4 | |
| 5 | #include "gtest/gtest.h" |
| 6 | |
| 7 | namespace aos { |
| 8 | namespace libc { |
| 9 | namespace testing { |
| 10 | |
| 11 | // Tests the examples from the Linux man-pages release 3.44 dirname(3). |
| 12 | TEST(DirnameTest, ManPageExamples) { |
| 13 | EXPECT_EQ("/usr", Dirname("/usr/lib")); |
| 14 | EXPECT_EQ("/", Dirname("/usr/")); |
| 15 | EXPECT_EQ(".", Dirname("usr")); |
| 16 | EXPECT_EQ("/", Dirname("/")); |
| 17 | EXPECT_EQ(".", Dirname(".")); |
| 18 | EXPECT_EQ(".", Dirname("..")); |
| 19 | } |
| 20 | |
| 21 | // Tests that it handles multiple '/'s in a row correctly. |
| 22 | TEST(DirnameTest, MultipleSlashes) { |
| 23 | EXPECT_EQ("//usr", Dirname("//usr//lib")); |
| 24 | EXPECT_EQ("//usr/lib", Dirname("//usr/lib//bla")); |
| 25 | EXPECT_EQ("/", Dirname("//usr//")); |
| 26 | EXPECT_EQ(".", Dirname("usr//")); |
| 27 | EXPECT_EQ("/", Dirname("//")); |
| 28 | EXPECT_EQ(".", Dirname(".//")); |
| 29 | EXPECT_EQ(".", Dirname("..//")); |
| 30 | } |
| 31 | |
| 32 | TEST(DirnameTest, WeirdInputs) { |
| 33 | EXPECT_EQ(".", Dirname("")); |
| 34 | EXPECT_EQ(".", Dirname("...")); |
| 35 | } |
| 36 | |
| 37 | // Runs through a bunch of randomly constructed pathnames and makes sure it |
| 38 | // gives the same result as dirname(3). |
| 39 | TEST(DirnameTest, Random) { |
| 40 | static const char kTestBytes[] = "a0//.. "; |
| 41 | static const size_t kTestBytesSize = sizeof(kTestBytes) - 1; |
| 42 | static const size_t kTestPathSize = 6; |
| 43 | |
| 44 | ::std::string test_string(kTestPathSize, '\0'); |
| 45 | char test_path[kTestPathSize + 1]; |
| 46 | for (size_t i0 = 0; i0 < kTestBytesSize; ++i0) { |
| 47 | test_string[0] = kTestBytes[i0]; |
| 48 | for (size_t i1 = 0; i1 < kTestBytesSize; ++i1) { |
| 49 | // dirname(3) returns "//" in this case which is weird and our Dirname |
| 50 | // doesn't. |
| 51 | if (test_string[0] == '/' && kTestBytes[i1] == '/') continue; |
| 52 | |
| 53 | test_string[1] = kTestBytes[i1]; |
| 54 | for (size_t i2 = 0; i2 < kTestBytesSize; ++i2) { |
| 55 | test_string[2] = kTestBytes[i2]; |
| 56 | for (size_t i3 = 0; i3 < kTestBytesSize; ++i3) { |
| 57 | test_string[3] = kTestBytes[i3]; |
| 58 | for (size_t i4 = 0; i4 < kTestBytesSize; ++i4) { |
| 59 | test_string[4] = kTestBytes[i4]; |
| 60 | for (size_t i5 = 0; i5 < kTestBytesSize; ++i5) { |
| 61 | test_string[5] = kTestBytes[i5]; |
| 62 | |
| 63 | memcpy(test_path, test_string.c_str(), kTestPathSize); |
| 64 | test_path[kTestPathSize] = '\0'; |
| 65 | |
| 66 | SCOPED_TRACE("path is '" + test_string + "'"); |
| 67 | EXPECT_EQ(::std::string(dirname(test_path)), |
| 68 | Dirname(test_string)); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | } // namespace testing |
| 78 | } // namespace libc |
| 79 | } // namespace aos |