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