blob: e5c58f301ec6495e85a9e116044dce98319b92ed [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
3#include <limits.h>
4
5#include "gtest/gtest.h"
6
7namespace aos {
8namespace util {
9namespace testing {
10
11TEST(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
23TEST(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
32namespace {
33void 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.
42TEST(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.
50TEST(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