xref: /glogg/tests/linepositionarrayTest.cpp (revision 6e2e573c451ec24d720c967fab68538eaa3f3ab5)
1 #include "gmock/gmock.h"
2 
3 #include "config.h"
4 
5 #include "log.h"
6 
7 #include "data/linepositionarray.h"
8 
9 using namespace std;
10 using namespace testing;
11 
12 class LinePositionArraySmall: public testing::Test {
13   public:
14     LinePositionArray line_array;
15 
16     LinePositionArraySmall() {
17         line_array.append( 4 );
18         line_array.append( 8 );
19         line_array.append( 10 );
20         // A longer (>128) line
21         line_array.append( 345 );
22         // An even longer (>16384) line
23         line_array.append( 20000 );
24         // And a short one again
25         line_array.append( 20020 );
26     }
27 };
28 
29 TEST_F( LinePositionArraySmall, HasACorrectSize ) {
30     ASSERT_THAT( line_array.size(), Eq( 6 ) );
31 }
32 
33 TEST_F( LinePositionArraySmall, RememberAddedLines ) {
34     ASSERT_THAT( line_array[0], Eq( 4 ) );
35     ASSERT_THAT( line_array[1], Eq( 8 ) );
36     ASSERT_THAT( line_array[2], Eq( 10 ) );
37     ASSERT_THAT( line_array[3], Eq( 345 ) );
38     ASSERT_THAT( line_array[4], Eq( 20000 ) );
39     ASSERT_THAT( line_array[5], Eq( 20020 ) );
40 
41     // This one again to eliminate caching effects
42     ASSERT_THAT( line_array[3], Eq( 345 ) );
43 }
44 
45 TEST_F( LinePositionArraySmall, FakeLFisNotKeptWhenAddingAfterIt ) {
46     line_array.setFakeFinalLF();
47     ASSERT_THAT( line_array[5], Eq( 20020 ) );
48     line_array.append( 20030 );
49     ASSERT_THAT( line_array[5], Eq( 20030 ) );
50 }
51 
52 class LinePositionArrayConcatOperation: public LinePositionArraySmall {
53   public:
54     FastLinePositionArray other_array;
55 
56     LinePositionArrayConcatOperation() {
57         other_array.append( 150000 );
58         other_array.append( 150023 );
59     }
60 };
61 
62 TEST_F( LinePositionArrayConcatOperation, SimpleConcat ) {
63     line_array.append_list( other_array );
64 
65     ASSERT_THAT( line_array.size(), Eq( 8 ) );
66 
67     ASSERT_THAT( line_array[0], Eq( 4 ) );
68     ASSERT_THAT( line_array[1], Eq( 8 ) );
69 
70     ASSERT_THAT( line_array[5], Eq( 20020 ) );
71     ASSERT_THAT( line_array[6], Eq( 150000 ) );
72     ASSERT_THAT( line_array[7], Eq( 150023 ) );
73 }
74 
75 TEST_F( LinePositionArrayConcatOperation, DoesNotKeepFakeLf ) {
76     line_array.setFakeFinalLF();
77     ASSERT_THAT( line_array[5], Eq( 20020 ) );
78 
79     line_array.append_list( other_array );
80     ASSERT_THAT( line_array[5], Eq( 150000 ) );
81     ASSERT_THAT( line_array.size(), Eq( 7 ) );
82 }
83 
84 class LinePositionArrayLong: public testing::Test {
85   public:
86     LinePositionArray line_array;
87 
88     LinePositionArrayLong() {
89         // Add 126 lines (of various sizes
90         for ( int i = 0; i < 127; i++ )
91             line_array.append( i * 4 );
92         // Line no 127
93         line_array.append( 514 );
94     }
95 };
96 
97 TEST_F( LinePositionArrayLong, LineNo128HasRightValue ) {
98     line_array.append( 524 );
99     ASSERT_THAT( line_array[128], Eq( 524 ) );
100 }
101 
102 TEST_F( LinePositionArrayLong, FakeLFisNotKeptWhenAddingAfterIt ) {
103     line_array.append( 524 );
104     line_array.setFakeFinalLF();
105     ASSERT_THAT( line_array[128], Eq( 524 ) );
106     line_array.append( 600 );
107     ASSERT_THAT( line_array[128], Eq( 600 ) );
108 }
109 
110 
111 class LinePositionArrayBig: public testing::Test {
112   public:
113     LinePositionArray line_array;
114 
115     LinePositionArrayBig() {
116         line_array.append( 4 );
117         line_array.append( 8 );
118         // A very big line
119         line_array.append( UINT32_MAX - 10 );
120         line_array.append( (uint64_t) UINT32_MAX + 10LL );
121         line_array.append( (uint64_t) UINT32_MAX + 30LL );
122         line_array.append( (uint64_t) 2*UINT32_MAX );
123         line_array.append( (uint64_t) 2*UINT32_MAX + 10LL );
124         line_array.append( (uint64_t) 2*UINT32_MAX + 1000LL );
125         line_array.append( (uint64_t) 3*UINT32_MAX );
126     }
127 };
128 
129 TEST_F( LinePositionArrayBig, IsTheRightSize ) {
130     ASSERT_THAT( line_array.size(), 9 );
131 }
132 
133 TEST_F( LinePositionArrayBig, HasRightData ) {
134     ASSERT_THAT( line_array[0], Eq( 4 ) );
135     ASSERT_THAT( line_array[1], Eq( 8 ) );
136     ASSERT_THAT( line_array[2], Eq( UINT32_MAX - 10 ) );
137     ASSERT_THAT( line_array[3], Eq( (uint64_t) UINT32_MAX + 10LL ) );
138     ASSERT_THAT( line_array[4], Eq( (uint64_t) UINT32_MAX + 30LL ) );
139     ASSERT_THAT( line_array[5], Eq( (uint64_t) 2*UINT32_MAX ) );
140     ASSERT_THAT( line_array[6], Eq( (uint64_t) 2*UINT32_MAX + 10LL ) );
141     ASSERT_THAT( line_array[7], Eq( (uint64_t) 2*UINT32_MAX + 1000LL ) );
142     ASSERT_THAT( line_array[8], Eq( (uint64_t) 3*UINT32_MAX ) );
143 }
144 
145 class LinePositionArrayBigConcat: public testing::Test {
146   public:
147     LinePositionArray line_array;
148     FastLinePositionArray other_array;
149 
150     LinePositionArrayBigConcat() {
151         line_array.append( 4 );
152         line_array.append( 8 );
153 
154         other_array.append( UINT32_MAX + 10 );
155         other_array.append( UINT32_MAX + 30 );
156     }
157 };
158 
159 TEST_F( LinePositionArrayBigConcat, SimpleBigConcat ) {
160     line_array.append_list( other_array );
161 
162     ASSERT_THAT( line_array.size(), Eq( 4 ) );
163 
164     ASSERT_THAT( line_array[0], Eq( 4 ) );
165     ASSERT_THAT( line_array[1], Eq( 8 ) );
166     ASSERT_THAT( line_array[2], Eq( UINT32_MAX + 10 ) );
167     ASSERT_THAT( line_array[3], Eq( UINT32_MAX + 30 ) );
168 }
169