xref: /glogg/tests/linepositionarrayTest.cpp (revision 7151e3ac37b1aab5aeda99b64e862aecf2a6be89)
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 
42 TEST_F( LinePositionArraySmall, FakeLFisNotKeptWhenAddingAfterIt ) {
43     line_array.setFakeFinalLF();
44     ASSERT_THAT( line_array[5], Eq( 20020 ) );
45     line_array.append( 20030 );
46     ASSERT_THAT( line_array[5], Eq( 20030 ) );
47 }
48 
49 class LinePositionArrayConcatOperation: public LinePositionArraySmall {
50   public:
51     FastLinePositionArray other_array;
52 
53     LinePositionArrayConcatOperation() {
54         other_array.append( 150000 );
55         other_array.append( 150023 );
56     }
57 };
58 
59 TEST_F( LinePositionArrayConcatOperation, SimpleConcat ) {
60     line_array.append_list( other_array );
61 
62     ASSERT_THAT( line_array.size(), Eq( 8 ) );
63 
64     ASSERT_THAT( line_array[0], Eq( 4 ) );
65     ASSERT_THAT( line_array[1], Eq( 8 ) );
66 
67     ASSERT_THAT( line_array[5], Eq( 20020 ) );
68     ASSERT_THAT( line_array[6], Eq( 150000 ) );
69     ASSERT_THAT( line_array[7], Eq( 150023 ) );
70 }
71 
72 TEST_F( LinePositionArrayConcatOperation, DoesNotKeepFakeLf ) {
73     line_array.setFakeFinalLF();
74     ASSERT_THAT( line_array[5], Eq( 20020 ) );
75 
76     line_array.append_list( other_array );
77     ASSERT_THAT( line_array[5], Eq( 150000 ) );
78     ASSERT_THAT( line_array.size(), Eq( 7 ) );
79 }
80 
81 class LinePositionArrayLong: public testing::Test {
82   public:
83     LinePositionArray line_array;
84 
85     LinePositionArrayLong() {
86         // Add 126 lines (of various sizes
87         for ( int i = 0; i < 127; i++ )
88             line_array.append( i * 4 );
89         // Line no 127
90         line_array.append( 514 );
91     }
92 };
93 
94 TEST_F( LinePositionArrayLong, LineNo128HasRightValue ) {
95     line_array.append( 524 );
96     ASSERT_THAT( line_array[128], Eq( 524 ) );
97 }
98 
99 TEST_F( LinePositionArrayLong, FakeLFisNotKeptWhenAddingAfterIt ) {
100     line_array.append( 524 );
101     line_array.setFakeFinalLF();
102     ASSERT_THAT( line_array[128], Eq( 524 ) );
103     line_array.append( 600 );
104     ASSERT_THAT( line_array[128], Eq( 600 ) );
105 }
106 
107 
108 class LinePositionArrayBig: public testing::Test {
109   public:
110     LinePositionArray line_array;
111 
112     LinePositionArrayBig() {
113         line_array.append( 4 );
114         line_array.append( 8 );
115         // A very big line
116         line_array.append( UINT32_MAX - 10 );
117         line_array.append( UINT32_MAX + 10 );
118         line_array.append( UINT32_MAX + 30 );
119     }
120 };
121 
122 TEST_F( LinePositionArrayBig, IsTheRightSize ) {
123     ASSERT_THAT( line_array.size(), 5 );
124 }
125 
126 TEST_F( LinePositionArrayBig, HasRightData ) {
127     ASSERT_THAT( line_array[0], Eq( 4 ) );
128     ASSERT_THAT( line_array[1], Eq( 8 ) );
129     ASSERT_THAT( line_array[2], Eq( UINT32_MAX - 10 ) );
130     ASSERT_THAT( line_array[3], Eq( UINT32_MAX + 10 ) );
131     ASSERT_THAT( line_array[4], Eq( UINT32_MAX + 30 ) );
132 }
133 
134 class LinePositionArrayBigConcat: public testing::Test {
135   public:
136     LinePositionArray line_array;
137     FastLinePositionArray other_array;
138 
139     LinePositionArrayBigConcat() {
140         line_array.append( 4 );
141         line_array.append( 8 );
142 
143         other_array.append( UINT32_MAX + 10 );
144         other_array.append( UINT32_MAX + 30 );
145     }
146 };
147 
148 TEST_F( LinePositionArrayBigConcat, SimpleBigConcat ) {
149     line_array.append_list( other_array );
150 
151     ASSERT_THAT( line_array.size(), Eq( 4 ) );
152 
153     ASSERT_THAT( line_array[0], Eq( 4 ) );
154     ASSERT_THAT( line_array[1], Eq( 8 ) );
155     ASSERT_THAT( line_array[2], Eq( UINT32_MAX + 10 ) );
156     ASSERT_THAT( line_array[3], Eq( UINT32_MAX + 30 ) );
157 }
158