#include #include #include #include #include using namespace Void::Variant; using V3 = std::variant; // ── match ───────────────────────────────────────────────────────────────────── Void::Testing::Test variant_match_int() { V3 v = 42; const auto s = match(v, [](int i) { return std::to_string(i); }, [](double d) { return std::to_string(d); }, [](const std::string& s) { return s; }); Void::Testing::expect_eq(s, std::string{"42"}); } Void::Testing::Test variant_match_string() { V3 v = std::string{"hello"}; bool got_string = false; match(v, [](int) {}, [](double) {}, [&](const std::string&) { got_string = true; }); Void::Testing::expect(got_string); } Void::Testing::Test variant_match_returns_value() { V3 v = 3.14; const int result = match(v, [](int i) { return i; }, [](double d) { return static_cast(d); }, [](const std::string&) { return 0; }); Void::Testing::expect_eq(result, 3); } // ── overload ────────────────────────────────────────────────────────────────── Void::Testing::Test variant_overload_helper() { V3 v = 99; const auto visitor = overload{ [](int i) { return i * 2; }, [](double d) { return static_cast(d); }, [](const std::string&) { return -1; } }; Void::Testing::expect_eq(std::visit(visitor, v), 198); } // ── holds ───────────────────────────────────────────────────────────────────── Void::Testing::Test variant_holds_correct_type() { V3 v = 1; Void::Testing::expect(holds(v)); Void::Testing::expect(!holds(v)); Void::Testing::expect(!holds(v)); } Void::Testing::Test variant_holds_after_assignment() { V3 v = 1; v = 3.14; Void::Testing::expect(!holds(v)); Void::Testing::expect(holds(v)); } // ── get_or ──────────────────────────────────────────────────────────────────── Void::Testing::Test variant_get_or_present() { V3 v = 7; Void::Testing::expect_eq(get_or(v, 0), 7); } Void::Testing::Test variant_get_or_absent() { V3 v = 3.14; Void::Testing::expect_eq(get_or(v, -1), -1); } // ── try_get ─────────────────────────────────────────────────────────────────── Void::Testing::Test variant_try_get_present() { V3 v = std::string{"world"}; const auto r = try_get(v); Void::Testing::expect(r.has_value()); Void::Testing::expect_eq(*r, std::string{"world"}); } Void::Testing::Test variant_try_get_absent() { V3 v = 1; Void::Testing::expect(!try_get(v).has_value()); } // ── index_of ───────────────────────────────────────────────────────────────── Void::Testing::Test variant_index_of_compile_time() { static_assert(index_of == 0); static_assert(index_of == 1); static_assert(index_of == 2); Void::Testing::expect(true); // compile-time assertion passed } // ── apply_to ───────────────────────────────────────────────────────────────── Void::Testing::Test variant_apply_to_matching() { V3 v = 5; const auto r = apply_to(v, [](int i) { return i * 10; }); Void::Testing::expect(r.has_value()); Void::Testing::expect_eq(*r, 50); } Void::Testing::Test variant_apply_to_non_matching() { V3 v = 3.14; const auto r = apply_to(v, [](int i) { return i; }); Void::Testing::expect(!r.has_value()); } // ── narrow ──────────────────────────────────────────────────────────────────── Void::Testing::Test variant_narrow_convertible() { V3 v = 7; const auto r = narrow(v); // int is convertible to double Void::Testing::expect(r.has_value()); Void::Testing::expect_eq(*r, 7.0); } Void::Testing::Test variant_narrow_not_convertible() { V3 v = std::string{"x"}; const auto r = narrow(v); Void::Testing::expect(!r.has_value()); }