1 2 #include <algorithm> 3 #include <cstddef> 4 #include <cstdint> 5 6 #include "catch.hpp" 7 8 #include <mapbox/variant.hpp> 9 #include <mapbox/variant_io.hpp> 10 11 struct some_struct 12 { 13 int a; 14 bool b; 15 std::string c; 16 }; 17 18 using variant_internal_index_type = size_t; 19 20 TEST_CASE("size of variants") 21 { 22 constexpr const auto min_overhead = sizeof(variant_internal_index_type); 23 24 using namespace std; // workaround for bug in GCC <= 4.8 where max_align_t is not in std 25 constexpr const auto max_overhead = alignof(max_align_t) + min_overhead; 26 27 using v1 = mapbox::util::variant<int>; 28 using v2 = mapbox::util::variant<int, bool, int64_t>; 29 using v3 = mapbox::util::variant<int, std::string>; 30 using v4 = mapbox::util::variant<std::string, std::string>; 31 using v5 = mapbox::util::variant<some_struct>; 32 33 constexpr const auto si = sizeof(int); 34 constexpr const auto sb = sizeof(bool); 35 constexpr const auto si64 = sizeof(int64_t); 36 constexpr const auto sd = sizeof(double); 37 constexpr const auto sstr = sizeof(std::string); 38 constexpr const auto spi = sizeof(std::pair<int, int>); 39 constexpr const auto ss = sizeof(some_struct); 40 41 REQUIRE(sizeof(v1) <= max_overhead + si); 42 REQUIRE(sizeof(v2) <= max_overhead + std::max({si, sb, si64})); 43 REQUIRE(sizeof(v3) <= max_overhead + std::max({si, sstr})); 44 REQUIRE(sizeof(v4) <= max_overhead + sstr); 45 REQUIRE(sizeof(v5) <= max_overhead + ss); 46 47 REQUIRE(sizeof(v1) >= min_overhead + si); 48 REQUIRE(sizeof(v2) >= min_overhead + std::max({si, sb, si64})); 49 REQUIRE(sizeof(v3) >= min_overhead + std::max({si, sstr})); 50 REQUIRE(sizeof(v4) >= min_overhead + sstr); 51 REQUIRE(sizeof(v5) >= min_overhead + ss); 52 } 53