Home | History | Annotate | Download | only in condition
      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 "frameworks/base/cmds/statsd/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 TEST(ConditionTrackerTest, TestUnknownCondition) {
     28     LogicalOperation operation = LogicalOperation::AND;
     29 
     30     vector<int> children;
     31     children.push_back(0);
     32     children.push_back(1);
     33     children.push_back(2);
     34 
     35     vector<ConditionState> conditionResults;
     36     conditionResults.push_back(ConditionState::kUnknown);
     37     conditionResults.push_back(ConditionState::kFalse);
     38     conditionResults.push_back(ConditionState::kTrue);
     39 
     40     EXPECT_EQ(evaluateCombinationCondition(children, operation, conditionResults),
     41               ConditionState::kUnknown);
     42 }
     43 TEST(ConditionTrackerTest, TestAndCondition) {
     44     // Set up the matcher
     45     LogicalOperation operation = LogicalOperation::AND;
     46 
     47     vector<int> children;
     48     children.push_back(0);
     49     children.push_back(1);
     50     children.push_back(2);
     51 
     52     vector<ConditionState> conditionResults;
     53     conditionResults.push_back(ConditionState::kTrue);
     54     conditionResults.push_back(ConditionState::kFalse);
     55     conditionResults.push_back(ConditionState::kTrue);
     56 
     57     EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
     58 
     59     conditionResults.clear();
     60     conditionResults.push_back(ConditionState::kTrue);
     61     conditionResults.push_back(ConditionState::kTrue);
     62     conditionResults.push_back(ConditionState::kTrue);
     63 
     64     EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
     65 }
     66 
     67 TEST(ConditionTrackerTest, TestOrCondition) {
     68     // Set up the matcher
     69     LogicalOperation operation = LogicalOperation::OR;
     70 
     71     vector<int> children;
     72     children.push_back(0);
     73     children.push_back(1);
     74     children.push_back(2);
     75 
     76     vector<ConditionState> conditionResults;
     77     conditionResults.push_back(ConditionState::kTrue);
     78     conditionResults.push_back(ConditionState::kFalse);
     79     conditionResults.push_back(ConditionState::kTrue);
     80 
     81     EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
     82 
     83     conditionResults.clear();
     84     conditionResults.push_back(ConditionState::kFalse);
     85     conditionResults.push_back(ConditionState::kFalse);
     86     conditionResults.push_back(ConditionState::kFalse);
     87 
     88     EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
     89 }
     90 
     91 TEST(ConditionTrackerTest, TestNotCondition) {
     92     // Set up the matcher
     93     LogicalOperation operation = LogicalOperation::NOT;
     94 
     95     vector<int> children;
     96     children.push_back(0);
     97 
     98     vector<ConditionState> conditionResults;
     99     conditionResults.push_back(ConditionState::kTrue);
    100 
    101     EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
    102 
    103     conditionResults.clear();
    104     conditionResults.push_back(ConditionState::kFalse);
    105     EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
    106 }
    107 
    108 TEST(ConditionTrackerTest, TestNandCondition) {
    109     // Set up the matcher
    110     LogicalOperation operation = LogicalOperation::NAND;
    111 
    112     vector<int> children;
    113     children.push_back(0);
    114     children.push_back(1);
    115 
    116     vector<ConditionState> conditionResults;
    117     conditionResults.push_back(ConditionState::kTrue);
    118     conditionResults.push_back(ConditionState::kFalse);
    119 
    120     EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
    121 
    122     conditionResults.clear();
    123     conditionResults.push_back(ConditionState::kFalse);
    124     conditionResults.push_back(ConditionState::kFalse);
    125     EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
    126 
    127     conditionResults.clear();
    128     conditionResults.push_back(ConditionState::kTrue);
    129     conditionResults.push_back(ConditionState::kTrue);
    130     EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
    131 }
    132 
    133 TEST(ConditionTrackerTest, TestNorCondition) {
    134     // Set up the matcher
    135     LogicalOperation operation = LogicalOperation::NOR;
    136 
    137     vector<int> children;
    138     children.push_back(0);
    139     children.push_back(1);
    140 
    141     vector<ConditionState> conditionResults;
    142     conditionResults.push_back(ConditionState::kTrue);
    143     conditionResults.push_back(ConditionState::kFalse);
    144 
    145     EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
    146 
    147     conditionResults.clear();
    148     conditionResults.push_back(ConditionState::kFalse);
    149     conditionResults.push_back(ConditionState::kFalse);
    150     EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
    151 
    152     conditionResults.clear();
    153     conditionResults.push_back(ConditionState::kTrue);
    154     conditionResults.push_back(ConditionState::kTrue);
    155     EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
    156 }
    157 
    158 #else
    159 GTEST_LOG_(INFO) << "This test does nothing.\n";
    160 #endif
    161