Skip to content

Commit cceff11

Browse files
committed
switch to boost-ext/ut unit testing module
1 parent 303463d commit cceff11

6 files changed

Lines changed: 269 additions & 264 deletions

File tree

test/CMakeLists.txt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
include(get_cpm)
2-
CPMAddPackage("gh:catchorg/Catch2@3.5.4")
3-
list(APPEND CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/extras)
4-
2+
CPMAddPackage("gh:boost-ext/ut@2.3.1")
53
include(CTest)
6-
include(Catch)
74

8-
add_executable(${PROJECT_NAME}-test)
5+
add_executable(${PROJECT_NAME}-test main.cpp)
96
target_sources(${PROJECT_NAME}-test
107
PRIVATE
118
integer.test.cpp
@@ -16,9 +13,10 @@ target_sources(${PROJECT_NAME}-test
1613
target_link_libraries(${PROJECT_NAME}-test
1714
PRIVATE
1815
${PROJECT_NAME}
19-
Catch2::Catch2WithMain
16+
ut
2017
)
21-
catch_discover_tests(${PROJECT_NAME}-test)
18+
add_test(NAME ${PROJECT_NAME}-test COMMAND ${PROJECT_NAME}-test)
19+
2220

2321
# https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
2422
# https://embeddedartistry.com/blog/2023/09/20/leveraging-your-toolchain-to-improve-security/

test/integer.test.cpp

Lines changed: 69 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,17 @@
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

76
using namespace bitfilled;
7+
using namespace boost::ut;
88

99
template <std::endian ENDIAN>
1010
struct 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-
7315
template <std::endian ENDIAN, std::size_t SIZE, typename T = sized_unsigned_t<std::bit_ceil(SIZE)>>
7416
struct 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+
};

test/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int main() {}

test/mmreg.test.cpp

Lines changed: 65 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include <bit>
22
#include "bitfilled.hpp"
3-
#include <catch2/catch_test_macros.hpp>
3+
#include <boost/ut.hpp>
44
#include <type_traits>
55

66
using namespace bitfilled;
7+
using namespace boost::ut;
78

89
enum enumeration
910
{
@@ -43,69 +44,75 @@ struct mmr : public bitfilled::mmreg<std::uint8_t, ACCESS>
4344
};
4445
static_assert(sizeof(mmr<access::rw>) == sizeof(std::uint8_t));
4546

46-
TEST_CASE("mmregs assignment")
47+
suite mmreg = []
4748
{
48-
std::uint8_t v[static_cast<unsigned>(access::rw) + 1]{0, 42, 0, 0};
49-
volatile mmr<access::rw> rw1;
50-
auto& rw2 = reinterpret_cast<volatile mmr<access::rw>&>(v[static_cast<unsigned>(access::rw)]);
51-
auto& ro = reinterpret_cast<volatile mmr<access::r>&>(v[static_cast<unsigned>(access::r)]);
52-
auto& wo = reinterpret_cast<volatile mmr<access::w>&>(v[static_cast<unsigned>(access::w)]);
49+
"mmregs assignment"_test = []
50+
{
51+
std::uint8_t v[static_cast<unsigned>(access::rw) + 1]{0, 42, 0, 0};
52+
volatile mmr<access::rw> rw1;
53+
auto& rw2 =
54+
reinterpret_cast<volatile mmr<access::rw>&>(v[static_cast<unsigned>(access::rw)]);
55+
auto& ro = reinterpret_cast<volatile mmr<access::r>&>(v[static_cast<unsigned>(access::r)]);
56+
auto& wo = reinterpret_cast<volatile mmr<access::w>&>(v[static_cast<unsigned>(access::w)]);
5357

5458
#if BITFILLED_ASSIGN_RETURNS_REF
55-
wo = rw1 = rw2 = ro;
59+
wo = rw1 = rw2 = ro;
5660
#else
57-
rw2 = ro;
58-
rw1 = rw2;
59-
wo = rw1;
61+
rw2 = ro;
62+
rw1 = rw2;
63+
wo = rw1;
6064
#endif
61-
CHECK(ro == 42);
62-
CHECK(rw2 == 42);
63-
CHECK(rw1 == 42);
64-
}
65-
66-
TEST_CASE("mmregs field assignment")
67-
{
68-
std::uint8_t v[static_cast<unsigned>(access::rw) + 1]{};
69-
volatile mmr<access::rw> rw1;
70-
auto& rw2 = reinterpret_cast<volatile mmr<access::rw>&>(v[static_cast<unsigned>(access::rw)]);
71-
auto& ro = reinterpret_cast<volatile mmr<access::r>&>(v[static_cast<unsigned>(access::r)]);
72-
auto& wo = reinterpret_cast<volatile mmr<access::w>&>(v[static_cast<unsigned>(access::w)]);
73-
74-
v[static_cast<unsigned>(access::r)] = 3 << mmr<access::rw>::integer_t::offset();
75-
76-
int integer = ro.integer;
77-
CHECK(ro.integer == 3);
78-
CHECK(integer == 3);
79-
wo.integer = integer;
65+
expect(ro == 42);
66+
expect(rw2 == 42);
67+
expect(rw1 == 42);
68+
};
69+
70+
"mmregs field assignment"_test = []
71+
{
72+
std::uint8_t v[static_cast<unsigned>(access::rw) + 1]{};
73+
volatile mmr<access::rw> rw1;
74+
auto& rw2 =
75+
reinterpret_cast<volatile mmr<access::rw>&>(v[static_cast<unsigned>(access::rw)]);
76+
auto& ro = reinterpret_cast<volatile mmr<access::r>&>(v[static_cast<unsigned>(access::r)]);
77+
auto& wo = reinterpret_cast<volatile mmr<access::w>&>(v[static_cast<unsigned>(access::w)]);
78+
79+
v[static_cast<unsigned>(access::r)] = 3 << mmr<access::rw>::integer_t::offset();
80+
81+
int integer = ro.integer;
82+
expect(ro.integer == 3);
83+
expect(integer == 3);
84+
wo.integer = integer;
8085

8186
#if BITFILLED_ASSIGN_RETURNS_REF
82-
wo.integer = rw1.integer = rw2.integer = ro.integer;
87+
wo.integer = rw1.integer = rw2.integer = ro.integer;
8388
#else
84-
rw2.integer = ro.integer;
85-
rw1.integer = rw2.integer;
86-
wo.integer = rw1.integer;
89+
rw2.integer = ro.integer;
90+
rw1.integer = rw2.integer;
91+
wo.integer = rw1.integer;
8792
#endif
88-
CHECK(ro == (3 << ro.integer.offset()));
89-
CHECK(rw1 == (3 << rw1.integer.offset()));
90-
CHECK(rw2 == (3 << rw2.integer.offset()));
91-
}
92-
93-
TEST_CASE("mmregs reference")
94-
{
95-
std::uint8_t v[static_cast<unsigned>(access::rw) + 1]{};
96-
volatile mmr<access::rw> rw1;
97-
auto& rw2 = reinterpret_cast<volatile mmr<access::rw>&>(v[static_cast<unsigned>(access::rw)]);
98-
auto& ro = reinterpret_cast<volatile mmr<access::r>&>(v[static_cast<unsigned>(access::r)]);
99-
auto& wo = reinterpret_cast<volatile mmr<access::w>&>(v[static_cast<unsigned>(access::w)]);
100-
volatile std::uint8_t& raw_rw1 = rw1;
101-
volatile std::uint8_t& raw_rw2 = rw2;
102-
const volatile std::uint8_t& raw_ro = ro;
103-
104-
CHECK((std::uintptr_t)&raw_rw1 == (std::uintptr_t)&rw1);
105-
CHECK((std::uintptr_t)&raw_rw2 == (std::uintptr_t)&rw2);
106-
CHECK((std::uintptr_t)&raw_ro == (std::uintptr_t)&ro);
107-
raw_rw1 = 0xaa;
108-
CHECK(rw1 == 0xaa);
109-
wo = 0xaa;
110-
wo = ro;
111-
}
93+
expect(ro == (3 << ro.integer.offset()));
94+
expect(rw1 == (3 << rw1.integer.offset()));
95+
expect(rw2 == (3 << rw2.integer.offset()));
96+
};
97+
98+
"mmregs reference"_test = []
99+
{
100+
std::uint8_t v[static_cast<unsigned>(access::rw) + 1]{};
101+
volatile mmr<access::rw> rw1;
102+
auto& rw2 =
103+
reinterpret_cast<volatile mmr<access::rw>&>(v[static_cast<unsigned>(access::rw)]);
104+
auto& ro = reinterpret_cast<volatile mmr<access::r>&>(v[static_cast<unsigned>(access::r)]);
105+
auto& wo = reinterpret_cast<volatile mmr<access::w>&>(v[static_cast<unsigned>(access::w)]);
106+
volatile std::uint8_t& raw_rw1 = rw1;
107+
volatile std::uint8_t& raw_rw2 = rw2;
108+
const volatile std::uint8_t& raw_ro = ro;
109+
110+
expect((std::uintptr_t)&raw_rw1 == (std::uintptr_t)&rw1);
111+
expect((std::uintptr_t)&raw_rw2 == (std::uintptr_t)&rw2);
112+
expect((std::uintptr_t)&raw_ro == (std::uintptr_t)&ro);
113+
raw_rw1 = 0xaa;
114+
expect(rw1 == 0xaa);
115+
wo = 0xaa;
116+
wo = ro;
117+
};
118+
};

test/size.test.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
#include "bitfilled/size.hpp"
2-
#include <catch2/catch_test_macros.hpp>
2+
#include <boost/ut.hpp>
33

44
using namespace bitfilled;
5+
using namespace boost::ut;
56

6-
TEST_CASE("sizes")
7+
suite size = []
78
{
8-
CHECK(byte_width(255u) == 1);
9-
CHECK(byte_width(256u) == 2);
10-
CHECK(byte_width(0) == 1);
11-
CHECK(byte_width(-128) == 1);
12-
CHECK(byte_width(-129) == 2);
13-
}
9+
"byte_width"_test = []
10+
{
11+
expect(byte_width(255u) == 1_u);
12+
expect(byte_width(256u) == 2_u);
13+
expect(byte_width(0) == 1_u);
14+
expect(byte_width(-128) == 1_u);
15+
expect(byte_width(-129) == 2_u);
16+
};
17+
};

0 commit comments

Comments
 (0)