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 | |
| 3 | #include <limits.h> |
| 4 | |
| 5 | #include "gtest/gtest.h" |
| 6 | |
| 7 | namespace aos { |
| 8 | namespace util { |
| 9 | namespace testing { |
| 10 | |
| 11 | TEST(WrappingCounterTest, Basic) { |
| 12 | WrappingCounter test_counter; |
| 13 | EXPECT_EQ(0, test_counter.count()); |
| 14 | EXPECT_EQ(1, test_counter.Update(1)); |
| 15 | EXPECT_EQ(1, test_counter.Update(1)); |
| 16 | EXPECT_EQ(2, test_counter.Update(2)); |
| 17 | EXPECT_EQ(7, test_counter.Update(7)); |
| 18 | EXPECT_EQ(7, test_counter.count()); |
| 19 | EXPECT_EQ(123, test_counter.Update(123)); |
| 20 | EXPECT_EQ(123, test_counter.count()); |
| 21 | } |
| 22 | |
| 23 | TEST(WrappingCounterTest, Reset) { |
| 24 | WrappingCounter test_counter; |
| 25 | test_counter.Update(5); |
| 26 | test_counter.Reset(); |
| 27 | EXPECT_EQ(0, test_counter.count()); |
| 28 | test_counter.Reset(56); |
| 29 | EXPECT_EQ(56, test_counter.count()); |
| 30 | } |
| 31 | |
| 32 | namespace { |
| 33 | void test_wrapping(int16_t start, int16_t step) { |
| 34 | WrappingCounter test_counter; |
| 35 | for (int16_t i = start; i < INT16_MAX - step; i += step) { |
| 36 | EXPECT_EQ(i, test_counter.Update(i & 0xFF)); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // This tests the basic wrapping functionality. |
| 42 | TEST(WrappingCounterTest, ReasonableWrapping) { |
| 43 | test_wrapping(0, 13); |
| 44 | test_wrapping(0, 53); |
| 45 | test_wrapping(0, 64); |
| 46 | test_wrapping(0, 73); |
| 47 | } |
| 48 | |
| 49 | // It would be reasonable for these to fail if the implementation changes. |
| 50 | TEST(WrappingCounterTest, UnreasonableWrapping) { |
| 51 | test_wrapping(0, 128); |
| 52 | test_wrapping(0, 213); |
| 53 | test_wrapping(0, 255); |
| 54 | } |
| 55 | |
| 56 | } // namespace testing |
| 57 | } // namespace util |
| 58 | } // namespace aos |