C++ Mathematical Expression Toolkit (ExprTk)
release
Toggle main menu visibility
Loading...
Searching...
No Matches
exprtk
exprtk_testgen.cpp
Go to the documentation of this file.
1
/*
2
**************************************************************
3
* C++ Mathematical Expression Toolkit Library *
4
* *
5
* Evaluated Test Expression Generator *
6
* Author: Arash Partow (1999-2025) *
7
* URL: https://www.partow.net/programming/exprtk/index.html *
8
* *
9
* Copyright notice: *
10
* Free use of the Mathematical Expression Toolkit Library is *
11
* permitted under the guidelines and in accordance with the *
12
* most current version of the MIT License. *
13
* https://www.opensource.org/licenses/MIT *
14
* SPDX-License-Identifier: MIT *
15
* *
16
**************************************************************
17
*/
18
19
20
#include <cstdio>
21
#include <fstream>
22
#include <string>
23
24
#include "
exprtk.hpp
"
25
26
27
/*
28
Example usage:
29
echo "x:=11.1237567810735" > base0.tmp
30
echo "y:=13.1239567810735" >> base0.tmp
31
echo "z:=15.1233567810735" >> base0.tmp
32
echo "w:=19.1235567810735" >> base0.tmp
33
./exprtk_exprgen > base1.tmp
34
cat base1.tmp | sed 's/[{(,)}]//g' | awk ' !x[$0]++' > base2.tmp
35
cat base0.tmp base1.tmp base2.tmp > exprtk_functional_ext_test.tmp
36
./exprtk_testgen exprtk_functional_ext_test.tmp > exprtk_functional_ext_test_`date "+%Y%m%d%H%M%S"`.txt
37
38
*/
39
40
41
template
<
typename
Allocator,
42
template
<
typename
,
typename
>
class
Sequence>
43
inline
std::size_t
load_expressions
(
const
std::string& file_name,
44
Sequence<std::string,Allocator>& sequence)
45
{
46
std::ifstream stream(file_name.c_str());
47
if
(!stream)
return
0;
48
std::string buffer;
49
buffer.reserve(1024);
50
std::size_t line_count = 0;
51
while
(std::getline(stream,buffer))
52
{
53
if
(buffer.empty())
54
continue
;
55
else
if
(
'#'
== buffer[0])
56
continue
;
57
++line_count;
58
sequence.push_back(buffer);
59
}
60
61
return
line_count;
62
}
63
64
template
<
typename
T>
65
inline
bool
test_gen
(
const
std::string& expr_file)
66
{
67
typedef
exprtk::expression<T>
expression_t;
68
69
T x = T(0);
70
T y = T(0);
71
T z = T(0);
72
T w = T(0);
73
74
exprtk::polynomial<T, 1>
poly01;
75
exprtk::polynomial<T, 2>
poly02;
76
exprtk::polynomial<T, 3>
poly03;
77
exprtk::polynomial<T, 4>
poly04;
78
exprtk::polynomial<T, 5>
poly05;
79
exprtk::polynomial<T, 6>
poly06;
80
exprtk::polynomial<T, 7>
poly07;
81
exprtk::polynomial<T, 8>
poly08;
82
exprtk::polynomial<T, 9>
poly09;
83
exprtk::polynomial<T,10>
poly10;
84
exprtk::polynomial<T,11>
poly11;
85
exprtk::polynomial<T,12>
poly12;
86
87
exprtk::symbol_table<T>
symbol_table;
88
symbol_table.
add_constants
();
89
symbol_table.
add_variable
(
"x"
,x);
90
symbol_table.
add_variable
(
"y"
,y);
91
symbol_table.
add_variable
(
"z"
,z);
92
symbol_table.
add_variable
(
"w"
,w);
93
symbol_table.
add_function
(
"poly01"
, poly01);
94
symbol_table.
add_function
(
"poly02"
, poly02);
95
symbol_table.
add_function
(
"poly03"
, poly03);
96
symbol_table.
add_function
(
"poly04"
, poly04);
97
symbol_table.
add_function
(
"poly05"
, poly05);
98
symbol_table.
add_function
(
"poly06"
, poly06);
99
symbol_table.
add_function
(
"poly07"
, poly07);
100
symbol_table.
add_function
(
"poly08"
, poly08);
101
symbol_table.
add_function
(
"poly09"
, poly09);
102
symbol_table.
add_function
(
"poly10"
, poly10);
103
symbol_table.
add_function
(
"poly11"
, poly11);
104
symbol_table.
add_function
(
"poly12"
, poly12);
105
106
expression_t expression;
107
expression.register_symbol_table(symbol_table);
108
109
exprtk::parser<T>
parser;
110
111
std::deque<std::string> expr_str_list;
112
113
if
(0 ==
load_expressions
(expr_file,expr_str_list))
114
{
115
return
true
;
116
}
117
118
std::deque<exprtk::expression<T> >
expression_list
;
119
120
bool
load_success =
true
;
121
122
for
(std::size_t i = 0; i < expr_str_list.size(); ++i)
123
{
124
exprtk::expression<T>
current_expression;
125
126
current_expression.
register_symbol_table
(symbol_table);
127
128
if
(!parser.
compile
(expr_str_list[i],current_expression))
129
{
130
load_success =
false
;
131
printf(
"test_gen() - Error: %s Expression: %s\n"
,
132
parser.
error
().c_str(),
133
expr_str_list[i].c_str());
134
}
135
else
136
expression_list
.push_back(current_expression);
137
}
138
139
if
(!load_success)
140
return
false
;
141
142
for
(std::size_t i = 0; i <
expression_list
.size(); ++i)
143
{
144
T result =
expression_list
[i].value();
145
146
printf(
"equal((%050.30f),(%s))\n"
,
147
result,
148
expr_str_list[i].c_str());
149
}
150
151
return
true
;
152
}
153
154
int
main
(
int
argc,
char
* argv[])
155
{
156
if
(argc != 2)
157
{
158
printf(
"usage: exprtk_testgen <file name>\n"
);
159
return
1;
160
}
161
162
const
std::string file_name = argv[1];
163
test_gen<double>
(file_name);
164
165
return
0;
166
}
exprtk::expression
Definition
exprtk.hpp:21832
exprtk::expression::register_symbol_table
bool register_symbol_table(symbol_table< T > &st)
Definition
exprtk.hpp:22071
exprtk::parser
Definition
exprtk.hpp:22525
exprtk::parser::compile
bool compile(const std::string &expression_string, expression< T > &expr)
Definition
exprtk.hpp:24826
exprtk::parser::error
std::string error() const
Definition
exprtk.hpp:25114
exprtk::polynomial
Definition
exprtk.hpp:42803
exprtk::symbol_table
Definition
exprtk.hpp:20090
exprtk::symbol_table::add_function
bool add_function(const std::string &function_name, function_t &function)
Definition
exprtk.hpp:21156
exprtk::symbol_table::add_constants
bool add_constants()
Definition
exprtk.hpp:21416
exprtk::symbol_table::add_variable
bool add_variable(const std::string &variable_name, T &t, const bool is_constant=false)
Definition
exprtk.hpp:21115
exprtk.hpp
main
int main()
Definition
exprtk_american_option_binomial_model.cpp:143
expression_list
static const std::string expression_list[]
Definition
exprtk_real_benchmark.cpp:32
test_gen
bool test_gen(const std::string &expr_file)
Definition
exprtk_testgen.cpp:65
load_expressions
std::size_t load_expressions(const std::string &file_name, Sequence< std::string, Allocator > &sequence)
Definition
exprtk_testgen.cpp:43
Generated by
1.17.0