proxygen
StringTest.cpp File Reference
#include <iosfwd>
#include <map>
#include <vector>
#include <folly/functional/ApplyTuple.h>
#include <folly/gen/String.h>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Typedefs

using vec = vector< string >
 

Functions

 TEST (StringGen, EmptySplit)
 
 TEST (StringGen, Split)
 
 TEST (StringGen, SplitByNewLine)
 
 TEST (StringGen, EmptyResplit)
 
 TEST (StringGen, Resplit)
 
 TEST (StringGen, ResplitKeepDelimiter)
 
 TEST (StringGen, EachToTuple)
 
 TEST (StringGen, EachToPair)
 
void checkResplitMaxLength (vector< string > ins, char delim, uint64_t maxLength, vector< string > outs)
 
 TEST (StringGen, ResplitMaxLength)
 
template<typename F >
void runUnsplitSuite (F fn)
 
 TEST (StringGen, Unsplit)
 
 TEST (StringGen, Batch)
 
 TEST (StringGen, UncurryTuple)
 
 TEST (StringGen, UncurryPair)
 

Variables

static auto collect = eachTo<std::string>() | as<vector>()
 

Typedef Documentation

using vec = vector<string>

Definition at line 35 of file StringTest.cpp.

Function Documentation

void checkResplitMaxLength ( vector< string ins,
char  delim,
uint64_t  maxLength,
vector< string outs 
)

Definition at line 265 of file StringTest.cpp.

References folly::Range< Iter >::begin(), folly::Range< Iter >::end(), EXPECT_EQ, i, folly::join(), s, and folly::gen::streamSplitter().

Referenced by TEST().

