John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #include "aos/util/wrapping_counter.h" |
Brian Silverman | 2f59450 | 2013-10-16 10:29:47 -0700 | [diff] [blame] | 2 | |
Stephan Pleines | b117767 | 2024-05-27 17:48:32 -0700 | [diff] [blame] | 3 | #include <memory> |
Brian Silverman | 2f59450 | 2013-10-16 10:29:47 -0700 | [diff] [blame] | 4 | |
| 5 | #include "gtest/gtest.h" |
| 6 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 7 | namespace aos::util::testing { |
Brian Silverman | 2f59450 | 2013-10-16 10:29:47 -0700 | [diff] [blame] | 8 | |
| 9 | TEST(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 | |
| 21 | TEST(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 | |
| 30 | namespace { |
| 31 | void 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 Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 37 | } // namespace |
Brian Silverman | 2f59450 | 2013-10-16 10:29:47 -0700 | [diff] [blame] | 38 | |
| 39 | // This tests the basic wrapping functionality. |
| 40 | TEST(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. |
| 48 | TEST(WrappingCounterTest, UnreasonableWrapping) { |
| 49 | test_wrapping(0, 128); |
| 50 | test_wrapping(0, 213); |
| 51 | test_wrapping(0, 255); |
| 52 | } |
| 53 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 54 | } // namespace aos::util::testing |