1f049f87fSNicolas Bonnefon #include "gmock/gmock.h"
2f049f87fSNicolas Bonnefon
3f049f87fSNicolas Bonnefon #include "config.h"
4f049f87fSNicolas Bonnefon
5f049f87fSNicolas Bonnefon #include "log.h"
6f049f87fSNicolas Bonnefon
7f049f87fSNicolas Bonnefon #include "data/linepositionarray.h"
8f049f87fSNicolas Bonnefon
9f049f87fSNicolas Bonnefon using namespace std;
10f049f87fSNicolas Bonnefon using namespace testing;
11f049f87fSNicolas Bonnefon
12f049f87fSNicolas Bonnefon class LinePositionArraySmall: public testing::Test {
13f049f87fSNicolas Bonnefon public:
14f049f87fSNicolas Bonnefon LinePositionArray line_array;
15f049f87fSNicolas Bonnefon
LinePositionArraySmall()16f049f87fSNicolas Bonnefon LinePositionArraySmall() {
17f049f87fSNicolas Bonnefon line_array.append( 4 );
18f049f87fSNicolas Bonnefon line_array.append( 8 );
19f049f87fSNicolas Bonnefon line_array.append( 10 );
207a2f3a57SNicolas Bonnefon // A longer (>128) line
217a2f3a57SNicolas Bonnefon line_array.append( 345 );
227a2f3a57SNicolas Bonnefon // An even longer (>16384) line
237a2f3a57SNicolas Bonnefon line_array.append( 20000 );
247a2f3a57SNicolas Bonnefon // And a short one again
257a2f3a57SNicolas Bonnefon line_array.append( 20020 );
26f049f87fSNicolas Bonnefon }
27f049f87fSNicolas Bonnefon };
28f049f87fSNicolas Bonnefon
TEST_F(LinePositionArraySmall,HasACorrectSize)29f049f87fSNicolas Bonnefon TEST_F( LinePositionArraySmall, HasACorrectSize ) {
307a2f3a57SNicolas Bonnefon ASSERT_THAT( line_array.size(), Eq( 6 ) );
31f049f87fSNicolas Bonnefon }
32f049f87fSNicolas Bonnefon
TEST_F(LinePositionArraySmall,RememberAddedLines)33f049f87fSNicolas Bonnefon TEST_F( LinePositionArraySmall, RememberAddedLines ) {
34f049f87fSNicolas Bonnefon ASSERT_THAT( line_array[0], Eq( 4 ) );
35f049f87fSNicolas Bonnefon ASSERT_THAT( line_array[1], Eq( 8 ) );
36f049f87fSNicolas Bonnefon ASSERT_THAT( line_array[2], Eq( 10 ) );
377a2f3a57SNicolas Bonnefon ASSERT_THAT( line_array[3], Eq( 345 ) );
387a2f3a57SNicolas Bonnefon ASSERT_THAT( line_array[4], Eq( 20000 ) );
397a2f3a57SNicolas Bonnefon ASSERT_THAT( line_array[5], Eq( 20020 ) );
406e2e573cSNicolas Bonnefon
416e2e573cSNicolas Bonnefon // This one again to eliminate caching effects
426e2e573cSNicolas Bonnefon ASSERT_THAT( line_array[3], Eq( 345 ) );
43f049f87fSNicolas Bonnefon }
44f049f87fSNicolas Bonnefon
TEST_F(LinePositionArraySmall,FakeLFisNotKeptWhenAddingAfterIt)45f049f87fSNicolas Bonnefon TEST_F( LinePositionArraySmall, FakeLFisNotKeptWhenAddingAfterIt ) {
46f049f87fSNicolas Bonnefon line_array.setFakeFinalLF();
473c46a469SNicolas Bonnefon ASSERT_THAT( line_array[5], Eq( 20020 ) );
483c46a469SNicolas Bonnefon line_array.append( 20030 );
493c46a469SNicolas Bonnefon ASSERT_THAT( line_array[5], Eq( 20030 ) );
50f049f87fSNicolas Bonnefon }
51f049f87fSNicolas Bonnefon
52f049f87fSNicolas Bonnefon class LinePositionArrayConcatOperation: public LinePositionArraySmall {
53f049f87fSNicolas Bonnefon public:
547151e3acSNicolas Bonnefon FastLinePositionArray other_array;
55f049f87fSNicolas Bonnefon
LinePositionArrayConcatOperation()56f049f87fSNicolas Bonnefon LinePositionArrayConcatOperation() {
573c46a469SNicolas Bonnefon other_array.append( 150000 );
583c46a469SNicolas Bonnefon other_array.append( 150023 );
59f049f87fSNicolas Bonnefon }
60f049f87fSNicolas Bonnefon };
61f049f87fSNicolas Bonnefon
TEST_F(LinePositionArrayConcatOperation,SimpleConcat)62f049f87fSNicolas Bonnefon TEST_F( LinePositionArrayConcatOperation, SimpleConcat ) {
637a2f3a57SNicolas Bonnefon line_array.append_list( other_array );
64f049f87fSNicolas Bonnefon
653c46a469SNicolas Bonnefon ASSERT_THAT( line_array.size(), Eq( 8 ) );
66f049f87fSNicolas Bonnefon
67f049f87fSNicolas Bonnefon ASSERT_THAT( line_array[0], Eq( 4 ) );
68f049f87fSNicolas Bonnefon ASSERT_THAT( line_array[1], Eq( 8 ) );
693c46a469SNicolas Bonnefon
703c46a469SNicolas Bonnefon ASSERT_THAT( line_array[5], Eq( 20020 ) );
713c46a469SNicolas Bonnefon ASSERT_THAT( line_array[6], Eq( 150000 ) );
723c46a469SNicolas Bonnefon ASSERT_THAT( line_array[7], Eq( 150023 ) );
73f049f87fSNicolas Bonnefon }
74f049f87fSNicolas Bonnefon
TEST_F(LinePositionArrayConcatOperation,DoesNotKeepFakeLf)75f049f87fSNicolas Bonnefon TEST_F( LinePositionArrayConcatOperation, DoesNotKeepFakeLf ) {
76f049f87fSNicolas Bonnefon line_array.setFakeFinalLF();
773c46a469SNicolas Bonnefon ASSERT_THAT( line_array[5], Eq( 20020 ) );
78f049f87fSNicolas Bonnefon
797a2f3a57SNicolas Bonnefon line_array.append_list( other_array );
803c46a469SNicolas Bonnefon ASSERT_THAT( line_array[5], Eq( 150000 ) );
813c46a469SNicolas Bonnefon ASSERT_THAT( line_array.size(), Eq( 7 ) );
82f049f87fSNicolas Bonnefon }
83f049f87fSNicolas Bonnefon
843c46a469SNicolas Bonnefon class LinePositionArrayLong: public testing::Test {
853c46a469SNicolas Bonnefon public:
863c46a469SNicolas Bonnefon LinePositionArray line_array;
873c46a469SNicolas Bonnefon
LinePositionArrayLong()883c46a469SNicolas Bonnefon LinePositionArrayLong() {
89*05467f52SNicolas Bonnefon // Add 255 lines (of various sizes
90*05467f52SNicolas Bonnefon for ( int i = 0; i < 255; i++ )
913c46a469SNicolas Bonnefon line_array.append( i * 4 );
92*05467f52SNicolas Bonnefon // Line no 256
93*05467f52SNicolas Bonnefon line_array.append( 255 * 4 );
943c46a469SNicolas Bonnefon }
953c46a469SNicolas Bonnefon };
963c46a469SNicolas Bonnefon
TEST_F(LinePositionArrayLong,LineNo128HasRightValue)973c46a469SNicolas Bonnefon TEST_F( LinePositionArrayLong, LineNo128HasRightValue ) {
98*05467f52SNicolas Bonnefon // Add line no 257
99*05467f52SNicolas Bonnefon line_array.append( 255 * 4 + 10 );
100*05467f52SNicolas Bonnefon ASSERT_THAT( line_array[256], Eq( 255 * 4 + 10 ) );
1013c46a469SNicolas Bonnefon }
1023c46a469SNicolas Bonnefon
TEST_F(LinePositionArrayLong,FakeLFisNotKeptWhenAddingAfterIt)1033c46a469SNicolas Bonnefon TEST_F( LinePositionArrayLong, FakeLFisNotKeptWhenAddingAfterIt ) {
104fcf40f17SNicolas Bonnefon for ( uint64_t i = 0; i < 1000; ++i ) {
105*05467f52SNicolas Bonnefon uint64_t pos = ( 257LL * 4 ) + i*35LL;
106fcf40f17SNicolas Bonnefon line_array.append( pos );
1073c46a469SNicolas Bonnefon line_array.setFakeFinalLF();
108*05467f52SNicolas Bonnefon ASSERT_THAT( line_array[256 + i], Eq( pos ) );
109fcf40f17SNicolas Bonnefon line_array.append( pos + 21LL );
110*05467f52SNicolas Bonnefon ASSERT_THAT( line_array[256 + i], Eq( pos + 21LL ) );
111fcf40f17SNicolas Bonnefon }
1123c46a469SNicolas Bonnefon }
1133c46a469SNicolas Bonnefon
1143c46a469SNicolas Bonnefon
115f049f87fSNicolas Bonnefon class LinePositionArrayBig: public testing::Test {
116f049f87fSNicolas Bonnefon public:
117f049f87fSNicolas Bonnefon LinePositionArray line_array;
118f049f87fSNicolas Bonnefon
LinePositionArrayBig()119f049f87fSNicolas Bonnefon LinePositionArrayBig() {
120f049f87fSNicolas Bonnefon line_array.append( 4 );
121f049f87fSNicolas Bonnefon line_array.append( 8 );
122f049f87fSNicolas Bonnefon // A very big line
123f049f87fSNicolas Bonnefon line_array.append( UINT32_MAX - 10 );
1246e2e573cSNicolas Bonnefon line_array.append( (uint64_t) UINT32_MAX + 10LL );
1256e2e573cSNicolas Bonnefon line_array.append( (uint64_t) UINT32_MAX + 30LL );
1266e2e573cSNicolas Bonnefon line_array.append( (uint64_t) 2*UINT32_MAX );
1276e2e573cSNicolas Bonnefon line_array.append( (uint64_t) 2*UINT32_MAX + 10LL );
1286e2e573cSNicolas Bonnefon line_array.append( (uint64_t) 2*UINT32_MAX + 1000LL );
1296e2e573cSNicolas Bonnefon line_array.append( (uint64_t) 3*UINT32_MAX );
130f049f87fSNicolas Bonnefon }
131f049f87fSNicolas Bonnefon };
132f049f87fSNicolas Bonnefon
TEST_F(LinePositionArrayBig,IsTheRightSize)133f049f87fSNicolas Bonnefon TEST_F( LinePositionArrayBig, IsTheRightSize ) {
1346e2e573cSNicolas Bonnefon ASSERT_THAT( line_array.size(), 9 );
135f049f87fSNicolas Bonnefon }
136f049f87fSNicolas Bonnefon
TEST_F(LinePositionArrayBig,HasRightData)137f049f87fSNicolas Bonnefon TEST_F( LinePositionArrayBig, HasRightData ) {
138f049f87fSNicolas Bonnefon ASSERT_THAT( line_array[0], Eq( 4 ) );
139f049f87fSNicolas Bonnefon ASSERT_THAT( line_array[1], Eq( 8 ) );
140f049f87fSNicolas Bonnefon ASSERT_THAT( line_array[2], Eq( UINT32_MAX - 10 ) );
1416e2e573cSNicolas Bonnefon ASSERT_THAT( line_array[3], Eq( (uint64_t) UINT32_MAX + 10LL ) );
1426e2e573cSNicolas Bonnefon ASSERT_THAT( line_array[4], Eq( (uint64_t) UINT32_MAX + 30LL ) );
143fcf40f17SNicolas Bonnefon ASSERT_THAT( line_array[5], Eq( (uint64_t) 2LL*UINT32_MAX ) );
144fcf40f17SNicolas Bonnefon ASSERT_THAT( line_array[6], Eq( (uint64_t) 2LL*UINT32_MAX + 10LL ) );
145fcf40f17SNicolas Bonnefon ASSERT_THAT( line_array[7], Eq( (uint64_t) 2LL*UINT32_MAX + 1000LL ) );
146fcf40f17SNicolas Bonnefon ASSERT_THAT( line_array[8], Eq( (uint64_t) 3LL*UINT32_MAX ) );
147f049f87fSNicolas Bonnefon }
148f049f87fSNicolas Bonnefon
TEST_F(LinePositionArrayBig,FakeLFisNotKeptWhenAddingAfterIt)149fcf40f17SNicolas Bonnefon TEST_F( LinePositionArrayBig, FakeLFisNotKeptWhenAddingAfterIt ) {
150fcf40f17SNicolas Bonnefon for ( uint64_t i = 0; i < 1000; ++i ) {
151fcf40f17SNicolas Bonnefon uint64_t pos = 3LL*UINT32_MAX + 524LL + i*35LL;
152fcf40f17SNicolas Bonnefon line_array.append( pos );
153fcf40f17SNicolas Bonnefon line_array.setFakeFinalLF();
154fcf40f17SNicolas Bonnefon ASSERT_THAT( line_array[9 + i], Eq( pos ) );
155fcf40f17SNicolas Bonnefon line_array.append( pos + 21LL );
156fcf40f17SNicolas Bonnefon ASSERT_THAT( line_array[9 + i], Eq( pos + 21LL ) );
157fcf40f17SNicolas Bonnefon }
158fcf40f17SNicolas Bonnefon }
159fcf40f17SNicolas Bonnefon
160fcf40f17SNicolas Bonnefon
161f049f87fSNicolas Bonnefon class LinePositionArrayBigConcat: public testing::Test {
162f049f87fSNicolas Bonnefon public:
163f049f87fSNicolas Bonnefon LinePositionArray line_array;
1647151e3acSNicolas Bonnefon FastLinePositionArray other_array;
165f049f87fSNicolas Bonnefon
LinePositionArrayBigConcat()166f049f87fSNicolas Bonnefon LinePositionArrayBigConcat() {
167f049f87fSNicolas Bonnefon line_array.append( 4 );
168f049f87fSNicolas Bonnefon line_array.append( 8 );
169f049f87fSNicolas Bonnefon
170f049f87fSNicolas Bonnefon other_array.append( UINT32_MAX + 10 );
171f049f87fSNicolas Bonnefon other_array.append( UINT32_MAX + 30 );
172f049f87fSNicolas Bonnefon }
173f049f87fSNicolas Bonnefon };
174f049f87fSNicolas Bonnefon
TEST_F(LinePositionArrayBigConcat,SimpleBigConcat)175f049f87fSNicolas Bonnefon TEST_F( LinePositionArrayBigConcat, SimpleBigConcat ) {
1767a2f3a57SNicolas Bonnefon line_array.append_list( other_array );
177f049f87fSNicolas Bonnefon
178f049f87fSNicolas Bonnefon ASSERT_THAT( line_array.size(), Eq( 4 ) );
179f049f87fSNicolas Bonnefon
180f049f87fSNicolas Bonnefon ASSERT_THAT( line_array[0], Eq( 4 ) );
181f049f87fSNicolas Bonnefon ASSERT_THAT( line_array[1], Eq( 8 ) );
182f049f87fSNicolas Bonnefon ASSERT_THAT( line_array[2], Eq( UINT32_MAX + 10 ) );
183f049f87fSNicolas Bonnefon ASSERT_THAT( line_array[3], Eq( UINT32_MAX + 30 ) );
184f049f87fSNicolas Bonnefon }
185