Skip to content

Commit

Permalink
#922: test: implement new test for static union switch
Browse files Browse the repository at this point in the history
  • Loading branch information
lifflander committed Jul 13, 2020
1 parent e85303d commit 71463c5
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions tests/unit/utils/test_safe_union.cc
Original file line number Diff line number Diff line change
Expand Up @@ -226,5 +226,75 @@ TEST_F(TestSafeUnion, test_safe_union_3) {
EXPECT_EQ(destroy_counter, 5);
}

struct MyTest4 { };

template <typename T>
struct TestFunctor;

template <>
struct TestFunctor<float> {
int operator()(vt::adt::SafeUnion<float, int, MyTest4>& in) {
EXPECT_FALSE(in.template is<int>());
EXPECT_FALSE(in.template is<MyTest4>());

EXPECT_TRUE(in.template is<float>());
EXPECT_GT(in.template get<float>(), 29.3);
EXPECT_LT(in.template get<float>(), 29.5);
return 1;
}
};


template <>
struct TestFunctor<int> {
int operator()(vt::adt::SafeUnion<float, int, MyTest4>& in) {
EXPECT_FALSE(in.template is<float>());
EXPECT_FALSE(in.template is<MyTest4>());

EXPECT_TRUE(in.template is<int>());
EXPECT_EQ(in.template get<int>(), 10);
return 2;
}
};

template <>
struct TestFunctor<MyTest4> {
int operator()(vt::adt::SafeUnion<float, int, MyTest4>& in) {
// never should happen
EXPECT_FALSE(true);
return 3;
}
};

// this gets triggered when no type is selected
template <>
struct TestFunctor<void> {
int operator()(vt::adt::SafeUnion<float, int, MyTest4>& in) {
EXPECT_FALSE(in.template is<float>());
EXPECT_FALSE(in.template is<int>());
EXPECT_FALSE(in.template is<MyTest4>());
return 0;
}
};


TEST_F(TestSafeUnion, test_safe_union_switch_4) {

vt::adt::SafeUnion<float, int, MyTest4> x;

EXPECT_EQ(x.template switchOn<TestFunctor>(x), 0);

x.init<int>();
x.get<int>() = 10;

EXPECT_EQ(x.template switchOn<TestFunctor>(x), 2);

x.reset();

x.init<float>();
x.get<float>() = 29.4;

EXPECT_EQ(x.template switchOn<TestFunctor>(x), 1);
}

}}} /* end namespace vt::tests::unit */

0 comments on commit 71463c5

Please sign in to comment.