blob: 7d452d7a76c6766259aeb27c847378803573fad2 [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#include "aos/util/wrapping_counter.h"
Brian Silverman2f594502013-10-16 10:29:47 -07002
Stephan Pleinesb1177672024-05-27 17:48:32 -07003#include <memory>
Brian Silverman2f594502013-10-16 10:29:47 -07004
5#include "gtest/gtest.h"
6
Stephan Pleinesf63bde82024-01-13 15:59:33 -08007namespace aos::util::testing {
Brian Silverman2f594502013-10-16 10:29:47 -07008
9TEST(WrappingCounterTest, Basic) {
10 WrappingCounter test_counter;
11 EXPECT_EQ(0, test_counter.count());
12 EXPECT_EQ(1, test_counter.Update(1));
13 EXPECT_EQ(1, test_counter.Update(1));
14 EXPECT_EQ(2, test_counter.Update(2));
15 EXPECT_EQ(7, test_counter.Update(7));
16 EXPECT_EQ(7, test_counter.count());
17 EXPECT_EQ(123, test_counter.Update(123));
18 EXPECT_EQ(123, test_counter.count());
19}
20
21TEST(WrappingCounterTest, Reset) {
22 WrappingCounter test_counter;
23 test_counter.Update(5);
24 test_counter.Reset();
25 EXPECT_EQ(0, test_counter.count());
26 test_counter.Reset(56);
27 EXPECT_EQ(56, test_counter.count());
28}
29
30namespace {
31void test_wrapping(int16_t start, int16_t step) {
32 WrappingCounter test_counter;
33 for (int16_t i = start; i < INT16_MAX - step; i += step) {
34 EXPECT_EQ(i, test_counter.Update(i & 0xFF));
35 }
36}
Tyler Chatowbf0609c2021-07-31 16:13:27 -070037} // namespace
Brian Silverman2f594502013-10-16 10:29:47 -070038
39// This tests the basic wrapping functionality.
40TEST(WrappingCounterTest, ReasonableWrapping) {
41 test_wrapping(0, 13);
42 test_wrapping(0, 53);
43 test_wrapping(0, 64);
44 test_wrapping(0, 73);
45}
46
47// It would be reasonable for these to fail if the implementation changes.
48TEST(WrappingCounterTest, UnreasonableWrapping) {
49 test_wrapping(0, 128);
50 test_wrapping(0, 213);
51 test_wrapping(0, 255);
52}
53
Stephan Pleinesf63bde82024-01-13 15:59:33 -080054} // namespace aos::util::testing