11#include " bitfilled/integer.hpp"
22#include " bitfilled/bits.hpp"
33#include " bitfilled/macros.hpp"
4- #include < catch2/catch_template_test_macros.hpp>
5- #include < catch2/catch_test_macros.hpp>
4+ #include < boost/ut.hpp>
65
76using namespace bitfilled ;
7+ using namespace boost ::ut;
88
99template <std::endian ENDIAN>
1010struct endian_type
1111{
1212 static constexpr auto endianness = ENDIAN;
1313};
1414
15- TEMPLATE_TEST_CASE (" integer_storage" , " [vector][template]" , endian_type<std::endian::little>,
16- endian_type<std::endian::big>)
17- {
18- const uint8_t u8 = 42 ;
19- const integer_storage<1 > us1{u8 , TestType::endianness};
20- CHECK (us1.to_integral <uint8_t >(TestType::endianness) == u8 );
21-
22- const integer_storage<2 > us2{u8 , TestType::endianness};
23- CHECK (us2.to_integral <uint8_t >(TestType::endianness) == u8 );
24-
25- const uint32_t u32 = 0x123456 ;
26- const integer_storage<3 > us3{u32 , TestType::endianness};
27- CHECK (us3.to_integral <uint32_t >(TestType::endianness) == u32 );
28-
29- const integer_storage<7 > us7{u32 , TestType::endianness};
30- CHECK (us7.to_integral <uint32_t >(TestType::endianness) == u32 );
31-
32- const int8_t i8 = -42 ;
33- const integer_storage<1 > s1{i8 , TestType::endianness};
34- CHECK (s1.to_integral <int8_t >(TestType::endianness) == i8 );
35-
36- const integer_storage<2 > s2{i8 , TestType::endianness};
37- CHECK (s1.to_integral <int8_t >(TestType::endianness) == i8 );
38-
39- const int32_t i32 = -876543 ;
40- const integer_storage<3 > s3{i32 , TestType::endianness};
41- CHECK (s3.to_integral <int32_t >(TestType::endianness) == i32 );
42-
43- const integer_storage<7 > s7{i32 , TestType::endianness};
44- CHECK (s7.to_integral <int32_t >(TestType::endianness) == i32 );
45-
46- #if 1
47- constexpr packed_integer<TestType::endianness, 1 , uint8_t > pus1{u8 };
48- static_assert (pus1 == u8 );
49- constexpr packed_integer<TestType::endianness, 2 , uint8_t > pus2{u8 };
50- static_assert (pus2 == u8 );
51- constexpr packed_integer<TestType::endianness, 3 , uint32_t > pus3{u32 };
52- static_assert (pus3 == u32 );
53- constexpr packed_integer<TestType::endianness, 7 , uint32_t > pus7{u32 };
54- static_assert (pus7 == u32 );
55- constexpr packed_integer<TestType::endianness, 1 , int8_t > ps1{i8 };
56- static_assert (ps1 == i8 );
57- constexpr packed_integer<TestType::endianness, 2 , int8_t > ps2{i8 };
58- static_assert (ps2 == i8 );
59- constexpr packed_integer<TestType::endianness, 3 , int32_t > ps3{i32 };
60- static_assert (ps3 == i32 );
61- constexpr packed_integer<TestType::endianness, 7 , int32_t > ps7{i32 };
62- static_assert (ps7 == i32 );
63-
64- constexpr uint32_t u32_ = 0x12345678 ;
65- constexpr packed_integer<TestType::endianness, 4 > pus4{u32_};
66- static_assert (pus4 == u32_);
67- constexpr int32_t i32_ = (int32_t )0x87654321 ;
68- constexpr packed_integer<TestType::endianness, 4 , int32_t > ps4{i32_};
69- static_assert (ps4 == i32_);
70- #endif
71- }
72-
7315template <std::endian ENDIAN, std::size_t SIZE, typename T = sized_unsigned_t <std::bit_ceil(SIZE)>>
7416struct packed_integer_with_bfs : packed_integer<ENDIAN, SIZE, T>
7517{
@@ -79,26 +21,73 @@ struct packed_integer_with_bfs : packed_integer<ENDIAN, SIZE, T>
7921 [[no_unique_address]] ::bitfilled::bitfield<unsigned , bf_ops, 0 , 15 > halfword{};
8022};
8123
82- struct demo : packed_integer<std::endian::little, 4 >
24+ suite integer = []
8325{
84- using superclass::operator =;
85- BF_BITS (unsigned , 0 , 2 ) a {};
86- };
26+ " integer_storage" _test = []<class TestType >
27+ {
28+ const uint8_t u8 = 42 ;
29+ const integer_storage<1 > us1{u8 , TestType::endianness};
30+ expect (us1.to_integral <uint8_t >(TestType::endianness) == u8 );
8731
88- TEMPLATE_TEST_CASE (" packed_integer_bits" , " [vector][template]" , endian_type<std::endian::little>,
89- endian_type<std::endian::big>)
90- {
91- packed_integer_with_bfs<TestType::endianness, 4 > pus4{};
92- CHECK (pus4.halfword == 0 );
93- pus4.halfword = 0xabcd ;
94- CHECK (pus4.halfword == 0xabcd );
95- CHECK (pus4 == 0xabcd );
96- pus4 = 0x12345678 ;
97- CHECK (pus4.halfword == 0x5678 );
32+ const integer_storage<2 > us2{u8 , TestType::endianness};
33+ expect (us2.to_integral <uint8_t >(TestType::endianness) == u8 );
34+
35+ const uint32_t u32 = 0x123456 ;
36+ const integer_storage<3 > us3{u32 , TestType::endianness};
37+ expect (us3.to_integral <uint32_t >(TestType::endianness) == u32 );
38+
39+ const integer_storage<7 > us7{u32 , TestType::endianness};
40+ expect (us7.to_integral <uint32_t >(TestType::endianness) == u32 );
41+
42+ const int8_t i8 = -42 ;
43+ const integer_storage<1 > s1{i8 , TestType::endianness};
44+ expect (s1.to_integral <int8_t >(TestType::endianness) == i8 );
9845
99- demo d{}, d2{23 };
100- d = d2;
101- d.a = 0 ;
102- d = 0 ;
103- d2.a = d.a ;
104- }
46+ const integer_storage<2 > s2{i8 , TestType::endianness};
47+ expect (s1.to_integral <int8_t >(TestType::endianness) == i8 );
48+
49+ const int32_t i32 = -876543 ;
50+ const integer_storage<3 > s3{i32 , TestType::endianness};
51+ expect (s3.to_integral <int32_t >(TestType::endianness) == i32 );
52+
53+ const integer_storage<7 > s7{i32 , TestType::endianness};
54+ expect (s7.to_integral <int32_t >(TestType::endianness) == i32 );
55+
56+ #if 1
57+ constexpr packed_integer<TestType::endianness, 1 , uint8_t > pus1{u8 };
58+ static_assert (pus1 == u8 );
59+ constexpr packed_integer<TestType::endianness, 2 , uint8_t > pus2{u8 };
60+ static_assert (pus2 == u8 );
61+ constexpr packed_integer<TestType::endianness, 3 , uint32_t > pus3{u32 };
62+ static_assert (pus3 == u32 );
63+ constexpr packed_integer<TestType::endianness, 7 , uint32_t > pus7{u32 };
64+ static_assert (pus7 == u32 );
65+ constexpr packed_integer<TestType::endianness, 1 , int8_t > ps1{i8 };
66+ static_assert (ps1 == i8 );
67+ constexpr packed_integer<TestType::endianness, 2 , int8_t > ps2{i8 };
68+ static_assert (ps2 == i8 );
69+ constexpr packed_integer<TestType::endianness, 3 , int32_t > ps3{i32 };
70+ static_assert (ps3 == i32 );
71+ constexpr packed_integer<TestType::endianness, 7 , int32_t > ps7{i32 };
72+ static_assert (ps7 == i32 );
73+
74+ constexpr uint32_t u32_ = 0x12345678 ;
75+ constexpr packed_integer<TestType::endianness, 4 > pus4{u32_};
76+ static_assert (pus4 == u32_);
77+ constexpr int32_t i32_ = (int32_t )0x87654321 ;
78+ constexpr packed_integer<TestType::endianness, 4 , int32_t > ps4{i32_};
79+ static_assert (ps4 == i32_);
80+ #endif
81+ } | std::tuple<endian_type<std::endian::little>, endian_type<std::endian::big>>{};
82+
83+ " packed_integer_bits" _test = []<class TestType >
84+ {
85+ packed_integer_with_bfs<TestType::endianness, 4 > pus4{};
86+ expect (pus4.halfword == 0 );
87+ pus4.halfword = 0xabcd ;
88+ expect (pus4.halfword == 0xabcd );
89+ expect (pus4 == 0xabcd );
90+ pus4 = 0x12345678 ;
91+ expect (pus4.halfword == 0x5678 );
92+ } | std::tuple<endian_type<std::endian::little>, endian_type<std::endian::big>>{};
93+ };
0 commit comments