[llvm-readobj] - Simplify stack-sizes.test test case.
[llvm-complete.git] / unittests / Support / LineIteratorTest.cpp
blob96cf37a26d3cf509ffadf4f40e541dc58575ac22
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 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/LineIterator.h"
10 #include "llvm/Support/MemoryBuffer.h"
11 #include "gtest/gtest.h"
13 using namespace llvm;
14 using namespace llvm::sys;
16 namespace {
18 TEST(LineIteratorTest, Basic) {
19 std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer("line 1\n"
20 "line 2\n"
21 "line 3");
23 line_iterator I = line_iterator(*Buffer), E;
25 EXPECT_FALSE(I.is_at_eof());
26 EXPECT_NE(E, I);
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;
38 EXPECT_TRUE(I.is_at_eof());
39 EXPECT_EQ(E, I);
42 TEST(LineIteratorTest, CommentAndBlankSkipping) {
43 std::unique_ptr<MemoryBuffer> Buffer(
44 MemoryBuffer::getMemBuffer("line 1\n"
45 "line 2\n"
46 "# Comment 1\n"
47 "\n"
48 "line 5\n"
49 "\n"
50 "# Comment 2"));
52 line_iterator I = line_iterator(*Buffer, true, '#'), E;
54 EXPECT_FALSE(I.is_at_eof());
55 EXPECT_NE(E, I);
57 EXPECT_EQ("line 1", *I);
58 EXPECT_EQ(1, I.line_number());
59 ++I;
60 EXPECT_EQ("line 2", *I);
61 EXPECT_EQ(2, I.line_number());
62 ++I;
63 EXPECT_EQ("line 5", *I);
64 EXPECT_EQ(5, I.line_number());
65 ++I;
67 EXPECT_TRUE(I.is_at_eof());
68 EXPECT_EQ(E, I);
71 TEST(LineIteratorTest, CommentSkippingKeepBlanks) {
72 std::unique_ptr<MemoryBuffer> Buffer(
73 MemoryBuffer::getMemBuffer("line 1\n"
74 "line 2\n"
75 "# Comment 1\n"
76 "# Comment 2\n"
77 "\n"
78 "line 6\n"
79 "\n"
80 "# Comment 3"));
82 line_iterator I = line_iterator(*Buffer, false, '#'), E;
84 EXPECT_FALSE(I.is_at_eof());
85 EXPECT_NE(E, I);
87 EXPECT_EQ("line 1", *I);
88 EXPECT_EQ(1, I.line_number());
89 ++I;
90 EXPECT_EQ("line 2", *I);
91 EXPECT_EQ(2, I.line_number());
92 ++I;
93 EXPECT_EQ("", *I);
94 EXPECT_EQ(5, I.line_number());
95 ++I;
96 EXPECT_EQ("line 6", *I);
97 EXPECT_EQ(6, I.line_number());
98 ++I;
99 EXPECT_EQ("", *I);
100 EXPECT_EQ(7, I.line_number());
101 ++I;
103 EXPECT_TRUE(I.is_at_eof());
104 EXPECT_EQ(E, I);
108 TEST(LineIteratorTest, BlankSkipping) {
109 std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer("\n\n\n"
110 "line 1\n"
111 "\n\n\n"
112 "line 2\n"
113 "\n\n\n");
115 line_iterator I = line_iterator(*Buffer), E;
117 EXPECT_FALSE(I.is_at_eof());
118 EXPECT_NE(E, I);
120 EXPECT_EQ("line 1", *I);
121 EXPECT_EQ(4, I.line_number());
122 ++I;
123 EXPECT_EQ("line 2", *I);
124 EXPECT_EQ(8, I.line_number());
125 ++I;
127 EXPECT_TRUE(I.is_at_eof());
128 EXPECT_EQ(E, I);
131 TEST(LineIteratorTest, BlankKeeping) {
132 std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer("\n\n"
133 "line 3\n"
134 "\n"
135 "line 5\n"
136 "\n\n");
137 line_iterator I = line_iterator(*Buffer, false), E;
139 EXPECT_FALSE(I.is_at_eof());
140 EXPECT_NE(E, I);
142 EXPECT_EQ("", *I);
143 EXPECT_EQ(1, I.line_number());
144 ++I;
145 EXPECT_EQ("", *I);
146 EXPECT_EQ(2, I.line_number());
147 ++I;
148 EXPECT_EQ("line 3", *I);
149 EXPECT_EQ(3, I.line_number());
150 ++I;
151 EXPECT_EQ("", *I);
152 EXPECT_EQ(4, I.line_number());
153 ++I;
154 EXPECT_EQ("line 5", *I);
155 EXPECT_EQ(5, I.line_number());
156 ++I;
157 EXPECT_EQ("", *I);
158 EXPECT_EQ(6, I.line_number());
159 ++I;
160 EXPECT_EQ("", *I);
161 EXPECT_EQ(7, I.line_number());
162 ++I;
164 EXPECT_TRUE(I.is_at_eof());
165 EXPECT_EQ(E, I);
168 TEST(LineIteratorTest, EmptyBuffers) {
169 std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer("");
170 EXPECT_TRUE(line_iterator(*Buffer).is_at_eof());
171 EXPECT_EQ(line_iterator(), line_iterator(*Buffer));
172 EXPECT_TRUE(line_iterator(*Buffer, false).is_at_eof());
173 EXPECT_EQ(line_iterator(), line_iterator(*Buffer, false));
175 Buffer = MemoryBuffer::getMemBuffer("\n\n\n");
176 EXPECT_TRUE(line_iterator(*Buffer).is_at_eof());
177 EXPECT_EQ(line_iterator(), line_iterator(*Buffer));
179 Buffer = MemoryBuffer::getMemBuffer("# foo\n"
180 "\n"
181 "# bar");
182 EXPECT_TRUE(line_iterator(*Buffer, true, '#').is_at_eof());
183 EXPECT_EQ(line_iterator(), line_iterator(*Buffer, true, '#'));
185 Buffer = MemoryBuffer::getMemBuffer("\n"
186 "# baz\n"
187 "\n");
188 EXPECT_TRUE(line_iterator(*Buffer, true, '#').is_at_eof());
189 EXPECT_EQ(line_iterator(), line_iterator(*Buffer, true, '#'));
192 } // anonymous namespace