File: exam_function_exvector.cpp

package info (click to toggle)
ginac 1.8.9-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 6,640 kB
  • sloc: cpp: 49,195; sh: 5,402; makefile: 448; python: 193
file content (116 lines) | stat: -rw-r--r-- 2,836 bytes parent folder | download | duplicates (3)
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
#ifdef IN_GINAC
#include "ginac.h"
#else
#include "ginac/ginac.h"
#endif

#include <vector>
#include <iostream>

using namespace GiNaC;
using namespace std;

DECLARE_FUNCTION_2P(foobar);

static bool eval_called = false;
static exvector eval_called_with = {};
static bool evalf_called = false;
static exvector evalf_called_with = {};

static void reset() {
	eval_called_with.clear();
	evalf_called_with.clear();
	evalf_called = false;
	eval_called = false;
}

static ex foobar_eval(const exvector& args) {
	eval_called = true;
	for (auto const& v: args)
		eval_called_with.push_back(v);

	return foobar(args[0], args[1]).hold();
}

static ex foobar_evalf(const exvector& args) {
	evalf_called = true;
	for (auto const& v: args)
		evalf_called_with.push_back(v);
	return foobar(args[0], args[1]).hold();
}


REGISTER_FUNCTION(foobar, eval_func(foobar_eval).
			  evalf_func(foobar_evalf));

static int check_exvector_eval() {
	symbol x("x"), y("y");
	int err = 1;

	reset();
	ex e = foobar(x, y);
	if (!eval_called) {
		clog << "*** Error: " << __func__ << ": foobar_eval hasn't been called" << endl;
		err *= 2;
	}
	if (eval_called_with.size() != 2) {
		clog << "*** Error: " << __func__ << ": fobar_eval: expected 2 arguments, got " <<
			eval_called_with.size() << endl;
		err *= 3;
	}
	if (eval_called_with[0] != x) {
		clog << "*** Error: " << __func__ << ": fobar_eval: wrong 1st argument, "
			"expected " << x << ", got " << eval_called_with[0] << endl;
		err *= 5;
	}
	if (eval_called_with[1] != y) {
		clog << "*** Error: " << __func__ << ": fobar_eval: wrong 1st argument, "
			"expected " << y << ", got " << eval_called_with[1] << endl;
		err *= 7;
	}
	return err - 1;
}

static int check_exvector_evalf() {
	int err = 1;

	reset();
	ex e = foobar(Pi, Euler);
	e = e.evalf();

	if (!evalf_called) {
		clog << "*** Error: " << __func__ << ": foobar_evalf hasn't been called" << endl;
		err *= 2;
	}
	if (evalf_called_with.size() != 2) {
		clog << __func__ << ": foobar_evalf: expected 2 arguments, got " <<
			evalf_called_with.size() << endl;
		err *= 3;
	}
	if (!is_a<numeric>(evalf_called_with[0])) {
		clog << "*** Error: " << __func__ << ": wrong 1st argument of foobar_evalf: "
			"expected a real number, got " << evalf_called_with[0] << endl;
		err *= 5;
	}
	if (!is_a<numeric>(evalf_called_with[1])) {
		clog << "*** Error: " << __func__ << ": wrong 1st argument of foobar_evalf: "
			"expected a real number, got " << evalf_called_with[0] << endl;
		err *= 7;
	}
	return err - 1;
}

int main(int argc, char** argv) {
	int ret = 0;
	auto err = check_exvector_evalf();
	if (err) {
		ret |= 1;
		clog << "*** Error " << (err + 1) << " (check_exvector_evalf)" << endl;
	}
	err = check_exvector_eval();
	if (err) { 
		ret |= 2;
		clog << "*** Error " << (err + 1) << " (check_exvector_evalf)" << endl;
	}
	return ret;
}