rippled
Loading...
Searching...
No Matches
tagged_integer.cpp
1#include <xrpl/basics/tagged_integer.h>
2
3#include <doctest/doctest.h>
4
5#include <type_traits>
6
7using namespace ripple;
8
9struct Tag1
10{
11};
12struct Tag2
13{
14};
15
16// Static checks that types are not interoperable
17
21
22// Check construction of tagged_integers
23static_assert(
25 "TagUInt1 should be constructible using a std::uint32_t");
26
27static_assert(
29 "TagUInt1 should not be constructible using a std::uint64_t");
30
31static_assert(
33 "TagUInt3 should be constructible using a std::uint32_t");
34
35static_assert(
37 "TagUInt3 should be constructible using a std::uint64_t");
38
39// Check assignment of tagged_integers
40static_assert(
42 "TagUInt1 should not be assignable with a std::uint32_t");
43
44static_assert(
46 "TagUInt1 should not be assignable with a std::uint64_t");
47
48static_assert(
50 "TagUInt3 should not be assignable with a std::uint32_t");
51
52static_assert(
54 "TagUInt3 should not be assignable with a std::uint64_t");
55
56static_assert(
58 "TagUInt1 should be assignable with a TagUInt1");
59
60static_assert(
62 "TagUInt1 should not be assignable with a TagUInt2");
63
64static_assert(
66 "TagUInt3 should be assignable with a TagUInt1");
67
68static_assert(
70 "TagUInt1 should not be assignable with a TagUInt3");
71
72static_assert(
74 "TagUInt3 should not be assignable with a TagUInt1");
75
76// Check convertibility of tagged_integers
77static_assert(
79 "std::uint32_t should not be convertible to a TagUInt1");
80
81static_assert(
83 "std::uint32_t should not be convertible to a TagUInt3");
84
85static_assert(
87 "std::uint64_t should not be convertible to a TagUInt3");
88
89static_assert(
91 "std::uint64_t should not be convertible to a TagUInt2");
92
93static_assert(
95 "TagUInt1 should not be convertible to TagUInt2");
96
97static_assert(
99 "TagUInt1 should not be convertible to TagUInt3");
100
101static_assert(
103 "TagUInt2 should not be convertible to a TagUInt3");
104
105TEST_SUITE_BEGIN("tagged_integer");
106
108
109TEST_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
135TEST_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
150TEST_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
170TEST_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
228TEST_SUITE_END();
A type-safe wrap around standard integral types.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
constexpr Number one
Definition Number.cpp:156