proxygen
IStreamTest.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 <folly/gen/IStream.h>
17 
18 #include <sstream>
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
23 #include <folly/gen/Base.h>
24 #include <folly/gen/String.h>
26 
27 namespace folly {
28 namespace gen {
29 
30 // Test mixed consumption of std::istream with and without folly::gen, and
31 // demonstrates partial consumption of input.
32 TEST(IStream, ByLine) {
33  std::stringstream in(
34  "2\n" // Count applies to both below groups.
35  "1\tred\n"
36  "3\tblue\n"
37  "3.4\t5.6\t7.8\n"
38  "1.1\t2.2\t3.3\n");
39  size_t n;
40  in >> n;
41  in.get(); // Consume trailing newline
42  auto colors = byLine(in) | take(n) | eachToPair<int, std::string>('\t') |
43  as<std::vector>();
44  auto coords = byLine(in) | take(n) | eachToTuple<float, float, float>('\t') |
45  as<std::vector>();
46  EXPECT_EQ(
47  colors,
48  (std::vector<std::pair<int, std::string>>{
49  {1, "red"},
50  {3, "blue"},
51  }));
52  EXPECT_EQ(
53  coords,
54  (std::vector<std::tuple<float, float, float>>({
55  std::tuple<float, float, float>{3.4f, 5.6f, 7.8f},
56  std::tuple<float, float, float>{1.1f, 2.2f, 3.3f},
57  })));
58 }
59 
60 } // namespace gen
61 } // namespace folly
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
auto byLine(File file, char delim= '\n')
Definition: File-inl.h:148
detail::Take take(Number count)
Definition: Base-inl.h:2582
TEST(IStream, ByLine)
Definition: IStreamTest.cpp:32