C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
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-2024) *
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
41template <typename Allocator,
42 template <typename,typename> class Sequence>
43inline 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
64template <typename T>
65inline 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
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
154int 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}
bool register_symbol_table(symbol_table< T > &st)
Definition exprtk.hpp:21726
bool compile(const std::string &expression_string, expression< T > &expr)
Definition exprtk.hpp:24443
std::string error() const
Definition exprtk.hpp:24722
bool add_function(const std::string &function_name, function_t &function)
Definition exprtk.hpp:20811
bool add_variable(const std::string &variable_name, T &t, const bool is_constant=false)
Definition exprtk.hpp:20770
static const std::string expression_list[]
bool test_gen(const std::string &expr_file)
std::size_t load_expressions(const std::string &file_name, Sequence< std::string, Allocator > &sequence)