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