269  {
270  vector<std::string> pieces;
271  auto splitter = streamSplitter(
272  delim,
273  [&pieces](StringPiece s) {
274  pieces.push_back(string(s.begin(), s.end()));
275  return true;
276  },
277  maxLength);
278  for (const auto& in : ins) {
279  splitter(in);
280  }
281  splitter.flush();
282 
283  EXPECT_EQ(outs.size(), pieces.size());
284  for (size_t i = 0; i < outs.size(); ++i) {
285  EXPECT_EQ(outs[i], pieces[i]);
286  }
287 
288  // Also check the concatenated input against the same output
289  if (ins.size() > 1) {
290  checkResplitMaxLength({folly::join("", ins)}, delim, maxLength, outs);
291  }
292 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void checkResplitMaxLength(vector< string > ins, char delim, uint64_t maxLength, vector< string > outs)
Definition: StringTest.cpp:265
constexpr Iter end() const
Definition: Range.h:455
constexpr Iter begin() const
Definition: Range.h:452
StreamSplitter< Callback > streamSplitter(char delimiter, Callback &&pieceCb, uint64_t capacity=0)
Definition: String.h:239
static set< string > s
void join(const Delim &delimiter, Iterator begin, Iterator end, String &output)
Definition: String-inl.h:498
template<typename F >
void runUnsplitSuite ( fn)

Definition at line 313 of file StringTest.cpp.

Referenced by TEST().

313  {
314  fn("hello, world");
315  fn("hello,world,goodbye");
316  fn(" ");
317  fn("");
318  fn(", ");
319  fn(", a, b,c");
320 }
TEST ( StringGen  ,
EmptySplit   
)

Definition at line 39 of file StringTest.cpp.

References folly::collect(), EXPECT_EQ, folly::gen::split(), and folly::gen::take().

39  {
40  {
41  auto input = "";
42  auto expected = vec{};
43  EXPECT_EQ(expected, split(input, ',') | collect);
44  }
45 
46  // The last delimiter is eaten, just like std::getline
47  {
48  auto input = ",";
49  auto expected = vec{""};
50  EXPECT_EQ(expected, split(input, ',') | collect);
51  }
52 
53  {
54  auto input = ",,";
55  auto expected = vec{"", ""};
56  EXPECT_EQ(expected, split(input, ',') | collect);
57  }
58 
59  {
60  auto input = ",,";
61  auto expected = vec{""};
62  EXPECT_EQ(expected, split(input, ',') | take(1) | collect);
63  }
64 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static auto collect
Definition: StringTest.cpp:37
S split(const StringPiece source, char delimiter)
Definition: String.h:61
Definition: Traits.h:588
detail::Take take(Number count)
Definition: Base-inl.h:2582
TEST ( StringGen  ,
Split   
)

Definition at line 66 of file StringTest.cpp.

References folly::collect(), EXPECT_EQ, folly::gen::split(), and folly::gen::take().

66  {
67  {
68  auto input = "hello,, world, goodbye, meow";
69  auto expected = vec{"hello", "", " world", " goodbye", " meow"};
70  EXPECT_EQ(expected, split(input, ',') | collect);
71  }
72 
73  {
74  auto input = "hello,, world, goodbye, meow";
75  auto expected = vec{"hello", "", " world"};
76  EXPECT_EQ(expected, split(input, ',') | take(3) | collect);
77  }
78 
79  {
80  auto input = "hello,, world, goodbye, meow";
81  auto expected = vec{"hello", "", " world", " goodbye", " meow"};
82  EXPECT_EQ(expected, split(input, ",") | take(5) | collect);
83  }
84 
85  {
86  auto input = "hello,, world, goodbye, meow";
87  auto expected = vec{"hello,", "world", "goodbye", "meow"};
88  EXPECT_EQ(expected, split(input, ", ") | collect);
89  }
90 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static auto collect
Definition: StringTest.cpp:37
S split(const StringPiece source, char delimiter)
Definition: String.h:61
Definition: Traits.h:588
detail::Take take(Number count)
Definition: Base-inl.h:2582
TEST ( StringGen  ,
SplitByNewLine   
)

Definition at line 92 of file StringTest.cpp.

References folly::collect(), EXPECT_EQ, and folly::gen::lines().

92  {
93  {
94  auto input = "hello\n\n world\r\n goodbye\r me\n\row";
95  auto expected = vec{"hello", "", " world", " goodbye", " me", "", "ow"};
96  EXPECT_EQ(expected, lines(input) | collect);
97  }
98 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static auto collect
Definition: StringTest.cpp:37
Definition: Traits.h:588
S lines(StringPiece source)
Definition: String.h:80
TEST ( StringGen  ,
EmptyResplit   
)

Definition at line 100 of file StringTest.cpp.

References folly::collect(), EXPECT_EQ, folly::gen::from(), and folly::gen::resplit().

100  {
101  {
102  auto input = vec{""};
103  auto expected = vec{};
104  EXPECT_EQ(expected, from(input) | resplit(',') | collect);
105  }
106 
107  // The last delimiter is eaten, just like std::getline
108  {
109  auto input = vec{","};
110  auto expected = vec{""};
111  EXPECT_EQ(expected, from(input) | resplit(',') | collect);
112  }
113 
114  {
115  auto input = vec{",,"};
116  auto expected = vec{"", ""};
117  EXPECT_EQ(expected, from(input) | resplit(',') | collect);
118  }
119 }
S resplit(char delimiter, bool keepDelimiter=false)
Definition: String.h:56
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static auto collect
Definition: StringTest.cpp:37
Definition: Traits.h:588
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from
TEST ( StringGen  ,
Resplit   
)

Definition at line 121 of file StringTest.cpp.

References folly::collect(), EXPECT_EQ, folly::gen::from(), and folly::gen::resplit().

121  {
122  {
123  auto input = vec{"hello,, world, goodbye, meow"};
124  auto expected = vec{"hello", "", " world", " goodbye", " meow"};
125  EXPECT_EQ(expected, from(input) | resplit(',') | collect);
126  }
127 
128  {
129  auto input = vec{"hel", "lo,", ", world", ", goodbye, m", "eow"};
130  auto expected = vec{"hello", "", " world", " goodbye", " meow"};
131  EXPECT_EQ(expected, from(input) | resplit(',') | collect);
132  }
133 }
S resplit(char delimiter, bool keepDelimiter=false)
Definition: String.h:56
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static auto collect
Definition: StringTest.cpp:37
Definition: Traits.h:588
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from
TEST ( StringGen  ,
ResplitKeepDelimiter   
)

Definition at line 135 of file StringTest.cpp.

References folly::collect(), EXPECT_EQ, folly::gen::from(), and folly::gen::resplit().

135  {
136  {
137  auto input = vec{"hello,, world, goodbye, meow"};
138  auto expected = vec{"hello,", ",", " world,", " goodbye,", " meow"};
139  EXPECT_EQ(expected, from(input) | resplit(',', true) | collect);
140  }
141 
142  {
143  auto input = vec{"hel", "lo,", ", world", ", goodbye, m", "eow"};
144  auto expected = vec{"hello,", ",", " world,", " goodbye,", " meow"};
145  EXPECT_EQ(expected, from(input) | resplit(',', true) | collect);
146  }
147 }
S resplit(char delimiter, bool keepDelimiter=false)
Definition: String.h:56
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static auto collect
Definition: StringTest.cpp:37
Definition: Traits.h:588
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from
TEST ( StringGen  ,
EachToTuple   
)

Definition at line 149 of file StringTest.cpp.

References EXPECT_EQ, EXPECT_THROW, folly::gen::lines(), std::tr1::make_tuple(), and folly::gen::split().

149  {
150  {
151  auto lines = "2:1.414:yo 3:1.732:hi";
152  // clang-format off
153  auto actual
154  = split(lines, ' ')
155  | eachToTuple<int, double, std::string>(':')
156  | as<vector>();
157  // clang-format on
158  vector<tuple<int, double, std::string>> expected{
159  make_tuple(2, 1.414, "yo"),
160  make_tuple(3, 1.732, "hi"),
161  };
162  EXPECT_EQ(expected, actual);
163  }
164  {
165  auto lines = "2 3";
166  // clang-format off
167  auto actual
168  = split(lines, ' ')
169  | eachToTuple<int>(',')
170  | as<vector>();
171  // clang-format on
172  vector<tuple<int>> expected{
173  make_tuple(2),
174  make_tuple(3),
175  };
176  EXPECT_EQ(expected, actual);
177  }
178  {
179  // StringPiece target
180  auto lines = "1:cat 2:dog";
181  // clang-format off
182  auto actual
183  = split(lines, ' ')
184  | eachToTuple<int, StringPiece>(':')
185  | as<vector>();
186  // clang-format on
187  vector<tuple<int, StringPiece>> expected{
188  make_tuple(1, "cat"),
189  make_tuple(2, "dog"),
190  };
191  EXPECT_EQ(expected, actual);
192  }
193  {
194  // Empty field
195  auto lines = "2:tjackson:4 3::5";
196  // clang-format off
197  auto actual
198  = split(lines, ' ')
199  | eachToTuple<int, fbstring, int>(':')
200  | as<vector>();
201  // clang-format on
202  vector<tuple<int, fbstring, int>> expected{
203  make_tuple(2, "tjackson", 4),
204  make_tuple(3, "", 5),
205  };
206  EXPECT_EQ(expected, actual);
207  }
208  {
209  // Excess fields
210  auto lines = "1:2 3:4:5";
211  // clang-format off
212  EXPECT_THROW(
213  (split(lines, ' ')
214  | eachToTuple<int, int>(':')
215  | as<vector>()),
216  std::runtime_error);
217  // clang-format on
218  }
219  {
220  // Missing fields
221  auto lines = "1:2:3 4:5";
222  // clang-format off
223  EXPECT_THROW(
224  (split(lines, ' ')
225  | eachToTuple<int, int, int>(':')
226  | as<vector>()),
227  std::runtime_error);
228  // clang-format on
229  }
230 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
tuple make_tuple()
Definition: gtest-tuple.h:675
S split(const StringPiece source, char delimiter)
Definition: String.h:61
S lines(StringPiece source)
Definition: String.h:80
TEST ( StringGen  ,
EachToPair   
)

Definition at line 232 of file StringTest.cpp.

References folly::gen::as(), EXPECT_EQ, folly::gen::lines(), and folly::gen::split().

232  {
233  {
234  // char delimiters
235  auto lines = "2:1.414 3:1.732";
236  // clang-format off
237  auto actual
238  = split(lines, ' ')
239  | eachToPair<int, double>(':')
240  | as<std::map<int, double>>();
241  // clang-format on
242  std::map<int, double> expected{
243  {3, 1.732},
244  {2, 1.414},
245  };
246  EXPECT_EQ(expected, actual);
247  }
248  {
249  // string delimiters
250  auto lines = "ab=>cd ef=>gh";
251  // clang-format off
252  auto actual
253  = split(lines, ' ')
254  | eachToPair<string, string>("=>")
255  | as<std::map<string, string>>();
256  // clang-format on
257  std::map<string, string> expected{
258  {"ab", "cd"},
259  {"ef", "gh"},
260  };
261  EXPECT_EQ(expected, actual);
262  }
263 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
S split(const StringPiece source, char delimiter)
Definition: String.h:61
S lines(StringPiece source)
Definition: String.h:80
Collect as()
Definition: Base.h:811
TEST ( StringGen  ,
ResplitMaxLength   
)

Definition at line 294 of file StringTest.cpp.

References checkResplitMaxLength().

294  {
295  // clang-format off
297  {"hel", "lo,", ", world", ", goodbye, m", "ew"}, ',', 5,
298  {"hello", ",", ",", " worl", "d,", " good", "bye,", " mew"});
299  // " meow" cannot be "end of stream", since it's maxLength long
301  {"hel", "lo,", ", world", ", goodbye, m", "eow"}, ',', 5,
302  {"hello", ",", ",", " worl", "d,", " good", "bye,", " meow", ""});
304  {"||", "", "", "", "|a|b", "cdefghijklmn", "|opqrst",
305  "uvwx|y|||", "z", "0123456789", "|", ""}, '|', 2,
306  {"|", "|", "|", "a|", "bc", "de", "fg", "hi", "jk", "lm", "n|", "op",
307  "qr", "st", "uv", "wx", "|", "y|", "|", "|", "z0", "12", "34", "56",
308  "78", "9|", ""});
309  // clang-format on
310 }
void checkResplitMaxLength(vector< string > ins, char delim, uint64_t maxLength, vector< string > outs)
Definition: StringTest.cpp:265
TEST ( StringGen  ,
Unsplit   
)

Definition at line 322 of file StringTest.cpp.

References buffer(), EXPECT_EQ, runUnsplitSuite(), s, folly::gen::seq(), folly::gen::split(), string, and folly::gen::unsplit().

322  {
323  auto basicFn = [](StringPiece s) {
324  EXPECT_EQ(split(s, ',') | unsplit(','), s);
325  };
326 
327  auto existingBuffer = [](StringPiece s) {
328  folly::fbstring buffer("asdf");
329  split(s, ',') | unsplit(',', &buffer);
330  auto expected = folly::to<folly::fbstring>("asdf", s.empty() ? "" : ",", s);
331  EXPECT_EQ(expected, buffer);
332  };
333 
334  auto emptyBuffer = [](StringPiece s) {
336  split(s, ',') | unsplit(',', &buffer);
337  EXPECT_EQ(s, buffer);
338  };
339 
340  auto stringDelim = [](StringPiece s) {
341  EXPECT_EQ(s, split(s, ',') | unsplit(","));
343  split(s, ',') | unsplit(",", &buffer);
344  EXPECT_EQ(buffer, s);
345  };
346 
347  runUnsplitSuite(basicFn);
348  runUnsplitSuite(existingBuffer);
349  runUnsplitSuite(emptyBuffer);
350  runUnsplitSuite(stringDelim);
351  EXPECT_EQ("1, 2, 3", seq(1, 3) | unsplit(", "));
352 }
std::vector< uint8_t > buffer(kBufferSize+16)
void runUnsplitSuite(F fn)
Definition: StringTest.cpp:313
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Gen seq(Value first, Value last)
Definition: Base.h:484
S split(const StringPiece source, char delimiter)
Definition: String.h:61
const char * string
Definition: Conv.cpp:212
static set< string > s
Unsplit unsplit(const Delimiter &delimiter)
Definition: String.h:101
TEST ( StringGen  ,
Batch   
)

Definition at line 354 of file StringTest.cpp.

References folly::gen::batch(), chunks, folly::gen::count, EXPECT_EQ, folly::gen::from(), folly::gen::lines(), folly::gen::rconcat, and folly::gen::resplit().

354  {
355  std::vector<std::string> chunks{
356  "on", "e\nt", "w", "o", "\nthr", "ee\nfo", "ur\n"};
357  std::vector<std::string> lines{"one", "two", "three", "four"};
358  EXPECT_EQ(4, from(chunks) | resplit('\n') | count);
359  EXPECT_EQ(4, from(chunks) | resplit('\n') | batch(2) | rconcat | count);
360  EXPECT_EQ(4, from(chunks) | resplit('\n') | batch(3) | rconcat | count);
361  // clang-format off
362  EXPECT_EQ(
363  lines,
364  from(chunks)
365  | resplit('\n')
366  | eachTo<std::string>()
367  | batch(3)
368  | rconcat
369  | as<vector>());
370  // clang-format on
371 }
auto chunks
S resplit(char delimiter, bool keepDelimiter=false)
Definition: String.h:56
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
detail::Batch batch(size_t batchSize)
Definition: Base-inl.h:2602
constexpr detail::RangeConcat rconcat
Definition: Base-inl.h:2571
S lines(StringPiece source)
Definition: String.h:80
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from
int * count
TEST ( StringGen  ,
UncurryTuple   
)

Definition at line 373 of file StringTest.cpp.

References EXPECT_EQ, folly::gen::map(), folly::gen::split(), folly::gen::sum, and folly::uncurry().

373  {
374  folly::StringPiece file = "1\t2\t3\n1\t4\t9";
375  auto rows = split(file, '\n') | eachToTuple<int, int, int>('\t');
376  auto productSum =
377  rows | map(uncurry([](int x, int y, int z) { return x * y * z; })) | sum;
378  EXPECT_EQ(42, productSum);
379 }
Definition: InvokeTest.cpp:58
std::atomic< int64_t > sum(0)
auto uncurry(F &&f) -> detail::apply_tuple::Uncurry< typename std::decay< F >::type >
Definition: ApplyTuple.h:178
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
S split(const StringPiece source, char delimiter)
Definition: String.h:61
static Map map(mapCap)
Definition: InvokeTest.cpp:72
Definition: InvokeTest.cpp:65
TEST ( StringGen  ,
UncurryPair   
)

Definition at line 381 of file StringTest.cpp.

References EXPECT_EQ, folly::gen::map(), folly::gen::split(), folly::gen::sum, and folly::uncurry().

381  {
382  folly::StringPiece file = "2\t3\n4\t9";
383  auto rows = split(file, '\n') | eachToPair<int, int>('\t');
384  auto productSum =
385  rows | map(uncurry([](int x, int y) { return x * y; })) | sum;
386  EXPECT_EQ(42, productSum);
387 }
Definition: InvokeTest.cpp:58
std::atomic< int64_t > sum(0)
auto uncurry(F &&f) -> detail::apply_tuple::Uncurry< typename std::decay< F >::type >
Definition: ApplyTuple.h:178
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
S split(const StringPiece source, char delimiter)
Definition: String.h:61
static Map map(mapCap)
Definition: InvokeTest.cpp:65

Variable Documentation

auto collect = eachTo<std::string>() | as<vector>()
static

Definition at line 37 of file StringTest.cpp.