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
|
/** @file exam_relational.cpp
*
* Small test for the relational objects and their conversion to bool. */
/*
* GiNaC Copyright (C) 1999-2026 Johannes Gutenberg University Mainz, Germany
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "ginac.h"
using namespace GiNaC;
#include <iostream>
using namespace std;
// Elementary relations should fall back to numeric comparisons.
static unsigned exam_relational_elementary()
{
unsigned result = 0;
ex one = 1, two = 2;
if (one == two) {
clog << "'" << one << " == " << two << "' was converted to true." << endl;
result += 1;
}
if (!(one != two)) {
clog << "'" << one << " != " << two << "' was not converted to true." << endl;
result += 1;
}
if (!(one < two)) {
clog << "'" << one << " < " << two << "' was not converted to true." << endl;
result += 1;
}
if (!(one <= two)) {
clog << "'" << one << " <= " << two << "' was not converted to true." << endl;
result += 1;
}
if (one > two) {
clog << "'" << one << " > " << two << "' was converted to true." << endl;
result += 1;
}
if (one >= two) {
clog << "'" << one << " >= " << two << "' was converted to true." << endl;
result += 1;
}
return result;
}
// These should fall back to looking up info flags.
static unsigned exam_relational_possymbol()
{
unsigned result = 0;
possymbol p("p");
if (p == 0) {
clog << "for positive p, 'p == 0' was converted to true." << endl;
result += 1;
}
if (!(p != 0)) {
clog << "for positive p, 'p != 0' was not converted to true." << endl;
result += 1;
}
if (p < 0) {
clog << "for positive p, 'p < 0' was converted to true." << endl;
result += 1;
}
if (p <= 0) {
clog << "for positive p, 'p <= 0' was converted to true." << endl;
result += 1;
}
if (!(p > 0)) {
clog << "for positive p, 'p > 0' was not converted to true." << endl;
result += 1;
}
if (!(p >= 0)) {
clog << "for positive p, 'p >= 0' was not converted to true." << endl;
result += 1;
}
return result;
}
// Very simple arithmetic should be supported, too.
static unsigned exam_relational_arith()
{
unsigned result = 0;
possymbol p("p");
if (!(p + 2 > p + 1)) {
clog << "for positive p, 'p + 2 > p + 1' was not converted to true." << endl;
result += 1;
}
if (!(p > -p)) {
clog << "for positive p, 'p > -p' was not converted to true." << endl;
result += 1;
}
if (!(2*p > p)) {
clog << "for positive p, '2*p > p' was not converted to true." << endl;
result += 1;
}
return result;
}
// Comparisons should maintain ordering invariants
static unsigned exam_relational_order()
{
unsigned result = 0;
numeric i = 1ll<<32, j = i+1;
symbol a;
relational x = i==a, y = j==a;
if (x.compare(y) != -y.compare(x)) {
clog << "comparison should be antisymmetric." << endl;
result += 1;
}
return result;
}
unsigned exam_relational()
{
unsigned result = 0;
cout << "examining relational objects" << flush;
result += exam_relational_elementary(); cout << '.' << flush;
result += exam_relational_possymbol(); cout << '.' << flush;
result += exam_relational_arith(); cout << '.' << flush;
result += exam_relational_order(); cout << '.' << flush;
return result;
}
int main(int argc, char** argv)
{
return exam_relational();
}
|