C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
Functions
exprtk_testgen.cpp File Reference
#include <cstdio>
#include <fstream>
#include <string>
#include "exprtk.hpp"
Include dependency graph for exprtk_testgen.cpp:

Go to the source code of this file.

Functions

template<typename Allocator , template< typename, typename > class Sequence>
std::size_t load_expressions (const std::string &file_name, Sequence< std::string, Allocator > &sequence)
 
template<typename T >
bool test_gen (const std::string &expr_file)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ load_expressions()

template<typename Allocator , template< typename, typename > class Sequence>
std::size_t load_expressions ( const std::string &  file_name,
Sequence< std::string, Allocator > &  sequence 
)
inline

Definition at line 43 of file exprtk_testgen.cpp.

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}

Referenced by test_gen().

Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 154 of file exprtk_testgen.cpp.

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}

◆ test_gen()

template<typename T >
bool test_gen ( const std::string &  expr_file)
inline

Definition at line 65 of file exprtk_testgen.cpp.

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}
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[]
std::size_t load_expressions(const std::string &file_name, Sequence< std::string, Allocator > &sequence)

References exprtk::symbol_table< T >::add_constants(), exprtk::symbol_table< T >::add_function(), exprtk::symbol_table< T >::add_variable(), exprtk::parser< T >::compile(), exprtk::parser< T >::error(), expression_list, load_expressions(), and exprtk::expression< T >::register_symbol_table().

Here is the call graph for this function: