xref: /glogg/tests/linepositionarrayTest.cpp (revision 05467f52d72e282658c219cc889ed6bc08e5dc01)
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 
LinePositionArraySmall()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 
TEST_F(LinePositionArraySmall,HasACorrectSize)29 TEST_F( LinePositionArraySmall, HasACorrectSize ) {
30     ASSERT_THAT( line_array.size(), Eq( 6 ) );
31 }
32 
TEST_F(LinePositionArraySmall,RememberAddedLines)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 
TEST_F(LinePositionArraySmall,FakeLFisNotKeptWhenAddingAfterIt)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 
LinePositionArrayConcatOperation()56     LinePositionArrayConcatOperation() {
57         other_array.append( 150000 );
58         other_array.append( 150023 );
59     }
60 };
61 
TEST_F(LinePositionArrayConcatOperation,SimpleConcat)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 
TEST_F(LinePositionArrayConcatOperation,DoesNotKeepFakeLf)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 
LinePositionArrayLong()88     LinePositionArrayLong() {
89         // Add 255 lines (of various sizes
90         for ( int i = 0; i < 255; i++ )
91             line_array.append( i * 4 );
92         // Line no 256
93         line_array.append( 255 * 4 );
94     }
95 };
96 
TEST_F(LinePositionArrayLong,LineNo128HasRightValue)97 TEST_F( LinePositionArrayLong, LineNo128HasRightValue ) {
98     // Add line no 257
99     line_array.append( 255 * 4 + 10 );
100     ASSERT_THAT( line_array[256], Eq( 255 * 4 + 10 ) );
101 }
102 
TEST_F(LinePositionArrayLong,FakeLFisNotKeptWhenAddingAfterIt)103 TEST_F( LinePositionArrayLong, FakeLFisNotKeptWhenAddingAfterIt ) {
104     for ( uint64_t i = 0; i < 1000; ++i ) {
105         uint64_t pos = ( 257LL * 4 ) + i*35LL;
106         line_array.append( pos );
107         line_array.setFakeFinalLF();
108         ASSERT_THAT( line_array[256 + i], Eq( pos ) );
109         line_array.append( pos + 21LL );
110         ASSERT_THAT( line_array[256 + i], Eq( pos + 21LL ) );
111     }
112 }
113 
114 
115 class LinePositionArrayBig: public testing::Test {
116   public:
117     LinePositionArray line_array;
118 
LinePositionArrayBig()119     LinePositionArrayBig() {
120         line_array.append( 4 );
121         line_array.append( 8 );
122         // A very big line
123         line_array.append( UINT32_MAX - 10 );
124         line_array.append( (uint64_t) UINT32_MAX + 10LL );
125         line_array.append( (uint64_t) UINT32_MAX + 30LL );
126         line_array.append( (uint64_t) 2*UINT32_MAX );
127         line_array.append( (uint64_t) 2*UINT32_MAX + 10LL );
128         line_array.append( (uint64_t) 2*UINT32_MAX + 1000LL );
129         line_array.append( (uint64_t) 3*UINT32_MAX );
130     }
131 };
132 
TEST_F(LinePositionArrayBig,IsTheRightSize)133 TEST_F( LinePositionArrayBig, IsTheRightSize ) {
134     ASSERT_THAT( line_array.size(), 9 );
135 }
136 
TEST_F(LinePositionArrayBig,HasRightData)137 TEST_F( LinePositionArrayBig, HasRightData ) {
138     ASSERT_THAT( line_array[0], Eq( 4 ) );
139     ASSERT_THAT( line_array[1], Eq( 8 ) );
140     ASSERT_THAT( line_array[2], Eq( UINT32_MAX - 10 ) );
141     ASSERT_THAT( line_array[3], Eq( (uint64_t) UINT32_MAX + 10LL ) );
142     ASSERT_THAT( line_array[4], Eq( (uint64_t) UINT32_MAX + 30LL ) );
143     ASSERT_THAT( line_array[5], Eq( (uint64_t) 2LL*UINT32_MAX ) );
144     ASSERT_THAT( line_array[6], Eq( (uint64_t) 2LL*UINT32_MAX + 10LL ) );
145     ASSERT_THAT( line_array[7], Eq( (uint64_t) 2LL*UINT32_MAX + 1000LL ) );
146     ASSERT_THAT( line_array[8], Eq( (uint64_t) 3LL*UINT32_MAX ) );
147 }
148 
TEST_F(LinePositionArrayBig,FakeLFisNotKeptWhenAddingAfterIt)149 TEST_F( LinePositionArrayBig, FakeLFisNotKeptWhenAddingAfterIt ) {
150     for ( uint64_t i = 0; i < 1000; ++i ) {
151         uint64_t pos = 3LL*UINT32_MAX + 524LL + i*35LL;
152         line_array.append( pos );
153         line_array.setFakeFinalLF();
154         ASSERT_THAT( line_array[9 + i], Eq( pos ) );
155         line_array.append( pos + 21LL );
156         ASSERT_THAT( line_array[9 + i], Eq( pos + 21LL ) );
157     }
158 }
159 
160 
161 class LinePositionArrayBigConcat: public testing::Test {
162   public:
163     LinePositionArray line_array;
164     FastLinePositionArray other_array;
165 
LinePositionArrayBigConcat()166     LinePositionArrayBigConcat() {
167         line_array.append( 4 );
168         line_array.append( 8 );
169 
170         other_array.append( UINT32_MAX + 10 );
171         other_array.append( UINT32_MAX + 30 );
172     }
173 };
174 
TEST_F(LinePositionArrayBigConcat,SimpleBigConcat)175 TEST_F( LinePositionArrayBigConcat, SimpleBigConcat ) {
176     line_array.append_list( other_array );
177 
178     ASSERT_THAT( line_array.size(), Eq( 4 ) );
179 
180     ASSERT_THAT( line_array[0], Eq( 4 ) );
181     ASSERT_THAT( line_array[1], Eq( 8 ) );
182     ASSERT_THAT( line_array[2], Eq( UINT32_MAX + 10 ) );
183     ASSERT_THAT( line_array[3], Eq( UINT32_MAX + 30 ) );
184 }
185