xref: /glogg/tests/linepositionarrayTest.cpp (revision fcf40f1761134a69881dfa6f1d28d01064374a37)
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     for ( uint64_t i = 0; i < 1000; ++i ) {
104         uint64_t pos = 524LL + i*35LL;
105         line_array.append( pos );
106         line_array.setFakeFinalLF();
107         ASSERT_THAT( line_array[128 + i], Eq( pos ) );
108         line_array.append( pos + 21LL );
109         ASSERT_THAT( line_array[128 + i], Eq( pos + 21LL ) );
110     }
111 }
112 
113 
114 class LinePositionArrayBig: public testing::Test {
115   public:
116     LinePositionArray line_array;
117 
118     LinePositionArrayBig() {
119         line_array.append( 4 );
120         line_array.append( 8 );
121         // A very big line
122         line_array.append( UINT32_MAX - 10 );
123         line_array.append( (uint64_t) UINT32_MAX + 10LL );
124         line_array.append( (uint64_t) UINT32_MAX + 30LL );
125         line_array.append( (uint64_t) 2*UINT32_MAX );
126         line_array.append( (uint64_t) 2*UINT32_MAX + 10LL );
127         line_array.append( (uint64_t) 2*UINT32_MAX + 1000LL );
128         line_array.append( (uint64_t) 3*UINT32_MAX );
129     }
130 };
131 
132 TEST_F( LinePositionArrayBig, IsTheRightSize ) {
133     ASSERT_THAT( line_array.size(), 9 );
134 }
135 
136 TEST_F( LinePositionArrayBig, HasRightData ) {
137     ASSERT_THAT( line_array[0], Eq( 4 ) );
138     ASSERT_THAT( line_array[1], Eq( 8 ) );
139     ASSERT_THAT( line_array[2], Eq( UINT32_MAX - 10 ) );
140     ASSERT_THAT( line_array[3], Eq( (uint64_t) UINT32_MAX + 10LL ) );
141     ASSERT_THAT( line_array[4], Eq( (uint64_t) UINT32_MAX + 30LL ) );
142     ASSERT_THAT( line_array[5], Eq( (uint64_t) 2LL*UINT32_MAX ) );
143     ASSERT_THAT( line_array[6], Eq( (uint64_t) 2LL*UINT32_MAX + 10LL ) );
144     ASSERT_THAT( line_array[7], Eq( (uint64_t) 2LL*UINT32_MAX + 1000LL ) );
145     ASSERT_THAT( line_array[8], Eq( (uint64_t) 3LL*UINT32_MAX ) );
146 }
147 
148 TEST_F( LinePositionArrayBig, FakeLFisNotKeptWhenAddingAfterIt ) {
149     for ( uint64_t i = 0; i < 1000; ++i ) {
150         uint64_t pos = 3LL*UINT32_MAX + 524LL + i*35LL;
151         line_array.append( pos );
152         line_array.setFakeFinalLF();
153         ASSERT_THAT( line_array[9 + i], Eq( pos ) );
154         line_array.append( pos + 21LL );
155         ASSERT_THAT( line_array[9 + i], Eq( pos + 21LL ) );
156     }
157 }
158 
159 
160 class LinePositionArrayBigConcat: public testing::Test {
161   public:
162     LinePositionArray line_array;
163     FastLinePositionArray other_array;
164 
165     LinePositionArrayBigConcat() {
166         line_array.append( 4 );
167         line_array.append( 8 );
168 
169         other_array.append( UINT32_MAX + 10 );
170         other_array.append( UINT32_MAX + 30 );
171     }
172 };
173 
174 TEST_F( LinePositionArrayBigConcat, SimpleBigConcat ) {
175     line_array.append_list( other_array );
176 
177     ASSERT_THAT( line_array.size(), Eq( 4 ) );
178 
179     ASSERT_THAT( line_array[0], Eq( 4 ) );
180     ASSERT_THAT( line_array[1], Eq( 8 ) );
181     ASSERT_THAT( line_array[2], Eq( UINT32_MAX + 10 ) );
182     ASSERT_THAT( line_array[3], Eq( UINT32_MAX + 30 ) );
183 }
184