39 return aStream << unitStr.ToStdString();
119 {
"@{}",
"@{}",
true },
122 {
"@{1}",
"1",
false },
125 {
"@{1.5}",
"1.5",
false },
129 {
"@{1+2}",
"3",
false },
130 {
"@{1 + 2}",
"3",
false },
131 {
"@{1.5 + 0.2 + 0.1}",
"1.8",
false },
132 {
"@{3 - 10}",
"-7",
false },
133 {
"@{1 + 2 + 10 + 1000.05}",
"1013.05",
false },
136 {
"@{1 + 2 - 4 * 20 / 2}",
"-37",
false },
139 {
"@{(1)}",
"1",
false },
140 {
"@{-(1 + (2 - 4)) * 20.8 / 2}",
"10.4",
false },
143 {
"@{+2 - 1}",
"1",
false },
151 {
"@{1+}",
"",
true },
154 {
"@{*2 + 1}",
"",
true },
157 {
"@{1 / 0}",
"",
true },
160 {
"@{1 + ${unknown}}",
"@{1 + ${unknown}}",
true },
163 {
"@{(1 + 2}",
"",
true },
164 {
"@{1 + 2)}",
"",
true },
167 {
"@{1 $ 2}",
"",
true },
183 if( testCase.shouldError )
213 if( testCase.input.Contains(
"${unknown}" ) )
238 const std::vector<VarTestCase> varCases = {
239 {
"@{${x}}",
"10",
false },
240 {
"@{${y}}",
"5",
false },
241 {
"@{${x} + ${y}}",
"15",
false },
242 {
"@{${x} * ${y}}",
"50",
false },
243 {
"@{${x} - ${y}}",
"5",
false },
244 {
"@{${x} / ${y}}",
"2",
false },
245 {
"@{(${x} + ${y}) * 2}",
"30",
false },
248 {
"@{${undefined}}",
"@{${undefined}}",
true },
251 {
"@{${x} + ${undefined}}",
"@{${x} + ${undefined}}",
true },
254 for(
const auto& testCase : varCases )
260 if( testCase.shouldError )
281 struct MathTestCase {
287 const std::vector<MathTestCase> mathCases = {
289 {
"@{abs(-5)}",
"5",
false },
290 {
"@{min(3, 7)}",
"3",
false },
291 {
"@{max(3, 7)}",
"7",
false },
292 {
"@{sqrt(16)}",
"4",
false },
293 {
"@{ceil(3.2)}",
"4",
false },
294 {
"@{floor(3.8)}",
"3",
false },
295 {
"@{round(3.6)}",
"4",
false },
296 {
"@{pow(2, 3)}",
"8",
false },
299 {
"@{sum(1, 2, 3)}",
"6",
false },
300 {
"@{avg(2, 4, 6)}",
"4",
false },
303 for(
const auto& testCase : mathCases )
309 if( testCase.shouldError )
344 BOOST_CHECK( !evaluator_mm.
HasErrors() );
348 if (varName ==
"width") {
360 BOOST_CHECK( !evaluator_callback.
HasErrors() );
415 persistent_eval.
Evaluate(
"@{1 + 1}" );
421 persistent_eval.
Evaluate(
"@{${test} + 8}" );
447 double numeric_result = wxAtof(
result );
448 BOOST_CHECK_CLOSE( numeric_result, 38.1, 0.01 );
454 result = evaluator_mm.
Evaluate(
"@{${inch_in_mm} + ${mil_in_mm}}" );
466 result = evaluator_mm.
Evaluate(
"@{${one_inch} + ${thousand_mils}}" );
522 std::cout <<
"DEBUG: @{1in} returned '" <<
result.ToStdString() <<
"'" << std::endl;
524 std::cout <<
"DEBUG: @{1in} Errors: " << evaluator_mm.
GetErrorSummary().ToStdString() << std::endl;
568 double result_val = wxAtof(
result);
569 BOOST_CHECK(
std::abs(result_val - 1.0) < 0.001 );
590 double result_val2 = wxAtof(
result);
591 BOOST_CHECK(
std::abs(result_val2 - 76.2) < 0.001 );
602 wxString
result = evaluator_mm.Evaluate(
"@{1xyz}" );
607 result = evaluator_mm.Evaluate(
"@{25.4}" );
611 result = evaluator_mm.Evaluate(
"@{0mm}" );
614 result = evaluator_mm.Evaluate(
"@{0in}" );
618 result = evaluator_mm.Evaluate(
"@{2.54cm}" );
621 result = evaluator_mm.Evaluate(
"@{0.5in}" );
625 result = evaluator_mm.Evaluate(
"@{1um}" );
637 BOOST_CHECK( eval.
Process(
"1 + 2" ) );
646 BOOST_CHECK( eval.
Process(
"x + y" ) );
651 BOOST_CHECK_CLOSE( eval.
GetVar(
"x" ), 5.0, 0.001 );
652 BOOST_CHECK_CLOSE( eval.
GetVar(
"y" ), 3.0, 0.001 );
653 BOOST_CHECK_CLOSE( eval.
GetVar(
"undefined" ), 0.0, 0.001 );
656 BOOST_CHECK( eval.
Process(
"1in + 1mm" ) );
661 BOOST_CHECK( eval.
Process(
"sqrt(16)" ) );
666 BOOST_CHECK( !eval.
Process(
"1 + * 2" ) );
667 BOOST_CHECK( !eval.
IsValid() );
671 BOOST_CHECK_CLOSE( eval.
GetVar(
"x" ), 5.0, 0.001 );
673 BOOST_CHECK( eval.
Process(
"x * 2" ) );
679 BOOST_CHECK_CLOSE( eval.
GetVar(
"x" ), 0.0, 0.001 );
680 BOOST_CHECK_CLOSE( eval.
GetVar(
"y" ), 3.0, 0.001 );
684 BOOST_CHECK_CLOSE( eval.
GetVar(
"y" ), 0.0, 0.001 );
687 BOOST_CHECK( eval.
Process(
"42" ) );
693 BOOST_CHECK( eval.
Process(
"3.14" ) );
High-level wrapper for evaluating mathematical and string expressions in wxString format.
EDA_UNITS GetDefaultUnits() const
Get the current default units.
bool HasVariableCallback() const
Check if a custom variable callback is set.
wxString Evaluate(const wxString &aInput)
Main evaluation function - processes input string and evaluates all} expressions.
bool RemoveVariable(const wxString &aName)
Remove a variable from the evaluator.
bool HasErrors() const
Check if the last evaluation had errors.
wxString GetErrorSummary() const
Get detailed error information from the last evaluation.
void SetDefaultUnits(EDA_UNITS aUnits)
Set the default units for expressions.
void ClearVariables()
Clear all stored variables.
bool HasVariable(const wxString &aName) const
Check if a variable exists in stored variables.
void SetVariable(const wxString &aName, double aValue)
Set a numeric variable for use in expressions.
NUMERIC_EVALUATOR compatible wrapper around EXPRESSION_EVALUATOR.
void LocaleChanged()
Handle locale changes (for decimal separator)
bool Process(const wxString &aString)
Process and evaluate an expression.
wxString Result() const
Get the result of the last evaluation.
void RemoveVar(const wxString &aString)
Remove a single variable.
void SetVar(const wxString &aString, double aValue)
Set a variable value.
void ClearVar()
Remove all variables.
double GetVar(const wxString &aString)
Get a variable value.
void Clear()
Clear parser state but retain variables.
bool IsValid() const
Check if the last evaluation was successful.
wxString OriginalText() const
Get the original input text.
KICOMMON_API wxString GetText(EDA_UNITS aUnits, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Get the units string for a given units type.
auto MakeValue(T aVal) -> Result< T >
auto MakeError(std::string aMsg) -> Result< T >
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
VECTOR3I expected(15, 30, 45)
static const std::vector< EVAL_INVALID_CASE > eval_cases_invalid
A list of invalid test strings.
static const std::vector< EVAL_CASE > eval_cases_valid
A list of valid test strings and the expected results.
BOOST_TEST_CONTEXT("Test Clearance")
BOOST_AUTO_TEST_CASE(Basic)
Basic functionality test.
std::ostream & operator<<(std::ostream &aStream, EDA_UNITS aUnits)
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")