/
symbolic_formula_test.cc
1428 lines (1237 loc) · 52 KB
/
symbolic_formula_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <algorithm>
#include <cmath>
#include <exception>
#include <limits>
#include <map>
#include <random>
#include <set>
#include <stdexcept>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <gtest/gtest.h>
#include "drake/common/drake_assert.h"
#include "drake/common/drake_copyable.h"
#include "drake/common/drake_throw.h"
#include "drake/common/symbolic.h"
#include "drake/common/test_utilities/is_memcpy_movable.h"
#include "drake/common/test_utilities/symbolic_test_util.h"
namespace drake {
using std::numeric_limits;
using test::IsMemcpyMovable;
namespace symbolic {
namespace {
using std::map;
using std::runtime_error;
using std::set;
using std::transform;
using std::unordered_map;
using std::unordered_set;
using std::vector;
using test::all_of;
using test::any_of;
using test::ExprEqual;
using test::FormulaEqual;
using test::FormulaLess;
using test::FormulaNotEqual;
using test::FormulaNotLess;
using test::VarEqual;
// Checks if a given 'formulas' is ordered by Formula::Less.
void CheckOrdering(const vector<Formula>& formulas) {
for (size_t i{0}; i < formulas.size(); ++i) {
for (size_t j{0}; j < formulas.size(); ++j) {
if (i < j) {
EXPECT_PRED2(FormulaLess, formulas[i], formulas[j])
<< "(Formulas[" << i << "] = " << formulas[i] << ")"
<< " is not less than "
<< "(Formulas[" << j << "] = " << formulas[j] << ")";
EXPECT_PRED2(FormulaNotLess, formulas[j], formulas[i])
<< "(Formulas[" << j << "] = " << formulas[j] << ")"
<< " is less than "
<< "(Formulas[" << i << "] = " << formulas[i] << ")";
} else if (i > j) {
EXPECT_PRED2(FormulaLess, formulas[j], formulas[i])
<< "(Formulas[" << j << "] = " << formulas[j] << ")"
<< " is not less than "
<< "(Formulas[" << i << "] = " << formulas[i] << ")";
EXPECT_PRED2(FormulaNotLess, formulas[i], formulas[j])
<< "(Formulas[" << i << "] = " << formulas[i] << ")"
<< " is less than "
<< "(Formulas[" << j << "] = " << formulas[j] << ")";
} else {
// i == j
EXPECT_PRED2(FormulaNotLess, formulas[i], formulas[j])
<< "(Formulas[" << i << "] = " << formulas[i] << ")"
<< " is less than "
<< "(Formulas[" << j << "] = " << formulas[j] << ")";
EXPECT_PRED2(FormulaNotLess, formulas[j], formulas[i])
<< "(Formulas[" << j << "] = " << formulas[j] << ")"
<< " is less than "
<< "(Formulas[" << i << "] = " << formulas[i] << ")";
}
}
}
}
// Provides common variables that are used by the following tests.
class SymbolicFormulaTest : public ::testing::Test {
protected:
const Variable var_x_{"x", Variable::Type::CONTINUOUS};
const Variable var_y_{"y", Variable::Type::CONTINUOUS};
const Variable var_z_{"z", Variable::Type::CONTINUOUS};
const Variable var_b1_{"x", Variable::Type::BOOLEAN};
const Variable var_b2_{"y", Variable::Type::BOOLEAN};
const Expression x_{var_x_};
const Expression y_{var_y_};
const Expression z_{var_z_};
const Expression e1_{x_ + y_};
const Expression e1_prime_{x_ + y_};
const Expression e2_{x_ - y_};
const Expression e3_{x_ + z_};
const Formula b1_{var_b1_};
const Formula b2_{var_b2_};
const Formula tt_{Formula::True()};
const Formula ff_{Formula::False()};
const Formula f1_{x_ + y_ > 0};
const Formula f2_{x_ * y_ < 5};
const Formula f3_{x_ / y_ < 5};
const Formula f4_{x_ - y_ < 5};
const Formula f_eq_{e1_ == 0.0};
const Formula f_neq_{e1_ != 0.0};
const Formula f_lt_{e1_ < 0.0};
const Formula f_lte_{e1_ <= 0.0};
const Formula f_gt_{e1_ > 0.0};
const Formula f_gte_{e1_ >= 0.0};
const Formula f_and_{f1_ && f2_};
const Formula f_or_{f1_ || f2_};
const Formula not_f_or_{!f_or_};
const Formula f_forall_{forall({var_x_, var_y_}, f_or_)};
const Formula f_isnan_{isnan(Expression::NaN())};
const Environment env1_{{var_x_, 1}, {var_y_, 1}};
const Environment env2_{{var_x_, 3}, {var_y_, 4}};
const Environment env3_{{var_x_, -2}, {var_y_, -5}};
const Environment env4_{{var_x_, -1}, {var_y_, -1}};
// The following matrices will be initialized in SetUp().
Eigen::Matrix<Expression, 2, 2> m_static_2x2_;
MatrixX<Expression> m_dynamic_2x2_;
Eigen::Matrix<Expression, 3, 3> m_static_3x3_;
// The following formulas will be initialized in SetUp().
Formula f_psd_static_2x2_;
Formula f_psd_dynamic_2x2_;
Formula f_psd_static_3x3_;
void SetUp() override {
// clang-format off
m_static_2x2_ << (x_ + y_), -1.0,
-1.0, y_;
m_dynamic_2x2_.resize(2, 2);
m_dynamic_2x2_ << (x_ + y_), 1.0,
1.0, y_;
m_static_3x3_ << (x_ + y_), 3.14, z_,
3.14, y_, z_ * z_,
z_, z_ * z_, 1.0;
// clang-format on
f_psd_static_2x2_ = positive_semidefinite(m_static_2x2_);
f_psd_dynamic_2x2_ = positive_semidefinite(m_dynamic_2x2_);
f_psd_static_3x3_ = positive_semidefinite(m_static_3x3_);
}
};
TEST_F(SymbolicFormulaTest, LessKind) {
// clang-format off
CheckOrdering({
Formula::False(),
Formula::True(),
b1_,
b2_,
x_ == y_,
x_ != y_,
x_> y_,
x_ >= y_,
x_ < y_,
x_ <= y_,
f1_ && f2_,
f1_ || f2_,
!f1_,
f_forall_,
f_isnan_,
f_psd_static_2x2_,
f_psd_dynamic_2x2_,
f_psd_static_3x3_});
// clang-format on
}
TEST_F(SymbolicFormulaTest, LessTrueFalse) {
CheckOrdering({Formula::False(), Formula::True()});
}
TEST_F(SymbolicFormulaTest, LessEq) {
const Formula f1{x_ == y_};
const Formula f2{x_ == z_};
const Formula f3{y_ == z_};
CheckOrdering({f1, f2, f3});
}
TEST_F(SymbolicFormulaTest, LessNeq) {
const Formula f1{x_ != y_};
const Formula f2{x_ != z_};
const Formula f3{y_ != z_};
CheckOrdering({f1, f2, f3});
}
TEST_F(SymbolicFormulaTest, LessGt) {
const Formula f1{x_ > y_};
const Formula f2{x_ > z_};
const Formula f3{y_ > z_};
CheckOrdering({f1, f2, f3});
}
TEST_F(SymbolicFormulaTest, LessGeq) {
const Formula f1{x_ >= y_};
const Formula f2{x_ >= z_};
const Formula f3{y_ >= z_};
CheckOrdering({f1, f2, f3});
}
TEST_F(SymbolicFormulaTest, LessLt) {
const Formula f1{x_ < y_};
const Formula f2{x_ < z_};
const Formula f3{y_ < z_};
CheckOrdering({f1, f2, f3});
}
TEST_F(SymbolicFormulaTest, LessLeq) {
const Formula f1{x_ <= y_};
const Formula f2{x_ <= z_};
const Formula f3{y_ <= z_};
CheckOrdering({f1, f2, f3});
}
TEST_F(SymbolicFormulaTest, LessAnd) {
const Formula and1{f1_ && f2_ && f3_};
const Formula and2{f1_ && f3_};
const Formula and3{f2_ && f3_};
CheckOrdering({and1, and2, and3});
}
TEST_F(SymbolicFormulaTest, LessOr) {
const Formula or1{f1_ || f2_ || f3_};
const Formula or2{f1_ || f3_};
const Formula or3{f2_ || f3_};
CheckOrdering({or1, or2, or3});
}
TEST_F(SymbolicFormulaTest, LessNot) {
const Formula not1{!f1_};
const Formula not2{!f2_};
const Formula not3{!f3_};
CheckOrdering({not1, not2, not3});
}
TEST_F(SymbolicFormulaTest, LessForall) {
const Formula forall1{forall({var_x_, var_y_}, f1_)};
const Formula forall2{forall({var_x_, var_y_, var_z_}, f1_)};
const Formula forall3{forall({var_x_, var_y_, var_z_}, f2_)};
CheckOrdering({forall1, forall2, forall3});
}
TEST_F(SymbolicFormulaTest, True) {
EXPECT_PRED2(FormulaEqual, Formula::True(), Formula{tt_});
EXPECT_TRUE(Formula::True().Evaluate());
EXPECT_EQ(Formula::True().GetFreeVariables().size(), 0u);
EXPECT_EQ(Formula::True().to_string(), "True");
EXPECT_TRUE(is_true(Formula::True()));
}
TEST_F(SymbolicFormulaTest, False) {
EXPECT_PRED2(FormulaEqual, Formula::False(), Formula{ff_});
EXPECT_FALSE(Formula::False().Evaluate());
EXPECT_EQ(Formula::False().GetFreeVariables().size(), 0u);
EXPECT_EQ(Formula::False().to_string(), "False");
EXPECT_TRUE(is_false(Formula::False()));
}
TEST_F(SymbolicFormulaTest, Variable) {
// Tests is_variable and get_variable functions.
EXPECT_TRUE(is_variable(b1_));
EXPECT_TRUE(is_variable(b2_));
EXPECT_PRED2(VarEqual, get_variable(b1_), var_b1_);
EXPECT_PRED2(VarEqual, get_variable(b2_), var_b2_);
}
TEST_F(SymbolicFormulaTest, IsNaN) {
// Things that aren't NaN are !isnan.
const Expression zero{0};
const Formula zero_is_nan{isnan(zero)};
EXPECT_FALSE(zero_is_nan.Evaluate());
// Things that _are_ NaN are exceptions, which is consistent with Expression
// disallowing NaNs to be evaluated at runtime.
const Expression nan{NAN};
const Formula nan_is_nan{isnan(nan)};
EXPECT_THROW(nan_is_nan.Evaluate(), runtime_error);
// Buried NaNs are safe.
const Formula ite_nan1{isnan(if_then_else(tt_, zero, nan))};
EXPECT_FALSE(ite_nan1.Evaluate());
// This case will be evaluated to NaN and we will have runtime_error.
const Formula ite_nan2{isnan(if_then_else(ff_, zero, nan))};
EXPECT_THROW(ite_nan2.Evaluate(), runtime_error);
// Formula isnan(x / y) should throw a runtime_error when evaluated with
// an environment mapping both of x and y to zero, because 0.0 / 0.0 = NaN.
const Expression x_div_y{x_ / y_};
const Environment env1{{var_x_, 0.0}, {var_y_, 0.0}};
EXPECT_THROW(isnan(x_div_y).Evaluate(env1), runtime_error);
// If the included expression `e` is not evaluated to NaN, `isnan(e)` should
// return false.
const Environment env2{{var_x_, 3.0}, {var_y_, 2.0}};
EXPECT_FALSE(isnan(x_div_y).Evaluate(env2));
}
TEST_F(SymbolicFormulaTest, EqualTo1) {
const Formula f_eq{x_ == y_};
const Formula f_ne{x_ != y_};
const Formula f_lt{x_ < y_};
const Formula f_le{x_ <= y_};
const Formula f_gt{x_ > y_};
const Formula f_ge{x_ >= y_};
EXPECT_PRED2(FormulaEqual, f_eq, f_eq);
EXPECT_PRED2(FormulaEqual, f_eq, x_ == y_);
EXPECT_PRED2(FormulaNotEqual, f_eq, x_ == z_);
EXPECT_PRED2(FormulaNotEqual, f_eq, f_ne);
EXPECT_PRED2(FormulaNotEqual, f_eq, f_lt);
EXPECT_PRED2(FormulaNotEqual, f_eq, f_le);
EXPECT_PRED2(FormulaNotEqual, f_eq, f_gt);
EXPECT_PRED2(FormulaNotEqual, f_eq, f_ge);
EXPECT_PRED2(FormulaNotEqual, f_ne, f_eq);
EXPECT_PRED2(FormulaEqual, f_ne, f_ne);
EXPECT_PRED2(FormulaEqual, f_ne, x_ != y_);
EXPECT_PRED2(FormulaNotEqual, f_ne, x_ != z_);
EXPECT_PRED2(FormulaNotEqual, f_ne, f_lt);
EXPECT_PRED2(FormulaNotEqual, f_ne, f_le);
EXPECT_PRED2(FormulaNotEqual, f_ne, f_gt);
EXPECT_PRED2(FormulaNotEqual, f_ne, f_ge);
EXPECT_PRED2(FormulaNotEqual, f_lt, f_eq);
EXPECT_PRED2(FormulaNotEqual, f_lt, f_ne);
EXPECT_PRED2(FormulaEqual, f_lt, f_lt);
EXPECT_PRED2(FormulaEqual, f_lt, x_ < y_);
EXPECT_PRED2(FormulaNotEqual, f_lt, x_ < z_);
EXPECT_PRED2(FormulaNotEqual, f_lt, f_le);
EXPECT_PRED2(FormulaNotEqual, f_lt, f_gt);
EXPECT_PRED2(FormulaNotEqual, f_lt, f_ge);
EXPECT_PRED2(FormulaNotEqual, f_le, f_eq);
EXPECT_PRED2(FormulaNotEqual, f_le, f_ne);
EXPECT_PRED2(FormulaNotEqual, f_le, f_lt);
EXPECT_PRED2(FormulaEqual, f_le, f_le);
EXPECT_PRED2(FormulaEqual, f_le, x_ <= y_);
EXPECT_PRED2(FormulaNotEqual, f_le, x_ <= z_);
EXPECT_PRED2(FormulaNotEqual, f_le, f_gt);
EXPECT_PRED2(FormulaNotEqual, f_le, f_ge);
EXPECT_PRED2(FormulaNotEqual, f_gt, f_eq);
EXPECT_PRED2(FormulaNotEqual, f_gt, f_ne);
EXPECT_PRED2(FormulaNotEqual, f_gt, f_lt);
EXPECT_PRED2(FormulaNotEqual, f_gt, f_le);
EXPECT_PRED2(FormulaEqual, f_gt, f_gt);
EXPECT_PRED2(FormulaEqual, f_gt, x_ > y_);
EXPECT_PRED2(FormulaNotEqual, f_gt, x_ > z_);
EXPECT_PRED2(FormulaNotEqual, f_gt, f_ge);
EXPECT_PRED2(FormulaNotEqual, f_ge, f_eq);
EXPECT_PRED2(FormulaNotEqual, f_ge, f_ne);
EXPECT_PRED2(FormulaNotEqual, f_ge, f_lt);
EXPECT_PRED2(FormulaNotEqual, f_ge, f_le);
EXPECT_PRED2(FormulaNotEqual, f_ge, f_gt);
EXPECT_PRED2(FormulaEqual, f_ge, f_ge);
EXPECT_PRED2(FormulaEqual, f_ge, x_ >= y_);
EXPECT_PRED2(FormulaNotEqual, f_ge, x_ >= z_);
}
TEST_F(SymbolicFormulaTest, EqualTo2) {
const Formula f1{x_ == y_};
const Formula f2{y_ > z_};
const Formula f_and{f1 && f2};
const Formula f_or{f1 || f2};
const Formula f_not{!f1};
EXPECT_PRED2(FormulaEqual, f_and, f_and);
EXPECT_PRED2(FormulaNotEqual, f_and, f_or);
EXPECT_PRED2(FormulaNotEqual, f_and, f_not);
EXPECT_PRED2(FormulaNotEqual, f_or, f_and);
EXPECT_PRED2(FormulaEqual, f_or, f_or);
EXPECT_PRED2(FormulaNotEqual, f_or, f_not);
EXPECT_PRED2(FormulaNotEqual, f_not, f_and);
EXPECT_PRED2(FormulaNotEqual, f_not, f_or);
EXPECT_PRED2(FormulaEqual, f_not, f_not);
}
TEST_F(SymbolicFormulaTest, EqualTo3) {
const Formula f_forall1{forall({var_x_, var_y_}, f_or_)};
const Formula f_forall2{forall({var_x_, var_y_, var_z_}, f_or_)};
const Formula f_forall3{forall({var_x_, var_y_}, f_and_)};
const Formula f_forall4{forall({var_x_, var_y_, var_z_}, f_and_)};
EXPECT_PRED2(FormulaEqual, f_forall1, f_forall1);
EXPECT_PRED2(FormulaNotEqual, f_forall1, f_forall2);
EXPECT_PRED2(FormulaNotEqual, f_forall1, f_forall3);
EXPECT_PRED2(FormulaNotEqual, f_forall1, f_forall4);
EXPECT_PRED2(FormulaNotEqual, f_forall2, f_forall1);
EXPECT_PRED2(FormulaEqual, f_forall2, f_forall2);
EXPECT_PRED2(FormulaNotEqual, f_forall2, f_forall3);
EXPECT_PRED2(FormulaNotEqual, f_forall2, f_forall4);
EXPECT_PRED2(FormulaNotEqual, f_forall3, f_forall1);
EXPECT_PRED2(FormulaNotEqual, f_forall3, f_forall2);
EXPECT_PRED2(FormulaEqual, f_forall3, f_forall3);
EXPECT_PRED2(FormulaNotEqual, f_forall3, f_forall4);
EXPECT_PRED2(FormulaNotEqual, f_forall4, f_forall1);
EXPECT_PRED2(FormulaNotEqual, f_forall4, f_forall2);
EXPECT_PRED2(FormulaNotEqual, f_forall4, f_forall3);
EXPECT_PRED2(FormulaEqual, f_forall4, f_forall4);
}
TEST_F(SymbolicFormulaTest, Eq) {
const Formula f1{e1_ == e1_}; // true
EXPECT_PRED2(FormulaEqual, f1, Formula::True());
EXPECT_PRED2(FormulaNotEqual, f1, Formula::False());
const Formula f2{e1_ == e1_prime_}; // true
EXPECT_PRED2(FormulaEqual, f2, Formula::True());
EXPECT_PRED2(FormulaNotEqual, f2, Formula::False());
const Formula f3{e1_ == e3_};
EXPECT_PRED2(FormulaNotEqual, f3, Formula::True());
const Formula f4{e2_ == e3_};
EXPECT_PRED2(FormulaNotEqual, f4, Formula::True());
const Formula f5{x_ == x_ + 5}; // false
EXPECT_PRED2(FormulaEqual, f5, Formula::False());
EXPECT_TRUE(all_of({f3, f4}, is_equal_to));
EXPECT_FALSE(any_of({f1, f2, f5}, is_equal_to));
const Environment env{{var_x_, 2}, {var_y_, 3}, {var_z_, 3}};
EXPECT_EQ(f3.Evaluate(env), (2 + 3) == (2 + 3));
EXPECT_EQ(f4.Evaluate(env), (2 - 3) == (2 + 3));
EXPECT_EQ((5 + x_ == 3 + y_).to_string(), "((5 + x) == (3 + y))");
}
TEST_F(SymbolicFormulaTest, Neq) {
const Formula f1{e1_ != e1_}; // false
EXPECT_PRED2(FormulaEqual, f1, Formula::False());
EXPECT_PRED2(FormulaNotEqual, f1, Formula::True());
const Formula f2{e1_ != e1_prime_}; // false
EXPECT_PRED2(FormulaEqual, f2, Formula::False());
EXPECT_PRED2(FormulaNotEqual, f2, Formula::True());
const Formula f3{e1_ != e3_};
EXPECT_PRED2(FormulaNotEqual, f3, Formula::False());
const Formula f4{e2_ != e3_};
EXPECT_PRED2(FormulaNotEqual, f4, Formula::False());
const Formula f5{x_ != x_ + 5}; // true
EXPECT_PRED2(FormulaEqual, f5, Formula::True());
EXPECT_TRUE(all_of({f3, f4}, is_not_equal_to));
EXPECT_FALSE(any_of({f1, f2, f5}, is_not_equal_to));
const Environment env{{var_x_, 2}, {var_y_, 3}, {var_z_, 3}};
EXPECT_EQ(f3.Evaluate(env), (2 + 3) != (2 + 3));
EXPECT_EQ(f4.Evaluate(env), (2 - 3) != (2 + 3));
EXPECT_EQ((5 + x_ != 3 + y_).to_string(), "((5 + x) != (3 + y))");
}
TEST_F(SymbolicFormulaTest, Lt) {
const Formula f1{e1_ < e1_}; // false
EXPECT_PRED2(FormulaEqual, f1, Formula::False());
EXPECT_PRED2(FormulaNotEqual, f1, Formula::True());
const Formula f2{e1_ < e1_prime_}; // false
EXPECT_PRED2(FormulaEqual, f2, Formula::False());
EXPECT_PRED2(FormulaNotEqual, f2, Formula::True());
const Formula f3{e1_ < e3_};
EXPECT_PRED2(FormulaNotEqual, f3, Formula::True());
const Formula f4{e2_ < e3_};
EXPECT_PRED2(FormulaNotEqual, f4, Formula::True());
const Formula f5{x_ < x_ + 5}; // true
EXPECT_PRED2(FormulaEqual, f5, Formula::True());
EXPECT_TRUE(all_of({f3, f4}, is_less_than));
EXPECT_FALSE(any_of({f1, f2, f5}, is_less_than));
const Environment env{{var_x_, 2}, {var_y_, 3}, {var_z_, 3}};
EXPECT_EQ(f3.Evaluate(env), (2 + 3) < (2 + 3));
EXPECT_EQ(f4.Evaluate(env), (2 - 3) < (2 + 3));
EXPECT_EQ((5 + x_ < 3 + y_).to_string(), "((5 + x) < (3 + y))");
}
TEST_F(SymbolicFormulaTest, Gt) {
const Formula f1{e1_ > e1_}; // false
EXPECT_PRED2(FormulaEqual, f1, Formula::False());
EXPECT_PRED2(FormulaNotEqual, f1, Formula::True());
const Formula f2{e1_ > e1_prime_}; // false
EXPECT_PRED2(FormulaEqual, f2, Formula::False());
EXPECT_PRED2(FormulaNotEqual, f2, Formula::True());
const Formula f3{e1_ > e3_};
EXPECT_PRED2(FormulaNotEqual, f3, Formula::True());
const Formula f4{e2_ > e3_};
EXPECT_PRED2(FormulaNotEqual, f4, Formula::True());
const Formula f5{x_ > x_ + 5}; // false
EXPECT_PRED2(FormulaEqual, f5, Formula::False());
EXPECT_TRUE(all_of({f3, f4}, is_greater_than));
EXPECT_FALSE(any_of({f1, f2, f5}, is_greater_than));
const Environment env{{var_x_, 2}, {var_y_, 3}, {var_z_, 3}};
EXPECT_EQ(f3.Evaluate(env), (2 + 3) > (2 + 3));
EXPECT_EQ(f4.Evaluate(env), (2 - 3) > (2 + 3));
EXPECT_EQ((5 + x_ > 3 + y_).to_string(), "((5 + x) > (3 + y))");
}
TEST_F(SymbolicFormulaTest, Leq) {
const Formula f1{e1_ <= e1_}; // true
EXPECT_PRED2(FormulaEqual, f1, Formula::True());
EXPECT_PRED2(FormulaNotEqual, f1, Formula::False());
const Formula f2{e1_ <= e1_prime_}; // true
EXPECT_PRED2(FormulaEqual, f2, Formula::True());
EXPECT_PRED2(FormulaNotEqual, f2, Formula::False());
const Formula f3{e1_ <= e3_};
EXPECT_PRED2(FormulaNotEqual, f3, Formula::True());
const Formula f4{e2_ <= e3_};
EXPECT_PRED2(FormulaNotEqual, f4, Formula::True());
const Formula f5{x_ <= x_ + 5}; // true
EXPECT_PRED2(FormulaEqual, f5, Formula::True());
EXPECT_TRUE(all_of({f3, f4}, is_less_than_or_equal_to));
EXPECT_FALSE(any_of({f1, f2, f5}, is_less_than_or_equal_to));
const Environment env{{var_x_, 2}, {var_y_, 3}, {var_z_, 3}};
EXPECT_EQ(f3.Evaluate(env), (2 + 3) <= (2 + 3));
EXPECT_EQ(f4.Evaluate(env), (2 - 3) <= (2 + 3));
EXPECT_EQ((5 + x_ <= 3 + y_).to_string(), "((5 + x) <= (3 + y))");
}
TEST_F(SymbolicFormulaTest, Geq) {
const Formula f1{e1_ >= e1_}; // true
EXPECT_PRED2(FormulaEqual, f1, Formula::True());
EXPECT_PRED2(FormulaNotEqual, f1, Formula::False());
const Formula f2{e1_ >= e1_prime_}; // true
EXPECT_PRED2(FormulaEqual, f2, Formula::True());
EXPECT_PRED2(FormulaNotEqual, f2, Formula::False());
const Formula f3{e1_ >= e3_};
EXPECT_PRED2(FormulaNotEqual, f3, Formula::True());
const Formula f4{e2_ >= e3_};
EXPECT_PRED2(FormulaNotEqual, f4, Formula::True());
const Formula f5{x_ >= x_ + 5}; // false
EXPECT_PRED2(FormulaEqual, f5, Formula::False());
EXPECT_TRUE(all_of({f3, f4}, is_greater_than_or_equal_to));
EXPECT_FALSE(any_of({f1, f2, f5}, is_greater_than_or_equal_to));
const Environment env{{var_x_, 2}, {var_y_, 3}, {var_z_, 3}};
EXPECT_EQ(f3.Evaluate(env), (2 + 3) >= (2 + 3));
EXPECT_EQ(f4.Evaluate(env), (2 - 3) >= (2 + 3));
EXPECT_EQ((5 + x_ >= 3 + y_).to_string(), "((5 + x) >= (3 + y))");
}
TEST_F(SymbolicFormulaTest, And1) {
EXPECT_PRED2(FormulaEqual, tt_, tt_ && tt_);
EXPECT_PRED2(FormulaEqual, ff_, ff_ && tt_);
EXPECT_PRED2(FormulaEqual, ff_, tt_ && ff_);
EXPECT_PRED2(FormulaEqual, ff_, ff_ && ff_);
EXPECT_PRED2(FormulaEqual, f1_, tt_ && f1_);
EXPECT_PRED2(FormulaEqual, f1_, f1_ && tt_);
EXPECT_PRED2(FormulaEqual, make_conjunction({tt_, f1_, tt_}), f1_);
EXPECT_PRED2(FormulaEqual, make_conjunction({tt_, tt_, ff_}), ff_);
// Checks if flattening works: (f₁ ∧ f₂) ∧ (f₃ ∧ f₄) => f₁ ∧ f₂ ∧ f₃ ∧ f₄.
EXPECT_PRED2(FormulaEqual, make_conjunction({f1_ && f2_, f3_ && f4_}),
make_conjunction({f1_, f2_, f3_, f4_}));
// Empty conjunction = True.
EXPECT_PRED2(FormulaEqual, make_conjunction({}), Formula::True());
}
TEST_F(SymbolicFormulaTest, And2) {
EXPECT_EQ(f_and_.Evaluate(env1_), (1 + 1 > 0) && (1 * 1 < 5));
EXPECT_EQ(f_and_.Evaluate(env2_), (3 + 4 > 0) && (3 * 4 < 5));
EXPECT_EQ(f_and_.Evaluate(env3_), (-2 + -5 > 0) && (-2 * -5 < 5));
EXPECT_EQ(f_and_.Evaluate(env4_), (-1 + -1 > 0) && (-1 * -1 < 5));
EXPECT_EQ((x_ == 3 && y_ == 5).to_string(), "((x == 3) and (y == 5))");
EXPECT_TRUE(is_conjunction(x_ == 3 && y_ == 5));
EXPECT_TRUE(is_nary(x_ == 3 && y_ == 5));
}
TEST_F(SymbolicFormulaTest, And3) {
// Flattening
EXPECT_PRED2(FormulaEqual, f1_ && f2_ && f3_ && f4_,
f1_ && f2_ && f3_ && f4_);
EXPECT_PRED2(FormulaEqual, (f1_ && f2_) && (f3_ && f4_),
f1_ && f2_ && f3_ && f4_);
EXPECT_PRED2(FormulaEqual, f1_ && (f2_ && f3_) && f4_,
f1_ && f2_ && f3_ && f4_);
EXPECT_PRED2(FormulaEqual, f1_ && ((f2_ && f3_) && f4_),
f1_ && f2_ && f3_ && f4_);
// Remove duplicate
EXPECT_PRED2(FormulaEqual, f1_ && f2_ && f1_, f1_ && f2_);
}
TEST_F(SymbolicFormulaTest, And4) {
// Simplification: f && f => f.
for (const Formula& f :
{b1_, b2_, tt_, ff_, f1_, f2_, f3_, f4_, f_eq_, f_neq_, f_lt_, f_lte_,
f_gt_, f_gte_, f_and_, f_or_, not_f_or_, f_forall_}) {
EXPECT_PRED2(FormulaEqual, f && f, f);
}
}
TEST_F(SymbolicFormulaTest, And5) {
// Flatten and removing duplicates. This is the example mentioned in
// symbolic_formula.h file:
// (f1 && f2) && f1 => f1 && f2
// f1 && (f2 && f1) => f1 && f2
EXPECT_PRED2(FormulaEqual, (f1_ && f2_) && f1_, f1_ && f2_);
EXPECT_PRED2(FormulaEqual, f1_ && (f2_ && f1_), f1_ && f2_);
EXPECT_PRED2(FormulaEqual, (f1_ && f2_) && f1_, f1_ && (f2_ && f1_));
}
TEST_F(SymbolicFormulaTest, AndWithBooleanVariableOperator) {
// Checks if operator&& works Boolean variables as expected.
const Formula f1{var_b1_ && var_b2_};
ASSERT_TRUE(is_conjunction(f1));
EXPECT_EQ(get_operands(f1).count(b1_), 1);
EXPECT_EQ(get_operands(f1).count(b2_), 1);
const Formula f2{var_b1_ && (y_ > 0)};
ASSERT_TRUE(is_conjunction(f2));
EXPECT_EQ(get_operands(f2).count(b1_), 1);
const Formula f3{(x_ > 0) && var_b2_};
ASSERT_TRUE(is_conjunction(f3));
EXPECT_EQ(get_operands(f3).count(b2_), 1);
}
TEST_F(SymbolicFormulaTest, AndWithBooleanVariableEvaluate) {
// Checks the evaluations of conjunctive formulas with Boolean variables.
const Formula f{b1_ && b2_};
const Environment env1{{var_b1_, true}, {var_b2_, true}};
const Environment env2{{var_b1_, true}, {var_b2_, false}};
const Environment env3{{var_b1_, false}, {var_b2_, true}};
const Environment env4{{var_b1_, false}, {var_b2_, false}};
EXPECT_TRUE(f.Evaluate(env1));
EXPECT_FALSE(f.Evaluate(env2));
EXPECT_FALSE(f.Evaluate(env3));
EXPECT_FALSE(f.Evaluate(env4));
}
TEST_F(SymbolicFormulaTest, Or1) {
EXPECT_PRED2(FormulaEqual, tt_, tt_ || tt_);
EXPECT_PRED2(FormulaEqual, tt_, ff_ || tt_);
EXPECT_PRED2(FormulaEqual, tt_, tt_ || ff_);
EXPECT_PRED2(FormulaEqual, ff_, ff_ || ff_);
EXPECT_PRED2(FormulaEqual, f1_, ff_ || f1_);
EXPECT_PRED2(FormulaEqual, f1_, f1_ || ff_);
EXPECT_PRED2(FormulaEqual, make_disjunction({ff_, f1_, ff_}), f1_);
EXPECT_PRED2(FormulaEqual, make_disjunction({ff_, ff_, tt_}), tt_);
// Checks if flattening works: (f₁ ∨ f₂) ∨ (f₃ ∨ f₄) => f₁ ∨ f₂ ∨ f₃ ∨ f₄.
EXPECT_PRED2(FormulaEqual, make_disjunction({f1_ || f2_, f3_ || f4_}),
make_disjunction({f1_, f2_, f3_, f4_}));
// Empty disjunction = False.
EXPECT_PRED2(FormulaEqual, make_disjunction({}), Formula::False());
}
TEST_F(SymbolicFormulaTest, Or2) {
EXPECT_EQ(f_or_.Evaluate(env1_), (1 + 1 > 0) || (1 * 1 < 5));
EXPECT_EQ(f_or_.Evaluate(env2_), (3 + 4 > 0) || (3 * 4 < 5));
EXPECT_EQ(f_or_.Evaluate(env3_), (-2 + -5 > 0) || (-2 * -5 < 5));
EXPECT_EQ(f_or_.Evaluate(env4_), (-1 + -1 > 0) || (-1 * -1 < 5));
EXPECT_EQ((x_ == 3 || y_ == 5).to_string(), "((x == 3) or (y == 5))");
EXPECT_TRUE(is_disjunction(x_ == 3 || y_ == 5));
EXPECT_TRUE(is_nary(x_ == 3 || y_ == 5));
}
TEST_F(SymbolicFormulaTest, Or3) {
// Flattening
EXPECT_PRED2(FormulaEqual, f1_ || f2_ || f3_ || f4_,
f1_ || f2_ || f3_ || f4_);
EXPECT_PRED2(FormulaEqual, (f1_ || f2_) || (f3_ || f4_),
f1_ || f2_ || f3_ || f4_);
EXPECT_PRED2(FormulaEqual, f1_ || (f2_ || f3_) || f4_,
f1_ || f2_ || f3_ || f4_);
EXPECT_PRED2(FormulaEqual, f1_ || ((f2_ || f3_) || f4_),
f1_ || f2_ || f3_ || f4_);
// Remove duplicate
EXPECT_PRED2(FormulaEqual, f1_ || f2_ || f1_, f1_ || f2_);
}
TEST_F(SymbolicFormulaTest, Or4) {
// Simplification: f || f => f.
for (const Formula& f :
{b1_, b2_, tt_, ff_, f1_, f2_, f3_, f4_, f_eq_, f_neq_, f_lt_, f_lte_,
f_gt_, f_gte_, f_and_, f_or_, not_f_or_, f_forall_}) {
EXPECT_PRED2(FormulaEqual, f || f, f);
}
}
TEST_F(SymbolicFormulaTest, Or5) {
// Flatten and removing duplicates. This is a disjunctive version of the
// example mentioned in symbolic_formula.h file:
// (f1 || f2) || f1 => f1 || f2
// f1 || (f2 || f1) => f1 || f2
EXPECT_PRED2(FormulaEqual, (f1_ || f2_) || f1_, f1_ || f2_);
EXPECT_PRED2(FormulaEqual, f1_ || (f2_ || f1_), f1_ || f2_);
EXPECT_PRED2(FormulaEqual, (f1_ || f2_) || f1_, f1_ || (f2_ || f1_));
}
TEST_F(SymbolicFormulaTest, OrWithBooleanVariableOperator) {
// Checks if operator|| works with Boolean variables as expected.
const Formula f1{var_b1_ || var_b2_};
ASSERT_TRUE(is_disjunction(f1));
EXPECT_EQ(get_operands(f1).count(b1_), 1);
EXPECT_EQ(get_operands(f1).count(b2_), 1);
const Formula f2{var_b1_ || (y_ > 0)};
ASSERT_TRUE(is_disjunction(f2));
EXPECT_EQ(get_operands(f2).count(b1_), 1);
const Formula f3{(x_ > 0) || var_b2_};
ASSERT_TRUE(is_disjunction(f3));
EXPECT_EQ(get_operands(f3).count(b2_), 1);
}
TEST_F(SymbolicFormulaTest, OrWithBooleanVariableEvaluate) {
// Checks the evaluations of disjunctive formulas with Boolean variables.
const Formula f{b1_ || b2_};
const Environment env1{{var_b1_, true}, {var_b2_, true}};
const Environment env2{{var_b1_, true}, {var_b2_, false}};
const Environment env3{{var_b1_, false}, {var_b2_, true}};
const Environment env4{{var_b1_, false}, {var_b2_, false}};
EXPECT_TRUE(f.Evaluate(env1));
EXPECT_TRUE(f.Evaluate(env2));
EXPECT_TRUE(f.Evaluate(env3));
EXPECT_FALSE(f.Evaluate(env4));
}
TEST_F(SymbolicFormulaTest, Not1) {
EXPECT_PRED2(FormulaEqual, ff_, !tt_);
EXPECT_PRED2(FormulaEqual, tt_, !ff_);
EXPECT_PRED2(FormulaEqual, tt_, !(!tt_));
EXPECT_PRED2(FormulaEqual, ff_, !(!ff_));
EXPECT_PRED2(FormulaEqual, !(x_ == 5), !(x_ == 5));
EXPECT_PRED2(FormulaNotEqual, !(x_ == 5), !(x_ == 6));
}
TEST_F(SymbolicFormulaTest, Not2) {
EXPECT_EQ(not_f_or_.Evaluate(env1_), !((1 + 1 > 0) || (1 * 1 < 5)));
EXPECT_EQ(not_f_or_.Evaluate(env2_), !((3 + 4 > 0) || (3 * 4 < 5)));
EXPECT_EQ(not_f_or_.Evaluate(env3_), !((-2 + -5 > 0) || (-2 * -5 < 5)));
EXPECT_EQ(not_f_or_.Evaluate(env4_), !((-1 + -1 > 0) || (-1 * -1 < 5)));
EXPECT_EQ((!(x_ == 5)).to_string(), "!((x == 5))");
EXPECT_TRUE(is_negation(!(x_ == 5)));
}
// Tests if `!(!f)` is simplified into `f` for all formulas in
// SymbolicFormulaTest.
TEST_F(SymbolicFormulaTest, DoubleNegationSimplification) {
const vector<Formula> collection{b1_, b2_, tt_, ff_, f1_,
f2_, f3_, f4_, f_eq_, f_neq_,
f_lt_, f_lte_, f_gt_, f_gte_, f_and_,
f_or_, not_f_or_, f_forall_, f_isnan_};
vector<Formula> negated_collection{collection.size()};
transform(collection.cbegin(), collection.cend(), negated_collection.begin(),
[](const Formula& f) { return !f; });
for (size_t i = 0; i < collection.size(); ++i) {
EXPECT_PRED2(FormulaEqual, collection[i], !(negated_collection[i]));
}
}
TEST_F(SymbolicFormulaTest, NotWithBooleanVariableOperator) {
// Checks if operator! works with Boolean variables as expected.
const Formula f{!var_b1_};
EXPECT_TRUE(is_negation(f));
ASSERT_TRUE(is_variable(get_operand(f)));
EXPECT_PRED2(VarEqual, get_variable(get_operand(f)), var_b1_);
}
TEST_F(SymbolicFormulaTest, NotWithBooleanVariableEvaluate) {
// Checks the evaluations of negation formulas with a Boolean variable.
const Formula f{!b1_};
const Environment env1{{var_b1_, true}};
const Environment env2{{var_b1_, false}};
EXPECT_FALSE(f.Evaluate(env1));
EXPECT_TRUE(f.Evaluate(env2));
}
TEST_F(SymbolicFormulaTest, Forall1) {
const Formula f1{forall({var_x_, var_y_}, x_ == y_)};
const Formula f2{forall({var_x_, var_y_}, x_ == y_)};
const Formula f3{forall({var_x_, var_y_}, x_ > y_)};
EXPECT_PRED2(FormulaEqual, f1, f2);
EXPECT_PRED2(FormulaNotEqual, f1, f3);
EXPECT_PRED2(FormulaNotEqual, f2, f3);
EXPECT_TRUE(all_of({f1, f2, f3}, is_forall));
}
TEST_F(SymbolicFormulaTest, Forall2) {
const Formula f1{forall({var_x_, var_y_}, x_ == y_)};
EXPECT_THROW(f1.Evaluate(), runtime_error);
}
TEST_F(SymbolicFormulaTest, PSD_Exception) {
auto non_square = Eigen::Matrix<Expression, 2, 3>::Zero().eval();
EXPECT_THROW(positive_semidefinite(non_square), runtime_error);
Eigen::Matrix<Expression, 3, 3> m;
// clang-format off
m << 1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
7.0, 8.0, 9.0;
// clang-format on
// m is not symmetric.
EXPECT_THROW(positive_semidefinite(m), runtime_error);
// positive_semidefinite only takes Eigen::Lower and Eigen::Upper.
EXPECT_THROW(positive_semidefinite(m, Eigen::UnitDiag), runtime_error);
EXPECT_THROW(positive_semidefinite(m, Eigen::ZeroDiag), runtime_error);
EXPECT_THROW(positive_semidefinite(m, Eigen::UnitLower), runtime_error);
EXPECT_THROW(positive_semidefinite(m, Eigen::UnitUpper), runtime_error);
EXPECT_THROW(positive_semidefinite(m, Eigen::StrictlyLower), runtime_error);
EXPECT_THROW(positive_semidefinite(m, Eigen::StrictlyUpper), runtime_error);
EXPECT_THROW(positive_semidefinite(m, Eigen::SelfAdjoint), runtime_error);
EXPECT_THROW(positive_semidefinite(m, Eigen::Symmetric), runtime_error);
}
TEST_F(SymbolicFormulaTest, PSD_GetFreeVariables) {
const Variables vars1{f_psd_static_2x2_.GetFreeVariables()};
EXPECT_EQ(vars1.size(), 2);
EXPECT_TRUE(vars1.include(var_x_));
EXPECT_TRUE(vars1.include(var_y_));
const Variables vars2{f_psd_dynamic_2x2_.GetFreeVariables()};
EXPECT_EQ(vars2.size(), 2);
EXPECT_TRUE(vars2.include(var_x_));
EXPECT_TRUE(vars2.include(var_y_));
const Variables vars3{f_psd_static_3x3_.GetFreeVariables()};
EXPECT_EQ(vars3.size(), 3);
EXPECT_TRUE(vars3.include(var_x_));
EXPECT_TRUE(vars3.include(var_y_));
EXPECT_TRUE(vars3.include(var_z_));
}
TEST_F(SymbolicFormulaTest, PSD_EqualTo) {
EXPECT_TRUE(f_psd_static_2x2_.EqualTo(f_psd_static_2x2_));
EXPECT_FALSE(f_psd_static_2x2_.EqualTo(f_psd_dynamic_2x2_));
EXPECT_FALSE(f_psd_static_2x2_.EqualTo(f_psd_static_3x3_));
EXPECT_FALSE(f_psd_dynamic_2x2_.EqualTo(f_psd_static_2x2_));
EXPECT_TRUE(f_psd_dynamic_2x2_.EqualTo(f_psd_dynamic_2x2_));
EXPECT_FALSE(f_psd_dynamic_2x2_.EqualTo(f_psd_static_3x3_));
EXPECT_FALSE(f_psd_static_3x3_.EqualTo(f_psd_static_2x2_));
EXPECT_FALSE(f_psd_static_3x3_.EqualTo(f_psd_dynamic_2x2_));
EXPECT_TRUE(f_psd_static_3x3_.EqualTo(f_psd_static_3x3_));
}
TEST_F(SymbolicFormulaTest, PSD_Evaluate) {
EXPECT_THROW(f_psd_static_2x2_.Evaluate(), runtime_error);
EXPECT_THROW(f_psd_dynamic_2x2_.Evaluate(), runtime_error);
EXPECT_THROW(f_psd_static_3x3_.Evaluate(), runtime_error);
}
TEST_F(SymbolicFormulaTest, GetFreeVariables) {
const Formula f1{x_ + y_ > 0};
const Formula f2{y_ * z_ < 5};
const Formula f_or{f1 || f2};
const Formula f_forall{forall({var_x_, var_y_}, f_or)};
const Variables vars1{f1.GetFreeVariables()}; // {x_, y_}
EXPECT_EQ(vars1.size(), 2u);
EXPECT_TRUE(vars1.include(var_x_));
EXPECT_TRUE(vars1.include(var_y_));
const Variables vars2{f2.GetFreeVariables()}; // {y_, z_}
EXPECT_EQ(vars2.size(), 2u);
EXPECT_TRUE(vars2.include(var_y_));
EXPECT_TRUE(vars2.include(var_z_));
const Variables vars3{f_or.GetFreeVariables()}; // {x_, y_, z_}
EXPECT_EQ(vars3.size(), 3u);
EXPECT_TRUE(vars3.include(var_x_));
EXPECT_TRUE(vars3.include(var_y_));
EXPECT_TRUE(vars3.include(var_z_));
const Variables vars4{f_forall.GetFreeVariables()}; // {z_}
EXPECT_EQ(vars4.size(), 1u);
EXPECT_TRUE(vars4.include(var_z_));
const Variables vars5{(!f1).GetFreeVariables()}; // {x_, y_}
EXPECT_EQ(vars5.size(), 2u);
EXPECT_TRUE(vars5.include(var_x_));
EXPECT_TRUE(vars5.include(var_y_));
}
TEST_F(SymbolicFormulaTest, ToString) {
EXPECT_EQ(f1_.to_string(), "((x + y) > 0)");
EXPECT_EQ(f2_.to_string(), "((x * y) < 5)");
EXPECT_EQ(f_or_.to_string(), "(((x + y) > 0) or ((x * y) < 5))");
EXPECT_EQ(f_forall_.to_string(),
"forall({x, y}. (((x + y) > 0) or ((x * y) < 5)))");
EXPECT_EQ(f_isnan_.to_string(), "isnan(NaN)");
}
TEST_F(SymbolicFormulaTest, IsTrue) {
EXPECT_TRUE(is_true(tt_));
EXPECT_FALSE(any_of({ff_, f_eq_, f_neq_, f_lt_, f_lte_, f_gt_, f_gte_, f_and_,
f_or_, not_f_or_, f_forall_, f_isnan_, f_psd_static_2x2_,
f_psd_dynamic_2x2_, f_psd_static_3x3_},
is_true));
}
TEST_F(SymbolicFormulaTest, IsFalse) {
EXPECT_TRUE(is_false(ff_));
EXPECT_FALSE(any_of({tt_, f_eq_, f_neq_, f_lt_, f_lte_, f_gt_, f_gte_, f_and_,
f_or_, not_f_or_, f_forall_, f_isnan_, f_psd_static_2x2_,
f_psd_dynamic_2x2_, f_psd_static_3x3_},
is_false));
}
TEST_F(SymbolicFormulaTest, IsEqualTo) {
EXPECT_TRUE(is_equal_to(f_eq_));
EXPECT_FALSE(any_of({tt_, ff_, f_neq_, f_lt_, f_lte_, f_gt_, f_gte_, f_and_,
f_or_, not_f_or_, f_forall_, f_isnan_, f_psd_static_2x2_,
f_psd_dynamic_2x2_, f_psd_static_3x3_},
is_equal_to));
}
TEST_F(SymbolicFormulaTest, IsNotEqualTo) {
EXPECT_TRUE(is_not_equal_to(f_neq_));
EXPECT_FALSE(any_of({tt_, ff_, f_eq_, f_lt_, f_lte_, f_gt_, f_gte_, f_and_,
f_or_, not_f_or_, f_forall_, f_isnan_, f_psd_static_2x2_,
f_psd_dynamic_2x2_, f_psd_static_3x3_},
is_not_equal_to));
}
TEST_F(SymbolicFormulaTest, IsLessThan) {
EXPECT_TRUE(is_less_than(f_lt_));
EXPECT_FALSE(any_of({tt_, ff_, f_eq_, f_neq_, f_lte_, f_gt_, f_gte_, f_and_,
f_or_, not_f_or_, f_forall_, f_isnan_, f_psd_static_2x2_,
f_psd_dynamic_2x2_, f_psd_static_3x3_},
is_less_than));
}
TEST_F(SymbolicFormulaTest, IsLessThanOrEqualTo) {
EXPECT_TRUE(is_less_than_or_equal_to(f_lte_));
EXPECT_FALSE(any_of({tt_, ff_, f_eq_, f_neq_, f_lt_, f_gt_, f_gte_, f_and_,
f_or_, not_f_or_, f_forall_, f_isnan_, f_psd_static_2x2_,
f_psd_dynamic_2x2_, f_psd_static_3x3_},
is_less_than_or_equal_to));
}
TEST_F(SymbolicFormulaTest, IsGreaterThan) {
EXPECT_TRUE(is_greater_than(f_gt_));
EXPECT_FALSE(any_of({tt_, ff_, f_eq_, f_neq_, f_lt_, f_lte_, f_gte_, f_and_,
f_or_, not_f_or_, f_forall_, f_isnan_, f_psd_static_2x2_,
f_psd_dynamic_2x2_, f_psd_static_3x3_},
is_greater_than));
}
TEST_F(SymbolicFormulaTest, IsGreaterThanOrEqualTo) {
EXPECT_TRUE(is_greater_than_or_equal_to(f_gte_));
EXPECT_FALSE(any_of({tt_, ff_, f_eq_, f_neq_, f_lt_, f_lte_, f_gt_, f_and_,
f_or_, not_f_or_, f_forall_, f_isnan_, f_psd_static_2x2_,
f_psd_dynamic_2x2_, f_psd_static_3x3_},
is_greater_than_or_equal_to));
}
TEST_F(SymbolicFormulaTest, IsRelational) {
EXPECT_TRUE(
all_of({f_eq_, f_neq_, f_lt_, f_lte_, f_gt_, f_gte_}, is_relational));
EXPECT_FALSE(
any_of({tt_, ff_, f_and_, f_or_, not_f_or_, f_forall_, f_isnan_,
f_psd_static_2x2_, f_psd_dynamic_2x2_, f_psd_static_3x3_},
is_relational));
}
TEST_F(SymbolicFormulaTest, IsConjunction) {
EXPECT_TRUE(is_conjunction(f_and_));
EXPECT_FALSE(any_of({tt_, ff_, f_eq_, f_neq_, f_lt_, f_lte_, f_gt_, f_gte_,
f_or_, not_f_or_, f_forall_, f_isnan_, f_psd_static_2x2_,
f_psd_dynamic_2x2_, f_psd_static_3x3_},
is_conjunction));
}
TEST_F(SymbolicFormulaTest, IsDisjunction) {
EXPECT_TRUE(is_disjunction(f_or_));
EXPECT_FALSE(
any_of({tt_, ff_, f_eq_, f_neq_, f_lt_, f_lte_, f_gt_, f_gte_, f_and_,
not_f_or_, f_forall_, f_isnan_, f_psd_static_2x2_,
f_psd_dynamic_2x2_, f_psd_static_3x3_},
is_disjunction));
}