David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 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 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 15 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
| 16 | #include "matchers/matcher_util.h" |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 17 | #include "stats_log_util.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 18 | #include "stats_util.h" |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 19 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 20 | #include <gtest/gtest.h> |
| 21 | #include <log/log_event_list.h> |
| 22 | #include <log/log_read.h> |
| 23 | #include <log/logprint.h> |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 24 | |
| 25 | #include <stdio.h> |
| 26 | |
| 27 | using namespace android::os::statsd; |
| 28 | using std::unordered_map; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 29 | using std::vector; |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 30 | |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 31 | const int32_t TAG_ID = 123; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 32 | const int FIELD_ID_1 = 1; |
| 33 | const int FIELD_ID_2 = 2; |
| 34 | const int FIELD_ID_3 = 2; |
| 35 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 36 | const int ATTRIBUTION_UID_FIELD_ID = 1; |
| 37 | const int ATTRIBUTION_TAG_FIELD_ID = 2; |
| 38 | |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame] | 39 | |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 40 | #ifdef __ANDROID__ |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 41 | TEST(AtomMatcherTest, TestSimpleMatcher) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 42 | UidMap uidMap; |
| 43 | |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 44 | // Set up the matcher |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 45 | AtomMatcher matcher; |
| 46 | auto simpleMatcher = matcher.mutable_simple_atom_matcher(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 47 | simpleMatcher->set_atom_id(TAG_ID); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 48 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 49 | LogEvent event(TAG_ID, 0); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 50 | EXPECT_TRUE(event.write(11)); |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 51 | event.init(); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 52 | |
| 53 | // Test |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 54 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 55 | |
| 56 | // Wrong tag id. |
| 57 | simpleMatcher->set_atom_id(TAG_ID + 1); |
| 58 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 61 | TEST(AtomMatcherTest, TestAttributionMatcher) { |
| 62 | UidMap uidMap; |
| 63 | AttributionNode attribution_node1; |
| 64 | attribution_node1.set_uid(1111); |
| 65 | attribution_node1.set_tag("location1"); |
| 66 | |
| 67 | AttributionNode attribution_node2; |
| 68 | attribution_node2.set_uid(2222); |
| 69 | attribution_node2.set_tag("location2"); |
| 70 | |
| 71 | AttributionNode attribution_node3; |
| 72 | attribution_node3.set_uid(3333); |
| 73 | attribution_node3.set_tag("location3"); |
| 74 | std::vector<AttributionNode> attribution_nodes = |
| 75 | { attribution_node1, attribution_node2, attribution_node3 }; |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 76 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 77 | // Set up the event |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 78 | LogEvent event(TAG_ID, 0); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 79 | event.write(attribution_nodes); |
| 80 | event.write("some value"); |
| 81 | // Convert to a LogEvent |
| 82 | event.init(); |
| 83 | |
| 84 | // Set up the matcher |
| 85 | AtomMatcher matcher; |
| 86 | auto simpleMatcher = matcher.mutable_simple_atom_matcher(); |
| 87 | simpleMatcher->set_atom_id(TAG_ID); |
| 88 | |
| 89 | // Match first node. |
| 90 | auto attributionMatcher = simpleMatcher->add_field_value_matcher(); |
| 91 | attributionMatcher->set_field(FIELD_ID_1); |
| 92 | attributionMatcher->set_position(Position::FIRST); |
| 93 | attributionMatcher->mutable_matches_tuple()->add_field_value_matcher()->set_field( |
| 94 | ATTRIBUTION_TAG_FIELD_ID); |
| 95 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string("tag"); |
| 96 | |
| 97 | auto fieldMatcher = simpleMatcher->add_field_value_matcher(); |
| 98 | fieldMatcher->set_field(FIELD_ID_2); |
| 99 | fieldMatcher->set_eq_string("some value"); |
| 100 | |
| 101 | // Tag not matched. |
| 102 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 103 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 104 | ->set_eq_string("location3"); |
| 105 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 106 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 107 | ->set_eq_string("location1"); |
| 108 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 109 | |
| 110 | // Match last node. |
| 111 | attributionMatcher->set_position(Position::LAST); |
| 112 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 113 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 114 | ->set_eq_string("location3"); |
| 115 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 116 | |
| 117 | // Match any node. |
| 118 | attributionMatcher->set_position(Position::ANY); |
| 119 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 120 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 121 | ->set_eq_string("location1"); |
| 122 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 123 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 124 | ->set_eq_string("location2"); |
| 125 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 126 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 127 | ->set_eq_string("location3"); |
| 128 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 129 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 130 | ->set_eq_string("location4"); |
| 131 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 132 | |
| 133 | // Attribution match but primitive field not match. |
| 134 | attributionMatcher->set_position(Position::ANY); |
| 135 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 136 | ->set_eq_string("location2"); |
| 137 | fieldMatcher->set_eq_string("wrong value"); |
| 138 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 139 | |
| 140 | fieldMatcher->set_eq_string("some value"); |
| 141 | |
| 142 | // Uid match. |
| 143 | attributionMatcher->set_position(Position::ANY); |
| 144 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_field( |
| 145 | ATTRIBUTION_UID_FIELD_ID); |
| 146 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string("pkg0"); |
| 147 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 148 | |
| 149 | uidMap.updateMap({1111, 1111, 2222, 3333, 3333} /* uid list */, |
| 150 | {1, 1, 2, 1, 2} /* version list */, |
| 151 | {android::String16("pkg0"), android::String16("pkg1"), |
| 152 | android::String16("pkg1"), android::String16("Pkg2"), |
| 153 | android::String16("PkG3")} /* package name list */); |
| 154 | |
| 155 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 156 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 157 | ->set_eq_string("pkg3"); |
| 158 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 159 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 160 | ->set_eq_string("pkg2"); |
| 161 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 162 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 163 | ->set_eq_string("pkg1"); |
| 164 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 165 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 166 | ->set_eq_string("pkg0"); |
| 167 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 168 | |
| 169 | attributionMatcher->set_position(Position::FIRST); |
| 170 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 171 | ->set_eq_string("pkg0"); |
| 172 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 173 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 174 | ->set_eq_string("pkg3"); |
| 175 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 176 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 177 | ->set_eq_string("pkg2"); |
| 178 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 179 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 180 | ->set_eq_string("pkg1"); |
| 181 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 182 | |
| 183 | attributionMatcher->set_position(Position::LAST); |
| 184 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 185 | ->set_eq_string("pkg0"); |
| 186 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 187 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 188 | ->set_eq_string("pkg3"); |
| 189 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 190 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 191 | ->set_eq_string("pkg2"); |
| 192 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 193 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 194 | ->set_eq_string("pkg1"); |
| 195 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 196 | |
| 197 | // Uid + tag. |
| 198 | attributionMatcher->set_position(Position::ANY); |
| 199 | attributionMatcher->mutable_matches_tuple()->add_field_value_matcher()->set_field( |
| 200 | ATTRIBUTION_TAG_FIELD_ID); |
| 201 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 202 | ->set_eq_string("pkg0"); |
| 203 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) |
| 204 | ->set_eq_string("location1"); |
| 205 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 206 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 207 | ->set_eq_string("pkg1"); |
| 208 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) |
| 209 | ->set_eq_string("location1"); |
| 210 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 211 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 212 | ->set_eq_string("pkg1"); |
| 213 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) |
| 214 | ->set_eq_string("location2"); |
| 215 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 216 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 217 | ->set_eq_string("pkg2"); |
| 218 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) |
| 219 | ->set_eq_string("location3"); |
| 220 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 221 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 222 | ->set_eq_string("pkg3"); |
| 223 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) |
| 224 | ->set_eq_string("location3"); |
| 225 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 226 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 227 | ->set_eq_string("pkg3"); |
| 228 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) |
| 229 | ->set_eq_string("location1"); |
| 230 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 231 | |
| 232 | attributionMatcher->set_position(Position::FIRST); |
| 233 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 234 | ->set_eq_string("pkg0"); |
| 235 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) |
| 236 | ->set_eq_string("location1"); |
| 237 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 238 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 239 | ->set_eq_string("pkg1"); |
| 240 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) |
| 241 | ->set_eq_string("location1"); |
| 242 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 243 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 244 | ->set_eq_string("pkg1"); |
| 245 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) |
| 246 | ->set_eq_string("location2"); |
| 247 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 248 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 249 | ->set_eq_string("pkg2"); |
| 250 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) |
| 251 | ->set_eq_string("location3"); |
| 252 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 253 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 254 | ->set_eq_string("pkg3"); |
| 255 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) |
| 256 | ->set_eq_string("location3"); |
| 257 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 258 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 259 | ->set_eq_string("pkg3"); |
| 260 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) |
| 261 | ->set_eq_string("location1"); |
| 262 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 263 | |
| 264 | attributionMatcher->set_position(Position::LAST); |
| 265 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 266 | ->set_eq_string("pkg0"); |
| 267 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) |
| 268 | ->set_eq_string("location1"); |
| 269 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 270 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 271 | ->set_eq_string("pkg1"); |
| 272 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) |
| 273 | ->set_eq_string("location1"); |
| 274 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 275 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 276 | ->set_eq_string("pkg1"); |
| 277 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) |
| 278 | ->set_eq_string("location2"); |
| 279 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 280 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 281 | ->set_eq_string("pkg2"); |
| 282 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) |
| 283 | ->set_eq_string("location3"); |
| 284 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 285 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 286 | ->set_eq_string("pkg3"); |
| 287 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) |
| 288 | ->set_eq_string("location3"); |
| 289 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 290 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) |
| 291 | ->set_eq_string("pkg3"); |
| 292 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) |
| 293 | ->set_eq_string("location1"); |
| 294 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 295 | } |
| 296 | |
| 297 | TEST(AtomMatcherTest, TestBoolMatcher) { |
| 298 | UidMap uidMap; |
| 299 | // Set up the matcher |
| 300 | AtomMatcher matcher; |
| 301 | auto simpleMatcher = matcher.mutable_simple_atom_matcher(); |
| 302 | simpleMatcher->set_atom_id(TAG_ID); |
| 303 | auto keyValue1 = simpleMatcher->add_field_value_matcher(); |
| 304 | keyValue1->set_field(FIELD_ID_1); |
| 305 | auto keyValue2 = simpleMatcher->add_field_value_matcher(); |
| 306 | keyValue2->set_field(FIELD_ID_2); |
| 307 | |
| 308 | // Set up the event |
| 309 | LogEvent event(TAG_ID, 0); |
| 310 | EXPECT_TRUE(event.write(true)); |
| 311 | EXPECT_TRUE(event.write(false)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 312 | // Convert to a LogEvent |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 313 | event.init(); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 314 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 315 | // Test |
| 316 | keyValue1->set_eq_bool(true); |
| 317 | keyValue2->set_eq_bool(false); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 318 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 319 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 320 | keyValue1->set_eq_bool(false); |
| 321 | keyValue2->set_eq_bool(false); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 322 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 323 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 324 | keyValue1->set_eq_bool(false); |
| 325 | keyValue2->set_eq_bool(true); |
| 326 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 327 | |
| 328 | keyValue1->set_eq_bool(true); |
| 329 | keyValue2->set_eq_bool(true); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 330 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 333 | TEST(AtomMatcherTest, TestStringMatcher) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 334 | UidMap uidMap; |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 335 | // Set up the matcher |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 336 | AtomMatcher matcher; |
| 337 | auto simpleMatcher = matcher.mutable_simple_atom_matcher(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 338 | simpleMatcher->set_atom_id(TAG_ID); |
| 339 | auto keyValue = simpleMatcher->add_field_value_matcher(); |
| 340 | keyValue->set_field(FIELD_ID_1); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 341 | keyValue->set_eq_string("some value"); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 342 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 343 | // Set up the event |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 344 | LogEvent event(TAG_ID, 0); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 345 | event.write("some value"); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 346 | // Convert to a LogEvent |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 347 | event.init(); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 348 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 349 | // Test |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 350 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 353 | TEST(AtomMatcherTest, TestMultiFieldsMatcher) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 354 | UidMap uidMap; |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 355 | // Set up the matcher |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 356 | AtomMatcher matcher; |
| 357 | auto simpleMatcher = matcher.mutable_simple_atom_matcher(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 358 | simpleMatcher->set_atom_id(TAG_ID); |
| 359 | auto keyValue1 = simpleMatcher->add_field_value_matcher(); |
| 360 | keyValue1->set_field(FIELD_ID_1); |
| 361 | auto keyValue2 = simpleMatcher->add_field_value_matcher(); |
| 362 | keyValue2->set_field(FIELD_ID_2); |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 363 | |
| 364 | // Set up the event |
| 365 | LogEvent event(TAG_ID, 0); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 366 | event.write(2); |
| 367 | event.write(3); |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 368 | |
| 369 | // Convert to a LogEvent |
| 370 | event.init(); |
| 371 | |
| 372 | // Test |
| 373 | keyValue1->set_eq_int(2); |
| 374 | keyValue2->set_eq_int(3); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 375 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 376 | |
| 377 | keyValue1->set_eq_int(2); |
| 378 | keyValue2->set_eq_int(4); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 379 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 380 | |
| 381 | keyValue1->set_eq_int(4); |
| 382 | keyValue2->set_eq_int(3); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 383 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 386 | TEST(AtomMatcherTest, TestIntComparisonMatcher) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 387 | UidMap uidMap; |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 388 | // Set up the matcher |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 389 | AtomMatcher matcher; |
| 390 | auto simpleMatcher = matcher.mutable_simple_atom_matcher(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 391 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 392 | simpleMatcher->set_atom_id(TAG_ID); |
| 393 | auto keyValue = simpleMatcher->add_field_value_matcher(); |
| 394 | keyValue->set_field(FIELD_ID_1); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 395 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 396 | // Set up the event |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 397 | LogEvent event(TAG_ID, 0); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 398 | event.write(11); |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 399 | event.init(); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 400 | |
| 401 | // Test |
| 402 | |
| 403 | // eq_int |
| 404 | keyValue->set_eq_int(10); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 405 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 406 | keyValue->set_eq_int(11); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 407 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 408 | keyValue->set_eq_int(12); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 409 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 410 | |
| 411 | // lt_int |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 412 | keyValue->set_lt_int(10); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 413 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 414 | keyValue->set_lt_int(11); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 415 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 416 | keyValue->set_lt_int(12); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 417 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 418 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 419 | // lte_int |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 420 | keyValue->set_lte_int(10); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 421 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 422 | keyValue->set_lte_int(11); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 423 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 424 | keyValue->set_lte_int(12); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 425 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 426 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 427 | // gt_int |
| 428 | keyValue->set_gt_int(10); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 429 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 430 | keyValue->set_gt_int(11); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 431 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 432 | keyValue->set_gt_int(12); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 433 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 434 | |
| 435 | // gte_int |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 436 | keyValue->set_gte_int(10); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 437 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 438 | keyValue->set_gte_int(11); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 439 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 440 | keyValue->set_gte_int(12); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 441 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 444 | TEST(AtomMatcherTest, TestFloatComparisonMatcher) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 445 | UidMap uidMap; |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 446 | // Set up the matcher |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 447 | AtomMatcher matcher; |
| 448 | auto simpleMatcher = matcher.mutable_simple_atom_matcher(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 449 | simpleMatcher->set_atom_id(TAG_ID); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 450 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 451 | auto keyValue = simpleMatcher->add_field_value_matcher(); |
| 452 | keyValue->set_field(FIELD_ID_1); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 453 | |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 454 | LogEvent event1(TAG_ID, 0); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 455 | keyValue->set_lt_float(10.0); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 456 | event1.write(10.1f); |
| 457 | event1.init(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 458 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event1)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 459 | |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 460 | LogEvent event2(TAG_ID, 0); |
| 461 | event2.write(9.9f); |
| 462 | event2.init(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 463 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event2)); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 464 | |
| 465 | LogEvent event3(TAG_ID, 0); |
| 466 | event3.write(10.1f); |
| 467 | event3.init(); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 468 | keyValue->set_gt_float(10.0); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 469 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event3)); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 470 | |
| 471 | LogEvent event4(TAG_ID, 0); |
| 472 | event4.write(9.9f); |
| 473 | event4.init(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 474 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event4)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | // Helper for the composite matchers. |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 478 | void addSimpleMatcher(SimpleAtomMatcher* simpleMatcher, int tag, int key, int val) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 479 | simpleMatcher->set_atom_id(tag); |
| 480 | auto keyValue = simpleMatcher->add_field_value_matcher(); |
| 481 | keyValue->set_field(key); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 482 | keyValue->set_eq_int(val); |
| 483 | } |
| 484 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 485 | TEST(AtomMatcherTest, TestAndMatcher) { |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 486 | // Set up the matcher |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 487 | LogicalOperation operation = LogicalOperation::AND; |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 488 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 489 | vector<int> children; |
| 490 | children.push_back(0); |
| 491 | children.push_back(1); |
| 492 | children.push_back(2); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 493 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 494 | vector<MatchingState> matcherResults; |
| 495 | matcherResults.push_back(MatchingState::kMatched); |
| 496 | matcherResults.push_back(MatchingState::kNotMatched); |
| 497 | matcherResults.push_back(MatchingState::kMatched); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 498 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 499 | EXPECT_FALSE(combinationMatch(children, operation, matcherResults)); |
| 500 | |
| 501 | matcherResults.clear(); |
| 502 | matcherResults.push_back(MatchingState::kMatched); |
| 503 | matcherResults.push_back(MatchingState::kMatched); |
| 504 | matcherResults.push_back(MatchingState::kMatched); |
| 505 | |
| 506 | EXPECT_TRUE(combinationMatch(children, operation, matcherResults)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 509 | TEST(AtomMatcherTest, TestOrMatcher) { |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 510 | // Set up the matcher |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 511 | LogicalOperation operation = LogicalOperation::OR; |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 512 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 513 | vector<int> children; |
| 514 | children.push_back(0); |
| 515 | children.push_back(1); |
| 516 | children.push_back(2); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 517 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 518 | vector<MatchingState> matcherResults; |
| 519 | matcherResults.push_back(MatchingState::kMatched); |
| 520 | matcherResults.push_back(MatchingState::kNotMatched); |
| 521 | matcherResults.push_back(MatchingState::kMatched); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 522 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 523 | EXPECT_TRUE(combinationMatch(children, operation, matcherResults)); |
| 524 | |
| 525 | matcherResults.clear(); |
| 526 | matcherResults.push_back(MatchingState::kNotMatched); |
| 527 | matcherResults.push_back(MatchingState::kNotMatched); |
| 528 | matcherResults.push_back(MatchingState::kNotMatched); |
| 529 | |
| 530 | EXPECT_FALSE(combinationMatch(children, operation, matcherResults)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 533 | TEST(AtomMatcherTest, TestNotMatcher) { |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 534 | // Set up the matcher |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 535 | LogicalOperation operation = LogicalOperation::NOT; |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 536 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 537 | vector<int> children; |
| 538 | children.push_back(0); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 539 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 540 | vector<MatchingState> matcherResults; |
| 541 | matcherResults.push_back(MatchingState::kMatched); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 542 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 543 | EXPECT_FALSE(combinationMatch(children, operation, matcherResults)); |
| 544 | |
| 545 | matcherResults.clear(); |
| 546 | matcherResults.push_back(MatchingState::kNotMatched); |
| 547 | EXPECT_TRUE(combinationMatch(children, operation, matcherResults)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 550 | TEST(AtomMatcherTest, TestNandMatcher) { |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 551 | // Set up the matcher |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 552 | LogicalOperation operation = LogicalOperation::NAND; |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 553 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 554 | vector<int> children; |
| 555 | children.push_back(0); |
| 556 | children.push_back(1); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 557 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 558 | vector<MatchingState> matcherResults; |
| 559 | matcherResults.push_back(MatchingState::kMatched); |
| 560 | matcherResults.push_back(MatchingState::kNotMatched); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 561 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 562 | EXPECT_TRUE(combinationMatch(children, operation, matcherResults)); |
| 563 | |
| 564 | matcherResults.clear(); |
| 565 | matcherResults.push_back(MatchingState::kNotMatched); |
| 566 | matcherResults.push_back(MatchingState::kNotMatched); |
| 567 | EXPECT_TRUE(combinationMatch(children, operation, matcherResults)); |
| 568 | |
| 569 | matcherResults.clear(); |
| 570 | matcherResults.push_back(MatchingState::kMatched); |
| 571 | matcherResults.push_back(MatchingState::kMatched); |
| 572 | EXPECT_FALSE(combinationMatch(children, operation, matcherResults)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 575 | TEST(AtomMatcherTest, TestNorMatcher) { |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 576 | // Set up the matcher |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 577 | LogicalOperation operation = LogicalOperation::NOR; |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 578 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 579 | vector<int> children; |
| 580 | children.push_back(0); |
| 581 | children.push_back(1); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 582 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 583 | vector<MatchingState> matcherResults; |
| 584 | matcherResults.push_back(MatchingState::kMatched); |
| 585 | matcherResults.push_back(MatchingState::kNotMatched); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 586 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 587 | EXPECT_FALSE(combinationMatch(children, operation, matcherResults)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 588 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 589 | matcherResults.clear(); |
| 590 | matcherResults.push_back(MatchingState::kNotMatched); |
| 591 | matcherResults.push_back(MatchingState::kNotMatched); |
| 592 | EXPECT_TRUE(combinationMatch(children, operation, matcherResults)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 593 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 594 | matcherResults.clear(); |
| 595 | matcherResults.push_back(MatchingState::kMatched); |
| 596 | matcherResults.push_back(MatchingState::kMatched); |
| 597 | EXPECT_FALSE(combinationMatch(children, operation, matcherResults)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 598 | } |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 599 | #else |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 600 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 601 | #endif |