rippled
Loading...
Searching...
No Matches
tests
libxrpl
basics
tagged_integer.cpp
1
#include <xrpl/basics/tagged_integer.h>
2
3
#include <doctest/doctest.h>
4
5
#include <
type_traits
>
6
7
using namespace
ripple
;
8
9
struct
Tag1
10
{
11
};
12
struct
Tag2
13
{
14
};
15
16
// Static checks that types are not interoperable
17
18
using
TagUInt1
=
tagged_integer<std::uint32_t, Tag1>
;
19
using
TagUInt2
=
tagged_integer<std::uint32_t, Tag2>
;
20
using
TagUInt3
=
tagged_integer<std::uint64_t, Tag1>
;
21
22
// Check construction of tagged_integers
23
static_assert
(
24
std::is_constructible<TagUInt1, std::uint32_t>::value
,
25
"TagUInt1 should be constructible using a std::uint32_t"
);
26
27
static_assert
(
28
!
std::is_constructible<TagUInt1, std::uint64_t>::value
,
29
"TagUInt1 should not be constructible using a std::uint64_t"
);
30
31
static_assert
(
32
std::is_constructible<TagUInt3, std::uint32_t>::value
,
33
"TagUInt3 should be constructible using a std::uint32_t"
);
34
35
static_assert
(
36
std::is_constructible<TagUInt3, std::uint64_t>::value
,
37
"TagUInt3 should be constructible using a std::uint64_t"
);
38
39
// Check assignment of tagged_integers
40
static_assert
(
41
!
std::is_assignable<TagUInt1, std::uint32_t>::value
,
42
"TagUInt1 should not be assignable with a std::uint32_t"
);
43
44
static_assert
(
45
!
std::is_assignable<TagUInt1, std::uint64_t>::value
,
46
"TagUInt1 should not be assignable with a std::uint64_t"
);
47
48
static_assert
(
49
!
std::is_assignable<TagUInt3, std::uint32_t>::value
,
50
"TagUInt3 should not be assignable with a std::uint32_t"
);
51
52
static_assert
(
53
!
std::is_assignable<TagUInt3, std::uint64_t>::value
,
54
"TagUInt3 should not be assignable with a std::uint64_t"
);
55
56
static_assert
(
57
std::is_assignable<TagUInt1, TagUInt1>::value
,
58
"TagUInt1 should be assignable with a TagUInt1"
);
59
60
static_assert
(
61
!
std::is_assignable<TagUInt1, TagUInt2>::value
,
62
"TagUInt1 should not be assignable with a TagUInt2"
);
63
64
static_assert
(
65
std::is_assignable<TagUInt3, TagUInt3>::value
,
66
"TagUInt3 should be assignable with a TagUInt1"
);
67
68
static_assert
(
69
!
std::is_assignable<TagUInt1, TagUInt3>::value
,
70
"TagUInt1 should not be assignable with a TagUInt3"
);
71
72
static_assert
(
73
!
std::is_assignable<TagUInt3, TagUInt1>::value
,
74
"TagUInt3 should not be assignable with a TagUInt1"
);
75
76
// Check convertibility of tagged_integers
77
static_assert
(
78
!
std::is_convertible<std::uint32_t, TagUInt1>::value
,
79
"std::uint32_t should not be convertible to a TagUInt1"
);
80
81
static_assert
(
82
!
std::is_convertible<std::uint32_t, TagUInt3>::value
,
83
"std::uint32_t should not be convertible to a TagUInt3"
);
84
85
static_assert
(
86
!
std::is_convertible<std::uint64_t, TagUInt3>::value
,
87
"std::uint64_t should not be convertible to a TagUInt3"
);
88
89
static_assert
(
90
!
std::is_convertible<std::uint64_t, TagUInt2>::value
,
91
"std::uint64_t should not be convertible to a TagUInt2"
);
92
93
static_assert
(
94
!
std::is_convertible<TagUInt1, TagUInt2>::value
,
95
"TagUInt1 should not be convertible to TagUInt2"
);
96
97
static_assert
(
98
!
std::is_convertible<TagUInt1, TagUInt3>::value
,
99
"TagUInt1 should not be convertible to TagUInt3"
);
100
101
static_assert
(
102
!
std::is_convertible<TagUInt2, TagUInt3>::value
,
103
"TagUInt2 should not be convertible to a TagUInt3"
);
104
105
TEST_SUITE_BEGIN(
"tagged_integer"
);
106
107
using
TagInt
=
tagged_integer<std::int32_t, Tag1>
;
108
109
TEST_CASE(
"comparison operators"
)
110
{
111
TagInt
const
zero(0);
112
TagInt
const
one
(1);
113
114
CHECK
(
one
==
one
);
115
CHECK
(!(
one
== zero));
116
117
CHECK
(
one
!= zero);
118
CHECK
(!(
one
!=
one
));
119
120
CHECK
(zero <
one
);
121
CHECK
(!(
one
< zero));
122
123
CHECK
(
one
> zero);
124
CHECK
(!(zero >
one
));
125
126
CHECK
(
one
>=
one
);
127
CHECK
(
one
>= zero);
128
CHECK
(!(zero >=
one
));
129
130
CHECK
(zero <=
one
);
131
CHECK
(zero <= zero);
132
CHECK
(!(
one
<= zero));
133
}
134
135
TEST_CASE(
"increment / decrement operators"
)
136
{
137
TagInt
const
zero(0);
138
TagInt
const
one
(1);
139
TagInt
a{0};
140
++a;
141
CHECK
(a ==
one
);
142
--a;
143
CHECK
(a == zero);
144
a++;
145
CHECK
(a ==
one
);
146
a--;
147
CHECK
(a == zero);
148
}
149
150
TEST_CASE(
"arithmetic operators"
)
151
{
152
TagInt
a{-2};
153
CHECK
(+a ==
TagInt
{-2});
154
CHECK
(-a ==
TagInt
{2});
155
CHECK
(
TagInt
{-3} +
TagInt
{4} ==
TagInt
{1});
156
CHECK
(
TagInt
{-3} -
TagInt
{4} ==
TagInt
{-7});
157
CHECK
(
TagInt
{-3} *
TagInt
{4} ==
TagInt
{-12});
158
CHECK
(
TagInt
{8} /
TagInt
{4} ==
TagInt
{2});
159
CHECK
(
TagInt
{7} %
TagInt
{4} ==
TagInt
{3});
160
161
CHECK
(~
TagInt
{8} ==
TagInt
{
~TagInt
::value_type{8}});
162
CHECK
((
TagInt
{6} &
TagInt
{3}) ==
TagInt
{2});
163
CHECK
((
TagInt
{6} |
TagInt
{3}) ==
TagInt
{7});
164
CHECK
((
TagInt
{6} ^
TagInt
{3}) ==
TagInt
{5});
165
166
CHECK
((
TagInt
{4} <<
TagInt
{2}) ==
TagInt
{16});
167
CHECK
((
TagInt
{16} >>
TagInt
{2}) ==
TagInt
{4});
168
}
169
170
TEST_CASE(
"assignment operators"
)
171
{
172
TagInt
a{-2};
173
TagInt
b{0};
174
b = a;
175
CHECK
(b ==
TagInt
{-2});
176
177
// -3 + 4 == 1
178
a =
TagInt
{-3};
179
a +=
TagInt
{4};
180
CHECK
(a ==
TagInt
{1});
181
182
// -3 - 4 == -7
183
a =
TagInt
{-3};
184
a -=
TagInt
{4};
185
CHECK
(a ==
TagInt
{-7});
186
187
// -3 * 4 == -12
188
a =
TagInt
{-3};
189
a *=
TagInt
{4};
190
CHECK
(a ==
TagInt
{-12});
191
192
// 8/4 == 2
193
a =
TagInt
{8};
194
a /=
TagInt
{4};
195
CHECK
(a ==
TagInt
{2});
196
197
// 7 % 4 == 3
198
a =
TagInt
{7};
199
a %=
TagInt
{4};
200
CHECK
(a ==
TagInt
{3});
201
202
// 6 & 3 == 2
203
a =
TagInt
{6};
204
a /=
TagInt
{3};
205
CHECK
(a ==
TagInt
{2});
206
207
// 6 | 3 == 7
208
a =
TagInt
{6};
209
a |=
TagInt
{3};
210
CHECK
(a ==
TagInt
{7});
211
212
// 6 ^ 3 == 5
213
a =
TagInt
{6};
214
a ^=
TagInt
{3};
215
CHECK
(a ==
TagInt
{5});
216
217
// 4 << 2 == 16
218
a =
TagInt
{4};
219
a <<=
TagInt
{2};
220
CHECK
(a ==
TagInt
{16});
221
222
// 16 >> 2 == 4
223
a =
TagInt
{16};
224
a >>=
TagInt
{2};
225
CHECK
(a ==
TagInt
{4});
226
}
227
228
TEST_SUITE_END();
ripple::tagged_integer
A type-safe wrap around standard integral types.
Definition
tagged_integer.h:37
std::is_assignable
std::is_constructible
std::is_convertible
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:6
ripple::one
constexpr Number one
Definition
Number.cpp:156
ripple::LedgerNameSpace::CHECK
@ CHECK
Tag1
Definition
tagged_integer.cpp:10
Tag2
Definition
tagged_integer.cpp:13
type_traits
Generated by
1.9.8