-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_test.cpp
345 lines (248 loc) · 10.8 KB
/
function_test.cpp
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
/****************************************************************************
** Copyright (c) 2023, Adel Kara Slimane <[email protected]>
**
** This file is part of ZeCalculator.
**
** ZeCalculators is free software: you may copy, redistribute and/or modify it
** under the terms of the GNU Affero General Public License as published by the
** Free Software Foundation, either version 3 of the License, or (at your
** option) any later version.
**
** This file 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 <http://www.gnu.org/licenses/>.
**
****************************************************************************/
#include <zecalculator/zecalculator.h>
// testing specific headers
#include <boost/ut.hpp>
#include <zecalculator/test-utils/print-utils.h>
#include <zecalculator/test-utils/structs.h>
#include <zecalculator/test-utils/utils.h>
#include <numbers>
using namespace zc;
int main()
{
using namespace boost::ut;
"multi-parameter function evaluation"_test = []<class StructType>()
{
constexpr parsing::Type type = std::is_same_v<StructType, FAST_TEST> ? parsing::Type::FAST : parsing::Type::RPN;
MathWorld<type> world;
auto& f = world.new_object() = "f(omega,t) = cos(omega * t) + omega * t";
const double omega = 2;
const double t = 3;
// note: the order of the arguments is important
const double res = f({omega, t}).value();
const double expected_res = std::cos(omega * t) + omega * t;
expect(res == expected_res);
// dynamic object tests
auto* obj = world.get("f");
expect(obj && (*obj)({omega, t}).value() == expected_res);
} | std::tuple<FAST_TEST, RPN_TEST>{};
"function evaluation shadowing a global constant"_test = []<class StructType>()
{
constexpr parsing::Type type = std::is_same_v<StructType, FAST_TEST> ? parsing::Type::FAST : parsing::Type::RPN;
MathWorld<type> world;
world.new_object() = "x = 2.0";
auto& f = world.new_object() = "f(x) = cos(x) + x";
const double res = f({1.0}).value();
const double expected_res = std::cos(1.0) + 1.0;
expect(res == expected_res);
// dynamic object tests
auto* x_obj = world.get("x");
expect(x_obj && (*x_obj)().value() == 2.0);
auto* f_obj = world.get("f");
expect(f_obj && (*f_obj)({1.0}).value() == expected_res);
} | std::tuple<FAST_TEST, RPN_TEST>{};
"function calling another function"_test = []<class StructType>()
{
constexpr parsing::Type type = std::is_same_v<StructType, FAST_TEST> ? parsing::Type::FAST : parsing::Type::RPN;
MathWorld<type> world;
auto& f1 = world.new_object();
auto& f2 = world.new_object();
f2 = "f2(x) = cos(x) + 2*x^2";
f1 = "f1(x) = cos(x) + x + f2(2*x)";
expect(bool(f1));
expect(bool(f2));
auto cpp_f2 = [](double x)
{
return std::cos(x) + 2*x*x;
};
auto cpp_f1 = [cpp_f2](double x)
{
return std::cos(x) + x + cpp_f2(2*x);
};
double x = 6.4;
const auto expected_res2 = f2({2*x});
const bool res2_status = bool(expected_res2);
expect(res2_status);
using namespace boost::ut::literals;
if (res2_status)
expect(expected_res2.value() - cpp_f2(2*x) == 0.0_d);
const auto expected_res1 = f1({x});
expect(bool(expected_res1));
if (bool(expected_res1))
expect(std::fabs(expected_res1.value() - cpp_f1(x)) < 1e-11) << expected_res1.value() << " = " << cpp_f1(x);
} | std::tuple<FAST_TEST, RPN_TEST>{};
"function overwrites"_test = []<class StructType>()
{
constexpr parsing::Type type = std::is_same_v<StructType, FAST_TEST> ? parsing::Type::FAST : parsing::Type::RPN;
MathWorld<type> world;
auto& cst = world.new_object() = "my_constant = 3.0";
auto& f = world.new_object() = "f(x) = x + my_constant + cos(math::pi)";
auto* f_obj = world.get("f");
expect(f_obj);
double cpp_cst = 3.0;
auto cpp_f_1 = [&](double x)
{
return x + cpp_cst + std::cos(std::numbers::pi);
};
expect(f({1}).value() == cpp_f_1(1.0));
expect((*f_obj)({1}) == cpp_f_1(1.0));
cst.template value_as<GlobalConstant>().value = 5.0;
cpp_cst = 5.0;
expect(f({1}).value() == cpp_f_1(1.0));
expect((*f_obj)({1}).value() == cpp_f_1(1.0));
world.new_object() = "g(z) = 2*z + my_constant";
// override expression and variable name of f
f = "f(y) = y + my_constant + g(y)";
auto cpp_g = [&](double z)
{
return 2*z + cpp_cst;
};
auto cpp_f_2 = [&](double y)
{
return y + cpp_cst + cpp_g(y);
};
expect(f({3}).value() == cpp_f_2(3));
expect((*f_obj)({3}).value() == cpp_f_2(3));
} | std::tuple<FAST_TEST, RPN_TEST>{};
"nested multi-variable functions"_test = []<class StructType>()
{
constexpr parsing::Type type = std::is_same_v<StructType, FAST_TEST> ? parsing::Type::FAST : parsing::Type::RPN;
MathWorld<type> world;
world.new_object() = "h(c,d) = c*d + c-d";
world.new_object() = "g(a,b) = h(a, a*b) + 3*a - b";
auto& f = world.new_object() = "f(x, y) = h(x, g(x, y)) + g(y, h(y, x))";
auto cpp_h = [](double c, double d) {
return c*d + c-d;
};
auto cpp_g = [&](double a, double b) {
return cpp_h(a, a*b) + 3*a - b; };
auto cpp_f = [&](double x, double y) {
return cpp_h(x, cpp_g(x, y)) + cpp_g(y, cpp_h(y, x));
};
const double x = 5, y = 3;
expect(f({x, y}).value() == cpp_f(x, y));
auto* f_obj = world.get("f");
expect(f_obj && (*f_obj)({x, y}).value() == cpp_f(x, y));
} | std::tuple<FAST_TEST, RPN_TEST>{};
"function with dot in name"_test = []<class StructType>()
{
constexpr parsing::Type type = std::is_same_v<StructType, FAST_TEST> ? parsing::Type::FAST : parsing::Type::RPN;
MathWorld<type> world;
// add a function named "f", note that the constant "my_constant" is only defined after
auto& fx = world.new_object() = "f.x(x) = 1 + x";
auto& fy = world.new_object() = "f.y = 2.0 + f.x(1)";
expect(fx({1}).value() == 2.0);
expect(fy().value() == 4.0);
} | std::tuple<FAST_TEST, RPN_TEST>{};
"calling function with wrong number of arguments"_test = []<class StructType>()
{
constexpr parsing::Type type = std::is_same_v<StructType, FAST_TEST> ? parsing::Type::FAST : parsing::Type::RPN;
MathWorld<type> world;
// add a function named "f", note that the constant "my_constant" is only defined after
world.new_object() = "f(x, y) = 1 + x + y";
std::string var_expr = "1 + f(1, 2, 3)";
tl::expected<double, Error> res = world.evaluate(var_expr);
expect(not res.has_value());
expect(res.error() == Error::mismatched_fun_args(parsing::tokens::Text{"1, 2, 3", 6}, var_expr))
<< res.error();
} | std::tuple<FAST_TEST, RPN_TEST>{};
"multi variable function"_test = []<class StructType>()
{
constexpr parsing::Type type = std::is_same_v<StructType, FAST_TEST> ? parsing::Type::FAST : parsing::Type::RPN;
MathWorld<type> world;
// add a function named "f", note that the constant "my_constant" is only defined after
auto& f = world.new_object() = "f(u, v, w, x, y, z) = 1 + u + v + w + x + y + z";
expect(f.has_value()) << fatal;
auto res = f({1, 1, 1, 1, 1, 1});
expect(bool(res)) << fatal;
expect(res.value() == 7_i);
} | std::tuple<FAST_TEST, RPN_TEST>{};
"parametric function benchmark"_test = []<class StructType>()
{
constexpr auto duration = nanoseconds(500ms);
{
constexpr parsing::Type type = std::is_same_v<StructType, FAST_TEST> ? parsing::Type::FAST : parsing::Type::RPN;
constexpr std::string_view data_type_str_v = std::is_same_v<StructType, FAST_TEST> ? "FAST" : "RPN";
MathWorld<type> world;
auto& t = (world.new_object() = "t = 1").template value_as<GlobalConstant>();
auto& f = (world.new_object() = "f(x) =3*cos(t*x) + 2*sin(x/t) + 4").template value_as<Function<type>>();
double x = 0;
double res = 0;
size_t iterations =
loop_call_for(duration, [&]{
res += f({x}).value();
x++;
t.value += 1;
});
std::cout << "Avg zc::Function<" << data_type_str_v << "> eval time: "
<< duration_cast<nanoseconds>(duration / iterations).count() << "ns"
<< std::endl;
std::cout << "dummy val: " << res << std::endl;
}
{
double cpp_t = 1;
auto cpp_f = [&](double x) {
return 3*cos(cpp_t*x) + 2*sin(x/cpp_t) + 4;
};
double x = 0;
double res = 0;
size_t iterations =
loop_call_for(duration, [&]{
res += cpp_f(x);
iterations++;
x++;
cpp_t++;
});
std::cout << "Avg C++ function eval time: " << duration_cast<nanoseconds>(duration/iterations).count() << "ns" << std::endl;
std::cout << "dummy val: " << res << std::endl;
}
} | std::tuple<FAST_TEST, RPN_TEST>{};
"sequence direct dependencies"_test = []<class StructType>()
{
constexpr parsing::Type type = std::is_same_v<StructType, FAST_TEST> ? parsing::Type::FAST : parsing::Type::RPN;
MathWorld<type> world;
// add a function named "f", note that the constant "my_constant" is only defined after
world.new_object() = "f(x, y) = 1 + x + y";
auto& seq = world.new_object() = "u(n) = 0 ; 1 + f(1, 1) + f(2, 2) + u(n-1) + 3*u(n-1) + cos(n)";
constexpr auto t = deps::Dep::FUNCTION;
expect(seq.direct_dependencies()
== deps::Deps{{"u", {t, {35, 46}}}, {"f", {t, {15, 25}}}, {"cos", {t, {55}}}});
} | std::tuple<FAST_TEST, RPN_TEST>{};
"function direct dependencies"_test = []<class StructType>()
{
constexpr parsing::Type type = std::is_same_v<StructType, FAST_TEST> ? parsing::Type::FAST : parsing::Type::RPN;
MathWorld<type> world;
// add a function named "f", note that the constant "my_constant" is only defined after
world.new_object() = "my_constant = 3.0";
auto& f = world.new_object() = "f( x) = x + my_constant + cos(math::pi)";
using namespace std::string_literals;
expect(f.direct_dependencies()
== deps::Deps{{"my_constant", {deps::Dep::VARIABLE, {13}}},
{"cos", {deps::Dep::FUNCTION, {27}}},
{"math::pi", {deps::Dep::VARIABLE, {31}}}}); // "u" and "f"
} | std::tuple<FAST_TEST, RPN_TEST>{};
"name of function in error state"_test = []<class StructType>()
{
constexpr parsing::Type type = std::is_same_v<StructType, FAST_TEST> ? parsing::Type::FAST : parsing::Type::RPN;
MathWorld<type> world;
auto& f = world.new_object() = "f( x) = cos(x) * g(X)";
expect(f.get_name() == "f");
} | std::tuple<FAST_TEST, RPN_TEST>{};
}