2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <folly/Array.h>
20 #include <folly/File.h>
21 #include <folly/Range.h>
22 #include <folly/experimental/TestUtil.h>
23 #include <folly/gen/Base.h>
24 #include <folly/gen/File.h>
25 #include <folly/portability/GTest.h>
27 using namespace folly::gen;
28 using namespace folly;
32 TEST(FileGen, ByLine) {
33 auto collect = eachTo<std::string>() | as<vector>();
34 const std::string cases[] = {
36 "This is the second line\n"
39 "a few empty lines above\n"
40 "incomplete last line",
42 "complete last line\n",
49 for (auto& lines : cases) {
50 test::TemporaryFile file("ByLine");
51 EXPECT_EQ(lines.size(), write(file.fd(), lines.data(), lines.size()));
53 auto expected = from({lines}) | resplit('\n') | collect;
54 auto found = byLine(file.path().string().c_str()) | collect;
56 EXPECT_EQ(expected, found) << "For Input: '" << lines << "'";
60 TEST(FileGen, ByLineFull) {
61 auto cases = std::vector<std::string> {
64 This is the second line
67 a few empty lines above
68 incomplete last line)"),
70 "complete last line\n",
76 for (auto& lines : cases) {
77 test::TemporaryFile file("ByLineFull");
78 EXPECT_EQ(lines.size(), write(file.fd(), lines.data(), lines.size()));
81 byLineFull(file.path().string().c_str()) | unsplit<std::string>("");
83 EXPECT_EQ(lines, found);
87 class FileGenBufferedTest : public ::testing::TestWithParam<int> {};
89 TEST_P(FileGenBufferedTest, FileWriter) {
90 size_t bufferSize = GetParam();
91 test::TemporaryFile file("FileWriter");
93 static const std::string lines(
95 "This is the second line\n"
98 "a few empty lines above\n");
100 auto src = from({lines, lines, lines, lines, lines, lines, lines, lines});
101 auto collect = eachTo<std::string>() | as<vector>();
102 auto expected = src | resplit('\n') | collect;
104 src | eachAs<StringPiece>() | toFile(File(file.fd()), bufferSize);
105 auto found = byLine(file.path().string().c_str()) | collect;
107 EXPECT_TRUE(expected == found);
110 TEST(FileGenBufferedTest, FileWriterSimple) {
111 test::TemporaryFile file("FileWriter");
112 auto toLine = [](int v) { return to<std::string>(v, '\n'); };
114 auto squares = seq(1, 100) | map([](int x) { return x * x; });
115 squares | map(toLine) | eachAs<StringPiece>() | toFile(File(file.fd()));
116 EXPECT_EQ(squares | sum,
117 byLine(File(file.path().string().c_str())) | eachTo<int>() | sum);
120 INSTANTIATE_TEST_CASE_P(
121 DifferentBufferSizes,
123 ::testing::Values(0, 1, 2, 4, 8, 64, 4096));