Commit c7c2f23c authored by Gauthier Quesnel's avatar Gauthier Quesnel
Browse files

test: added tagged-union unit test

parent ecb4ab20
#include <vector>
#include "tagged-union.hpp"
#include "unit-test.hpp"
static void
check_tagged_union()
{
bits::tagged_union real{ 3.0 };
Ensures(real.type == bits::tagged_union::tag::real);
bits::tagged_union integer{ 1000 };
Ensures(integer.type == bits::tagged_union::tag::integer);
bits::tagged_union str{ "hello world" };
Ensures(str.type == bits::tagged_union::tag::string);
str = real;
Ensures(str.type == bits::tagged_union::tag::real);
Ensures(str.d == 3.0);
str = integer;
Ensures(str.type == bits::tagged_union::tag::integer);
Ensures(str.l == 1000);
std::vector<bits::tagged_union> x(100);
for (auto& elem : x) {
Ensures(elem.type == bits::tagged_union::tag::integer);
Ensures(elem.l == 0);
}
auto y = bits::tagged_union(4.0);
Ensures(y.type == bits::tagged_union::tag::real);
Ensures(y.d == 4.0);
x[0] = bits::tagged_union(5.0);
Ensures(x[0].type == bits::tagged_union::tag::real);
Ensures(x[0].d == 5.0);
x[0].swap(x[1]);
Ensures(x[0].type == bits::tagged_union::tag::integer);
Ensures(x[0].l == 0l);
Ensures(x[1].type == bits::tagged_union::tag::real);
Ensures(x[1].d == 5.0);
x[2] = std::move(x[1]);
Ensures(x[0].type == bits::tagged_union::tag::integer);
Ensures(x[0].l == 0l);
Ensures(x[1].type == bits::tagged_union::tag::integer);
Ensures(x[1].l == 0l);
Ensures(x[2].type == bits::tagged_union::tag::real);
EnsuresEqual(x[2].d, 5.0);
EnsuresNotEqual(x[2].d, 6.0);
x[3] = bits::tagged_union(std::string("hello world!"));
Ensures(x[3].type == bits::tagged_union::tag::string);
Ensures(x[3].s == "hello world!");
}
int
main(int argc, char const* argv[])
{
check_tagged_union();
return unit_test::report_errors();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment