Skip to content

Commit d4cecb6

Browse files
committed
Add tombstone_traits tests
1 parent 9862f4a commit d4cecb6

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

source/bounded/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ target_sources(bounded_test PRIVATE
130130
test/conditional.cpp
131131
test/stream.cpp
132132
test/to_integer.cpp
133+
test/tombstone_traits.cpp
133134
)
134135

135136
target_link_libraries(bounded_test PUBLIC
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright David Stone 2024.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
export module bounded.test.tombstone_traits;
7+
8+
import bounded.arithmetic.unary_minus;
9+
import bounded.comparison;
10+
import bounded.integer;
11+
import bounded.literal;
12+
import bounded.tombstone_traits;
13+
14+
namespace {
15+
using namespace bounded::literal;
16+
17+
struct a {
18+
int m;
19+
};
20+
21+
struct b {
22+
constexpr b():
23+
m(1)
24+
{
25+
}
26+
a m;
27+
private:
28+
constexpr explicit b(bounded::tombstone_tag, auto const make):
29+
m(make())
30+
{
31+
}
32+
friend bounded::tombstone_traits_composer<&b::m>;
33+
};
34+
35+
} // namespace
36+
37+
template<>
38+
struct bounded::tombstone_traits<a> {
39+
static constexpr auto spare_representations = 1_bi;
40+
static constexpr auto make(bounded::constant_t<0>) noexcept -> a {
41+
return a(0);
42+
}
43+
static constexpr auto index(a const & value) noexcept -> bounded::integer<-1, 0> {
44+
if (value.m == 0) {
45+
return 0_bi;
46+
}
47+
return -1_bi;
48+
}
49+
};
50+
51+
template<>
52+
struct bounded::tombstone_traits<b> : bounded::tombstone_traits_composer<&b::m> {
53+
};
54+
55+
namespace {
56+
57+
using a_traits = bounded::tombstone_traits<a>;
58+
static_assert(a_traits::spare_representations == 1_bi);
59+
static_assert(a_traits::make(0_bi).m == 0);
60+
static_assert(a_traits::index(a(0)) == 0_bi);
61+
static_assert(a_traits::index(a(1)) == -1_bi);
62+
63+
using b_traits = bounded::tombstone_traits<b>;
64+
static_assert(b_traits::spare_representations == 1_bi);
65+
static_assert(b_traits::make(0_bi).m.m == 0);
66+
static_assert(b_traits::index(b_traits::make(0_bi)) == 0_bi);
67+
static_assert(b_traits::index(b()) == -1_bi);
68+
69+
} // namespace

0 commit comments

Comments
 (0)