From 3115fa9b3ac400a8ac2350bc0997ac520acc071d Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Fri, 27 Feb 2026 23:54:14 +0100 Subject: [PATCH] Bug fixes, enhanced tests --- src/tl/tl/tlExpression.cc | 12 +++-- src/tl/unit_tests/tlExpressionTests.cc | 68 ++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/src/tl/tl/tlExpression.cc b/src/tl/tl/tlExpression.cc index 8a9b997ee..49e18f8c2 100644 --- a/src/tl/tl/tlExpression.cc +++ b/src/tl/tl/tlExpression.cc @@ -3789,16 +3789,18 @@ Eval::read_number (ExpressionParserContext &ex, tl::Variant &t) } else { - if (m_sloppy) { - t = tl::Variant (t.to_double () * f); - } else { - double gg = t.to_double (); - double g = floor (0.5 + t.to_double ()); + double gg = t.to_double () * f; + + if (! m_sloppy) { + // check, if the result is an integer + double g = floor (0.5 + gg); if (fabs (g) < 1e12 && fabs (g - gg) > 1e-3) { throw EvalError (tl::to_string (tr ("Value is not a multiple of the database unit")), ex1); } } + t = tl::Variant (gg); + } } diff --git a/src/tl/unit_tests/tlExpressionTests.cc b/src/tl/unit_tests/tlExpressionTests.cc index d60cf118c..ef862b33e 100644 --- a/src/tl/unit_tests/tlExpressionTests.cc +++ b/src/tl/unit_tests/tlExpressionTests.cc @@ -1263,3 +1263,71 @@ TEST(20) EXPECT_EQ (v.to_string (), std::string ("0.3")); } +namespace +{ + +class MyContextHandler + : public tl::ContextHandler +{ +public: + MyContextHandler () { } + + virtual tl::Variant eval_bracket (const std::string &content) const + { + return tl::Variant (content); + } + + virtual tl::Variant eval_double_bracket (const std::string &content) const + { + return tl::Variant ("{" + content + "}"); + } + + virtual double dbu () const + { + return 0.25; + } +}; + +} + +// with context handler +TEST(21) +{ + tl::Eval e; + MyContextHandler ctx; + e.set_ctx_handler (&ctx); + + tl::Variant v; + v = e.parse ("1um").execute (); + EXPECT_EQ (v.to_parsable_string (), std::string ("##4")); + v = e.parse ("250nm").execute (); + EXPECT_EQ (v.to_parsable_string (), std::string ("##1")); + v = e.parse ("2mm").execute (); + EXPECT_EQ (v.to_parsable_string (), std::string ("##8000")); + v = e.parse ("2m").execute (); + EXPECT_EQ (v.to_parsable_string (), std::string ("##8000000")); + v = e.parse ("1um2+0.5um2").execute (); + EXPECT_EQ (v.to_parsable_string (), std::string ("##24")); + v = e.parse ("1mm2").execute (); + EXPECT_EQ (v.to_parsable_string (), std::string ("##16000000")); + v = e.parse ("1m2").execute (); + EXPECT_EQ (v.to_parsable_string (), std::string ("##16000000000000")); + v = e.parse ("<>").execute (); + EXPECT_EQ (v.to_parsable_string (), std::string ("'{xxx}'")); + v = e.parse ("").execute (); + EXPECT_EQ (v.to_parsable_string (), std::string ("'yyy'")); + + e.set_ctx_handler (0); + try { + v = e.parse ("1um").execute (); + EXPECT_EQ (0, 1); + } catch (...) { + } + + tl::Eval es (0, true /*sloppy*/); + v = es.parse ("1.5").execute (); + EXPECT_EQ (v.to_parsable_string (), std::string ("##1.5")); + v = es.parse ("1.5um").execute (); + EXPECT_EQ (v.to_parsable_string (), std::string ("nil")); +} +