proxygen
LineReaderTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2013-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 
18 
19 #include <glog/logging.h>
20 
21 #include <folly/FileUtil.h>
24 
25 namespace folly {
26 namespace symbolizer {
27 namespace test {
28 
30 
31 void writeAll(int fd, const char* str) {
32  ssize_t n = strlen(str);
33  CHECK_EQ(n, writeFull(fd, str, n));
34 }
35 
36 void expect(LineReader& lr, const char* expected) {
37  StringPiece line;
38  size_t expectedLen = strlen(expected);
39  EXPECT_EQ(
40  expectedLen != 0 ? LineReader::kReading : LineReader::kEof,
41  lr.readLine(line));
42  EXPECT_EQ(expectedLen, line.size());
43  EXPECT_EQ(std::string(expected, expectedLen), line.str());
44 }
45 
46 TEST(LineReader, Simple) {
47  TemporaryFile file;
48  int fd = file.fd();
49  writeAll(
50  fd,
51  "Meow\n"
52  "Hello world\n"
53  "This is a long line. It is longer than the other lines.\n"
54  "\n"
55  "Incomplete last line");
56 
57  {
58  CHECK_ERR(lseek(fd, 0, SEEK_SET));
59  char buf[10];
60  LineReader lr(fd, buf, sizeof(buf));
61  expect(lr, "Meow\n");
62  expect(lr, "Hello worl");
63  expect(lr, "d\n");
64  expect(lr, "This is a ");
65  expect(lr, "long line.");
66  expect(lr, " It is lon");
67  expect(lr, "ger than t");
68  expect(lr, "he other l");
69  expect(lr, "ines.\n");
70  expect(lr, "\n");
71  expect(lr, "Incomplete");
72  expect(lr, " last line");
73  expect(lr, "");
74  }
75 
76  {
77  CHECK_ERR(lseek(fd, 0, SEEK_SET));
78  char buf[80];
79  LineReader lr(fd, buf, sizeof(buf));
80  expect(lr, "Meow\n");
81  expect(lr, "Hello world\n");
82  expect(lr, "This is a long line. It is longer than the other lines.\n");
83  expect(lr, "\n");
84  expect(lr, "Incomplete last line");
85  expect(lr, "");
86  }
87 }
88 } // namespace test
89 } // namespace symbolizer
90 } // namespace folly
std::string str() const
Definition: Range.h:591
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr size_type size() const
Definition: Range.h:431
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
TEST(LineReader, Simple)
State readLine(StringPiece &line)
Definition: LineReader.cpp:35
void writeAll(int fd, const char *str)
ssize_t writeFull(int fd, const void *buf, size_t count)
Definition: FileUtil.cpp:134
void expect(LineReader &lr, const char *expected)
const char * string
Definition: Conv.cpp:212