From 71463c5b04a7766929554b6a699b075ac7f7b2cd Mon Sep 17 00:00:00 2001 From: Jonathan Lifflander Date: Mon, 13 Jul 2020 14:54:51 -0700 Subject: [PATCH] #922: test: implement new test for static union switch --- tests/unit/utils/test_safe_union.cc | 70 +++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/tests/unit/utils/test_safe_union.cc b/tests/unit/utils/test_safe_union.cc index 3991b530f0..de4907e2e4 100644 --- a/tests/unit/utils/test_safe_union.cc +++ b/tests/unit/utils/test_safe_union.cc @@ -226,5 +226,75 @@ TEST_F(TestSafeUnion, test_safe_union_3) { EXPECT_EQ(destroy_counter, 5); } +struct MyTest4 { }; + +template +struct TestFunctor; + +template <> +struct TestFunctor { + int operator()(vt::adt::SafeUnion& in) { + EXPECT_FALSE(in.template is()); + EXPECT_FALSE(in.template is()); + + EXPECT_TRUE(in.template is()); + EXPECT_GT(in.template get(), 29.3); + EXPECT_LT(in.template get(), 29.5); + return 1; + } +}; + + +template <> +struct TestFunctor { + int operator()(vt::adt::SafeUnion& in) { + EXPECT_FALSE(in.template is()); + EXPECT_FALSE(in.template is()); + + EXPECT_TRUE(in.template is()); + EXPECT_EQ(in.template get(), 10); + return 2; + } +}; + +template <> +struct TestFunctor { + int operator()(vt::adt::SafeUnion& in) { + // never should happen + EXPECT_FALSE(true); + return 3; + } +}; + +// this gets triggered when no type is selected +template <> +struct TestFunctor { + int operator()(vt::adt::SafeUnion& in) { + EXPECT_FALSE(in.template is()); + EXPECT_FALSE(in.template is()); + EXPECT_FALSE(in.template is()); + return 0; + } +}; + + +TEST_F(TestSafeUnion, test_safe_union_switch_4) { + + vt::adt::SafeUnion x; + + EXPECT_EQ(x.template switchOn(x), 0); + + x.init(); + x.get() = 10; + + EXPECT_EQ(x.template switchOn(x), 2); + + x.reset(); + + x.init(); + x.get() = 29.4; + + EXPECT_EQ(x.template switchOn(x), 1); +} }}} /* end namespace vt::tests::unit */