Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
test_static_variant.cpp
Go to the documentation of this file.
1#define BOOST_TEST_MODULE static_variant
2#include <boost/test/included/unit_test.hpp>
5#include <fc/variant.hpp>
6
7BOOST_AUTO_TEST_SUITE(static_variant_test_suite)
8 BOOST_AUTO_TEST_CASE(to_from_fc_variant)
9 {
10 using variant_type = std::variant<int32_t, bool>;
11 auto std_variant_1 = variant_type{false};
12 auto fc_variant = fc::variant{};
13
14 fc::to_variant(std_variant_1, fc_variant);
15
16 auto std_variant_2 = variant_type{};
17 fc::from_variant(fc_variant, std_variant_2);
18
19 BOOST_REQUIRE(std_variant_1 == std_variant_2);
20 }
21
23 {
24 using variant_type = std::variant<int32_t, bool, std::string>;
25
26 auto v1 = variant_type{std::string{"hello world"}};
27 BOOST_CHECK_EXCEPTION(std::get<int32_t>(v1), std::bad_variant_access, [](const auto& e) { return true; });
28 auto result1 = std::get<std::string>(v1);
29 BOOST_REQUIRE(result1 == std::string{"hello world"});
30
31 const auto v2 = variant_type{std::string{"hello world"}};
32 BOOST_CHECK_EXCEPTION(std::get<int32_t>(v2), std::bad_variant_access, [](const auto& e) { return true; });
33 const auto result2 = std::get<std::string>(v2);
34 BOOST_REQUIRE(result2 == std::string{"hello world"});
35 }
36
37 BOOST_AUTO_TEST_CASE(static_variant_from_index)
38 {
39 using variant_type = std::variant<int32_t, bool, std::string>;
40 auto v = variant_type{};
41
42 BOOST_CHECK_EXCEPTION(fc::from_index(v, 3), fc::assert_exception, [](const auto& e) { return e.code() == fc::assert_exception_code; });
43
44 fc::from_index(v, 2);
45 BOOST_REQUIRE(std::string{} == std::get<std::string>(v));
46 }
47
48 BOOST_AUTO_TEST_CASE(static_variant_get_index)
49 {
50 using variant_type = std::variant<int32_t, bool, std::string>;
51 BOOST_REQUIRE((fc::get_index<variant_type, int32_t>() == 0));
52 BOOST_REQUIRE((fc::get_index<variant_type, bool>() == 1));
53 BOOST_REQUIRE((fc::get_index<variant_type, std::string>() == 2));
54 BOOST_REQUIRE((fc::get_index<variant_type, double>() == std::variant_size_v<variant_type>)); // Isn't a type contained in variant.
55 }
56BOOST_AUTO_TEST_SUITE_END()
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
Defines exception's used by fc.
@ assert_exception_code
Definition exception.hpp:30
void from_variant(const fc::variant &v, sysio::chain::chain_id_type &cid)
void to_variant(const sysio::chain::shared_public_key &var, fc::variant &vo)
Definition authority.cpp:4
constexpr std::size_t get_index()
void from_index(variant &v, int index)
BOOST_AUTO_TEST_CASE(to_from_fc_variant)