cc5bfa19fbd7c91cded09b6b22e991171fc9bf12
[folly.git] / folly / gen / test / FileTest.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #include <string>
17 #include <vector>
18
19 #include <folly/File.h>
20 #include <folly/Range.h>
21 #include <folly/experimental/TestUtil.h>
22 #include <folly/gen/Base.h>
23 #include <folly/gen/File.h>
24 #include <folly/portability/GTest.h>
25
26 using namespace folly::gen;
27 using namespace folly;
28 using std::string;
29 using std::vector;
30
31 TEST(FileGen, ByLine) {
32   auto collect = eachTo<std::string>() | as<vector>();
33   const std::string cases[] = {
34       "Hello world\n"
35       "This is the second line\n"
36       "\n"
37       "\n"
38       "a few empty lines above\n"
39       "incomplete last line",
40
41       "complete last line\n",
42
43       "\n",
44
45       "",
46   };
47
48   for (auto& lines : cases) {
49     test::TemporaryFile file("ByLine");
50     EXPECT_EQ(lines.size(), write(file.fd(), lines.data(), lines.size()));
51
52     auto expected = from({lines}) | resplit('\n') | collect;
53     auto found = byLine(file.path().string().c_str()) | collect;
54
55     EXPECT_EQ(expected, found) << "For Input: '" << lines << "'";
56   }
57 }
58
59 class FileGenBufferedTest : public ::testing::TestWithParam<int> { };
60
61 TEST_P(FileGenBufferedTest, FileWriter) {
62   size_t bufferSize = GetParam();
63   test::TemporaryFile file("FileWriter");
64
65   static const std::string lines(
66       "Hello world\n"
67       "This is the second line\n"
68       "\n"
69       "\n"
70       "a few empty lines above\n");
71
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;
75
76   src | eachAs<StringPiece>() | toFile(File(file.fd()), bufferSize);
77   auto found = byLine(file.path().string().c_str()) | collect;
78
79   EXPECT_TRUE(expected == found);
80 }
81
82 TEST(FileGenBufferedTest, FileWriterSimple) {
83   test::TemporaryFile file("FileWriter");
84   auto toLine = [](int v) { return to<std::string>(v, '\n'); };
85
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().string().c_str())) | eachTo<int>() | sum);
90 }
91
92 INSTANTIATE_TEST_CASE_P(
93     DifferentBufferSizes,
94     FileGenBufferedTest,
95     ::testing::Values(0, 1, 2, 4, 8, 64, 4096));