proxygen
FileTest.cpp
Go to the documentation of this file.
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>
23 #include <folly/gen/Base.h>
24 #include <folly/gen/File.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 
77  for (auto& lines : cases) {
78  test::TemporaryFile file("ByLineFull");
79  EXPECT_EQ(lines.size(), write(file.fd(), lines.data(), lines.size()));
80 
81  auto found =
82  byLineFull(file.path().string().c_str()) | unsplit<std::string>("");
83 
84  EXPECT_EQ(lines, found);
85  }
86 }
87 
88 class FileGenBufferedTest : public ::testing::TestWithParam<int> {};
89 
91  size_t bufferSize = GetParam();
92  test::TemporaryFile file("FileWriter");
93 
94  static const std::string lines(
95  "Hello world\n"
96  "This is the second line\n"
97  "\n"
98  "\n"
99  "a few empty lines above\n");
100 
101  auto src = from({lines, lines, lines, lines, lines, lines, lines, lines});
102  auto collect = eachTo<std::string>() | as<vector>();
103  auto expected = src | resplit('\n') | collect;
104 
105  src | eachAs<StringPiece>() | toFile(File(file.fd()), bufferSize);
106  auto found = byLine(file.path().string().c_str()) | collect;
107 
108  EXPECT_TRUE(expected == found);
109 }
110 
111 TEST(FileGenBufferedTest, FileWriterSimple) {
112  test::TemporaryFile file("FileWriter");
113  auto toLine = [](int v) { return to<std::string>(v, '\n'); };
114 
115  auto squares = seq(1, 100) | map([](int x) { return x * x; });
116  squares | map(toLine) | eachAs<StringPiece>() | toFile(File(file.fd()));
117  EXPECT_EQ(
118  squares | sum,
119  byLine(File(file.path().string().c_str())) | eachTo<int>() | sum);
120 }
121 
123  DifferentBufferSizes,
125  ::testing::Values(0, 1, 2, 4, 8, 64, 4096));
Definition: InvokeTest.cpp:58
S resplit(char delimiter, bool keepDelimiter=false)
Definition: String.h:56
auto v
void write(const T &in, folly::io::Appender &appender)
Definition: Types-inl.h:112
From from(Container &source)
Definition: Base.h:438
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const int x
Gen seq(Value first, Value last)
Definition: Base.h:484
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
auto byLineFull(File file, char delim= '\n')
Definition: File-inl.h:136
std::string stripLeftMargin(std::string s)
Definition: String.cpp:704
constexpr detail::Sum sum
Definition: Base-inl.h:2549
INSTANTIATE_TEST_CASE_P(DifferentBufferSizes, FileGenBufferedTest,::testing::Values(0, 1, 2, 4, 8, 64, 4096))
Map map(Predicate pred=Predicate())
Definition: Base.h:545
const fs::path & path() const
Definition: TestUtil.cpp:85
S lines(StringPiece source)
Definition: String.h:80
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
Future< std::vector< typename std::iterator_traits< InputIterator >::value_type::value_type > > collect(InputIterator first, InputIterator last)
Definition: Future-inl.h:1536
auto byLine(File file, char delim= '\n')
Definition: File-inl.h:148
const char * string
Definition: Conv.cpp:212
S toFile(File file, size_t bufferSize=4096)
Definition: File.h:55
TEST_P(FileGenBufferedTest, FileWriter)
Definition: FileTest.cpp:90
TEST(IStream, ByLine)
Definition: IStreamTest.cpp:32