Adds writer test case for RCU
[folly.git] / folly / gen / test / FileTest.cpp
1 /*
2  * Copyright 2014-present 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/container/Array.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>
26
27 using namespace folly::gen;
28 using namespace folly;
29 using std::string;
30 using std::vector;
31
32 TEST(FileGen, ByLine) {
33   auto collect = eachTo<std::string>() | as<vector>();
34   const std::string cases[] = {
35       "Hello world\n"
36       "This is the second line\n"
37       "\n"
38       "\n"
39       "a few empty lines above\n"
40       "incomplete last line",
41
42       "complete last line\n",
43
44       "\n",
45
46       "",
47   };
48
49   for (auto& lines : cases) {
50     test::TemporaryFile file("ByLine");
51     EXPECT_EQ(lines.size(), write(file.fd(), lines.data(), lines.size()));
52
53     auto expected = from({lines}) | resplit('\n') | collect;
54     auto found = byLine(file.path().string().c_str()) | collect;
55
56     EXPECT_EQ(expected, found) << "For Input: '" << lines << "'";
57   }
58 }
59
60 TEST(FileGen, ByLineFull) {
61   auto cases = std::vector<std::string> {
62        stripLeftMargin(R"(
63          Hello world
64          This is the second line
65
66
67          a few empty lines above
68          incomplete last line)"),
69
70          "complete last line\n",
71
72          "\n",
73
74          ""};
75
76   for (auto& lines : cases) {
77     test::TemporaryFile file("ByLineFull");
78     EXPECT_EQ(lines.size(), write(file.fd(), lines.data(), lines.size()));
79
80     auto found =
81         byLineFull(file.path().string().c_str()) | unsplit<std::string>("");
82
83     EXPECT_EQ(lines, found);
84   }
85 }
86
87 class FileGenBufferedTest : public ::testing::TestWithParam<int> {};
88
89 TEST_P(FileGenBufferedTest, FileWriter) {
90   size_t bufferSize = GetParam();
91   test::TemporaryFile file("FileWriter");
92
93   static const std::string lines(
94       "Hello world\n"
95       "This is the second line\n"
96       "\n"
97       "\n"
98       "a few empty lines above\n");
99
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;
103
104   src | eachAs<StringPiece>() | toFile(File(file.fd()), bufferSize);
105   auto found = byLine(file.path().string().c_str()) | collect;
106
107   EXPECT_TRUE(expected == found);
108 }
109
110 TEST(FileGenBufferedTest, FileWriterSimple) {
111   test::TemporaryFile file("FileWriter");
112   auto toLine = [](int v) { return to<std::string>(v, '\n'); };
113
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);
118 }
119
120 INSTANTIATE_TEST_CASE_P(
121     DifferentBufferSizes,
122     FileGenBufferedTest,
123     ::testing::Values(0, 1, 2, 4, 8, 64, 4096));