From: Marc Horowitz Date: Tue, 12 May 2015 02:23:45 +0000 (-0700) Subject: Refactor tests to verify that a single folly target can be used successfully X-Git-Tag: v0.40.0~8 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=b0131bea126f9febe31c825fc4cd11d5d8996304;p=folly.git Refactor tests to verify that a single folly target can be used successfully Summary: This includes a change to Range to move operator<< into the header, to achieve the goal. Specifically, see format_test, dynamic_test, json_test, demangle_test Test Plan: fbconfig -r buck && fbmake runtests Reviewed By: njormrod@fb.com Subscribers: darshan, tjackson, folly-diffs@, yfeldblum, chalfant FB internal diff: D2063698 Signature: t1:2063698:1431467309:069da6d74bb5c384e7a21e6be19a4b20466bdd92 --- diff --git a/folly/Range.cpp b/folly/Range.cpp index aa94d583..bf4e28d0 100644 --- a/folly/Range.cpp +++ b/folly/Range.cpp @@ -32,16 +32,6 @@ namespace folly { const AsciiCaseSensitive asciiCaseSensitive = AsciiCaseSensitive(); const AsciiCaseInsensitive asciiCaseInsensitive = AsciiCaseInsensitive(); -std::ostream& operator<<(std::ostream& os, const StringPiece piece) { - os.write(piece.start(), piece.size()); - return os; -} - -std::ostream& operator<<(std::ostream& os, const MutableStringPiece piece) { - os.write(piece.start(), piece.size()); - return os; -} - namespace { // It's okay if pages are bigger than this (as powers of two), but they should diff --git a/folly/Range.h b/folly/Range.h index 2942ea98..c34d5cfe 100644 --- a/folly/Range.h +++ b/folly/Range.h @@ -836,8 +836,17 @@ typedef Range MutableStringPiece; typedef Range ByteRange; typedef Range MutableByteRange; -std::ostream& operator<<(std::ostream& os, const StringPiece piece); -std::ostream& operator<<(std::ostream& os, const MutableStringPiece piece); +inline std::ostream& operator<<(std::ostream& os, + const StringPiece piece) { + os.write(piece.start(), piece.size()); + return os; +} + +inline std::ostream& operator<<(std::ostream& os, + const MutableStringPiece piece) { + os.write(piece.start(), piece.size()); + return os; +} /** * Templated comparison operators diff --git a/folly/test/DemangleTest.cpp b/folly/test/DemangleTest.cpp new file mode 100644 index 00000000..eab9cff7 --- /dev/null +++ b/folly/test/DemangleTest.cpp @@ -0,0 +1,55 @@ +/* + * Copyright 2015 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include + +using folly::demangle; + +namespace folly_test { +struct ThisIsAVeryLongStructureName { +}; +} // namespace folly_test + +#if FOLLY_HAVE_CPLUS_DEMANGLE_V3_CALLBACK +TEST(Demangle, demangle) { + char expected[] = "folly_test::ThisIsAVeryLongStructureName"; + EXPECT_STREQ( + expected, + demangle(typeid(folly_test::ThisIsAVeryLongStructureName)).c_str()); + + { + char buf[sizeof(expected)]; + EXPECT_EQ(sizeof(expected) - 1, + demangle(typeid(folly_test::ThisIsAVeryLongStructureName), + buf, sizeof(buf))); + EXPECT_STREQ(expected, buf); + + EXPECT_EQ(sizeof(expected) - 1, + demangle(typeid(folly_test::ThisIsAVeryLongStructureName), + buf, 11)); + EXPECT_STREQ("folly_test", buf); + } +} +#endif + +int main(int argc, char *argv[]) { + testing::InitGoogleTest(&argc, argv); + gflags::ParseCommandLineFlags(&argc, &argv, true); + return RUN_ALL_TESTS(); +} diff --git a/folly/test/DynamicOtherTest.cpp b/folly/test/DynamicOtherTest.cpp new file mode 100644 index 00000000..f4a208a2 --- /dev/null +++ b/folly/test/DynamicOtherTest.cpp @@ -0,0 +1,125 @@ +/* + * Copyright 2015 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include + +#include +#include + +#include + +using folly::dynamic; +using folly::TypeError; + +TEST(Dynamic, ArrayGenerator) { + // Make sure arrays can be used with folly::gen. + using namespace folly::gen; + dynamic arr { 1, 2, 3, 4 }; + EXPECT_EQ(from(arr) | take(3) | member(&dynamic::asInt) | sum, 6); +} + +TEST(Dynamic, StringPtrs) { + dynamic str = "12.0"; + dynamic num = 12.0; + dynamic nullStr = folly::parseJson("\"foo\\u0000bar\""); + + EXPECT_EQ(0, strcmp(str.c_str(), "12.0")); + EXPECT_EQ(0, strncmp(str.data(), "12.0", str.asString().length())); + EXPECT_EQ(str.stringPiece(), "12.0"); + + EXPECT_THROW(num.c_str(), TypeError); + EXPECT_THROW(num.data(), TypeError); + EXPECT_THROW(num.stringPiece(), TypeError); + + EXPECT_EQ(nullStr.stringPiece(), folly::StringPiece("foo\0bar", 7)); + + nullStr.getString()[3] = '|'; + EXPECT_EQ(nullStr.stringPiece(), "foo|bar"); +} + +TEST(Dynamic, Getters) { + dynamic dStr = folly::parseJson("\"foo\\u0000bar\""); + dynamic dInt = 1; + dynamic dDouble = 0.5; + dynamic dBool = true; + + EXPECT_EQ(dStr.getString(), std::string("foo\0bar", 7)); + EXPECT_EQ(dInt.getInt(), 1); + EXPECT_EQ(dDouble.getDouble(), 0.5); + EXPECT_EQ(dBool.getBool(), true); + + dStr.getString()[3] = '|'; + EXPECT_EQ(dStr.getString(), "foo|bar"); + + dInt.getInt() = 2; + EXPECT_EQ(dInt.getInt(), 2); + + dDouble.getDouble() = 0.7; + EXPECT_EQ(dDouble.getDouble(), 0.7); + + dBool.getBool() = false; + EXPECT_EQ(dBool.getBool(), false); + + EXPECT_THROW(dStr.getInt(), TypeError); + EXPECT_THROW(dStr.getDouble(), TypeError); + EXPECT_THROW(dStr.getBool(), TypeError); + + EXPECT_THROW(dInt.getString(), TypeError); + EXPECT_THROW(dInt.getDouble(), TypeError); + EXPECT_THROW(dInt.getBool(), TypeError); + + EXPECT_THROW(dDouble.getString(), TypeError); + EXPECT_THROW(dDouble.getInt(), TypeError); + EXPECT_THROW(dDouble.getBool(), TypeError); + + EXPECT_THROW(dBool.getString(), TypeError); + EXPECT_THROW(dBool.getInt(), TypeError); + EXPECT_THROW(dBool.getDouble(), TypeError); +} + +TEST(Dynamic, FormattedIO) { + std::ostringstream out; + dynamic doubl = 123.33; + dynamic dint = 12; + out << "0x" << std::hex << ++dint << ' ' << std::setprecision(1) + << doubl << '\n'; + EXPECT_EQ(out.str(), "0xd 1e+02\n"); + + out.str(""); + dynamic arrr = { 1, 2, 3 }; + out << arrr; + EXPECT_EQ(out.str(), "[1,2,3]"); + + out.str(""); + dynamic objy = dynamic::object("a", 12); + out << objy; + EXPECT_EQ(out.str(), R"({"a":12})"); + + out.str(""); + dynamic objy2 = { objy, dynamic::object(12, "str"), + dynamic::object(true, false) }; + out << objy2; + EXPECT_EQ(out.str(), R"([{"a":12},{12:"str"},{true:false}])"); +} + +int main(int argc, char** argv) { + testing::InitGoogleTest(&argc, argv); + gflags::ParseCommandLineFlags(&argc, &argv, true); + return RUN_ALL_TESTS(); +} diff --git a/folly/test/DynamicTest.cpp b/folly/test/DynamicTest.cpp index 0249fca4..ba179c1b 100644 --- a/folly/test/DynamicTest.cpp +++ b/folly/test/DynamicTest.cpp @@ -14,17 +14,22 @@ * limitations under the License. */ +#include + #include #include #include -#include -#include -#include -#include - using folly::dynamic; -using folly::TypeError; + +// This test runs without any external dependencies, including json. +// This means that if there's a test failure, there's no way to print +// a useful runtime representation of the folly::dynamic. We will +// live with this in order to test dependencies. This method is +// normally provided by json.cpp. +void dynamic::print_as_pseudo_json(std::ostream& out) const { + out << ""; +} TEST(Dynamic, ObjectBasics) { dynamic obj = dynamic::object("a", false); @@ -233,50 +238,6 @@ TEST(Dynamic, Conversions) { EXPECT_EQ(12.0, num.asDouble()); } -TEST(Dynamic, StringPtrs) { - dynamic str = "12.0"; - dynamic num = 12.0; - dynamic nullStr = folly::parseJson("\"foo\\u0000bar\""); - - EXPECT_EQ(0, strcmp(str.c_str(), "12.0")); - EXPECT_EQ(0, strncmp(str.data(), "12.0", str.asString().length())); - EXPECT_EQ(str.stringPiece(), "12.0"); - - EXPECT_THROW(num.c_str(), TypeError); - EXPECT_THROW(num.data(), TypeError); - EXPECT_THROW(num.stringPiece(), TypeError); - - EXPECT_EQ(nullStr.stringPiece(), folly::StringPiece("foo\0bar", 7)); - - nullStr.getString()[3] = '|'; - EXPECT_EQ(nullStr.stringPiece(), "foo|bar"); -} - -TEST(Dynamic, FormattedIO) { - std::ostringstream out; - dynamic doubl = 123.33; - dynamic dint = 12; - out << "0x" << std::hex << ++dint << ' ' << std::setprecision(1) - << doubl << '\n'; - EXPECT_EQ(out.str(), "0xd 1e+02\n"); - - out.str(""); - dynamic arrr = { 1, 2, 3 }; - out << arrr; - EXPECT_EQ(out.str(), "[1,2,3]"); - - out.str(""); - dynamic objy = dynamic::object("a", 12); - out << objy; - EXPECT_EQ(out.str(), R"({"a":12})"); - - out.str(""); - dynamic objy2 = { objy, dynamic::object(12, "str"), - dynamic::object(true, false) }; - out << objy2; - EXPECT_EQ(out.str(), R"([{"a":12},{12:"str"},{true:false}])"); -} - TEST(Dynamic, GetSetDefaultTest) { dynamic d1 = dynamic::object("foo", "bar"); EXPECT_EQ(d1.getDefault("foo", "baz"), "bar"); @@ -324,58 +285,8 @@ TEST(Dynamic, GetPtr) { EXPECT_EQ(dynamic(2), *cobject.get_ptr("two")); } -TEST(Dynamic, ArrayGenerator) { - // Make sure arrays can be used with folly::gen. - using namespace folly::gen; - dynamic arr { 1, 2, 3, 4 }; - EXPECT_EQ(from(arr) | take(3) | member(&dynamic::asInt) | sum, 6); -} - -TEST(Dynamic, Getters) { - dynamic dStr = folly::parseJson("\"foo\\u0000bar\""); - dynamic dInt = 1; - dynamic dDouble = 0.5; - dynamic dBool = true; - - EXPECT_EQ(dStr.getString(), std::string("foo\0bar", 7)); - EXPECT_EQ(dInt.getInt(), 1); - EXPECT_EQ(dDouble.getDouble(), 0.5); - EXPECT_EQ(dBool.getBool(), true); - - dStr.getString()[3] = '|'; - EXPECT_EQ(dStr.getString(), "foo|bar"); - - dInt.getInt() = 2; - EXPECT_EQ(dInt.getInt(), 2); - - dDouble.getDouble() = 0.7; - EXPECT_EQ(dDouble.getDouble(), 0.7); - - dBool.getBool() = false; - EXPECT_EQ(dBool.getBool(), false); - - EXPECT_THROW(dStr.getInt(), TypeError); - EXPECT_THROW(dStr.getDouble(), TypeError); - EXPECT_THROW(dStr.getBool(), TypeError); - - EXPECT_THROW(dInt.getString(), TypeError); - EXPECT_THROW(dInt.getDouble(), TypeError); - EXPECT_THROW(dInt.getBool(), TypeError); - - EXPECT_THROW(dDouble.getString(), TypeError); - EXPECT_THROW(dDouble.getInt(), TypeError); - EXPECT_THROW(dDouble.getBool(), TypeError); - - EXPECT_THROW(dBool.getString(), TypeError); - EXPECT_THROW(dBool.getInt(), TypeError); - EXPECT_THROW(dBool.getDouble(), TypeError); -} - int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); gflags::ParseCommandLineFlags(&argc, &argv, true); - if (FLAGS_benchmark) { - folly::runBenchmarks(); - } return RUN_ALL_TESTS(); } diff --git a/folly/test/FormatOtherTest.cpp b/folly/test/FormatOtherTest.cpp new file mode 100644 index 00000000..c41f1f06 --- /dev/null +++ b/folly/test/FormatOtherTest.cpp @@ -0,0 +1,83 @@ +/* + * Copyright 2015 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include +#include + +#include +#include +#include + +using namespace folly; + +TEST(FormatOther, file) { + // Test writing to FILE. I'd use open_memstream but that's not available + // outside of Linux (even though it's in POSIX.1-2008). + { + int fds[2]; + CHECK_ERR(pipe(fds)); + SCOPE_EXIT { closeNoInt(fds[1]); }; + { + FILE* fp = fdopen(fds[1], "wb"); + PCHECK(fp); + SCOPE_EXIT { fclose(fp); }; + writeTo(fp, format("{} {}", 42, 23)); // <= 512 bytes (PIPE_BUF) + } + + char buf[512]; + ssize_t n = readFull(fds[0], buf, sizeof(buf)); + CHECK_GE(n, 0); + + EXPECT_EQ("42 23", std::string(buf, n)); + } +} + +TEST(FormatOther, dynamic) { + auto dyn = parseJson( + "{\n" + " \"hello\": \"world\",\n" + " \"x\": [20, 30],\n" + " \"y\": {\"a\" : 42}\n" + "}"); + + EXPECT_EQ("world", sformat("{0[hello]}", dyn)); + EXPECT_THROW(sformat("{0[none]}", dyn), std::out_of_range); + EXPECT_EQ("world", sformat("{0[hello]}", defaulted(dyn, "meow"))); + EXPECT_EQ("meow", sformat("{0[none]}", defaulted(dyn, "meow"))); + + EXPECT_EQ("20", sformat("{0[x.0]}", dyn)); + EXPECT_THROW(sformat("{0[x.2]}", dyn), std::out_of_range); + + // No support for "deep" defaulting (dyn["x"] is not defaulted) + auto v = dyn.at("x"); + EXPECT_EQ("20", sformat("{0[0]}", v)); + EXPECT_THROW(sformat("{0[2]}", v), std::out_of_range); + EXPECT_EQ("20", sformat("{0[0]}", defaulted(v, 42))); + EXPECT_EQ("42", sformat("{0[2]}", defaulted(v, 42))); + + EXPECT_EQ("42", sformat("{0[y.a]}", dyn)); + + EXPECT_EQ("(null)", sformat("{}", dynamic(nullptr))); +} + +int main(int argc, char *argv[]) { + testing::InitGoogleTest(&argc, argv); + gflags::ParseCommandLineFlags(&argc, &argv, true); + return RUN_ALL_TESTS(); +} diff --git a/folly/test/FormatTest.cpp b/folly/test/FormatTest.cpp index 5ad38ef6..4385401b 100644 --- a/folly/test/FormatTest.cpp +++ b/folly/test/FormatTest.cpp @@ -16,15 +16,9 @@ #include -#include #include #include -#include -#include -#include -#include - #include using namespace folly; @@ -36,7 +30,8 @@ void compareOctal(Uint u) { char* p = buf1 + detail::uintToOctal(buf1, detail::kMaxOctalLength, u); char buf2[detail::kMaxOctalLength + 1]; - sprintf(buf2, "%jo", static_cast(u)); + EXPECT_LT(snprintf(buf2, sizeof(buf2), "%jo", static_cast(u)), + sizeof(buf2)); EXPECT_EQ(std::string(buf2), std::string(p)); } @@ -48,7 +43,8 @@ void compareHex(Uint u) { char* p = buf1 + detail::uintToHexLower(buf1, detail::kMaxHexLength, u); char buf2[detail::kMaxHexLength + 1]; - sprintf(buf2, "%jx", static_cast(u)); + EXPECT_LT(snprintf(buf2, sizeof(buf2), "%jx", static_cast(u)), + sizeof(buf2)); EXPECT_EQ(std::string(buf2), std::string(p)); } @@ -195,26 +191,6 @@ TEST(Format, Simple) { format(&s, "{} {}", 42, 23); format(&s, " hello {:X<7}", "world"); EXPECT_EQ("42 23 hello worldXX", s); - - // Test writing to FILE. I'd use open_memstream but that's not available - // outside of Linux (even though it's in POSIX.1-2008). - { - int fds[2]; - CHECK_ERR(pipe(fds)); - SCOPE_EXIT { closeNoInt(fds[1]); }; - { - FILE* fp = fdopen(fds[1], "wb"); - PCHECK(fp); - SCOPE_EXIT { fclose(fp); }; - writeTo(fp, format("{} {}", 42, 23)); // <= 512 bytes (PIPE_BUF) - } - - char buf[512]; - ssize_t n = readFull(fds[0], buf, sizeof(buf)); - CHECK_GE(n, 0); - - EXPECT_EQ("42 23", std::string(buf, n)); - } } TEST(Format, Float) { @@ -259,34 +235,6 @@ TEST(Format, MultiLevel) { EXPECT_EQ("world", sformat("{[0.hello]}", v)); } -TEST(Format, dynamic) { - auto dyn = parseJson( - "{\n" - " \"hello\": \"world\",\n" - " \"x\": [20, 30],\n" - " \"y\": {\"a\" : 42}\n" - "}"); - - EXPECT_EQ("world", sformat("{0[hello]}", dyn)); - EXPECT_THROW(sformat("{0[none]}", dyn), std::out_of_range); - EXPECT_EQ("world", sformat("{0[hello]}", defaulted(dyn, "meow"))); - EXPECT_EQ("meow", sformat("{0[none]}", defaulted(dyn, "meow"))); - - EXPECT_EQ("20", sformat("{0[x.0]}", dyn)); - EXPECT_THROW(sformat("{0[x.2]}", dyn), std::out_of_range); - - // No support for "deep" defaulting (dyn["x"] is not defaulted) - auto v = dyn.at("x"); - EXPECT_EQ("20", sformat("{0[0]}", v)); - EXPECT_THROW(sformat("{0[2]}", v), std::out_of_range); - EXPECT_EQ("20", sformat("{0[0]}", defaulted(v, 42))); - EXPECT_EQ("42", sformat("{0[2]}", defaulted(v, 42))); - - EXPECT_EQ("42", sformat("{0[y.a]}", dyn)); - - EXPECT_EQ("(null)", sformat("{}", dynamic(nullptr))); -} - TEST(Format, separatorDecimalInteger) { EXPECT_EQ("0", sformat("{:,d}", 0)); EXPECT_EQ("1", sformat("{:d}", 1)); @@ -350,8 +298,8 @@ TEST(Format, separatorNumber) { // insertThousandsGroupingUnsafe requires non-const params static void testGrouping(const char* a_str, const char* expected) { char str[256]; - strcpy(str, a_str); - char * end_ptr = str + strlen(str); + char* end_ptr = str + snprintf(str, sizeof(str), "%s", a_str); + ASSERT_LT(end_ptr, str + sizeof(str)); folly::detail::insertThousandsGroupingUnsafe(str, &end_ptr); ASSERT_STREQ(expected, str); } diff --git a/folly/test/JsonOtherTest.cpp b/folly/test/JsonOtherTest.cpp new file mode 100644 index 00000000..cb0d0dee --- /dev/null +++ b/folly/test/JsonOtherTest.cpp @@ -0,0 +1,152 @@ +/* + * Copyright 2015 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include + +#include +#include + +using folly::dynamic; +using folly::parseJson; +using folly::toJson; + +TEST(Json, StripComments) { + const std::string kTestDir = "folly/test/"; + const std::string kTestFile = "json_test_data/commented.json"; + const std::string kTestExpected = "json_test_data/commented.json.exp"; + + std::string testStr; + std::string expectedStr; + if (!folly::readFile(kTestFile.data(), testStr) && + !folly::readFile((kTestDir + kTestFile).data(), testStr)) { + FAIL() << "can not read test file " << kTestFile; + } + if (!folly::readFile(kTestExpected.data(), expectedStr) && + !folly::readFile((kTestDir + kTestExpected).data(), expectedStr)) { + FAIL() << "can not read test file " << kTestExpected; + } + EXPECT_EQ(expectedStr, folly::json::stripComments(testStr)); +} + +BENCHMARK(jsonSerialize, iters) { + folly::json::serialization_opts opts; + for (size_t i = 0; i < iters; ++i) { + folly::json::serialize( + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy", + opts); + } +} + +BENCHMARK(jsonSerializeWithNonAsciiEncoding, iters) { + folly::json::serialization_opts opts; + opts.encode_non_ascii = true; + + for (size_t i = 0; i < iters; ++i) { + folly::json::serialize( + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy", + opts); + } +} + +BENCHMARK(jsonSerializeWithUtf8Validation, iters) { + folly::json::serialization_opts opts; + opts.validate_utf8 = true; + + for (size_t i = 0; i < iters; ++i) { + folly::json::serialize( + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy", + opts); + } +} + +BENCHMARK(parseSmallStringWithUtf, iters) { + for (size_t i = 0; i < iters << 4; ++i) { + parseJson("\"I \\u2665 UTF-8 thjasdhkjh blah blah blah\""); + } +} + +BENCHMARK(parseNormalString, iters) { + for (size_t i = 0; i < iters << 4; ++i) { + parseJson("\"akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk\""); + } +} + +BENCHMARK(parseBigString, iters) { + for (size_t i = 0; i < iters; ++i) { + parseJson("\"" + "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" + "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" + "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" + "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" + "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" + "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" + "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" + "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" + "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" + "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" + "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" + "\""); + } +} + +BENCHMARK(toJson, iters) { + dynamic something = parseJson( + "{\"old_value\":40,\"changed\":true,\"opened\":false,\"foo\":[1,2,3,4,5,6]}" + ); + + for (size_t i = 0; i < iters; i++) { + toJson(something); + } +} + +int main(int argc, char** argv) { + testing::InitGoogleTest(&argc, argv); + gflags::ParseCommandLineFlags(&argc, &argv, true); + if (FLAGS_benchmark) { + folly::runBenchmarks(); + } + return RUN_ALL_TESTS(); +} diff --git a/folly/test/JsonTest.cpp b/folly/test/JsonTest.cpp index 274e3e41..75d08166 100644 --- a/folly/test/JsonTest.cpp +++ b/folly/test/JsonTest.cpp @@ -14,15 +14,12 @@ * limitations under the License. */ -#include #include + #include #include -#include #include -#include #include -#include using folly::dynamic; using folly::parseJson; @@ -380,127 +377,8 @@ TEST(Json, SortKeys) { EXPECT_EQ(sorted_keys, folly::json::serialize(value, opts_on)); } -TEST(Json, StripComments) { - const std::string kTestDir = "folly/test/"; - const std::string kTestFile = "json_test_data/commented.json"; - const std::string kTestExpected = "json_test_data/commented.json.exp"; - - std::string testStr; - std::string expectedStr; - if (!folly::readFile(kTestFile.data(), testStr) && - !folly::readFile((kTestDir + kTestFile).data(), testStr)) { - FAIL() << "can not read test file " << kTestFile; - } - if (!folly::readFile(kTestExpected.data(), expectedStr) && - !folly::readFile((kTestDir + kTestExpected).data(), expectedStr)) { - FAIL() << "can not read test file " << kTestExpected; - } - EXPECT_EQ(expectedStr, folly::json::stripComments(testStr)); -} - -BENCHMARK(jsonSerialize, iters) { - folly::json::serialization_opts opts; - for (size_t i = 0; i < iters; ++i) { - folly::json::serialize( - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy", - opts); - } -} - -BENCHMARK(jsonSerializeWithNonAsciiEncoding, iters) { - folly::json::serialization_opts opts; - opts.encode_non_ascii = true; - - for (size_t i = 0; i < iters; ++i) { - folly::json::serialize( - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy", - opts); - } -} - -BENCHMARK(jsonSerializeWithUtf8Validation, iters) { - folly::json::serialization_opts opts; - opts.validate_utf8 = true; - - for (size_t i = 0; i < iters; ++i) { - folly::json::serialize( - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy", - opts); - } -} - -BENCHMARK(parseSmallStringWithUtf, iters) { - for (size_t i = 0; i < iters << 4; ++i) { - parseJson("\"I \\u2665 UTF-8 thjasdhkjh blah blah blah\""); - } -} - -BENCHMARK(parseNormalString, iters) { - for (size_t i = 0; i < iters << 4; ++i) { - parseJson("\"akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk\""); - } -} - -BENCHMARK(parseBigString, iters) { - for (size_t i = 0; i < iters; ++i) { - parseJson("\"" - "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" - "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" - "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" - "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" - "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" - "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" - "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" - "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" - "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" - "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" - "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" - "\""); - } -} - -BENCHMARK(toJson, iters) { - dynamic something = parseJson( - "{\"old_value\":40,\"changed\":true,\"opened\":false,\"foo\":[1,2,3,4,5,6]}" - ); - - for (size_t i = 0; i < iters; i++) { - toJson(something); - } -} - int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); gflags::ParseCommandLineFlags(&argc, &argv, true); - if (FLAGS_benchmark) { - folly::runBenchmarks(); - } return RUN_ALL_TESTS(); } diff --git a/folly/test/StringTest.cpp b/folly/test/StringTest.cpp index fa0ee559..492534b6 100644 --- a/folly/test/StringTest.cpp +++ b/folly/test/StringTest.cpp @@ -607,33 +607,6 @@ TEST(System, errnoStr) { EXPECT_EQ(EACCES, errno); } -namespace folly_test { -struct ThisIsAVeryLongStructureName { -}; -} // namespace folly_test - -#if FOLLY_HAVE_CPLUS_DEMANGLE_V3_CALLBACK -TEST(System, demangle) { - char expected[] = "folly_test::ThisIsAVeryLongStructureName"; - EXPECT_STREQ( - expected, - demangle(typeid(folly_test::ThisIsAVeryLongStructureName)).c_str()); - - { - char buf[sizeof(expected)]; - EXPECT_EQ(sizeof(expected) - 1, - demangle(typeid(folly_test::ThisIsAVeryLongStructureName), - buf, sizeof(buf))); - EXPECT_STREQ(expected, buf); - - EXPECT_EQ(sizeof(expected) - 1, - demangle(typeid(folly_test::ThisIsAVeryLongStructureName), - buf, 11)); - EXPECT_STREQ("folly_test", buf); - } -} -#endif - namespace { template class VectorType>