]> www.ginac.de Git - ginac.git/blob - check/exam_function_exvector.cpp
Finalize 1.8.1 release.
[ginac.git] / check / exam_function_exvector.cpp
1 #ifdef IN_GINAC
2 #include "ginac.h"
3 #else
4 #include "ginac/ginac.h"
5 #endif
6
7 #include <vector>
8 #include <iostream>
9
10 using namespace GiNaC;
11 using namespace std;
12
13 DECLARE_FUNCTION_2P(foobar);
14
15 static bool eval_called = false;
16 static exvector eval_called_with = {};
17 static bool evalf_called = false;
18 static exvector evalf_called_with = {};
19
20 static void reset() {
21         eval_called_with.clear();
22         evalf_called_with.clear();
23         evalf_called = false;
24         eval_called = false;
25 }
26
27 static ex foobar_eval(const exvector& args) {
28         eval_called = true;
29         for (auto const& v: args)
30                 eval_called_with.push_back(v);
31
32         return foobar(args[0], args[1]).hold();
33 }
34
35 static ex foobar_evalf(const exvector& args) {
36         evalf_called = true;
37         for (auto const& v: args)
38                 evalf_called_with.push_back(v);
39         return foobar(args[0], args[1]).hold();
40 }
41
42
43 REGISTER_FUNCTION(foobar, eval_func(foobar_eval).
44                           evalf_func(foobar_evalf));
45
46 static int check_exvector_eval() {
47         symbol x("x"), y("y");
48         int err = 1;
49
50         reset();
51         ex e = foobar(x, y);
52         if (!eval_called) {
53                 clog << "*** Error: " << __func__ << ": foobar_eval hasn't been called" << endl;
54                 err *= 2;
55         }
56         if (eval_called_with.size() != 2) {
57                 clog << "*** Error: " << __func__ << ": fobar_eval: expected 2 arguments, got " <<
58                         eval_called_with.size() << endl;
59                 err *= 3;
60         }
61         if (eval_called_with[0] != x) {
62                 clog << "*** Error: " << __func__ << ": fobar_eval: wrong 1st argument, "
63                         "expected " << x << ", got " << eval_called_with[0] << endl;
64                 err *= 5;
65         }
66         if (eval_called_with[1] != y) {
67                 clog << "*** Error: " << __func__ << ": fobar_eval: wrong 1st argument, "
68                         "expected " << y << ", got " << eval_called_with[1] << endl;
69                 err *= 7;
70         }
71         return err - 1;
72 }
73
74 static int check_exvector_evalf() {
75         int err = 1;
76
77         reset();
78         ex e = foobar(Pi, Euler);
79         e = e.evalf();
80
81         if (!evalf_called) {
82                 clog << "*** Error: " << __func__ << ": foobar_evalf hasn't been called" << endl;
83                 err *= 2;
84         }
85         if (evalf_called_with.size() != 2) {
86                 clog << __func__ << ": foobar_evalf: expected 2 arguments, got " <<
87                         evalf_called_with.size() << endl;
88                 err *= 3;
89         }
90         if (!is_a<numeric>(evalf_called_with[0])) {
91                 clog << "*** Error: " << __func__ << ": wrong 1st argument of foobar_evalf: "
92                         "expected a real number, got " << evalf_called_with[0] << endl;
93                 err *= 5;
94         }
95         if (!is_a<numeric>(evalf_called_with[1])) {
96                 clog << "*** Error: " << __func__ << ": wrong 1st argument of foobar_evalf: "
97                         "expected a real number, got " << evalf_called_with[0] << endl;
98                 err *= 7;
99         }
100         return err - 1;
101 }
102
103 int main(int argc, char** argv) {
104         int ret = 0;
105         auto err = check_exvector_evalf();
106         if (err) {
107                 ret |= 1;
108                 clog << "*** Error " << (err + 1) << " (check_exvector_evalf)" << endl;
109         }
110         err = check_exvector_eval();
111         if (err) { 
112                 ret |= 2;
113                 clog << "*** Error " << (err + 1) << " (check_exvector_evalf)" << endl;
114         }
115         return ret;
116 }