KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_numeric_evaluator.cpp
Go to the documentation of this file.
1
/*
2
* This program source code file is part of KiCad, a free EDA CAD application.
3
*
4
* Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18
*/
19
24
25
#include <
qa_utils/wx_utils/unit_test_utils.h
>
26
27
#include <
libeval/numeric_evaluator.h
>
28
29
struct
NUM_EVAL_FIXTURE
30
{
31
NUM_EVAL_FIXTURE
() :
32
m_eval
(
EDA_UNITS
::
MM
)
33
{
34
}
35
36
NUMERIC_EVALUATOR
m_eval
;
37
};
38
39
43
BOOST_FIXTURE_TEST_SUITE( NumericEvaluator,
NUM_EVAL_FIXTURE
)
44
45
46
49
struct
EVAL_CASE
50
{
51
wxString
input
;
52
wxString
exp_result
;
53
};
54
55
59
BOOST_AUTO_TEST_CASE
( Basic )
60
{
61
m_eval.Process(
"1"
);
62
BOOST_CHECK_EQUAL
( m_eval.Result(),
"1"
);
63
}
64
68
BOOST_AUTO_TEST_CASE
( SetVar )
69
{
70
m_eval.SetVar(
"MoL"
, 42 );
71
72
m_eval.Process(
"1 + MoL"
);
73
BOOST_CHECK_EQUAL
( m_eval.GetVar(
"MoL"
), 42 );
74
BOOST_CHECK_EQUAL
( m_eval.Result(),
"43"
);
75
76
m_eval.SetVar(
"MoL"
, 422 );
77
78
// have to process again to re-evaluate
79
m_eval.Process(
"1 + MoL"
);
80
BOOST_CHECK_EQUAL
( m_eval.Result(),
"423"
);
81
82
// Can remove one var
83
m_eval.SetVar(
"pi"
, 3.14 );
84
BOOST_CHECK_EQUAL
( m_eval.GetVar(
"pi"
), 3.14 );
85
m_eval.RemoveVar(
"pi"
);
86
BOOST_CHECK_EQUAL
( m_eval.GetVar(
"pi"
), 0.0 );
87
88
// Other is still there
89
BOOST_CHECK_EQUAL
( m_eval.GetVar(
"MoL"
), 422 );
90
91
// Add another one back
92
m_eval.SetVar(
"piish"
, 3.1 );
93
94
// String clear doesn't clear vars
95
m_eval.Clear();
96
m_eval.Process(
"1 + MoL + piish"
);
97
BOOST_CHECK_EQUAL
( m_eval.Result(),
"426.1"
);
98
99
// Clear both
100
m_eval.ClearVar();
101
BOOST_CHECK_EQUAL
( m_eval.GetVar(
"MoL"
), 0.0 );
102
BOOST_CHECK_EQUAL
( m_eval.GetVar(
"piish"
), 0.0 );
103
}
104
108
static
const
std::vector<EVAL_CASE>
eval_cases_valid
= {
109
// Empty case
110
{
""
,
"0"
},
111
// Trivial eval
112
{
"1"
,
"1"
},
113
// Decimal separators
114
{
"1.5"
,
"1.5"
},
115
{
"1,5"
,
"1.5"
},
116
// Semicolon is valid, but the result is NaN
117
{
"1;"
,
"NaN"
},
118
// With own unit
119
{
"1mm"
,
"1"
},
120
// Unit that's not the evaluator's unit
121
{
"1in"
,
"25.4"
},
122
// Unit with white-space
123
{
"1 in"
,
"25.4"
},
124
// Unit-less arithmetic
125
{
"1+2"
,
"3"
},
126
// Multiple units
127
{
"1 + 10mm + 1\" + 1.5in + 500mil"
,
"87.2"
},
128
// Any White-space is OK
129
{
" 1 + 2 "
,
"3"
},
130
// Decimals are OK in expressions
131
{
"1.5 + 0.2 + .1"
,
"1.8"
},
132
// Negatives are OK
133
{
"3 - 10"
,
"-7"
},
134
// Lots of operands
135
{
"1 + 2 + 10 + 1000.05"
,
"1013.05"
},
136
// Operator precedence
137
{
"1 + 2 - 4 * 20 / 2"
,
"-37"
},
138
// Parens
139
{
"(1)"
,
"1"
},
140
// Parens affect precedence
141
{
"-(1 + (2 - 4)) * 20.8 / 2"
,
"10.4"
},
142
// Unary addition is a sign, not a leading operator
143
{
"+2 - 1"
,
"1"
},
144
// Set var in-string
145
{
"x = 1; 1 + x"
,
"2"
},
146
// Multiple set vars
147
{
"x = 1; y = 2; 10 + x - y"
,
"9"
},
148
149
// Unicode units - these currently fail
150
// { wxT( "1um" ), "0.001" }, // GREEK SMALL LETTER MU
151
// { wxT( "1µm" ), "0.001" }, // GREEK SMALL LETTER MU
152
// { wxT( "1 µm" ), "0.001" }, // GREEK SMALL LETTER MU
153
// { wxT( "1µm" ), "0.001" }, // MICRO SIGN
154
// { wxT( "1 µm" ), "0.001" }, // MICRO SIGN
155
};
156
157
161
BOOST_AUTO_TEST_CASE
( Results )
162
{
163
for
(
const
auto
& c :
eval_cases_valid
)
164
{
165
BOOST_TEST_CONTEXT
( c.input +
" -> "
+ c.exp_result )
166
{
167
// Clear for new string input
168
m_eval.Clear();
169
170
m_eval.Process( c.input );
171
172
// These are all valid
173
BOOST_CHECK_EQUAL
( m_eval.IsValid(),
true
);
174
BOOST_CHECK_EQUAL
( m_eval.Result(), c.exp_result );
175
176
// Does original text still match?
177
BOOST_CHECK_EQUAL
( m_eval.OriginalText(), c.input );
178
}
179
}
180
}
181
182
186
BOOST_AUTO_TEST_CASE
( UnicodeDegree )
187
{
188
wxString degreeInput = wxT(
"1\u00B0"
);
189
190
// Set to degrees and make ready for input
191
m_eval.SetDefaultUnits(
EDA_UNITS::DEGREES
);
192
m_eval.Clear();
193
194
m_eval.Process( degreeInput );
195
196
// These are all valid
197
BOOST_CHECK_EQUAL
( m_eval.IsValid(),
true
);
198
199
// Currently disabled since the parser doesn't parse the unicode correctly
200
//BOOST_CHECK_EQUAL( m_eval.Result(), degreeInput );
201
202
// Does original text still match?
203
BOOST_CHECK_EQUAL
( m_eval.OriginalText(), degreeInput );
204
}
205
206
207
struct
EVAL_INVALID_CASE
208
{
209
wxString
input
;
210
};
211
215
static
const
std::vector<EVAL_INVALID_CASE>
eval_cases_invalid
= {
216
// Trailing operator
217
{
"1+"
},
218
// Leading operator
219
{
"*2 + 1"
},
220
// No operator
221
{
"1 2"
},
222
{
"(1)(2)"
},
223
// Unknown operator
224
{
"1 $ 2"
},
225
// Mismatched parens
226
{
"(1 + 2"
},
227
{
"1 + 2)"
},
228
// random text
229
{
"sdfsdf sdfsd"
},
230
// Div by 0
231
{
"1 / 0"
},
232
// Unknown vars are errors
233
{
"1 + unknown"
},
234
// Semicolons can't be empty or redundant
235
{
";"
},
236
{
";1"
},
237
{
";1;"
},
238
};
239
243
BOOST_AUTO_TEST_CASE
( ResultsInvalid )
244
{
245
for
(
const
auto
& c :
eval_cases_invalid
)
246
{
247
BOOST_TEST_CONTEXT
( c.input )
248
{
249
// Clear for new string input
250
m_eval.Clear();
251
252
m_eval.Process( c.input );
253
254
// These are all valid
255
BOOST_CHECK_EQUAL
( m_eval.IsValid(),
false
);
256
257
// Does original text still match?
258
BOOST_CHECK_EQUAL
( m_eval.OriginalText(), c.input );
259
}
260
}
261
}
262
263
BOOST_AUTO_TEST_SUITE_END
()
NUMERIC_EVALUATOR
Definition
numeric_evaluator.h:96
EDA_UNITS
EDA_UNITS
Definition
eda_units.h:44
EDA_UNITS::DEGREES
@ DEGREES
Definition
eda_units.h:48
numeric_evaluator.h
EVAL_CASE
Declares the struct as the Boost test fixture.
Definition
test_numeric_evaluator.cpp:50
EVAL_CASE::input
wxString input
Definition
test_numeric_evaluator.cpp:51
EVAL_CASE::exp_result
wxString exp_result
Definition
test_numeric_evaluator.cpp:52
EVAL_INVALID_CASE
Definition
test_numeric_evaluator.cpp:208
EVAL_INVALID_CASE::input
wxString input
Definition
test_numeric_evaluator.cpp:209
NUM_EVAL_FIXTURE
Definition
test_numeric_evaluator.cpp:30
NUM_EVAL_FIXTURE::NUM_EVAL_FIXTURE
NUM_EVAL_FIXTURE()
Definition
test_numeric_evaluator.cpp:31
NUM_EVAL_FIXTURE::m_eval
NUMERIC_EVALUATOR m_eval
Definition
test_numeric_evaluator.cpp:36
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
MM
static const long long MM
Definition
test_net_chain_total_length.cpp:25
eval_cases_invalid
static const std::vector< EVAL_INVALID_CASE > eval_cases_invalid
A list of invalid test strings.
Definition
test_numeric_evaluator.cpp:215
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(Basic)
Basic class ops: set one up, trivial input, tear it down.
Definition
test_numeric_evaluator.cpp:59
eval_cases_valid
static const std::vector< EVAL_CASE > eval_cases_valid
A list of valid test strings and the expected results.
Definition
test_numeric_evaluator.cpp:108
BOOST_TEST_CONTEXT
BOOST_TEST_CONTEXT("Test Clearance")
Definition
test_shape_arc.cpp:654
BOOST_CHECK_EQUAL
BOOST_CHECK_EQUAL(result, "25.4")
unit_test_utils.h
src
qa
tests
common
libeval
test_numeric_evaluator.cpp
Generated on Fri Jun 26 2026 00:05:43 for KiCad PCB EDA Suite by
1.13.2