Fix resplit | batch
authorTom Jackson <tjackson@fb.com>
Fri, 26 Jun 2015 20:30:07 +0000 (13:30 -0700)
committerSara Golemon <sgolemon@fb.com>
Mon, 29 Jun 2015 18:00:32 +0000 (11:00 -0700)
Summary: Resplit violated the contract of `apply()`; it returned `false` even though the input sequence was fully consumed.

Reviewed By: @philippv

Differential Revision: D2195889

folly/gen/String-inl.h
folly/gen/test/StringTest.cpp

index 4fb0f60b80bf56e2eea1c52ca5eef9bae9b45553..579e5b1b9534254b13329869e801876dab634524 100644 (file)
@@ -231,7 +231,7 @@ class StringResplitter : public Operator<StringResplitter> {
             // The stream ended with a delimiter; our contract is to swallow
             // the final empty piece.
             if (s.empty()) {
-              return false;
+              return true;
             }
             if (s.back() != this->delimiter_) {
               return body(s);
index 9aea442a9c653ebd5858636c4df5ca90f2b6d217..8ed1979930d3a702ea5178bfbf0b1d2f86787257 100644 (file)
@@ -348,6 +348,20 @@ TEST(StringGen, Unsplit) {
   EXPECT_EQ("1, 2, 3", seq(1, 3) | unsplit(", "));
 }
 
+TEST(StringGen, Batch) {
+  std::vector<std::string> chunks{
+      "on", "e\nt", "w", "o", "\nthr", "ee\nfo", "ur\n",
+  };
+  std::vector<std::string> lines{
+      "one", "two", "three", "four",
+  };
+  EXPECT_EQ(4, from(chunks) | resplit('\n') | count);
+  EXPECT_EQ(4, from(chunks) | resplit('\n') | batch(2) | rconcat | count);
+  EXPECT_EQ(4, from(chunks) | resplit('\n') | batch(3) | rconcat | count);
+  EXPECT_EQ(lines, from(chunks) | resplit('\n') | eachTo<std::string>() |
+                       batch(3) | rconcat | as<vector>());
+}
+
 int main(int argc, char *argv[]) {
   testing::InitGoogleTest(&argc, argv);
   gflags::ParseCommandLineFlags(&argc, &argv, true);