]> www.ginac.de Git - ginac.git/blob - check/exam_relational.cpp
[BUGFIX] Fix crash in parser.
[ginac.git] / check / exam_relational.cpp
1 /** @file exam_relational.cpp
2  *
3  *  Small test for the relational objects and their conversion to bool. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2023 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #include "ginac.h"
24 using namespace GiNaC;
25
26 #include <iostream>
27 using namespace std;
28
29 // Elementary relations should fall back to numeric comparisons.
30 static unsigned exam_relational_elementary()
31 {
32         unsigned result = 0;
33         ex one = 1, two = 2;
34
35         if (one == two) {
36                 clog << "'" << one << " == " << two << "' was converted to true." << endl;
37                 result += 1;
38         }
39         if (!(one != two)) {
40                 clog << "'" << one << " != " << two << "' was not converted to true." << endl;
41                 result += 1;
42         }
43         if (!(one < two)) {
44                 clog << "'" << one << " < " << two << "' was not converted to true." << endl;
45                 result += 1;
46         }
47         if (!(one <= two)) {
48                 clog << "'" << one << " <= " << two << "' was not converted to true." << endl;
49                 result += 1;
50         }
51         if (one > two) {
52                 clog << "'" << one << " > " << two << "' was converted to true." << endl;
53                 result += 1;
54         }
55         if (one >= two) {
56                 clog << "'" << one << " >= " << two << "' was converted to true." << endl;
57                 result += 1;
58         }
59
60         return result;
61 }
62
63 // These should fall back to looking up info flags.
64 static unsigned exam_relational_possymbol()
65 {
66         unsigned result = 0;
67         possymbol p("p");
68
69         if (p == 0) {
70                 clog << "for positive p, 'p == 0' was converted to true." << endl;
71                 result += 1;
72         }
73         if (!(p != 0)) {
74                 clog << "for positive p, 'p != 0' was not converted to true." << endl;
75                 result += 1;
76         }
77         if (p < 0) {
78                 clog << "for positive p, 'p < 0' was converted to true." << endl;
79                 result += 1;
80         }
81         if (p <= 0) {
82                 clog << "for positive p, 'p <= 0' was converted to true." << endl;
83                 result += 1;
84         }
85         if (!(p > 0)) {
86                 clog << "for positive p, 'p > 0' was not converted to true." << endl;
87                 result += 1;
88         }
89         if (!(p >= 0)) {
90                 clog << "for positive p, 'p >= 0' was not converted to true." << endl;
91                 result += 1;
92         }
93
94         return result;
95 }
96
97 // Very simple arithmetic should be supported, too.
98 static unsigned exam_relational_arith()
99 {
100         unsigned result = 0;
101         possymbol p("p");
102
103         if (!(p + 2 > p + 1)) {
104                 clog << "for positive p, 'p + 2 > p + 1' was not converted to true." << endl;
105                 result += 1;
106         }
107         if (!(p > -p)) {
108                 clog << "for positive p, 'p > -p' was not converted to true." << endl;
109                 result += 1;
110         }
111         if (!(2*p > p)) {
112                 clog << "for positive p, '2*p > p' was not converted to true." << endl;
113                 result += 1;
114         }
115
116         return result;
117 }
118
119 // Comparisons should maintain ordering invariants
120 static unsigned exam_relational_order()
121 {
122         unsigned result = 0;
123         numeric i = 1ll<<32, j = i+1;
124         symbol a;
125         relational x = i==a, y = j==a;
126         if (x.compare(y) != -y.compare(x)) {
127                 clog << "comparison should be antisymmetric." << endl;
128                 result += 1;
129         }
130
131         return result;
132 }
133
134 unsigned exam_relational()
135 {
136         unsigned result = 0;
137
138         cout << "examining relational objects" << flush;
139
140         result += exam_relational_elementary(); cout << '.' << flush;
141         result += exam_relational_possymbol(); cout << '.' << flush;
142         result += exam_relational_arith(); cout << '.' << flush;
143         result += exam_relational_order(); cout << '.' << flush;
144
145         return result;
146 }
147
148 int main(int argc, char** argv)
149 {
150         return exam_relational();
151 }