1//===- LineIterator.cpp - Unit tests --------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Support/LineIterator.h"
11#include "llvm/Support/MemoryBuffer.h"
12#include "gtest/gtest.h"
13
14using namespace llvm;
15using namespace llvm::sys;
16
17namespace {
18
19TEST(LineIteratorTest, Basic) {
20  std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer("line 1\n"
21                                                                    "line 2\n"
22                                                                    "line 3");
23
24  line_iterator I = line_iterator(*Buffer), E;
25
26  EXPECT_FALSE(I.is_at_eof());
27  EXPECT_NE(E, I);
28
29  EXPECT_EQ("line 1", *I);
30  EXPECT_EQ(1, I.line_number());
31  ++I;
32  EXPECT_EQ("line 2", *I);
33  EXPECT_EQ(2, I.line_number());
34  ++I;
35  EXPECT_EQ("line 3", *I);
36  EXPECT_EQ(3, I.line_number());
37  ++I;
38
39  EXPECT_TRUE(I.is_at_eof());
40  EXPECT_EQ(E, I);
41}
42
43TEST(LineIteratorTest, CommentAndBlankSkipping) {
44  std::unique_ptr<MemoryBuffer> Buffer(
45      MemoryBuffer::getMemBuffer("line 1\n"
46                                 "line 2\n"
47                                 "# Comment 1\n"
48                                 "\n"
49                                 "line 5\n"
50                                 "\n"
51                                 "# Comment 2"));
52
53  line_iterator I = line_iterator(*Buffer, true, '#'), E;
54
55  EXPECT_FALSE(I.is_at_eof());
56  EXPECT_NE(E, I);
57
58  EXPECT_EQ("line 1", *I);
59  EXPECT_EQ(1, I.line_number());
60  ++I;
61  EXPECT_EQ("line 2", *I);
62  EXPECT_EQ(2, I.line_number());
63  ++I;
64  EXPECT_EQ("line 5", *I);
65  EXPECT_EQ(5, I.line_number());
66  ++I;
67
68  EXPECT_TRUE(I.is_at_eof());
69  EXPECT_EQ(E, I);
70}
71
72TEST(LineIteratorTest, CommentSkippingKeepBlanks) {
73  std::unique_ptr<MemoryBuffer> Buffer(
74      MemoryBuffer::getMemBuffer("line 1\n"
75                                 "line 2\n"
76                                 "# Comment 1\n"
77                                 "# Comment 2\n"
78                                 "\n"
79                                 "line 6\n"
80                                 "\n"
81                                 "# Comment 3"));
82
83  line_iterator I = line_iterator(*Buffer, false, '#'), E;
84
85  EXPECT_FALSE(I.is_at_eof());
86  EXPECT_NE(E, I);
87
88  EXPECT_EQ("line 1", *I);
89  EXPECT_EQ(1, I.line_number());
90  ++I;
91  EXPECT_EQ("line 2", *I);
92  EXPECT_EQ(2, I.line_number());
93  ++I;
94  EXPECT_EQ("", *I);
95  EXPECT_EQ(5, I.line_number());
96  ++I;
97  EXPECT_EQ("line 6", *I);
98  EXPECT_EQ(6, I.line_number());
99  ++I;
100  EXPECT_EQ("", *I);
101  EXPECT_EQ(7, I.line_number());
102  ++I;
103
104  EXPECT_TRUE(I.is_at_eof());
105  EXPECT_EQ(E, I);
106}
107
108
109TEST(LineIteratorTest, BlankSkipping) {
110  std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer("\n\n\n"
111                                                                    "line 1\n"
112                                                                    "\n\n\n"
113                                                                    "line 2\n"
114                                                                    "\n\n\n");
115
116  line_iterator I = line_iterator(*Buffer), E;
117
118  EXPECT_FALSE(I.is_at_eof());
119  EXPECT_NE(E, I);
120
121  EXPECT_EQ("line 1", *I);
122  EXPECT_EQ(4, I.line_number());
123  ++I;
124  EXPECT_EQ("line 2", *I);
125  EXPECT_EQ(8, I.line_number());
126  ++I;
127
128  EXPECT_TRUE(I.is_at_eof());
129  EXPECT_EQ(E, I);
130}
131
132TEST(LineIteratorTest, BlankKeeping) {
133  std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer("\n\n"
134                                                                    "line 3\n"
135                                                                    "\n"
136                                                                    "line 5\n"
137                                                                    "\n\n");
138  line_iterator I = line_iterator(*Buffer, false), E;
139
140  EXPECT_FALSE(I.is_at_eof());
141  EXPECT_NE(E, I);
142
143  EXPECT_EQ("", *I);
144  EXPECT_EQ(1, I.line_number());
145  ++I;
146  EXPECT_EQ("", *I);
147  EXPECT_EQ(2, I.line_number());
148  ++I;
149  EXPECT_EQ("line 3", *I);
150  EXPECT_EQ(3, I.line_number());
151  ++I;
152  EXPECT_EQ("", *I);
153  EXPECT_EQ(4, I.line_number());
154  ++I;
155  EXPECT_EQ("line 5", *I);
156  EXPECT_EQ(5, I.line_number());
157  ++I;
158  EXPECT_EQ("", *I);
159  EXPECT_EQ(6, I.line_number());
160  ++I;
161  EXPECT_EQ("", *I);
162  EXPECT_EQ(7, I.line_number());
163  ++I;
164
165  EXPECT_TRUE(I.is_at_eof());
166  EXPECT_EQ(E, I);
167}
168
169TEST(LineIteratorTest, EmptyBuffers) {
170  std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer("");
171  EXPECT_TRUE(line_iterator(*Buffer).is_at_eof());
172  EXPECT_EQ(line_iterator(), line_iterator(*Buffer));
173  EXPECT_TRUE(line_iterator(*Buffer, false).is_at_eof());
174  EXPECT_EQ(line_iterator(), line_iterator(*Buffer, false));
175
176  Buffer = MemoryBuffer::getMemBuffer("\n\n\n");
177  EXPECT_TRUE(line_iterator(*Buffer).is_at_eof());
178  EXPECT_EQ(line_iterator(), line_iterator(*Buffer));
179
180  Buffer = MemoryBuffer::getMemBuffer("# foo\n"
181                                      "\n"
182                                      "# bar");
183  EXPECT_TRUE(line_iterator(*Buffer, true, '#').is_at_eof());
184  EXPECT_EQ(line_iterator(), line_iterator(*Buffer, true, '#'));
185
186  Buffer = MemoryBuffer::getMemBuffer("\n"
187                                      "# baz\n"
188                                      "\n");
189  EXPECT_TRUE(line_iterator(*Buffer, true, '#').is_at_eof());
190  EXPECT_EQ(line_iterator(), line_iterator(*Buffer, true, '#'));
191}
192
193} // anonymous namespace
194