• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
14 using namespace llvm;
15 using namespace llvm::sys;
16 
17 namespace {
18 
TEST(LineIteratorTest,Basic)19 TEST(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 
TEST(LineIteratorTest,CommentSkipping)43 TEST(LineIteratorTest, CommentSkipping) {
44   std::unique_ptr<MemoryBuffer> Buffer(
45       MemoryBuffer::getMemBuffer("line 1\n"
46                                  "line 2\n"
47                                  "# Comment 1\n"
48                                  "line 4\n"
49                                  "# Comment 2"));
50 
51   line_iterator I = line_iterator(*Buffer, '#'), E;
52 
53   EXPECT_FALSE(I.is_at_eof());
54   EXPECT_NE(E, I);
55 
56   EXPECT_EQ("line 1", *I);
57   EXPECT_EQ(1, I.line_number());
58   ++I;
59   EXPECT_EQ("line 2", *I);
60   EXPECT_EQ(2, I.line_number());
61   ++I;
62   EXPECT_EQ("line 4", *I);
63   EXPECT_EQ(4, I.line_number());
64   ++I;
65 
66   EXPECT_TRUE(I.is_at_eof());
67   EXPECT_EQ(E, I);
68 }
69 
TEST(LineIteratorTest,BlankSkipping)70 TEST(LineIteratorTest, BlankSkipping) {
71   std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer("\n\n\n"
72                                                                   "line 1\n"
73                                                                   "\n\n\n"
74                                                                   "line 2\n"
75                                                                   "\n\n\n"));
76 
77   line_iterator I = line_iterator(*Buffer), E;
78 
79   EXPECT_FALSE(I.is_at_eof());
80   EXPECT_NE(E, I);
81 
82   EXPECT_EQ("line 1", *I);
83   EXPECT_EQ(4, I.line_number());
84   ++I;
85   EXPECT_EQ("line 2", *I);
86   EXPECT_EQ(8, I.line_number());
87   ++I;
88 
89   EXPECT_TRUE(I.is_at_eof());
90   EXPECT_EQ(E, I);
91 }
92 
TEST(LineIteratorTest,EmptyBuffers)93 TEST(LineIteratorTest, EmptyBuffers) {
94   std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer(""));
95   EXPECT_TRUE(line_iterator(*Buffer).is_at_eof());
96   EXPECT_EQ(line_iterator(), line_iterator(*Buffer));
97 
98   Buffer.reset(MemoryBuffer::getMemBuffer("\n\n\n"));
99   EXPECT_TRUE(line_iterator(*Buffer).is_at_eof());
100   EXPECT_EQ(line_iterator(), line_iterator(*Buffer));
101 
102   Buffer.reset(MemoryBuffer::getMemBuffer("# foo\n"
103                                           "\n"
104                                           "# bar"));
105   EXPECT_TRUE(line_iterator(*Buffer, '#').is_at_eof());
106   EXPECT_EQ(line_iterator(), line_iterator(*Buffer, '#'));
107 
108   Buffer.reset(MemoryBuffer::getMemBuffer("\n"
109                                           "# baz\n"
110                                           "\n"));
111   EXPECT_TRUE(line_iterator(*Buffer, '#').is_at_eof());
112   EXPECT_EQ(line_iterator(), line_iterator(*Buffer, '#'));
113 }
114 
115 } // anonymous namespace
116