/** @file DConstant.test.cpp * * @author Roland Conybeare, Jan 2026 **/ #include "init_expression2.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace ut { using xo::S_expression2_tag; using xo::scm::DConstant; using xo::scm::DFloat; using xo::scm::DInteger; using xo::scm::AExpression; using xo::mm::CollectorTypeRegistry; using xo::mm::AAllocator; using xo::mm::ACollector; using xo::mm::AGCObject; using xo::mm::DX1Collector; using xo::mm::X1CollectorConfig; using xo::mm::ArenaConfig; using xo::print::APrintable; using xo::print::ppstate_standalone; using xo::print::ppconfig; using xo::facet::FacetRegistry; using xo::facet::with_facet; using xo::facet::obj; using xo::facet::typeseq; using xo::reflect::Reflect; using xo::InitEvidence; using xo::InitSubsys; using xo::scope; // Ensure subsystem initialized before tests static InitEvidence s_init = InitSubsys::require(); TEST_CASE("DConstant-init", "[expression2][DConstant]") { // Verify subsystem initialization succeeded REQUIRE(s_init.evidence()); } TEST_CASE("DConstant-from-float", "[expression2][DConstant]") { REQUIRE(s_init.evidence()); X1CollectorConfig cfg{ .name_ = "dconstant_float_test", .arena_config_ = ArenaConfig{ .size_ = 8192, .store_header_flag_ = true}, .object_types_z_ = 16384, .gc_trigger_v_{{4096, 4096}}, .debug_flag_ = false, }; DX1Collector gc(cfg); auto alloc = with_facet::mkobj(&gc); auto coll = with_facet::mkobj(&gc); bool ok = CollectorTypeRegistry::instance().install_types(coll); REQUIRE(ok); // Box a float value obj fval = DFloat::box(alloc, 3.14); REQUIRE(fval.data() != nullptr); // Create DConstant from the boxed float auto expr = DConstant::make(alloc, fval); REQUIRE(expr.data() != nullptr); // Verify expression type REQUIRE(expr.data()->extype() == xo::scm::exprtype::constant); // Verify valuetype is double (DFloat::value_type) REQUIRE(expr.data()->valuetype() == Reflect::require()); // Verify value is accessible REQUIRE(expr.data()->value().data() != nullptr); } TEST_CASE("DConstant-from-integer", "[expression2][DConstant]") { REQUIRE(s_init.evidence()); X1CollectorConfig cfg{ .name_ = "dconstant_int_test", .arena_config_ = ArenaConfig{ .size_ = 8192, .store_header_flag_ = true}, .object_types_z_ = 16384, .gc_trigger_v_{{4096, 4096}}, .debug_flag_ = false, }; DX1Collector gc(cfg); auto alloc = with_facet::mkobj(&gc); auto coll = with_facet::mkobj(&gc); bool ok = CollectorTypeRegistry::instance().install_types(coll); REQUIRE(ok); // Box an integer value obj ival = DInteger::box(alloc, 42); REQUIRE(ival.data() != nullptr); // Create DConstant from the boxed integer auto expr = DConstant::make(alloc, ival); REQUIRE(expr.data() != nullptr); // Verify expression type REQUIRE(expr.data()->extype() == xo::scm::exprtype::constant); // Verify valuetype is long (DInteger::value_type) REQUIRE(expr.data()->valuetype() == Reflect::require()); // Verify value is accessible REQUIRE(expr.data()->value().data() != nullptr); } TEST_CASE("DConstant-pretty-float", "[expression2][DConstant][pp]") { scope log(XO_DEBUG(true)); REQUIRE(s_init.evidence()); X1CollectorConfig cfg{ .name_ = "dconstant_pp_float_test", .arena_config_ = ArenaConfig{ .size_ = 8192, .store_header_flag_ = true}, .object_types_z_ = 16384, .gc_trigger_v_{{4096, 4096}}, .debug_flag_ = false, }; DX1Collector gc(cfg); auto alloc = with_facet::mkobj(&gc); auto coll = with_facet::mkobj(&gc); bool ok = CollectorTypeRegistry::instance().install_types(coll); REQUIRE(ok); // Box a float value obj fval = DFloat::box(alloc, 2.718); auto expr = DConstant::make(alloc, fval); REQUIRE(expr.data() != nullptr); // Pretty print std::stringstream ss; ppconfig ppc; ppstate_standalone pps(&ss, 0, &ppc); obj expr_pr(expr.data()); pps.pretty(expr_pr); std::string output = ss.str(); log && log(output); // Output should contain "DConstant" struct name CHECK(output.find("DConstant") != std::string::npos); } TEST_CASE("DConstant-pretty-integer", "[expression2][DConstant][pp]") { scope log(XO_DEBUG(false)); REQUIRE(s_init.evidence()); X1CollectorConfig cfg{ .name_ = "dconstant_pp_int_test", .arena_config_ = ArenaConfig{ .size_ = 8192, .store_header_flag_ = true}, .object_types_z_ = 16384, .gc_trigger_v_{{4096, 4096}}, .debug_flag_ = false, }; DX1Collector gc(cfg); auto alloc = with_facet::mkobj(&gc); auto coll = with_facet::mkobj(&gc); bool ok = CollectorTypeRegistry::instance().install_types(coll); REQUIRE(ok); // Box an integer value obj ival = DInteger::box(alloc, 123); auto expr = DConstant::make(alloc, ival); REQUIRE(expr.data() != nullptr); // Pretty print std::stringstream ss; ppconfig ppc; ppstate_standalone pps(&ss, 0, &ppc); obj expr_pr(expr.data()); pps.pretty(expr_pr); std::string output = ss.str(); log && log(output); // Output should contain "DConstant" struct name CHECK(output.find("DConstant") != std::string::npos); } } /* end DConstant.test.cpp */