1 /*
2  * Copyright 2023 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 #include "../reader/mapper/SlopController.h"
18 
19 #include <gtest/gtest.h>
20 
21 namespace android {
22 
23 // --- SlopControllerTest ---
24 
TEST(SlopControllerTest,PositiveValues)25 TEST(SlopControllerTest, PositiveValues) {
26     SlopController controller = SlopController(/*slopThreshold=*/5, /*slopDurationNanos=*/100);
27 
28     ASSERT_EQ(0, controller.consumeEvent(1000, 1));
29     ASSERT_EQ(0, controller.consumeEvent(1003, 3));
30     ASSERT_EQ(2, controller.consumeEvent(1005, 3));
31     ASSERT_EQ(4, controller.consumeEvent(1009, 4));
32 
33     SlopController controller2 = SlopController(/*slopThreshold=*/5, /*slopDurationNanos=*/100);
34 
35     ASSERT_EQ(0, controller2.consumeEvent(1000, 5));
36     ASSERT_EQ(3, controller2.consumeEvent(1003, 3));
37     ASSERT_EQ(4, controller2.consumeEvent(1005, 4));
38 }
39 
TEST(SlopControllerTest,NegativeValues)40 TEST(SlopControllerTest, NegativeValues) {
41     SlopController controller = SlopController(/*slopThreshold=*/5, /*slopDurationNanos=*/100);
42 
43     ASSERT_EQ(0, controller.consumeEvent(1000, -1));
44     ASSERT_EQ(0, controller.consumeEvent(1003, -3));
45     ASSERT_EQ(-2, controller.consumeEvent(1005, -3));
46     ASSERT_EQ(-4, controller.consumeEvent(1009, -4));
47 
48     SlopController controller2 = SlopController(/*slopThreshold=*/5, /*slopDurationNanos=*/100);
49 
50     ASSERT_EQ(0, controller2.consumeEvent(1000, -5));
51     ASSERT_EQ(-3, controller2.consumeEvent(1003, -3));
52     ASSERT_EQ(-4, controller2.consumeEvent(1005, -4));
53 }
54 
TEST(SlopControllerTest,ZeroDoesNotResetSlop)55 TEST(SlopControllerTest, ZeroDoesNotResetSlop) {
56     SlopController controller = SlopController(/*slopThreshold=*/5, /*slopDurationNanos=*/100);
57 
58     ASSERT_EQ(1, controller.consumeEvent(1005, 6));
59     ASSERT_EQ(0, controller.consumeEvent(1006, 0));
60     ASSERT_EQ(2, controller.consumeEvent(1008, 2));
61 }
62 
TEST(SlopControllerTest,SignChange_ResetsSlop)63 TEST(SlopControllerTest, SignChange_ResetsSlop) {
64     SlopController controller = SlopController(/*slopThreshold=*/5, /*slopDurationNanos=*/100);
65 
66     ASSERT_EQ(0, controller.consumeEvent(1000, 2));
67     ASSERT_EQ(0, controller.consumeEvent(1001, -4));
68     ASSERT_EQ(0, controller.consumeEvent(1002, 3));
69     ASSERT_EQ(0, controller.consumeEvent(1003, -2));
70 
71     ASSERT_EQ(1, controller.consumeEvent(1005, 6));
72     ASSERT_EQ(0, controller.consumeEvent(1006, 0));
73     ASSERT_EQ(2, controller.consumeEvent(1008, 2));
74 
75     ASSERT_EQ(0, controller.consumeEvent(1010, -4));
76     ASSERT_EQ(-1, controller.consumeEvent(1011, -2));
77 
78     ASSERT_EQ(0, controller.consumeEvent(1015, 5));
79     ASSERT_EQ(2, controller.consumeEvent(1016, 2));
80 
81     ASSERT_EQ(0, controller.consumeEvent(1017, -5));
82     ASSERT_EQ(-2, controller.consumeEvent(1018, -2));
83 }
84 
TEST(SlopControllerTest,OldAge_ResetsSlop)85 TEST(SlopControllerTest, OldAge_ResetsSlop) {
86     SlopController controller = SlopController(/*slopThreshold=*/5, /*slopDurationNanos=*/100);
87 
88     ASSERT_EQ(1, controller.consumeEvent(1005, 6));
89     ASSERT_EQ(0, controller.consumeEvent(1108, 2)); // age exceeds slop duration
90 
91     ASSERT_EQ(1, controller.consumeEvent(1110, 4));
92     ASSERT_EQ(0, controller.consumeEvent(1210, 2)); // age equals slop duration
93 
94     ASSERT_EQ(0, controller.consumeEvent(1215, -3));
95     ASSERT_EQ(-2, controller.consumeEvent(1216, -4));
96     ASSERT_EQ(-5, controller.consumeEvent(1315, -5));
97 }
98 
99 } // namespace android
100