From a0198f14f7a4e29159ac959d56770380c447b2bc Mon Sep 17 00:00:00 2001 From: Tom Jackson Date: Fri, 26 Jun 2015 13:30:07 -0700 Subject: [PATCH] Fix resplit | batch 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 | 2 +- folly/gen/test/StringTest.cpp | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/folly/gen/String-inl.h b/folly/gen/String-inl.h index 4fb0f60b..579e5b1b 100644 --- a/folly/gen/String-inl.h +++ b/folly/gen/String-inl.h @@ -231,7 +231,7 @@ class StringResplitter : public Operator { // 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); diff --git a/folly/gen/test/StringTest.cpp b/folly/gen/test/StringTest.cpp index 9aea442a..8ed19799 100644 --- a/folly/gen/test/StringTest.cpp +++ b/folly/gen/test/StringTest.cpp @@ -348,6 +348,20 @@ TEST(StringGen, Unsplit) { EXPECT_EQ("1, 2, 3", seq(1, 3) | unsplit(", ")); } +TEST(StringGen, Batch) { + std::vector chunks{ + "on", "e\nt", "w", "o", "\nthr", "ee\nfo", "ur\n", + }; + std::vector 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() | + batch(3) | rconcat | as()); +} + int main(int argc, char *argv[]) { testing::InitGoogleTest(&argc, argv); gflags::ParseCommandLineFlags(&argc, &argv, true); -- 2.34.1