2 * Copyright (c) 2019 Inria
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Daniel Carvalho
31 #include <gtest/gtest.h>
35 #include "base/sat_counter.hh"
38 * Test if the maximum value is indeed the maximum value reachable.
40 TEST(SatCounterTest
, MaximumValue
)
42 const unsigned bits
= 3;
43 const unsigned max_value
= (1 << bits
) - 1;
44 SatCounter
counter(bits
);
46 for (int i
= 0; i
< 2*max_value
; i
++) {
50 ASSERT_EQ(counter
, max_value
);
54 * Test if the minimum value is indeed the mimimum value reachable.
56 TEST(SatCounterTest
, MinimumValue
)
58 const unsigned bits
= 3;
59 SatCounter
counter(bits
);
61 for (int i
= 0; i
< 2; i
++) {
65 ASSERT_EQ(counter
, 0);
69 * Test initializing the counter with a value, updating it and then resetting.
71 TEST(SatCounterTest
, InitialValue
)
73 const unsigned bits
= 3;
74 const unsigned initial_value
= 4;
75 SatCounter
counter(bits
, initial_value
);
76 ASSERT_EQ(counter
, initial_value
);
79 ASSERT_EQ(counter
, initial_value
);
83 * Test calculating saturation percentile.
85 TEST(SatCounterTest
, SaturationPercentile
)
87 const unsigned bits
= 3;
88 const unsigned max_value
= (1 << bits
) - 1;
89 SatCounter
counter(bits
);
91 ASSERT_FALSE(counter
.isSaturated());
92 for (double value
= 0.0; value
<= max_value
; value
++, counter
++) {
93 const double saturation
= value
/ max_value
;
94 ASSERT_DOUBLE_EQ(counter
.calcSaturation(), saturation
);
96 ASSERT_TRUE(counter
.isSaturated());
100 * Test back and forth against an int.
102 TEST(SatCounterTest
, IntComparison
)
104 const unsigned bits
= 3;
105 SatCounter
counter(bits
);
108 ASSERT_EQ(counter
++, value
++);
109 ASSERT_EQ(counter
++, value
++);
110 ASSERT_EQ(counter
--, value
--);
111 ASSERT_EQ(counter
++, value
++);
112 ASSERT_EQ(counter
++, value
++);
113 ASSERT_EQ(counter
--, value
--);
114 ASSERT_EQ(counter
++, value
++);
115 ASSERT_EQ(counter
--, value
--);
116 ASSERT_EQ(counter
--, value
--);
117 ASSERT_EQ(counter
++, value
++);
118 ASSERT_EQ(counter
--, value
--);
119 ASSERT_EQ(counter
--, value
--);
120 ASSERT_EQ(counter
, 0);
124 * Test shift operators.
126 TEST(SatCounterTest
, Shift
)
128 const unsigned bits
= 3;
129 const unsigned max_value
= (1 << bits
) - 1;
130 const unsigned initial_value
= 1;
131 SatCounter
counter(bits
, initial_value
);
132 SatCounter
other(bits
, initial_value
);
133 // The saturated shift value is just enough to saturate, since greater
134 // values could generate undefined behavior
135 SatCounter
saturated_counter(bits
, bits
);
136 int value
= initial_value
;
138 // Test random shifts
141 ASSERT_EQ(counter
, value
);
144 ASSERT_EQ(counter
, value
);
148 ASSERT_EQ(counter
, max_value
);
152 ASSERT_EQ(counter
, 0);
154 // Test saturation against other saturating counter
156 value
= initial_value
;
159 ASSERT_EQ(counter
, value
);
160 counter
<<= saturated_counter
;
162 ASSERT_EQ(counter
, max_value
);
164 // Test zeroing against other saturating counter
167 ASSERT_EQ(counter
, value
);
168 counter
>>= saturated_counter
;
169 ASSERT_EQ(counter
, 0);
173 * Test both pre and post operators.
175 TEST(SatCounterTest
, PrePostOperators
)
177 const unsigned bits
= 3;
178 const unsigned max_value
= (1 << bits
) - 1;
179 SatCounter
counter_pre(bits
);
180 SatCounter
counter_post(bits
);
182 for (int i
= 0; i
< 2*max_value
; i
++) {
184 SatCounter value_pre
= ++counter_pre
;
185 ASSERT_EQ(counter_post
, value_pre
);
188 ASSERT_EQ(counter_pre
, max_value
);
189 ASSERT_EQ(counter_post
, max_value
);
191 for (int i
= 0; i
< 2*max_value
; i
++) {
193 SatCounter value_pre
= --counter_pre
;
194 ASSERT_EQ(counter_post
, value_pre
);
197 ASSERT_EQ(counter_pre
, 0);
198 ASSERT_EQ(counter_post
, 0);
202 * Test copy and move for both constructor and assignment.
204 TEST(SatCounterTest
, CopyMove
)
206 const unsigned bits
= 3;
207 const unsigned max_value
= (1 << bits
) - 1;
208 const unsigned initial_value
= 1;
209 SatCounter
counter(bits
, initial_value
);
210 SatCounter
deep_copy(1);
211 SatCounter
counter_copy(2);
213 // Increase counter value so that we can check if the inner counter is
217 // Copy counter using both the copy constructor and the copy assignment
218 SatCounter
counter_copy_constructor(counter
);
219 deep_copy
= counter_copy
= counter
;
220 ASSERT_EQ(counter_copy_constructor
, initial_value
+ 1);
221 ASSERT_EQ(counter_copy
, initial_value
+ 1);
222 ASSERT_EQ(deep_copy
, initial_value
+ 1);
224 // Make sure max value is the same for all of them, and that modifying
225 // the copies does not modify the original
226 for (int i
= 0; i
< 2*max_value
; i
++) {
227 counter_copy_constructor
++;
231 ASSERT_EQ(counter
, initial_value
+ 1);
232 ASSERT_EQ(counter_copy_constructor
, max_value
);
233 ASSERT_EQ(counter_copy
, max_value
);
234 ASSERT_EQ(deep_copy
, max_value
);
236 // Make sure initial value is the same for all of them
237 counter_copy_constructor
.reset();
238 counter_copy
.reset();
240 ASSERT_EQ(counter_copy_constructor
, initial_value
);
241 ASSERT_EQ(counter_copy
, initial_value
);
242 ASSERT_EQ(deep_copy
, initial_value
);
245 SatCounter
counter_move_constructor(std::move(counter
));
246 ASSERT_EQ(counter
, 0);
247 ASSERT_EQ(counter_move_constructor
, initial_value
+ 1);
249 SatCounter
counter_move(bits
);
250 counter_move
= std::move(counter_move_constructor
);
251 ASSERT_EQ(counter_move_constructor
, 0);
252 ASSERT_EQ(counter_move
, initial_value
+ 1);
256 * Test add-assignment and subtract assignment.
258 TEST(SatCounterTest
, AddSubAssignment
)
260 const unsigned bits
= 3;
261 const unsigned max_value
= (1 << bits
) - 1;
262 SatCounter
counter(bits
);
263 SatCounter
other(bits
, 2);
264 SatCounter
saturated_counter(bits
, max_value
);
267 // Test add-assignment for a few random values and then saturate
270 ASSERT_EQ(counter
, value
);
273 ASSERT_EQ(counter
, value
);
274 counter
+= max_value
;
276 ASSERT_EQ(counter
, value
);
278 // Test subtract-assignment for a few random values until back to zero
281 ASSERT_EQ(counter
, value
);
284 ASSERT_EQ(counter
, value
);
285 counter
-= max_value
;
287 ASSERT_EQ(counter
, value
);
289 // Test add-assignment of other saturating counter
292 ASSERT_EQ(counter
, value
);
293 counter
+= saturated_counter
;
295 ASSERT_EQ(counter
, saturated_counter
);
297 // Test subtract-assignment of other saturating counter
300 ASSERT_EQ(counter
, value
);
301 counter
-= saturated_counter
;
302 ASSERT_EQ(counter
, 0);