1 /*
2 * Copyright (c) Facebook, Inc. and its affiliates.
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
17 #include <folly/experimental/symbolizer/LineReader.h>
18
19 #include <glog/logging.h>
20
21 #include <folly/FileUtil.h>
22 #include <folly/experimental/TestUtil.h>
23 #include <folly/portability/GTest.h>
24
25 namespace folly {
26 namespace symbolizer {
27 namespace test {
28
29 using folly::test::TemporaryFile;
30
writeAll(int fd,const char * str)31 void writeAll(int fd, const char* str) {
32 ssize_t n = strlen(str);
33 CHECK_EQ(n, writeFull(fd, str, n));
34 }
35
expect(LineReader & lr,const char * expected)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
TEST(LineReader,Simple)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
91