X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2Ftest%2FStringTest.cpp;h=76cf6ac40f78c0bdd8d300ed2dff5e98e945b02c;hb=b179601dfc42d8e3230d6477d7db8f3d5a8f64dc;hp=55ceefa03fa019c41916dfe8044ed26b0908a770;hpb=948ec49b5a2d1601eae65d19d8da3d2c9d6752e6;p=folly.git diff --git a/folly/test/StringTest.cpp b/folly/test/StringTest.cpp index 55ceefa0..76cf6ac4 100644 --- a/folly/test/StringTest.cpp +++ b/folly/test/StringTest.cpp @@ -19,6 +19,8 @@ #include #include +#include + using namespace folly; using namespace std; @@ -411,8 +413,8 @@ TEST(PrettyToDouble, Basic) { double recoveredX = 0; try{ recoveredX = prettyToDouble(testString, formatType); - } catch (std::range_error &ex){ - EXPECT_TRUE(false); + } catch (const std::range_error& ex) { + EXPECT_TRUE(false) << testCase.prettyString << " -> " << ex.what(); } double relativeError = fabs(x) < 1e-5 ? (x-recoveredX) : (x - recoveredX) / x; @@ -906,8 +908,36 @@ TEST(Split, fixed_convert) { EXPECT_EQ(13, b); EXPECT_EQ("14.7:b", d); - EXPECT_THROW(folly::split(':', "a:13:14.7:b", a, b, c), - std::range_error); + + // Enable verifying that a line only contains one field + EXPECT_TRUE(folly::split(' ', "hello", a)); + EXPECT_FALSE(folly::split(' ', "hello world", a)); +} + +namespace my { + +enum class Color { + Red, + Blue, +}; + +void parseTo(folly::StringPiece in, Color& out) { + if (in == "R") { + out = Color::Red; + } else if (in == "B") { + out = Color::Blue; + } else { + throw runtime_error(""); + } +} +} + +TEST(Split, fixed_convert_custom) { + my::Color c1, c2; + + EXPECT_TRUE(folly::split(',', "R,B", c1, c2)); + EXPECT_EQ(c1, my::Color::Red); + EXPECT_EQ(c2, my::Color::Blue); } TEST(String, join) { @@ -944,7 +974,7 @@ TEST(String, hexlify) { string input1 = "0123"; string output1; EXPECT_TRUE(hexlify(input1, output1)); - EXPECT_EQ(output1, "30313233"); + EXPECT_EQ("30313233", output1); fbstring input2 = "abcdefg"; input2[1] = 0; @@ -952,7 +982,11 @@ TEST(String, hexlify) { input2[5] = 0xb6; fbstring output2; EXPECT_TRUE(hexlify(input2, output2)); - EXPECT_EQ(output2, "610063ff65b667"); + EXPECT_EQ("610063ff65b667", output2); + + EXPECT_EQ("666f6f626172", hexlify("foobar")); + auto bytes = folly::make_array(1, 2, 3, 4); + EXPECT_EQ("01020304", hexlify(ByteRange{bytes.data(), bytes.size()})); } TEST(String, unhexlify) { @@ -980,6 +1014,10 @@ TEST(String, unhexlify) { string input4 = "xy"; string output4; EXPECT_FALSE(unhexlify(input4, output4)); + + EXPECT_EQ("foobar", unhexlify("666f6f626172")); + EXPECT_EQ(StringPiece("foo\0bar", 7), unhexlify("666f6f00626172")); + EXPECT_THROW(unhexlify("666f6fzz626172"), std::domain_error); } TEST(String, backslashify) { @@ -1295,7 +1333,7 @@ TEST(String, stripLeftMargin_no_post_whitespace) { EXPECT_EQ(expected, stripLeftMargin(input)); } -const folly::StringPiece kTestUTF8 = "This is \U0001F602 stuff!"; +const folly::StringPiece kTestUTF8 = u8"This is \U0001F602 stuff!"; TEST(UTF8StringPiece, valid_utf8) { folly::StringPiece sp = kTestUTF8; @@ -1317,3 +1355,11 @@ TEST(UTF8StringPiece, empty_mid_codepoint) { TEST(UTF8StringPiece, invalid_mid_codepoint) { EXPECT_THROW(UTF8StringPiece(kTestUTF8.subpiece(9, 1)), std::out_of_range); } + +TEST(UTF8StringPiece, valid_implicit_conversion) { + std::string input = u8"\U0001F602\U0001F602\U0001F602"; + auto checkImplicitCtor = [](UTF8StringPiece implicitCtor) { + return implicitCtor.walk_size(); + }; + EXPECT_EQ(3, checkImplicitCtor(input)); +}