X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2Ftest%2FFBStringTest.cpp;h=9f51947af6c855bc88f92f749818712523b18cbf;hb=eba5e7f2236a0950957a8a247a0e83385149d13c;hp=15783ea1b6d60dd6d9b8af1dc074a566718dcf66;hpb=c5ec113681e195a0802b0f7c32c6b0e5e6daad46;p=folly.git diff --git a/folly/test/FBStringTest.cpp b/folly/test/FBStringTest.cpp index 15783ea1..9f51947a 100644 --- a/folly/test/FBStringTest.cpp +++ b/folly/test/FBStringTest.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2015 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,24 +21,24 @@ #include #include - -#include -#include #include +#include +#include + #include #include -#include - -#include -#include +#include #include #include -#include +#include +#include using namespace std; using namespace folly; +namespace { + static const int seed = folly::randomNumberSeed(); typedef boost::mt19937 RandomT; static RandomT rng(seed); @@ -75,6 +75,7 @@ std::list RandomList(unsigned int maxSize) { } return lst; } +} // namespace //////////////////////////////////////////////////////////////////////////////// // Tests begin here @@ -142,12 +143,16 @@ template void clause11_21_4_2_h(String & test) { EXPECT_EQ(test, s2); // Constructor from other iterators std::list lst; - for (auto c : test) lst.push_back(c); + for (auto c : test) { + lst.push_back(c); + } String s3(lst.begin(), lst.end()); EXPECT_EQ(test, s3); // Constructor from wchar_t iterators std::list lst1; - for (auto c : test) lst1.push_back(c); + for (auto c : test) { + lst1.push_back(c); + } String s4(lst1.begin(), lst1.end()); EXPECT_EQ(test, s4); // Constructor from wchar_t pointers @@ -207,7 +212,8 @@ template void clause11_21_4_2_lprime(String & test) { } template void clause11_21_4_2_m(String & test) { // Assignment from char - test = random('a', 'z'); + using value_type = typename String::value_type; + test = random(static_cast('a'), static_cast('z')); } template void clause11_21_4_2_n(String & test) { // Assignment from initializer_list @@ -247,8 +253,11 @@ template void clause11_21_4_4(String & test) { // exercise empty string empty("empty"); string notempty("not empty"); - if (test.empty()) test = String(empty.begin(), empty.end()); - else test = String(notempty.begin(), notempty.end()); + if (test.empty()) { + test = String(empty.begin(), empty.end()); + } else { + test = String(notempty.begin(), notempty.end()); + } } template void clause11_21_4_5(String & test) { @@ -573,10 +582,10 @@ template void clause11_21_4_6_6(String & test) { template void clause11_21_4_6_7(String & test) { std::vector vec(random(0, maxString)); - test.copy( - &vec[0], - vec.size(), - random(0, test.size())); + if (vec.empty()) { + return; + } + test.copy(vec.data(), vec.size(), random(0, test.size())); } template void clause11_21_4_6_8(String & test) { @@ -592,7 +601,7 @@ template void clause11_21_4_7_1(String & test) { // exercise get_allocator() String s; randomString(&s, maxString); - assert(test.get_allocator() == s.get_allocator()); + DCHECK(test.get_allocator() == s.get_allocator()); } template void clause11_21_4_7_2_a(String & test) { @@ -826,8 +835,11 @@ template void clause11_21_4_7_9_a(String & test) { String s; randomString(&s, maxString); int tristate = test.compare(s); - if (tristate > 0) tristate = 1; - else if (tristate < 0) tristate = 2; + if (tristate > 0) { + tristate = 1; + } else if (tristate < 0) { + tristate = 2; + } Num2String(test, tristate); } @@ -838,8 +850,11 @@ template void clause11_21_4_7_9_b(String & test) { random(0, test.size()), random(0, test.size()), s); - if (tristate > 0) tristate = 1; - else if (tristate < 0) tristate = 2; + if (tristate > 0) { + tristate = 1; + } else if (tristate < 0) { + tristate = 2; + } Num2String(test, tristate); } @@ -852,8 +867,11 @@ template void clause11_21_4_7_9_c(String & test) { str, random(0, str.size()), random(0, str.size())); - if (tristate > 0) tristate = 1; - else if (tristate < 0) tristate = 2; + if (tristate > 0) { + tristate = 1; + } else if (tristate < 0) { + tristate = 2; + } Num2String(test, tristate); } @@ -861,9 +879,12 @@ template void clause11_21_4_7_9_d(String & test) { String s; randomString(&s, maxString); int tristate = test.compare(s.c_str()); - if (tristate > 0) tristate = 1; - else if (tristate < 0) tristate = 2; - Num2String(test, tristate); + if (tristate > 0) { + tristate = 1; + } else if (tristate < 0) { + tristate = 2; + } + Num2String(test, tristate); } template void clause11_21_4_7_9_e(String & test) { @@ -874,8 +895,11 @@ template void clause11_21_4_7_9_e(String & test) { random(0, test.size()), str.c_str(), random(0, str.size())); - if (tristate > 0) tristate = 1; - else if (tristate < 0) tristate = 2; + if (tristate > 0) { + tristate = 1; + } else if (tristate < 0) { + tristate = 2; + } Num2String(test, tristate); } @@ -982,9 +1006,11 @@ template void clause11_21_4_8_9_a(String & test) { TEST(FBString, testAllClauses) { EXPECT_TRUE(1) << "Starting with seed: " << seed; std::string r; - std::wstring wr; folly::fbstring c; +#if FOLLY_HAVE_WCHAR_SUPPORT + std::wstring wr; folly::basic_fbstring wc; +#endif int count = 0; auto l = [&](const char * const clause, @@ -992,12 +1018,17 @@ TEST(FBString, testAllClauses) { void(*f_fbstring)(folly::fbstring&), void(*f_wfbstring)(folly::basic_fbstring&)) { do { - if (1) {} else EXPECT_TRUE(1) << "Testing clause " << clause; + if (true) { + } else { + EXPECT_TRUE(1) << "Testing clause " << clause; + } randomString(&r); c = r; EXPECT_EQ(c, r); +#if FOLLY_HAVE_WCHAR_SUPPORT wr = std::wstring(r.begin(), r.end()); wc = folly::basic_fbstring(wr.c_str()); +#endif auto localSeed = seed + count; rng = RandomT(localSeed); f_string(r); @@ -1007,16 +1038,21 @@ TEST(FBString, testAllClauses) { << "Lengths: " << r.size() << " vs. " << c.size() << "\nReference: '" << r << "'" << "\nActual: '" << c.data()[0] << "'"; +#if FOLLY_HAVE_WCHAR_SUPPORT rng = RandomT(localSeed); f_wfbstring(wc); int wret = wcslen(wc.c_str()); - char mb[wret+1]; - int ret = wcstombs(mb, wc.c_str(), sizeof(mb)); - if (ret == wret) mb[wret] = '\0'; + auto mbv = std::vector(wret + 1); + auto mb = mbv.data(); + int ret = wcstombs(mb, wc.c_str(), wret + 1); + if (ret == wret) { + mb[wret] = '\0'; + } const char *mc = c.c_str(); std::string one(mb); std::string two(mc); EXPECT_EQ(one, two); +#endif } while (++count % 100 != 0); }; @@ -1116,7 +1152,7 @@ TEST(FBString, testAllClauses) { } TEST(FBString, testGetline) { - fbstring s1 = "\ + string s1 = "\ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras accumsan \n\ elit ut urna consectetur in sagittis mi auctor. Nulla facilisi. In nec \n\ dolor leo, vitae imperdiet neque. Donec ut erat mauris, a faucibus \n\ @@ -1134,28 +1170,17 @@ massa, ut accumsan magna. Donec imperdiet tempor nisi et \n\ laoreet. Phasellus lectus quam, ultricies ut tincidunt in, dignissim \n\ id eros. Mauris vulputate tortor nec neque pellentesque sagittis quis \n\ sed nisl. In diam lacus, lobortis ut posuere nec, ornare id quam."; - char f[] = "/tmp/fbstring_testing.XXXXXX"; - int fd = mkstemp(f); - EXPECT_TRUE(fd > 0); - if (fd > 0) { - close(fd); // Yeah - std::ofstream out(f); - if (!(out << s1)) { - EXPECT_TRUE(0) << "Couldn't write to temp file."; - return; - } - } + vector v; boost::split(v, s1, boost::is_any_of("\n")); { - ifstream input(f); + istringstream input(s1); fbstring line; FOR_EACH (i, v) { EXPECT_TRUE(!getline(input, line).fail()); EXPECT_EQ(line, *i); } } - unlink(f); } TEST(FBString, testMoveCtor) { @@ -1211,7 +1236,7 @@ TEST(FBString, testMoveOperatorPlusRhs) { // other than libstdc++. Someday if we deem it important to present // identical undefined behavior for other platforms, we can re-visit this. TEST(FBString, testConstructionFromLiteralZero) { - EXPECT_THROW(fbstring s(0), std::logic_error); + EXPECT_THROW(fbstring s(nullptr), std::logic_error); } TEST(FBString, testFixedBugs) { @@ -1259,6 +1284,32 @@ TEST(FBString, testFixedBugs) { copy.push_back('b'); EXPECT_GE(copy.capacity(), 1); } + { // D2813713 + fbstring s1("a"); + s1.reserve(8); // Trigger the optimized code path. + auto test1 = '\0' + std::move(s1); + EXPECT_EQ(2, test1.size()); + + fbstring s2(1, '\0'); + s2.reserve(8); + auto test2 = "a" + std::move(s2); + EXPECT_EQ(2, test2.size()); + } + { // D3698862 + EXPECT_EQ(fbstring().find(fbstring(), 4), fbstring::npos); + } + if (usingJEMalloc()) { // D4355440 + fbstring str(1337, 'f'); + str.reserve(3840); + EXPECT_NE(str.capacity(), 3840); + + struct { + std::atomic refCount_; + } dummyRefCounted; + EXPECT_EQ( + str.capacity(), + goodMallocSize(3840) - sizeof(dummyRefCounted) - sizeof(char)); + } } TEST(FBString, findWithNpos) { @@ -1277,6 +1328,20 @@ TEST(FBString, testHash) { EXPECT_NE(hashfunc(a), hashfunc(b)); } +#if FOLLY_HAVE_WCHAR_SUPPORT +TEST(FBString, testHashChar16) { + using u16fbstring = folly::basic_fbstring; + u16fbstring a; + u16fbstring b; + a.push_back(0); + a.push_back(1); + b.push_back(0); + b.push_back(2); + std::hash hashfunc; + EXPECT_NE(hashfunc(a), hashfunc(b)); +} +#endif + TEST(FBString, testFrontBack) { fbstring str("hello"); EXPECT_EQ(str.front(), 'h'); @@ -1381,7 +1446,7 @@ struct TestStructStringAllocator : std::allocator { } }; -} // anon namespace +} // namespace TEST(FBStringCtorTest, DefaultInitStructDefaultAlloc) { TestStructDefaultAllocator t1 { }; @@ -1395,8 +1460,218 @@ TEST(FBStringCtorTest, DefaultInitStructAlloc) { EXPECT_EQ(allocatorConstructedCount.load(), 1); } -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - gflags::ParseCommandLineFlags(&argc, &argv, true); - return RUN_ALL_TESTS(); -} +TEST(FBStringCtorTest, NullZeroConstruction) { + char* p = nullptr; + int n = 0; + folly::fbstring f(p, n); + EXPECT_EQ(f.size(), 0); +} + +// Tests for the comparison operators. I use EXPECT_TRUE rather than EXPECT_LE +// because what's under test is the operator rather than the relation between +// the objects. + +TEST(FBString, compareToStdString) { + using folly::fbstring; + using namespace std::string_literals; + auto stdA = "a"s; + auto stdB = "b"s; + fbstring fbA("a"); + fbstring fbB("b"); + EXPECT_TRUE(stdA == fbA); + EXPECT_TRUE(fbB == stdB); + EXPECT_TRUE(stdA != fbB); + EXPECT_TRUE(fbA != stdB); + EXPECT_TRUE(stdA < fbB); + EXPECT_TRUE(fbA < stdB); + EXPECT_TRUE(stdB > fbA); + EXPECT_TRUE(fbB > stdA); + EXPECT_TRUE(stdA <= fbB); + EXPECT_TRUE(fbA <= stdB); + EXPECT_TRUE(stdA <= fbA); + EXPECT_TRUE(fbA <= stdA); + EXPECT_TRUE(stdB >= fbA); + EXPECT_TRUE(fbB >= stdA); + EXPECT_TRUE(stdB >= fbB); + EXPECT_TRUE(fbB >= stdB); +} + +TEST(U16FBString, compareToStdU16String) { + using folly::basic_fbstring; + using namespace std::string_literals; + auto stdA = u"a"s; + auto stdB = u"b"s; + basic_fbstring fbA(u"a"); + basic_fbstring fbB(u"b"); + EXPECT_TRUE(stdA == fbA); + EXPECT_TRUE(fbB == stdB); + EXPECT_TRUE(stdA != fbB); + EXPECT_TRUE(fbA != stdB); + EXPECT_TRUE(stdA < fbB); + EXPECT_TRUE(fbA < stdB); + EXPECT_TRUE(stdB > fbA); + EXPECT_TRUE(fbB > stdA); + EXPECT_TRUE(stdA <= fbB); + EXPECT_TRUE(fbA <= stdB); + EXPECT_TRUE(stdA <= fbA); + EXPECT_TRUE(fbA <= stdA); + EXPECT_TRUE(stdB >= fbA); + EXPECT_TRUE(fbB >= stdA); + EXPECT_TRUE(stdB >= fbB); + EXPECT_TRUE(fbB >= stdB); +} + +TEST(U32FBString, compareToStdU32String) { + using folly::basic_fbstring; + using namespace std::string_literals; + auto stdA = U"a"s; + auto stdB = U"b"s; + basic_fbstring fbA(U"a"); + basic_fbstring fbB(U"b"); + EXPECT_TRUE(stdA == fbA); + EXPECT_TRUE(fbB == stdB); + EXPECT_TRUE(stdA != fbB); + EXPECT_TRUE(fbA != stdB); + EXPECT_TRUE(stdA < fbB); + EXPECT_TRUE(fbA < stdB); + EXPECT_TRUE(stdB > fbA); + EXPECT_TRUE(fbB > stdA); + EXPECT_TRUE(stdA <= fbB); + EXPECT_TRUE(fbA <= stdB); + EXPECT_TRUE(stdA <= fbA); + EXPECT_TRUE(fbA <= stdA); + EXPECT_TRUE(stdB >= fbA); + EXPECT_TRUE(fbB >= stdA); + EXPECT_TRUE(stdB >= fbB); + EXPECT_TRUE(fbB >= stdB); +} + +TEST(WFBString, compareToStdWString) { + using folly::basic_fbstring; + using namespace std::string_literals; + auto stdA = L"a"s; + auto stdB = L"b"s; + basic_fbstring fbA(L"a"); + basic_fbstring fbB(L"b"); + EXPECT_TRUE(stdA == fbA); + EXPECT_TRUE(fbB == stdB); + EXPECT_TRUE(stdA != fbB); + EXPECT_TRUE(fbA != stdB); + EXPECT_TRUE(stdA < fbB); + EXPECT_TRUE(fbA < stdB); + EXPECT_TRUE(stdB > fbA); + EXPECT_TRUE(fbB > stdA); + EXPECT_TRUE(stdA <= fbB); + EXPECT_TRUE(fbA <= stdB); + EXPECT_TRUE(stdA <= fbA); + EXPECT_TRUE(fbA <= stdA); + EXPECT_TRUE(stdB >= fbA); + EXPECT_TRUE(fbB >= stdA); + EXPECT_TRUE(stdB >= fbB); + EXPECT_TRUE(fbB >= stdB); +} + +// Same again, but with a more challenging input - a common prefix and different +// lengths. + +TEST(FBString, compareToStdStringLong) { + using folly::fbstring; + using namespace std::string_literals; + auto stdA = "1234567890a"s; + auto stdB = "1234567890ab"s; + fbstring fbA("1234567890a"); + fbstring fbB("1234567890ab"); + EXPECT_TRUE(stdA == fbA); + EXPECT_TRUE(fbB == stdB); + EXPECT_TRUE(stdA != fbB); + EXPECT_TRUE(fbA != stdB); + EXPECT_TRUE(stdA < fbB); + EXPECT_TRUE(fbA < stdB); + EXPECT_TRUE(stdB > fbA); + EXPECT_TRUE(fbB > stdA); + EXPECT_TRUE(stdA <= fbB); + EXPECT_TRUE(fbA <= stdB); + EXPECT_TRUE(stdA <= fbA); + EXPECT_TRUE(fbA <= stdA); + EXPECT_TRUE(stdB >= fbA); + EXPECT_TRUE(fbB >= stdA); + EXPECT_TRUE(stdB >= fbB); + EXPECT_TRUE(fbB >= stdB); +} + +TEST(U16FBString, compareToStdU16StringLong) { + using folly::basic_fbstring; + using namespace std::string_literals; + auto stdA = u"1234567890a"s; + auto stdB = u"1234567890ab"s; + basic_fbstring fbA(u"1234567890a"); + basic_fbstring fbB(u"1234567890ab"); + EXPECT_TRUE(stdA == fbA); + EXPECT_TRUE(fbB == stdB); + EXPECT_TRUE(stdA != fbB); + EXPECT_TRUE(fbA != stdB); + EXPECT_TRUE(stdA < fbB); + EXPECT_TRUE(fbA < stdB); + EXPECT_TRUE(stdB > fbA); + EXPECT_TRUE(fbB > stdA); + EXPECT_TRUE(stdA <= fbB); + EXPECT_TRUE(fbA <= stdB); + EXPECT_TRUE(stdA <= fbA); + EXPECT_TRUE(fbA <= stdA); + EXPECT_TRUE(stdB >= fbA); + EXPECT_TRUE(fbB >= stdA); + EXPECT_TRUE(stdB >= fbB); + EXPECT_TRUE(fbB >= stdB); +} + +#if FOLLY_HAVE_WCHAR_SUPPORT +TEST(U32FBString, compareToStdU32StringLong) { + using folly::basic_fbstring; + using namespace std::string_literals; + auto stdA = U"1234567890a"s; + auto stdB = U"1234567890ab"s; + basic_fbstring fbA(U"1234567890a"); + basic_fbstring fbB(U"1234567890ab"); + EXPECT_TRUE(stdA == fbA); + EXPECT_TRUE(fbB == stdB); + EXPECT_TRUE(stdA != fbB); + EXPECT_TRUE(fbA != stdB); + EXPECT_TRUE(stdA < fbB); + EXPECT_TRUE(fbA < stdB); + EXPECT_TRUE(stdB > fbA); + EXPECT_TRUE(fbB > stdA); + EXPECT_TRUE(stdA <= fbB); + EXPECT_TRUE(fbA <= stdB); + EXPECT_TRUE(stdA <= fbA); + EXPECT_TRUE(fbA <= stdA); + EXPECT_TRUE(stdB >= fbA); + EXPECT_TRUE(fbB >= stdA); + EXPECT_TRUE(stdB >= fbB); + EXPECT_TRUE(fbB >= stdB); +} + +TEST(WFBString, compareToStdWStringLong) { + using folly::basic_fbstring; + using namespace std::string_literals; + auto stdA = L"1234567890a"s; + auto stdB = L"1234567890ab"s; + basic_fbstring fbA(L"1234567890a"); + basic_fbstring fbB(L"1234567890ab"); + EXPECT_TRUE(stdA == fbA); + EXPECT_TRUE(fbB == stdB); + EXPECT_TRUE(stdA != fbB); + EXPECT_TRUE(fbA != stdB); + EXPECT_TRUE(stdA < fbB); + EXPECT_TRUE(fbA < stdB); + EXPECT_TRUE(stdB > fbA); + EXPECT_TRUE(fbB > stdA); + EXPECT_TRUE(stdA <= fbB); + EXPECT_TRUE(fbA <= stdB); + EXPECT_TRUE(stdA <= fbA); + EXPECT_TRUE(fbA <= stdA); + EXPECT_TRUE(stdB >= fbA); + EXPECT_TRUE(fbB >= stdA); + EXPECT_TRUE(stdB >= fbB); + EXPECT_TRUE(fbB >= stdB); +} +#endif