API
 
Loading...
Searching...
No Matches
indiCompRules_test.cpp
Go to the documentation of this file.
1#if(__cplusplus == 201703L)
2
3#include "../../../tests/catch2/catch.hpp"
4
5#include "../indiCompRules.hpp"
6
7
8SCENARIO( "basic INDI Property Element-value rules", "[stateRuleEngine::rules]" )
9{
10 GIVEN("string comparison")
11 {
12 pcf::IndiProperty prop1(pcf::IndiProperty::Text);
13 prop1.setDevice("ruleTest");
14 prop1.setName("prop1");
15 prop1.setPerm(pcf::IndiProperty::ReadWrite);
16 prop1.setState(pcf::IndiProperty::Idle);
17 prop1.add(pcf::IndiElement("current"));
18 prop1["current"] = "test";
19 prop1.add(pcf::IndiElement("target"));
20 prop1["target"] = "tset";
21
22 txtValRule rule1;
23
24 rule1.property(&prop1);
25 rule1.element("current");
26
27 WHEN("string should be equal and is")
28 {
30 rule1.target("test");
31
32 REQUIRE(rule1.value() == true);
33 }
34
35 WHEN("string should be equal and is not")
36 {
38 rule1.target("tset");
39
40 REQUIRE(rule1.value() == false);
41 }
42
43 WHEN("string should be not equal and is not equal")
44 {
46 rule1.target("tset");
47
48 REQUIRE(rule1.value() == true);
49 }
50
51 WHEN("string should be not equal and is equal")
52 {
54 rule1.target("test");
55
56 REQUIRE(rule1.value() == false);
57 }
58 }
59
60 GIVEN("float comparison")
61 {
62 pcf::IndiProperty prop1(pcf::IndiProperty::Number);
63 prop1.setDevice("ruleTest");
64 prop1.setName("prop1");
65 prop1.setPerm(pcf::IndiProperty::ReadWrite);
66 prop1.setState(pcf::IndiProperty::Idle);
67 prop1.add(pcf::IndiElement("current"));
68 prop1["current"].setValue(2.314159);
69 prop1.add(pcf::IndiElement("target"));
70 prop1["target"].setValue(1.567202);
71
72 numValRule rule1;
73
74 rule1.property(&prop1);
75 rule1.element("current");
76
77 WHEN("float should be equal and is")
78 {
80 rule1.target(2.314159);
81 REQUIRE(rule1.value() == true);
82 }
83
84 WHEN("float should be equal and aren't")
85 {
87 rule1.target(3.314159);
88 REQUIRE(rule1.value() == false);
89 }
90
91 WHEN("float should be equal and aren't within tol")
92 {
94 rule1.target(2.314158);
95
96 REQUIRE(rule1.value() == false);
97 }
98
99 WHEN("float should be equal and are within tol")
100 {
102 rule1.target(2.314158);
103 rule1.tol(1e-4);
104
105 REQUIRE(rule1.value() == true);
106 }
107
108 WHEN("float should be less than and is")
109 {
111 rule1.target(4.67892);
112 REQUIRE(rule1.value() == true);
113 }
114
115 WHEN("float should be less than but is not")
116 {
118 rule1.target(1.67892);
119 REQUIRE(rule1.value() == false);
120 }
121
122 WHEN("float should be greater than and is")
123 {
125 rule1.target(1.2);
126 REQUIRE(rule1.value() == true);
127 }
128
129 WHEN("float should be greater than but is not")
130 {
132 rule1.target(7.9);
133 REQUIRE(rule1.value() == false);
134 }
135
136 WHEN("float should be less-or-equal than and is less than")
137 {
139 rule1.target(4.67892);
140 REQUIRE(rule1.value() == true);
141 }
142
143 WHEN("float should be less-or-equal than and is equal")
144 {
146 rule1.target(2.314159);
147 REQUIRE(rule1.value() == true);
148 }
149
150 WHEN("float should be less-or-equal than but is not")
151 {
153 rule1.target(0.789);
154 REQUIRE(rule1.value() == false);
155 }
156
157 WHEN("float should be greater-or-equal than and is greater than")
158 {
160 rule1.target(2.05);
161 REQUIRE(rule1.value() == true);
162 }
163
164 WHEN("float should be greater-or-equal than and is equal")
165 {
167 rule1.target(2.314159);
168 REQUIRE(rule1.value() == true);
169 }
170
171 WHEN("float should be greater-or-equal than but is not")
172 {
174 rule1.target(3.789);
175 REQUIRE(rule1.value() == false);
176 }
177 }
178
179 GIVEN("switch comparison")
180 {
181 pcf::IndiProperty prop1(pcf::IndiProperty::Switch);
182 prop1.setDevice("ruleTest");
183 prop1.setName("prop1");
184 prop1.setPerm(pcf::IndiProperty::ReadWrite);
185 prop1.setState(pcf::IndiProperty::Idle);
186 prop1.add(pcf::IndiElement("toggle"));
187
188 swValRule rule1;
189
190 rule1.property(&prop1);
191 rule1.element("toggle");
192
193 WHEN("switch is on and should be equal and is")
194 {
195 prop1["toggle"].setSwitchState(pcf::IndiElement::On);
196 rule1.target("On");
198 REQUIRE(rule1.value() == true);
199 }
200
201 WHEN("switch is on and should be equal but isn't")
202 {
203 prop1["toggle"].setSwitchState(pcf::IndiElement::On);
204 rule1.target("Off");
206 REQUIRE(rule1.value() == false);
207 }
208
209 WHEN("switch is on and should be not equal and is not")
210 {
211 prop1["toggle"].setSwitchState(pcf::IndiElement::On);
212 rule1.target("Off");
214 REQUIRE(rule1.value() == true);
215 }
216
217 WHEN("switch is on and should be not equal but is")
218 {
219 prop1["toggle"].setSwitchState(pcf::IndiElement::On);
220 rule1.target("On");
222 REQUIRE(rule1.value() == false);
223 }
224
225 WHEN("switch is off and should be equal and is")
226 {
227 prop1["toggle"].setSwitchState(pcf::IndiElement::Off);
228 rule1.target("Off");
230 REQUIRE(rule1.value() == true);
231 }
232
233 WHEN("switch is off and should be equal but isn't")
234 {
235 prop1["toggle"].setSwitchState(pcf::IndiElement::Off);
236 rule1.target("On");
238 REQUIRE(rule1.value() == false);
239 }
240
241 WHEN("switch is off and should be not equal and is not")
242 {
243 prop1["toggle"].setSwitchState(pcf::IndiElement::Off);
244 rule1.target("On");
246 REQUIRE(rule1.value() == true);
247 }
248
249 WHEN("switch is off and should be not equal but is")
250 {
251 prop1["toggle"].setSwitchState(pcf::IndiElement::Off);
252 rule1.target("Off");
254 REQUIRE(rule1.value() == false);
255 }
256 }
257}
258
259SCENARIO( "INDI element comparison", "[stateRuleEngine::rules]" )
260{
261 GIVEN("string comparison within same property")
262 {
263 pcf::IndiProperty prop1(pcf::IndiProperty::Text);
264 prop1.setDevice("ruleTest");
265 prop1.setName("prop1");
266 prop1.setPerm(pcf::IndiProperty::ReadWrite);
267 prop1.setState(pcf::IndiProperty::Idle);
268 prop1.add(pcf::IndiElement("current"));
269 prop1.add(pcf::IndiElement("target"));
270
271 elCompTxtRule rule1;
272 rule1.property1(&prop1);
273 rule1.property2(&prop1);
274 rule1.element1("current");
275 rule1.element2("target");
276
277 WHEN("string elements wihtin same property should be equal and are")
278 {
279 prop1["current"] = "test";
280 prop1["target"] = "test";
282
283 REQUIRE(rule1.value() == true);
284 }
285
286 WHEN("string elements within same property should be equal and are not")
287 {
288 prop1["current"] = "test";
289 prop1["target"] = "tset";
291
292 REQUIRE(rule1.value() == false);
293 }
294
295 WHEN("string elements within same property should not be equal and are not")
296 {
297 prop1["current"] = "test";
298 prop1["target"] = "tset";
300
301 REQUIRE(rule1.value() == true);
302 }
303
304 WHEN("string elements within same property should not be equal and are")
305 {
306 prop1["current"] = "test";
307 prop1["target"] = "test";
309
310 REQUIRE(rule1.value() == false);
311 }
312 }
313
314 GIVEN("switch comparison")
315 {
316 pcf::IndiProperty prop1(pcf::IndiProperty::Switch);
317 prop1.setDevice("ruleTest1");
318 prop1.setName("prop1");
319 prop1.setPerm(pcf::IndiProperty::ReadWrite);
320 prop1.setState(pcf::IndiProperty::Idle);
321 prop1.add(pcf::IndiElement("nameTest"));
322
323 pcf::IndiProperty prop2(pcf::IndiProperty::Switch);
324 prop2.setDevice("ruleTest2");
325 prop2.setName("prop2");
326 prop2.setPerm(pcf::IndiProperty::ReadWrite);
327 prop2.setState(pcf::IndiProperty::Idle);
328 prop2.add(pcf::IndiElement("badgeTest"));
329
330 elCompSwRule rule1;
331 rule1.property1(&prop1);
332 rule1.property2(&prop2);
333 rule1.element1("nameTest");
334 rule1.element2("badgeTest");
335
336 WHEN("switches should be On and equal and are")
337 {
338 prop1["nameTest"].setSwitchState(pcf::IndiElement::On);
339 prop2["badgeTest"].setSwitchState(pcf::IndiElement::On);
341
342 REQUIRE(rule1.value() == true);
343 }
344
345 WHEN("switches should be On and equal but are not")
346 {
347 prop1["nameTest"].setSwitchState(pcf::IndiElement::On);
348 prop2["badgeTest"].setSwitchState(pcf::IndiElement::Off);
350
351 REQUIRE(rule1.value() == false);
352 }
353
354 WHEN("switches should be On and not equal and are not")
355 {
356 prop1["nameTest"].setSwitchState(pcf::IndiElement::On);
357 prop2["badgeTest"].setSwitchState(pcf::IndiElement::Off);
359
360 REQUIRE(rule1.value() == true);
361 }
362
363 WHEN("switches should be On and not equal but are")
364 {
365 prop1["nameTest"].setSwitchState(pcf::IndiElement::On);
366 prop2["badgeTest"].setSwitchState(pcf::IndiElement::On);
368
369 REQUIRE(rule1.value() == false);
370 }
371
372 WHEN("switches should be Off and equal and are")
373 {
374 prop1["nameTest"].setSwitchState(pcf::IndiElement::Off);
375 prop2["badgeTest"].setSwitchState(pcf::IndiElement::Off);
377
378 REQUIRE(rule1.value() == true);
379 }
380
381 WHEN("switches should be Off and equal but are not")
382 {
383 prop1["nameTest"].setSwitchState(pcf::IndiElement::Off);
384 prop2["badgeTest"].setSwitchState(pcf::IndiElement::On);
386
387 REQUIRE(rule1.value() == false);
388 }
389
390 WHEN("switches should be Off and not equal and are not")
391 {
392 prop1["nameTest"].setSwitchState(pcf::IndiElement::Off);
393 prop2["badgeTest"].setSwitchState(pcf::IndiElement::On);
395
396 REQUIRE(rule1.value() == true);
397 }
398
399 WHEN("switches should be Off and not equal but are")
400 {
401 prop1["nameTest"].setSwitchState(pcf::IndiElement::Off);
402 prop2["badgeTest"].setSwitchState(pcf::IndiElement::Off);
404
405 REQUIRE(rule1.value() == false);
406 }
407 }
408 GIVEN("numeric comparison")
409 {
410 pcf::IndiProperty prop1(pcf::IndiProperty::Number);
411 prop1.setDevice("ruleTest1");
412 prop1.setName("prop1");
413 prop1.setPerm(pcf::IndiProperty::ReadWrite);
414 prop1.setState(pcf::IndiProperty::Idle);
415 prop1.add(pcf::IndiElement("nameTest"));
416
417 pcf::IndiProperty prop2(pcf::IndiProperty::Number);
418 prop2.setDevice("ruleTest2");
419 prop2.setName("prop2");
420 prop2.setPerm(pcf::IndiProperty::ReadWrite);
421 prop2.setState(pcf::IndiProperty::Idle);
422 prop2.add(pcf::IndiElement("badgeTest"));
423
424 elCompNumRule rule1;
425 rule1.property1(&prop1);
426 rule1.property2(&prop2);
427 rule1.element1("nameTest");
428 rule1.element2("badgeTest");
429
430 WHEN("numbers should be equal and are")
431 {
432 prop1["nameTest"].set(2.5);
433 prop2["badgeTest"].set(2.5);
435
436 REQUIRE(rule1.value() == true);
437 }
438
439 WHEN("numbers should be equal but are not")
440 {
441 prop1["nameTest"].set(2.5);
442 prop2["badgeTest"].set(2.6);
444
445 REQUIRE(rule1.value() == false);
446 }
447 }
448}
449
450
451SCENARIO( "basic rule comparisons", "[stateRuleEngine::rules]" )
452{
453 GIVEN("INDI Property rule comparison")
454 {
455 WHEN("two strings should be equal and are")
456 {
457 pcf::IndiProperty prop1(pcf::IndiProperty::Text);
458 prop1.setDevice("ruleTest");
459 prop1.setName("prop1");
460 prop1.setPerm(pcf::IndiProperty::ReadWrite);
461 prop1.setState(pcf::IndiProperty::Idle);
462 prop1.add(pcf::IndiElement("current"));
463 prop1["current"] = "test";
464 prop1.add(pcf::IndiElement("target"));
465 prop1["target"] = "tset";
466
467 txtValRule rule1;
468
469 rule1.property(&prop1);
470 rule1.element("current");
472 rule1.target("test");
473
474
475 pcf::IndiProperty prop2(pcf::IndiProperty::Text);
476 prop2.setDevice("ruleTest2");
477 prop2.setName("prop2");
478 prop2.setPerm(pcf::IndiProperty::ReadWrite);
479 prop2.setState(pcf::IndiProperty::Idle);
480 prop2.add(pcf::IndiElement("current"));
481 prop2["current"] = "fail";
482 prop2.add(pcf::IndiElement("target"));
483 prop2["target"] = "liaf";
484
485 txtValRule rule2;
486
487 rule2.property(&prop2);
488 rule2.element("target");
490 rule2.target("liaf");
491
492
493 ruleCompRule rule3;
494 rule3.rule1(&rule1);
495 rule3.rule2(&rule2);
497
498 REQUIRE(rule3.value() == true);
499
500 }
501 }
502}
503
504SCENARIO( "compound rule compariaons", "[stateRuleEngine::rules]" )
505{
506 GIVEN("(A && B) || C")
507 {
508 pcf::IndiProperty prop1(pcf::IndiProperty::Text);
509 prop1.setDevice("ruleTest");
510 prop1.setName("prop1");
511 prop1.setPerm(pcf::IndiProperty::ReadWrite);
512 prop1.setState(pcf::IndiProperty::Idle);
513 prop1.add(pcf::IndiElement("current"));
514 prop1["current"] = "test";
515 prop1.add(pcf::IndiElement("target"));
516 prop1["target"] = "tset";
517
518 txtValRule rule1; //A
519
520 rule1.property(&prop1);
521 rule1.element("current");
523
524 pcf::IndiProperty prop2(pcf::IndiProperty::Text);
525 prop2.setDevice("ruleTest2");
526 prop2.setName("prop2");
527 prop2.setPerm(pcf::IndiProperty::ReadWrite);
528 prop2.setState(pcf::IndiProperty::Idle);
529 prop2.add(pcf::IndiElement("current"));
530 prop2["current"] = "fail";
531 prop2.add(pcf::IndiElement("target"));
532 prop2["target"] = "liaf";
533
534 txtValRule rule2; //B
535
536 rule2.property(&prop2);
537 rule2.element("target");
539
540 ruleCompRule rule3; //A&&B
541 rule3.rule1(&rule1);
542 rule3.rule2(&rule2);
544
545 pcf::IndiProperty prop3(pcf::IndiProperty::Text);
546 prop3.setDevice("ruleTest3");
547 prop3.setName("prop3");
548 prop3.setPerm(pcf::IndiProperty::ReadWrite);
549 prop3.setState(pcf::IndiProperty::Idle);
550 prop3.add(pcf::IndiElement("current"));
551 prop3["current"] = "pass";
552 prop3.add(pcf::IndiElement("target"));
553 prop3["target"] = "ssap";
554
555 txtValRule rule4; //C
556
557 rule4.property(&prop3);
558 rule4.element("current");
560
561 ruleCompRule rule5; // (A&&B) || C
562 rule5.rule1(&rule3); // A&&B
563 rule5.rule2(&rule4); // C
565
566 WHEN("A==1, B==0, C==1")
567 {
568 rule1.target("test"); //A==1
569 rule2.target("fail"); //B==0
570 rule4.target("pass"); //C==1
571
572 //(A && B) || C
573 REQUIRE(rule5.value() == true);
574
575 }
576
577 WHEN("A==0, B==0, C==1")
578 {
579 rule1.target("tset"); //A==0
580 rule2.target("fail"); //B==0
581 rule4.target("pass"); //C==1
582
583 //(A && B) || C
584 REQUIRE(rule5.value() == true);
585 }
586
587 WHEN("A==1, B==0, C==0")
588 {
589 rule1.target("test"); //A==1
590 rule2.target("fail"); //B==0
591 rule4.target("ssap"); //C==0
592
593 //(A && B) || C
594 REQUIRE(rule5.value() == false);
595 }
596
597 WHEN("A==1, B==1, C==0")
598 {
599 rule1.target("test"); //A==1
600 rule2.target("liaf"); //B==1
601 rule4.target("ssap"); //C==0
602
603 //(A && B) || C
604 REQUIRE(rule5.value() == true);
605 }
606 }
607}
608
609#endif
#define GIVEN(desc)
Definition catch.hpp:17763
#define WHEN(desc)
Definition catch.hpp:17765
#define SCENARIO(...)
Definition catch.hpp:17760
#define REQUIRE(...)
Definition catch.hpp:17676
@ Gt
Greater than.
@ Or
boolean or
@ Lt
Less than.
@ LtEq
Less than or equal to.
@ GtEq
Greater than or equal to.
@ And
boolean and
@ Neq
Not equal.
Compare two elements based on their numeric values.
virtual bool value()
Get the value of this rule.
Compare two elements based on their switch values.
virtual bool value()
Get the value of this rule.
Compare two elements based on their text values.
virtual bool value()
Get the value of this rule.
void comparison(const ruleComparison &c)
Set the comparison for this rule.
Compare the value of a number element to a target.
void target(const double &tgt)
Set the target for the comparison.
void tol(const double &t)
Set the tolerance.
virtual bool value()
Get the value of this rule.
void property(pcf::IndiProperty *property)
Set the property pointer.
void element(const std::string &el)
Set the element name.
A rule to compare two rules.
void rule2(indiCompRule *r)
Set the pointer to the second rule.
void rule1(indiCompRule *r)
Set the pointer to the first rule.
virtual bool value()
Get the value of this rule.
Compare the value of a switch to a target value.
virtual bool value()
Get the value of this rule.
void target(const pcf::IndiElement::SwitchStateType &ss)
Set the target for the comparison.
void property1(pcf::IndiProperty *property)
Set the first property pointer.
void element2(const std::string &el)
Set the second element name.
void element1(const std::string &el)
Set the first element name.
void property2(pcf::IndiProperty *property)
Set the second property pointer.
Compare the value of a text element to a target value.
void target(const std::string &target)
Set the target for the comparison.
virtual bool value()
Get the value of this rule.