-
Notifications
You must be signed in to change notification settings - Fork 11
/
exprtk_testgen.cpp
166 lines (137 loc) · 4.75 KB
/
exprtk_testgen.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
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Evaluated Test Expression Generator *
* Author: Arash Partow (1999-2024) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* SPDX-License-Identifier: MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <fstream>
#include <string>
#include "exprtk.hpp"
/*
Example usage:
echo "x:=11.1237567810735" > base0.tmp
echo "y:=13.1239567810735" >> base0.tmp
echo "z:=15.1233567810735" >> base0.tmp
echo "w:=19.1235567810735" >> base0.tmp
./exprtk_exprgen > base1.tmp
cat base1.tmp | sed 's/[{(,)}]//g' | awk ' !x[$0]++' > base2.tmp
cat base0.tmp base1.tmp base2.tmp > exprtk_functional_ext_test.tmp
./exprtk_testgen exprtk_functional_ext_test.tmp > exprtk_functional_ext_test_`date "+%Y%m%d%H%M%S"`.txt
*/
template <typename Allocator,
template <typename,typename> class Sequence>
inline std::size_t load_expressions(const std::string& file_name,
Sequence<std::string,Allocator>& sequence)
{
std::ifstream stream(file_name.c_str());
if (!stream) return 0;
std::string buffer;
buffer.reserve(1024);
std::size_t line_count = 0;
while (std::getline(stream,buffer))
{
if (buffer.empty())
continue;
else if ('#' == buffer[0])
continue;
++line_count;
sequence.push_back(buffer);
}
return line_count;
}
template <typename T>
inline bool test_gen(const std::string& expr_file)
{
typedef exprtk::expression<T> expression_t;
T x = T(0);
T y = T(0);
T z = T(0);
T w = T(0);
exprtk::polynomial<T, 1> poly01;
exprtk::polynomial<T, 2> poly02;
exprtk::polynomial<T, 3> poly03;
exprtk::polynomial<T, 4> poly04;
exprtk::polynomial<T, 5> poly05;
exprtk::polynomial<T, 6> poly06;
exprtk::polynomial<T, 7> poly07;
exprtk::polynomial<T, 8> poly08;
exprtk::polynomial<T, 9> poly09;
exprtk::polynomial<T,10> poly10;
exprtk::polynomial<T,11> poly11;
exprtk::polynomial<T,12> poly12;
exprtk::symbol_table<T> symbol_table;
symbol_table.add_constants();
symbol_table.add_variable("x",x);
symbol_table.add_variable("y",y);
symbol_table.add_variable("z",z);
symbol_table.add_variable("w",w);
symbol_table.add_function("poly01", poly01);
symbol_table.add_function("poly02", poly02);
symbol_table.add_function("poly03", poly03);
symbol_table.add_function("poly04", poly04);
symbol_table.add_function("poly05", poly05);
symbol_table.add_function("poly06", poly06);
symbol_table.add_function("poly07", poly07);
symbol_table.add_function("poly08", poly08);
symbol_table.add_function("poly09", poly09);
symbol_table.add_function("poly10", poly10);
symbol_table.add_function("poly11", poly11);
symbol_table.add_function("poly12", poly12);
expression_t expression;
expression.register_symbol_table(symbol_table);
exprtk::parser<T> parser;
std::deque<std::string> expr_str_list;
if (0 == load_expressions(expr_file,expr_str_list))
{
return true;
}
std::deque<exprtk::expression<T> > expression_list;
bool load_success = true;
for (std::size_t i = 0; i < expr_str_list.size(); ++i)
{
exprtk::expression<T> current_expression;
current_expression.register_symbol_table(symbol_table);
if (!parser.compile(expr_str_list[i],current_expression))
{
load_success = false;
printf("test_gen() - Error: %s Expression: %s\n",
parser.error().c_str(),
expr_str_list[i].c_str());
}
else
expression_list.push_back(current_expression);
}
if (!load_success)
return false;
for (std::size_t i = 0; i < expression_list.size(); ++i)
{
T result = expression_list[i].value();
printf("equal((%050.30f),(%s))\n",
result,
expr_str_list[i].c_str());
}
return true;
}
int main(int argc, char* argv[])
{
if (argc != 2)
{
printf("usage: exprtk_testgen <file name>\n");
return 1;
}
const std::string file_name = argv[1];
test_gen<double>(file_name);
return 0;
}