2 * Copyright 2016 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.
16 #include <gtest/gtest.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>
26 using namespace folly::gen;
27 using namespace folly;
31 TEST(FileGen, ByLine) {
32 auto collect = eachTo<std::string>() | as<vector>();
33 const std::string cases[] = {
35 "This is the second line\n"
38 "a few empty lines above\n"
39 "incomplete last line",
41 "complete last line\n",
48 for (auto& lines : cases) {
49 test::TemporaryFile file("ByLine");
50 EXPECT_EQ(lines.size(), write(file.fd(), lines.data(), lines.size()));
52 auto expected = from({lines}) | resplit('\n') | collect;
53 auto found = byLine(file.path().c_str()) | collect;
55 EXPECT_EQ(expected, found) << "For Input: '" << lines << "'";
59 class FileGenBufferedTest : public ::testing::TestWithParam<int> { };
61 TEST_P(FileGenBufferedTest, FileWriter) {
62 size_t bufferSize = GetParam();
63 test::TemporaryFile file("FileWriter");
65 static const std::string lines(
67 "This is the second line\n"
70 "a few empty lines above\n");
72 auto src = from({lines, lines, lines, lines, lines, lines, lines, lines});
73 auto collect = eachTo<std::string>() | as<vector>();
74 auto expected = src | resplit('\n') | collect;
76 src | eachAs<StringPiece>() | toFile(File(file.fd()), bufferSize);
77 auto found = byLine(file.path().c_str()) | collect;
79 EXPECT_TRUE(expected == found);
82 TEST(FileGenBufferedTest, FileWriterSimple) {
83 test::TemporaryFile file("FileWriter");
84 auto toLine = [](int v) { return to<std::string>(v, '\n'); };
86 auto squares = seq(1, 100) | map([](int x) { return x * x; });
87 squares | map(toLine) | eachAs<StringPiece>() | toFile(File(file.fd()));
88 EXPECT_EQ(squares | sum,
89 byLine(File(file.path().c_str())) | eachTo<int>() | sum);
92 INSTANTIATE_TEST_CASE_P(
95 ::testing::Values(0, 1, 2, 4, 8, 64, 4096));
97 int main(int argc, char *argv[]) {
98 testing::InitGoogleTest(&argc, argv);
99 gflags::ParseCommandLineFlags(&argc, &argv, true);
100 return RUN_ALL_TESTS();