1 // Copyright (C) 2017 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "condition/condition_util.h"
16 #include "src/statsd_config.pb.h"
17 
18 #include <gtest/gtest.h>
19 
20 #include <stdio.h>
21 #include <vector>
22 
23 using namespace android::os::statsd;
24 using std::vector;
25 
26 #ifdef __ANDROID__
27 
28 TEST(ConditionTrackerTest, TestUnknownCondition) {
29     LogicalOperation operation = LogicalOperation::AND;
30 
31     vector<int> children;
32     children.push_back(0);
33     children.push_back(1);
34     children.push_back(2);
35 
36     vector<ConditionState> conditionResults;
37     conditionResults.push_back(ConditionState::kUnknown);
38     conditionResults.push_back(ConditionState::kFalse);
39     conditionResults.push_back(ConditionState::kTrue);
40 
41     EXPECT_EQ(evaluateCombinationCondition(children, operation, conditionResults),
42               ConditionState::kUnknown);
43 }
44 
45 TEST(ConditionTrackerTest, TestAndCondition) {
46     // Set up the matcher
47     LogicalOperation operation = LogicalOperation::AND;
48 
49     vector<int> children;
50     children.push_back(0);
51     children.push_back(1);
52     children.push_back(2);
53 
54     vector<ConditionState> conditionResults;
55     conditionResults.push_back(ConditionState::kTrue);
56     conditionResults.push_back(ConditionState::kFalse);
57     conditionResults.push_back(ConditionState::kTrue);
58 
59     EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
60 
61     conditionResults.clear();
62     conditionResults.push_back(ConditionState::kTrue);
63     conditionResults.push_back(ConditionState::kTrue);
64     conditionResults.push_back(ConditionState::kTrue);
65 
66     EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
67 }
68 
69 TEST(ConditionTrackerTest, TestOrCondition) {
70     // Set up the matcher
71     LogicalOperation operation = LogicalOperation::OR;
72 
73     vector<int> children;
74     children.push_back(0);
75     children.push_back(1);
76     children.push_back(2);
77 
78     vector<ConditionState> conditionResults;
79     conditionResults.push_back(ConditionState::kTrue);
80     conditionResults.push_back(ConditionState::kFalse);
81     conditionResults.push_back(ConditionState::kTrue);
82 
83     EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
84 
85     conditionResults.clear();
86     conditionResults.push_back(ConditionState::kFalse);
87     conditionResults.push_back(ConditionState::kFalse);
88     conditionResults.push_back(ConditionState::kFalse);
89 
90     EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
91 }
92 
93 TEST(ConditionTrackerTest, TestNotCondition) {
94     // Set up the matcher
95     LogicalOperation operation = LogicalOperation::NOT;
96 
97     vector<int> children;
98     children.push_back(0);
99 
100     vector<ConditionState> conditionResults;
101     conditionResults.push_back(ConditionState::kTrue);
102 
103     EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
104 
105     conditionResults.clear();
106     conditionResults.push_back(ConditionState::kFalse);
107     EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
108 
109     children.clear();
110     conditionResults.clear();
111     EXPECT_EQ(evaluateCombinationCondition(children, operation, conditionResults),
112               ConditionState::kUnknown);
113 }
114 
115 TEST(ConditionTrackerTest, TestNandCondition) {
116     // Set up the matcher
117     LogicalOperation operation = LogicalOperation::NAND;
118 
119     vector<int> children;
120     children.push_back(0);
121     children.push_back(1);
122 
123     vector<ConditionState> conditionResults;
124     conditionResults.push_back(ConditionState::kTrue);
125     conditionResults.push_back(ConditionState::kFalse);
126 
127     EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
128 
129     conditionResults.clear();
130     conditionResults.push_back(ConditionState::kFalse);
131     conditionResults.push_back(ConditionState::kFalse);
132     EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
133 
134     conditionResults.clear();
135     conditionResults.push_back(ConditionState::kTrue);
136     conditionResults.push_back(ConditionState::kTrue);
137     EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
138 }
139 
140 TEST(ConditionTrackerTest, TestNorCondition) {
141     // Set up the matcher
142     LogicalOperation operation = LogicalOperation::NOR;
143 
144     vector<int> children;
145     children.push_back(0);
146     children.push_back(1);
147 
148     vector<ConditionState> conditionResults;
149     conditionResults.push_back(ConditionState::kTrue);
150     conditionResults.push_back(ConditionState::kFalse);
151 
152     EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
153 
154     conditionResults.clear();
155     conditionResults.push_back(ConditionState::kFalse);
156     conditionResults.push_back(ConditionState::kFalse);
157     EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
158 
159     conditionResults.clear();
160     conditionResults.push_back(ConditionState::kTrue);
161     conditionResults.push_back(ConditionState::kTrue);
162     EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
163 }
164 
165 #else
166 GTEST_LOG_(INFO) << "This test does nothing.\n";
167 #endif
168