@@ -2425,6 +2425,101 @@ TEST_CASE("Is variable used", "[functions]")
24252425 CHECK_FALSE (p.is_variable_used ((" zz" )));
24262426 CHECK_FALSE (p.is_variable_used ((" TRESS_L" )));
24272427 }
2428+
2429+ TEST_CASE (" Remove unused variables" , " [variables]" )
2430+ {
2431+ te_parser p;
2432+ // nothing in here yet
2433+ p.remove_unused_variables_and_functions ();
2434+
2435+ p.set_variables_and_functions ({
2436+ {" STRESS_L" , static_cast <te_type>(10.1 ) },
2437+ {" P_LEVEL" , static_cast <te_type>(.5 ) },
2438+ {" z" , static_cast <te_type>(.75 ) } });
2439+ CHECK (p.get_variables_and_functions ().size () == 3 );
2440+
2441+ p.compile ((" z + P_LEVEL" ));
2442+ CHECK (p.is_variable_used ((" Z" )));
2443+ CHECK_FALSE (p.is_variable_used ((" STRESS_L" )));
2444+ CHECK (p.is_variable_used ((" P_LEVEL" )));
2445+ CHECK (p.success ());
2446+ CHECK (p.get_variables_and_functions ().size () == 3 );
2447+ p.remove_unused_variables_and_functions ();
2448+ CHECK (p.get_variables_and_functions ().size () == 2 );
2449+
2450+ p.compile ((" P_LEVEL" ));
2451+ CHECK (p.success ());
2452+ CHECK_FALSE (p.is_variable_used ((" Z" )));
2453+ CHECK_FALSE (p.is_variable_used ((" STRESS_L" )));
2454+ CHECK (p.is_variable_used ((" P_LEVEL" )));
2455+ p.remove_unused_variables_and_functions ();
2456+ CHECK (p.get_variables_and_functions ().size () == 1 );
2457+
2458+ p.compile ((" 5 + 2" ));
2459+ p.remove_unused_variables_and_functions ();
2460+ CHECK (p.get_variables_and_functions ().empty ());
2461+
2462+ // remove all at once
2463+ p.set_variables_and_functions ({
2464+ {" STRESS_L" , static_cast <te_type>(10.1 ) },
2465+ {" P_LEVEL" , static_cast <te_type>(.5 ) },
2466+ {" z" , static_cast <te_type>(.75 ) } });
2467+ p.compile ((" 5 + 2" ));
2468+ p.remove_unused_variables_and_functions ();
2469+ CHECK (p.get_variables_and_functions ().empty ());
2470+
2471+ // shouldn't do anything
2472+ p.remove_unused_variables_and_functions ();
2473+ }
2474+
2475+ TEST_CASE (" Remove unused functions" , " [functions]" )
2476+ {
2477+ te_parser p;
2478+ // nothing in here yet
2479+ p.remove_unused_variables_and_functions ();
2480+
2481+ p.set_variables_and_functions ({
2482+ {" STRESS_L" , return5 },
2483+ {" P_LEVEL" , __mult },
2484+ {" z" , AddEm } });
2485+ CHECK (p.get_variables_and_functions ().size () == 3 );
2486+
2487+ p.compile ((" z(2,5) + P_LEVEL(2,30,4,5)" ));
2488+ CHECK (p.is_function_used ((" Z" )));
2489+ CHECK_FALSE (p.is_function_used ((" STRESS_L" )));
2490+ CHECK (p.is_function_used ((" P_LEVEL" )));
2491+ CHECK (p.success ());
2492+ p.remove_unused_variables_and_functions ();
2493+ CHECK (p.get_variables_and_functions ().size () == 2 );
2494+
2495+ p.compile ((" P_LEVEL(2,30,4,5)" ));
2496+ CHECK (p.success ());
2497+ CHECK_FALSE (p.is_function_used ((" Z" )));
2498+ CHECK_FALSE (p.is_function_used ((" STRESS_L" )));
2499+ CHECK (p.is_function_used ((" P_LEVEL" )));
2500+ p.remove_unused_variables_and_functions ();
2501+ CHECK (p.get_variables_and_functions ().size () == 1 );
2502+
2503+ p.compile ((" 5 + 2" ));
2504+ p.remove_unused_variables_and_functions ();
2505+ CHECK (p.get_variables_and_functions ().empty ());
2506+
2507+ // remove all at once
2508+ p.set_variables_and_functions ({
2509+ {" STRESS_L" , return5 },
2510+ {" P_LEVEL" , __mult },
2511+ {" z" , AddEm } });
2512+ p.compile ((" 5 + 2" ));
2513+ p.remove_unused_variables_and_functions ();
2514+ CHECK (p.get_variables_and_functions ().empty ());
2515+
2516+ // function shouldn't be recognized now
2517+ p.compile ((" P_LEVEL(2,30,4,5)" ));
2518+ CHECK_FALSE (p.success ());
2519+
2520+ // shouldn't do anything
2521+ p.remove_unused_variables_and_functions ();
2522+ }
24282523#endif
24292524
24302525TEST_CASE (" Custom test" , " [functions]" )
0 commit comments