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