rippled
Loading...
Searching...
No Matches
SeqProxy_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2018 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/beast/unit_test.h>
21#include <xrpl/protocol/SeqProxy.h>
22
23#include <limits>
24#include <sstream>
25
26namespace ripple {
27
29{
30 // Exercise value(), isSeq(), and isTicket().
31 static constexpr bool
33 {
34 bool const expectSeq{type == SeqProxy::seq};
35 return (seqProx.value() == value) && (seqProx.isSeq() == expectSeq) &&
36 (seqProx.isTicket() == !expectSeq);
37 }
38
39 // Exercise all SeqProxy comparison operators expecting lhs < rhs.
40 static constexpr bool
42 {
43 return (lhs < rhs) && (lhs <= rhs) && (!(lhs == rhs)) && (lhs != rhs) &&
44 (!(lhs >= rhs)) && (!(lhs > rhs));
45 }
46
47 // Exercise all SeqProxy comparison operators expecting lhs == rhs.
48 static constexpr bool
50 {
51 return (!(lhs < rhs)) && (lhs <= rhs) && (lhs == rhs) &&
52 (!(lhs != rhs)) && (lhs >= rhs) && (!(lhs > rhs));
53 }
54
55 // Exercise all SeqProxy comparison operators expecting lhs > rhs.
56 static constexpr bool
58 {
59 return (!(lhs < rhs)) && (!(lhs <= rhs)) && (!(lhs == rhs)) &&
60 (lhs != rhs) && (lhs >= rhs) && (lhs > rhs);
61 }
62
63 // Verify streaming.
64 bool
66 {
67 std::string const type{seqProx.isSeq() ? "sequence" : "ticket"};
68 std::string const value{std::to_string(seqProx.value())};
69
71 ss << seqProx;
72 std::string str{ss.str()};
73
74 return str.find(type) == 0 && str[type.size()] == ' ' &&
75 str.find(value) == (type.size() + 1);
76 }
77
78 void
79 run() override
80 {
81 // While SeqProxy supports values of zero, they are not
82 // expected in the wild. Nevertheless they are tested here.
83 // But so are values of 1, which are expected to occur in the wild.
84 static constexpr std::uint32_t uintMax{
86 static constexpr SeqProxy::Type seq{SeqProxy::seq};
87 static constexpr SeqProxy::Type ticket{SeqProxy::ticket};
88
89 static constexpr SeqProxy seqZero{seq, 0};
90 static constexpr SeqProxy seqSmall{seq, 1};
91 static constexpr SeqProxy seqMid0{seq, 2};
92 static constexpr SeqProxy seqMid1{seqMid0};
93 static constexpr SeqProxy seqBig{seq, uintMax};
94
95 static constexpr SeqProxy ticZero{ticket, 0};
96 static constexpr SeqProxy ticSmall{ticket, 1};
97 static constexpr SeqProxy ticMid0{ticket, 2};
98 static constexpr SeqProxy ticMid1{ticMid0};
99 static constexpr SeqProxy ticBig{ticket, uintMax};
100
101 // Verify operation of value(), isSeq() and isTicket().
102 static_assert(expectValues(seqZero, 0, seq), "");
103 static_assert(expectValues(seqSmall, 1, seq), "");
104 static_assert(expectValues(seqMid0, 2, seq), "");
105 static_assert(expectValues(seqMid1, 2, seq), "");
106 static_assert(expectValues(seqBig, uintMax, seq), "");
107
108 static_assert(expectValues(ticZero, 0, ticket), "");
109 static_assert(expectValues(ticSmall, 1, ticket), "");
110 static_assert(expectValues(ticMid0, 2, ticket), "");
111 static_assert(expectValues(ticMid1, 2, ticket), "");
112 static_assert(expectValues(ticBig, uintMax, ticket), "");
113
114 // Verify expected behavior of comparison operators.
115 static_assert(expectEq(seqZero, seqZero), "");
116 static_assert(expectLt(seqZero, seqSmall), "");
117 static_assert(expectLt(seqZero, seqMid0), "");
118 static_assert(expectLt(seqZero, seqMid1), "");
119 static_assert(expectLt(seqZero, seqBig), "");
120 static_assert(expectLt(seqZero, ticZero), "");
121 static_assert(expectLt(seqZero, ticSmall), "");
122 static_assert(expectLt(seqZero, ticMid0), "");
123 static_assert(expectLt(seqZero, ticMid1), "");
124 static_assert(expectLt(seqZero, ticBig), "");
125
126 static_assert(expectGt(seqSmall, seqZero), "");
127 static_assert(expectEq(seqSmall, seqSmall), "");
128 static_assert(expectLt(seqSmall, seqMid0), "");
129 static_assert(expectLt(seqSmall, seqMid1), "");
130 static_assert(expectLt(seqSmall, seqBig), "");
131 static_assert(expectLt(seqSmall, ticZero), "");
132 static_assert(expectLt(seqSmall, ticSmall), "");
133 static_assert(expectLt(seqSmall, ticMid0), "");
134 static_assert(expectLt(seqSmall, ticMid1), "");
135 static_assert(expectLt(seqSmall, ticBig), "");
136
137 static_assert(expectGt(seqMid0, seqZero), "");
138 static_assert(expectGt(seqMid0, seqSmall), "");
139 static_assert(expectEq(seqMid0, seqMid0), "");
140 static_assert(expectEq(seqMid0, seqMid1), "");
141 static_assert(expectLt(seqMid0, seqBig), "");
142 static_assert(expectLt(seqMid0, ticZero), "");
143 static_assert(expectLt(seqMid0, ticSmall), "");
144 static_assert(expectLt(seqMid0, ticMid0), "");
145 static_assert(expectLt(seqMid0, ticMid1), "");
146 static_assert(expectLt(seqMid0, ticBig), "");
147
148 static_assert(expectGt(seqMid1, seqZero), "");
149 static_assert(expectGt(seqMid1, seqSmall), "");
150 static_assert(expectEq(seqMid1, seqMid0), "");
151 static_assert(expectEq(seqMid1, seqMid1), "");
152 static_assert(expectLt(seqMid1, seqBig), "");
153 static_assert(expectLt(seqMid1, ticZero), "");
154 static_assert(expectLt(seqMid1, ticSmall), "");
155 static_assert(expectLt(seqMid1, ticMid0), "");
156 static_assert(expectLt(seqMid1, ticMid1), "");
157 static_assert(expectLt(seqMid1, ticBig), "");
158
159 static_assert(expectGt(seqBig, seqZero), "");
160 static_assert(expectGt(seqBig, seqSmall), "");
161 static_assert(expectGt(seqBig, seqMid0), "");
162 static_assert(expectGt(seqBig, seqMid1), "");
163 static_assert(expectEq(seqBig, seqBig), "");
164 static_assert(expectLt(seqBig, ticZero), "");
165 static_assert(expectLt(seqBig, ticSmall), "");
166 static_assert(expectLt(seqBig, ticMid0), "");
167 static_assert(expectLt(seqBig, ticMid1), "");
168 static_assert(expectLt(seqBig, ticBig), "");
169
170 static_assert(expectGt(ticZero, seqZero), "");
171 static_assert(expectGt(ticZero, seqSmall), "");
172 static_assert(expectGt(ticZero, seqMid0), "");
173 static_assert(expectGt(ticZero, seqMid1), "");
174 static_assert(expectGt(ticZero, seqBig), "");
175 static_assert(expectEq(ticZero, ticZero), "");
176 static_assert(expectLt(ticZero, ticSmall), "");
177 static_assert(expectLt(ticZero, ticMid0), "");
178 static_assert(expectLt(ticZero, ticMid1), "");
179 static_assert(expectLt(ticZero, ticBig), "");
180
181 static_assert(expectGt(ticSmall, seqZero), "");
182 static_assert(expectGt(ticSmall, seqSmall), "");
183 static_assert(expectGt(ticSmall, seqMid0), "");
184 static_assert(expectGt(ticSmall, seqMid1), "");
185 static_assert(expectGt(ticSmall, seqBig), "");
186 static_assert(expectGt(ticSmall, ticZero), "");
187 static_assert(expectEq(ticSmall, ticSmall), "");
188 static_assert(expectLt(ticSmall, ticMid0), "");
189 static_assert(expectLt(ticSmall, ticMid1), "");
190 static_assert(expectLt(ticSmall, ticBig), "");
191
192 static_assert(expectGt(ticMid0, seqZero), "");
193 static_assert(expectGt(ticMid0, seqSmall), "");
194 static_assert(expectGt(ticMid0, seqMid0), "");
195 static_assert(expectGt(ticMid0, seqMid1), "");
196 static_assert(expectGt(ticMid0, seqBig), "");
197 static_assert(expectGt(ticMid0, ticZero), "");
198 static_assert(expectGt(ticMid0, ticSmall), "");
199 static_assert(expectEq(ticMid0, ticMid0), "");
200 static_assert(expectEq(ticMid0, ticMid1), "");
201 static_assert(expectLt(ticMid0, ticBig), "");
202
203 static_assert(expectGt(ticMid1, seqZero), "");
204 static_assert(expectGt(ticMid1, seqSmall), "");
205 static_assert(expectGt(ticMid1, seqMid0), "");
206 static_assert(expectGt(ticMid1, seqMid1), "");
207 static_assert(expectGt(ticMid1, seqBig), "");
208 static_assert(expectGt(ticMid1, ticZero), "");
209 static_assert(expectGt(ticMid1, ticSmall), "");
210 static_assert(expectEq(ticMid1, ticMid0), "");
211 static_assert(expectEq(ticMid1, ticMid1), "");
212 static_assert(expectLt(ticMid1, ticBig), "");
213
214 static_assert(expectGt(ticBig, seqZero), "");
215 static_assert(expectGt(ticBig, seqSmall), "");
216 static_assert(expectGt(ticBig, seqMid0), "");
217 static_assert(expectGt(ticBig, seqMid1), "");
218 static_assert(expectGt(ticBig, seqBig), "");
219 static_assert(expectGt(ticBig, ticZero), "");
220 static_assert(expectGt(ticBig, ticSmall), "");
221 static_assert(expectGt(ticBig, ticMid0), "");
222 static_assert(expectGt(ticBig, ticMid1), "");
223 static_assert(expectEq(ticBig, ticBig), "");
224
225 // Verify streaming.
226 BEAST_EXPECT(streamTest(seqZero));
227 BEAST_EXPECT(streamTest(seqSmall));
228 BEAST_EXPECT(streamTest(seqMid0));
229 BEAST_EXPECT(streamTest(seqMid1));
230 BEAST_EXPECT(streamTest(seqBig));
231 BEAST_EXPECT(streamTest(ticZero));
232 BEAST_EXPECT(streamTest(ticSmall));
233 BEAST_EXPECT(streamTest(ticMid0));
234 BEAST_EXPECT(streamTest(ticMid1));
235 BEAST_EXPECT(streamTest(ticBig));
236 }
237};
238
239BEAST_DEFINE_TESTSUITE(SeqProxy, protocol, ripple);
240
241} // namespace ripple
A testsuite class.
Definition suite.h:55
A type that represents either a sequence value or a ticket value.
Definition SeqProxy.h:56
constexpr bool isSeq() const
Definition SeqProxy.h:88
constexpr std::uint32_t value() const
Definition SeqProxy.h:82
constexpr bool isTicket() const
Definition SeqProxy.h:94
T max(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
T str(T... args)
bool streamTest(SeqProxy seqProx)
static constexpr bool expectLt(SeqProxy lhs, SeqProxy rhs)
static constexpr bool expectValues(SeqProxy seqProx, std::uint32_t value, SeqProxy::Type type)
static constexpr bool expectEq(SeqProxy lhs, SeqProxy rhs)
static constexpr bool expectGt(SeqProxy lhs, SeqProxy rhs)
void run() override
Runs the suite.
T to_string(T... args)