X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2Ftest%2FStringTest.cpp;h=4140d7d2904429fe80691e7b8374e4e169e2de6b;hb=3c127a77f9b9fb201f98da646bbe0ea06cd6d55b;hp=526afeecbe129bb3dd0f161733cf1aaf59ace56d;hpb=b0193e80a8b1a9a74d5e90a51d2490b68cbdf69a;p=folly.git diff --git a/folly/test/StringTest.cpp b/folly/test/StringTest.cpp index 526afeec..4140d7d2 100644 --- a/folly/test/StringTest.cpp +++ b/folly/test/StringTest.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. @@ -14,14 +14,18 @@ * limitations under the License. */ +#ifndef __STDC_FORMAT_MACROS +#define __STDC_FORMAT_MACROS 1 +#endif + #include -#include -#include -#include -#include +#include -#include +#include + +#include +#include using namespace folly; using namespace std; @@ -37,12 +41,15 @@ TEST(StringPrintf, BasicTest) { TEST(StringPrintf, NumericFormats) { EXPECT_EQ("12", stringPrintf("%d", 12)); - EXPECT_EQ("5000000000", stringPrintf("%ld", 5000000000UL)); - EXPECT_EQ("5000000000", stringPrintf("%ld", 5000000000L)); - EXPECT_EQ("-5000000000", stringPrintf("%ld", -5000000000L)); + EXPECT_EQ("2000000000", stringPrintf("%ld", 2000000000UL)); + EXPECT_EQ("2000000000", stringPrintf("%ld", 2000000000L)); + EXPECT_EQ("-2000000000", stringPrintf("%ld", -2000000000L)); + EXPECT_EQ("5000000000", stringPrintf("%lld", 5000000000ULL)); + EXPECT_EQ("5000000000", stringPrintf("%lld", 5000000000LL)); + EXPECT_EQ("-5000000000", stringPrintf("%lld", -5000000000LL)); EXPECT_EQ("-1", stringPrintf("%d", 0xffffffff)); - EXPECT_EQ("-1", stringPrintf("%ld", 0xffffffffffffffff)); - EXPECT_EQ("-1", stringPrintf("%ld", 0xffffffffffffffffUL)); + EXPECT_EQ("-1", stringPrintf("%" PRId64, 0xffffffffffffffff)); + EXPECT_EQ("-1", stringPrintf("%" PRId64, 0xffffffffffffffffUL)); EXPECT_EQ("7.7", stringPrintf("%1.1f", 7.7)); EXPECT_EQ("7.7", stringPrintf("%1.1lf", 7.7)); @@ -155,39 +162,6 @@ TEST(StringPrintf, oldStringAppendf) { EXPECT_EQ(string("helloa/b/c/d"), s); } -// A simple benchmark that tests various output sizes for a simple -// input; the goal is to measure the output buffer resize code cost. -void stringPrintfOutputSize(int iters, int param) { - string buffer; - BENCHMARK_SUSPEND { buffer.resize(param, 'x'); } - - for (int64_t i = 0; i < iters; ++i) { - string s = stringPrintf("msg: %d, %d, %s", 10, 20, buffer.c_str()); - } -} - -// The first few of these tend to fit in the inline buffer, while the -// subsequent ones cross that limit, trigger a second vsnprintf, and -// exercise a different codepath. -BENCHMARK_PARAM(stringPrintfOutputSize, 1) -BENCHMARK_PARAM(stringPrintfOutputSize, 4) -BENCHMARK_PARAM(stringPrintfOutputSize, 16) -BENCHMARK_PARAM(stringPrintfOutputSize, 64) -BENCHMARK_PARAM(stringPrintfOutputSize, 256) -BENCHMARK_PARAM(stringPrintfOutputSize, 1024) - -// Benchmark simple stringAppendf behavior to show a pathology Lovro -// reported (t5735468). -BENCHMARK(stringPrintfAppendfBenchmark, iters) { - for (unsigned int i = 0; i < iters; ++i) { - string s; - BENCHMARK_SUSPEND { s.reserve(300000); } - for (int j = 0; j < 300000; ++j) { - stringAppendf(&s, "%d", 1); - } - } -} - TEST(Escape, cEscape) { EXPECT_EQ("hello world", cEscape("hello world")); EXPECT_EQ("hello \\\\world\\\" goodbye", @@ -250,7 +224,7 @@ void expectPrintable(StringPiece s) { EXPECT_GE(127, c); } } -} // namespace +} // namespace TEST(Escape, uriEscapeAllCombinations) { char c[3]; @@ -278,7 +252,7 @@ bool isHex(int v) { (v >= 'A' && v <= 'F') || (v >= 'a' && v <= 'f')); } -} // namespace +} // namespace TEST(Escape, uriUnescapePercentDecoding) { char c[4] = {'%', '\0', '\0', '\0'}; @@ -303,103 +277,13 @@ TEST(Escape, uriUnescapePercentDecoding) { } } -namespace { -fbstring cbmString; -fbstring cbmEscapedString; -fbstring cEscapedString; -fbstring cUnescapedString; -const size_t kCBmStringLength = 64 << 10; -const uint32_t kCPrintablePercentage = 90; - -fbstring uribmString; -fbstring uribmEscapedString; -fbstring uriEscapedString; -fbstring uriUnescapedString; -const size_t kURIBmStringLength = 256; -const uint32_t kURIPassThroughPercentage = 50; - -void initBenchmark() { - std::mt19937 rnd; - - // C escape - std::uniform_int_distribution printable(32, 126); - std::uniform_int_distribution nonPrintable(0, 160); - std::uniform_int_distribution percentage(0, 99); - - cbmString.reserve(kCBmStringLength); - for (size_t i = 0; i < kCBmStringLength; ++i) { - unsigned char c; - if (percentage(rnd) < kCPrintablePercentage) { - c = printable(rnd); - } else { - c = nonPrintable(rnd); - // Generate characters in both non-printable ranges: - // 0..31 and 127..255 - if (c >= 32) { - c += (126 - 32) + 1; - } - } - cbmString.push_back(c); - } - - cbmEscapedString = cEscape(cbmString); - - // URI escape - std::uniform_int_distribution passthrough('a', 'z'); - std::string encodeChars = " ?!\"',+[]"; - std::uniform_int_distribution encode(0, encodeChars.size() - 1); - - uribmString.reserve(kURIBmStringLength); - for (size_t i = 0; i < kURIBmStringLength; ++i) { - unsigned char c; - if (percentage(rnd) < kURIPassThroughPercentage) { - c = passthrough(rnd); - } else { - c = encodeChars[encode(rnd)]; - } - uribmString.push_back(c); - } - - uribmEscapedString = uriEscape(uribmString); -} - -BENCHMARK(BM_cEscape, iters) { - while (iters--) { - cEscapedString = cEscape(cbmString); - doNotOptimizeAway(cEscapedString.size()); - } -} - -BENCHMARK(BM_cUnescape, iters) { - while (iters--) { - cUnescapedString = cUnescape(cbmEscapedString); - doNotOptimizeAway(cUnescapedString.size()); - } -} - -BENCHMARK(BM_uriEscape, iters) { - while (iters--) { - uriEscapedString = uriEscape(uribmString); - doNotOptimizeAway(uriEscapedString.size()); - } -} - -BENCHMARK(BM_uriUnescape, iters) { - while (iters--) { - uriUnescapedString = uriUnescape(uribmEscapedString); - doNotOptimizeAway(uriUnescapedString.size()); - } -} - -} // namespace - namespace { double pow2(int exponent) { return double(int64_t(1) << exponent); } -} // namespace +} // namespace struct PrettyTestCase{ std::string prettyString; double realValue; @@ -535,8 +419,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) { + ADD_FAILURE() << testCase.prettyString << " -> " << ex.what(); } double relativeError = fabs(x) < 1e-5 ? (x-recoveredX) : (x - recoveredX) / x; @@ -553,8 +437,8 @@ TEST(PrettyToDouble, Basic) { try{ recoveredX = prettyToDouble(prettyPrint(x, formatType, addSpace), formatType); - } catch (std::range_error &ex){ - EXPECT_TRUE(false); + } catch (std::range_error&) { + ADD_FAILURE(); } double relativeError = (x - recoveredX) / x; EXPECT_NEAR(0, relativeError, 1e-3); @@ -609,7 +493,7 @@ TEST(System, errnoStr) { namespace { -template class VectorType> +template