1 /*
2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /*
18 * Test MonotonicCounter
19 */
20
21 #include <iostream>
22
23 #include <gtest/gtest.h>
24
25 #include "utility/MonotonicCounter.h"
26
TEST(test_monotonic_counter,builtin_wrap)27 TEST(test_monotonic_counter, builtin_wrap) {
28 int32_t x = 0x7FFFFFF0;
29 int32_t y = 0x80000010;
30 int32_t delta;
31 // delta = y - x; // This would cause a numeric overflow!
32 __builtin_sub_overflow(y, x, &delta);
33 ASSERT_EQ(0x20, delta);
34 }
35
36 // test updating past some overflow points
TEST(test_monotonic_counter,mono_counter_update32_wrap)37 TEST(test_monotonic_counter, mono_counter_update32_wrap) {
38 MonotonicCounter counter;
39 ASSERT_EQ(0, counter.get());
40
41 static constexpr uint32_t x = (uint32_t) 0x7FFFFFF0;
42 counter.update32(x);
43 ASSERT_EQ((int64_t)0x7FFFFFF0, counter.get());
44
45 static constexpr uint32_t y = (uint32_t) 0x80000010;
46 counter.update32(y);
47 ASSERT_EQ((int64_t)0x80000010, counter.get());
48
49 counter.update32(0);
50 ASSERT_EQ((int64_t)0x100000000, counter.get());
51 }
52
TEST(test_monotonic_counter,mono_counter_roundup)53 TEST(test_monotonic_counter, mono_counter_roundup) {
54 MonotonicCounter counter;
55 static constexpr uint32_t x = 2345;
56 counter.update32(x);
57 ASSERT_EQ((int64_t)x, counter.get());
58
59 counter.roundUp64(100);
60 ASSERT_EQ((int64_t)2400, counter.get());
61 }
62
TEST(test_monotonic_counter,mono_counter_catchup)63 TEST(test_monotonic_counter, mono_counter_catchup) {
64 MonotonicCounter counter;
65 counter.update32(7654);
66 counter.catchUpTo(5000); // already past 5000 so no change
67 ASSERT_EQ((int64_t)7654, counter.get());
68 counter.catchUpTo(9876); // jumps
69 ASSERT_EQ((int64_t)9876, counter.get());
70 }
71
TEST(test_monotonic_counter,mono_counter_increment)72 TEST(test_monotonic_counter, mono_counter_increment) {
73 MonotonicCounter counter;
74 counter.update32(1000);
75 counter.increment(-234); // will not go backwards
76 ASSERT_EQ((int64_t)1000, counter.get());
77 counter.increment(96); // advances
78 ASSERT_EQ((int64_t)1096, counter.get());
79 }
80
TEST(test_monotonic_counter,mono_counter_reset)81 TEST(test_monotonic_counter, mono_counter_reset) {
82 MonotonicCounter counter;
83 counter.update32(1000);
84 // Counter is monotonic and should not go backwards.
85 counter.update32(500); // No change because 32-bit counter is already past 1000.
86 ASSERT_EQ((int64_t)1000, counter.get());
87
88 counter.reset32();
89 counter.update32(500);
90 ASSERT_EQ((int64_t)1500, counter.get());
91 }
92