48 std::string expression;
53 const std::vector<TestCase> cases = {
55 {
"Text @{2 + 3} more text",
"Text 5 more text",
false },
56 {
"@{10 - 4}",
"6",
false },
57 {
"@{7 * 8}",
"56",
false },
58 {
"@{15 / 3}",
"5",
false },
59 {
"@{17 % 5}",
"2",
false },
60 {
"@{2^3}",
"8",
false },
63 {
"@{2 + 3 * 4}",
"14",
false },
64 {
"@{(2 + 3) * 4}",
"20",
false },
65 {
"@{2^3^2}",
"512",
false },
66 {
"@{-5}",
"-5",
false },
67 {
"@{+5}",
"5",
false },
70 {
"@{3.14 + 1.86}",
"5",
false },
71 {
"@{10.5 / 2}",
"5.25",
false },
72 {
"@{3.5 * 2}",
"7",
false },
75 {
"@{1 / 0}",
"Text @{1 / 0} more text",
true },
76 {
"@{1 % 0}",
"Text @{1 % 0} more text",
true },
79 {
"@{2 + 2} and @{3 * 3}",
"4 and 9",
false },
82 for(
const auto& testCase : cases )
86 if( testCase.shouldError )
108 evaluator.
SetVariable( wxString(
"name"), wxString(
"KiCad") );
112 std::string expression;
117 const std::vector<TestCase> cases = {
119 {
"@{${x}}",
"10",
false },
120 {
"@{${y}}",
"5",
false },
121 {
"Hello ${name}!",
"Hello KiCad!",
false },
124 {
"@{${x} + ${y}}",
"15",
false },
125 {
"@{${x} * ${y}}",
"50",
false },
126 {
"@{${x} - ${y}}",
"5",
false },
127 {
"@{${x} / ${y}}",
"2",
false },
130 {
"Product: @{${x} * ${y}} units",
"Product: 50 units",
false },
131 {
"Version ${version}.0",
"Version 8.0",
false },
134 {
"@{${undefined}}",
"@{${undefined}}",
true },
137 {
"Welcome to ${name}",
"Welcome to KiCad",
false },
140 for(
const auto& testCase : cases )
144 if( testCase.shouldError )
162 evaluator.
SetVariable( wxString(
"prefix"), wxString(
"Hello") );
163 evaluator.
SetVariable( wxString(
"suffix"), wxString(
"World") );
166 std::string expression;
171 const std::vector<TestCase> cases = {
173 {
"@{\"Hello\" + \" \" + \"World\"}",
"Hello World",
false },
174 {
"@{${prefix} + \" \" + ${suffix}}",
"Hello World",
false },
177 {
"@{\"Count: \" + 42}",
"Count: 42",
false },
178 {
"@{42 + \" items\"}",
"42 items",
false },
181 {
"@{\"Simple string\"}",
"Simple string",
false },
182 {
"Prefix @{\"middle\"} suffix",
"Prefix middle suffix",
false },
185 for(
const auto& testCase : cases )
202 std::string expression;
208 const std::vector<TestCase> cases = {
210 {
"@{abs(-5)}",
"5",
false, 0.001 },
211 {
"@{abs(3.14)}",
"3.14",
false, 0.001 },
212 {
"@{sqrt(16)}",
"4",
false, 0.001 },
213 {
"@{sqrt(2)}",
"1.414",
false, 0.01 },
214 {
"@{pow(2, 3)}",
"8",
false, 0.001 },
215 {
"@{pow(3, 2)}",
"9",
false, 0.001 },
218 {
"@{floor(3.7)}",
"3",
false, 0.001 },
219 {
"@{ceil(3.2)}",
"4",
false, 0.001 },
220 {
"@{round(3.7)}",
"4",
false, 0.001 },
221 {
"@{round(3.2)}",
"3",
false, 0.001 },
222 {
"@{round(3.14159, 2)}",
"3.14",
false, 0.001 },
225 {
"@{min(5, 3, 8, 1)}",
"1",
false, 0.001 },
226 {
"@{max(5, 3, 8, 1)}",
"8",
false, 0.001 },
227 {
"@{min(3.5, 3.1)}",
"3.1",
false, 0.001 },
230 {
"@{sum(1, 2, 3, 4)}",
"10",
false, 0.001 },
231 {
"@{avg(2, 4, 6)}",
"4",
false, 0.001 },
234 {
"@{sqrt(-1)}",
"Text @{sqrt(-1)} more text",
true, 0 },
237 for(
const auto& testCase : cases )
241 if( testCase.shouldError )
249 if( testCase.tolerance > 0 )
252 double actualValue = wxStrtod(
result,
nullptr );
253 double expectedValue = wxStrtod( testCase.expected,
nullptr );
254 BOOST_CHECK_CLOSE( actualValue, expectedValue, testCase.tolerance * 100 );
270 evaluator.
SetVariable( wxString(
"text"), wxString(
"Hello World") );
273 std::string expression;
278 const std::vector<TestCase> cases = {
280 {
"@{upper(\"hello world\")}",
"HELLO WORLD",
false },
281 {
"@{lower(\"HELLO WORLD\")}",
"hello world",
false },
282 {
"@{upper(${text})}",
"HELLO WORLD",
false },
285 {
"@{concat(\"Hello\", \" \", \"World\")}",
"Hello World",
false },
286 {
"@{concat(\"Count: \", 42, \" items\")}",
"Count: 42 items",
false },
288 {
"@{beforefirst(\"hello.world.txt\", \".\")}",
"hello",
false },
289 {
"@{beforelast(\"hello.world.txt\", \".\")}",
"hello.world",
false },
290 {
"@{afterfirst(\"hello.world.txt\", \".\")}",
"world.txt",
false },
291 {
"@{afterlast(\"hello.world.txt\", \".\")}",
"txt",
false },
292 {
"@{beforefirst(${text}, \" \")}",
"Hello",
false },
294 {
"@{replace(\"Hello World\", \"l\", \"L\")}",
"HeLLo WorLd",
false },
295 {
"@{replace(\"Hello World\", \"llo\", \"y there\")}",
"Hey there World",
false },
296 {
"@{replace(${text}, \"l\", \"L\")}",
"HeLLo WorLd",
false },
299 for(
const auto& testCase : cases )
316 std::string expression;
321 const std::vector<TestCase> cases = {
323 {
"@{format(3.14159)}",
"3.14",
false },
324 {
"@{format(3.14159, 3)}",
"3.142",
false },
325 {
"@{format(1234.5)}",
"1234.50",
false },
326 {
"@{fixed(3.14159, 2)}",
"3.14",
false },
329 {
"@{currency(1234.56)}",
"$1234.56",
false },
330 {
"@{currency(999.99, \"€\")}",
"€999.99",
false },
333 for(
const auto& testCase : cases )
353 std::string expression;
354 bool shouldContainNumbers;
358 const std::vector<TestCase> cases = {
360 {
"@{today()}",
true,
false },
361 {
"@{now()}",
true,
false },
364 {
"@{dateformat(0)}",
false,
false },
365 {
"@{dateformat(0, \"ISO\")}",
false,
false },
366 {
"@{dateformat(0, \"US\")}",
false,
false },
367 {
"@{weekdayname(0)}",
false,
false },
370 for(
const auto& testCase : cases )
374 if( testCase.shouldError )
383 if( testCase.shouldContainNumbers )
387 [](
char c ) { return std::isdigit( c ) || c ==
'.' || c ==
'-'; } ) );
393 auto result1 = evaluator.
Evaluate(
"@{dateformat(0, \"ISO\")}" );
396 auto result2 = evaluator.
Evaluate(
"@{weekdayname(0)}" );
412 std::string expression;
416 const std::vector<TestCase> cases = {
418 {
"@{vcsidentifier()}",
false },
419 {
"@{vcsidentifier(7)}",
false },
420 {
"@{vcsbranch()}",
false },
421 {
"@{vcsauthor()}",
false },
422 {
"@{vcsauthoremail()}",
false },
423 {
"@{vcscommitter()}",
false },
424 {
"@{vcscommitteremail()}",
false },
425 {
"@{vcsnearestlabel()}",
false },
426 {
"@{vcslabeldistance()}",
false },
427 {
"@{vcsdirty()}",
false },
428 {
"@{vcsdirtysuffix()}",
false },
429 {
"@{vcscommitdate()}",
false },
432 {
"@{vcsfileidentifier(\".\")}",
false },
435 for(
const auto& testCase : cases )
439 if( testCase.shouldError )
461 std::string expression;
466 const std::vector<TestCase> cases = {
468 {
"@{if(1, \"true\", \"false\")}",
"true",
false },
469 {
"@{if(0, \"true\", \"false\")}",
"false",
false },
470 {
"@{if(${x} > ${y}, \"greater\", \"not greater\")}",
"greater",
false },
471 {
"@{if(${x} < ${y}, \"less\", \"not less\")}",
"not less",
false },
474 {
"@{if(1, 42, 24)}",
"42",
false },
475 {
"@{if(0, 42, 24)}",
"24",
false },
478 for(
const auto& testCase : cases )
498 double randomValue = wxStrtod(
result,
nullptr );
499 BOOST_CHECK_GE( randomValue, 0.0 );
500 BOOST_CHECK_LT( randomValue, 1.0 );
503 auto result2 = evaluator.
Evaluate(
"@{random()}" );
504 double randomValue2 = wxStrtod( result2,
nullptr );
507 BOOST_CHECK_NE( randomValue, randomValue2 );
518 std::string expression;
520 std::string description;
523 const std::vector<TestCase> cases = {
525 {
"@{2 +}",
true,
"incomplete expression" },
526 {
"@{(2 + 3",
true,
"unmatched parenthesis" },
527 {
"@{2 + 3)}",
true,
"extra closing parenthesis" },
528 {
"@{}",
true,
"empty calculation" },
531 {
"@{unknownfunc(1, 2)}",
true,
"unknown function" },
534 {
"@{abs()}",
true,
"abs with no arguments" },
535 {
"@{abs(1, 2)}",
true,
"abs with too many arguments" },
536 {
"@{sqrt()}",
true,
"sqrt with no arguments" },
539 {
"@{1 / 0}",
true,
"division by zero" },
540 {
"@{sqrt(-1)}",
true,
"square root of negative" },
543 {
"Plain text",
false,
"plain text should not error" },
544 {
"@{2 + 2}",
false,
"simple calculation should work" },
547 for(
const auto& testCase : cases )
551 if( testCase.shouldError )
553 BOOST_CHECK_MESSAGE( evaluator.
HasErrors(),
554 "Expected error for: " + testCase.description );
558 BOOST_CHECK_MESSAGE( !evaluator.
HasErrors(),
559 "Unexpected error for: " + testCase.description );
574 std::string expression;
579 const std::vector<TestCase> cases = {
581 {
"@{2 * ${pi} * ${radius}}",
"31.42", 0.01 },
582 {
"@{${pi} * pow(${radius}, 2)}",
"78.54", 0.01 },
583 {
"@{sqrt(pow(3, 2) + pow(4, 2))}",
"5", 0.001 },
586 {
"@{max(abs(-5), sqrt(16), floor(3.7))}",
"5", 0.001 },
587 {
"@{round(avg(1.1, 2.2, 3.3), 1)}",
"2.2", 0.001 },
590 {
"Circle with radius @{${radius}} has area @{format(${pi} * pow(${radius}, 2), 1)}",
591 "Circle with radius 5 has area 78.5", 0 },
594 for(
const auto& testCase : cases )
597 std::string resultStr =
result.ToStdString();
600 if( testCase.tolerance > 0 )
603 std::regex numberRegex( R
"([\d.]+)" );
606 if( std::regex_search( resultStr, match, numberRegex ) )
608 double actualValue = std::stod( match[0].str() );
609 double expectedValue = std::stod( testCase.expected );
610 BOOST_CHECK_CLOSE( actualValue, expectedValue, testCase.tolerance * 100 );
628 std::string largeExpression =
"Result: ";
629 for(
int i = 0; i < 50; ++i )
631 largeExpression +=
"@{" + std::to_string(i) +
" * 2} ";
634 auto start = std::chrono::high_resolution_clock::now();
636 auto end = std::chrono::high_resolution_clock::now();
638 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
end - start );
644#if defined( KICAD_SANITIZE_THREADS ) || defined( KICAD_SANITIZE_ADDRESS )
645 BOOST_TEST_MESSAGE(
"Instrumented large expression evaluation: " << duration.count() <<
" ms" );
647 BOOST_CHECK_LT( duration.count(), 1000 );
High-level wrapper for evaluating mathematical and string expressions in wxString format.
wxString Evaluate(const wxString &aInput)
Main evaluation function - processes input string and evaluates all '@{}' expressions.
bool HasErrors() const
Check if the last evaluation had errors.
void SetVariable(const wxString &aName, double aValue)
Set a numeric variable for use in expressions.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_AUTO_TEST_SUITE_END()
VECTOR3I expected(15, 30, 45)
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
wxString result
Test unit parsing edge cases and error handling.
BOOST_AUTO_TEST_CASE(BasicArithmetic)
Declare the test suite.