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#include <limits>
23#include <sstream>
24
25namespace ripple {
26
28{
29 // Exercise value(), isSeq(), and isTicket().
30 static constexpr bool
32 {
33 bool const expectSeq{type == SeqProxy::seq};
34 return (seqProx.value() == value) && (seqProx.isSeq() == expectSeq) &&
35 (seqProx.isTicket() == !expectSeq);
36 }
37
38 // Exercise all SeqProxy comparison operators expecting lhs < rhs.
39 static constexpr bool
41 {
42 return (lhs < rhs) && (lhs <= rhs) && (!(lhs == rhs)) && (lhs != rhs) &&
43 (!(lhs >= rhs)) && (!(lhs > rhs));
44 }
45
46 // Exercise all SeqProxy comparison operators expecting lhs == rhs.
47 static constexpr bool
49 {
50 return (!(lhs < rhs)) && (lhs <= rhs) && (lhs == rhs) &&
51 (!(lhs != rhs)) && (lhs >= rhs) && (!(lhs > rhs));
52 }
53
54 // Exercise all SeqProxy comparison operators expecting lhs > rhs.
55 static constexpr bool
57 {
58 return (!(lhs < rhs)) && (!(lhs <= rhs)) && (!(lhs == rhs)) &&
59 (lhs != rhs) && (lhs >= rhs) && (lhs > rhs);
60 }
61
62 // Verify streaming.
63 bool
65 {
66 std::string const type{seqProx.isSeq() ? "sequence" : "ticket"};
67 std::string const value{std::to_string(seqProx.value())};
68
70 ss << seqProx;
71 std::string str{ss.str()};
72
73 return str.find(type) == 0 && str[type.size()] == ' ' &&
74 str.find(value) == (type.size() + 1);
75 }
76
77 void
78 run() override
79 {
80 // While SeqProxy supports values of zero, they are not
81 // expected in the wild. Nevertheless they are tested here.
82 // But so are values of 1, which are expected to occur in the wild.
83 static constexpr std::uint32_t uintMax{
85 static constexpr SeqProxy::Type seq{SeqProxy::seq};
86 static constexpr SeqProxy::Type ticket{SeqProxy::ticket};
87
88 static constexpr SeqProxy seqZero{seq, 0};
89 static constexpr SeqProxy seqSmall{seq, 1};
90 static constexpr SeqProxy seqMid0{seq, 2};
91 static constexpr SeqProxy seqMid1{seqMid0};
92 static constexpr SeqProxy seqBig{seq, uintMax};
93
94 static constexpr SeqProxy ticZero{ticket, 0};
95 static constexpr SeqProxy ticSmall{ticket, 1};
96 static constexpr SeqProxy ticMid0{ticket, 2};
97 static constexpr SeqProxy ticMid1{ticMid0};
98 static constexpr SeqProxy ticBig{ticket, uintMax};
99
100 // Verify operation of value(), isSeq() and isTicket().
101 static_assert(expectValues(seqZero, 0, seq), "");
102 static_assert(expectValues(seqSmall, 1, seq), "");
103 static_assert(expectValues(seqMid0, 2, seq), "");
104 static_assert(expectValues(seqMid1, 2, seq), "");
105 static_assert(expectValues(seqBig, uintMax, seq), "");
106
107 static_assert(expectValues(ticZero, 0, ticket), "");
108 static_assert(expectValues(ticSmall, 1, ticket), "");
109 static_assert(expectValues(ticMid0, 2, ticket), "");
110 static_assert(expectValues(ticMid1, 2, ticket), "");
111 static_assert(expectValues(ticBig, uintMax, ticket), "");
112
113 // Verify expected behavior of comparison operators.
114 static_assert(expectEq(seqZero, seqZero), "");
115 static_assert(expectLt(seqZero, seqSmall), "");
116 static_assert(expectLt(seqZero, seqMid0), "");
117 static_assert(expectLt(seqZero, seqMid1), "");
118 static_assert(expectLt(seqZero, seqBig), "");
119 static_assert(expectLt(seqZero, ticZero), "");
120 static_assert(expectLt(seqZero, ticSmall), "");
121 static_assert(expectLt(seqZero, ticMid0), "");
122 static_assert(expectLt(seqZero, ticMid1), "");
123 static_assert(expectLt(seqZero, ticBig), "");
124
125 static_assert(expectGt(seqSmall, seqZero), "");
126 static_assert(expectEq(seqSmall, seqSmall), "");
127 static_assert(expectLt(seqSmall, seqMid0), "");
128 static_assert(expectLt(seqSmall, seqMid1), "");
129 static_assert(expectLt(seqSmall, seqBig), "");
130 static_assert(expectLt(seqSmall, ticZero), "");
131 static_assert(expectLt(seqSmall, ticSmall), "");
132 static_assert(expectLt(seqSmall, ticMid0), "");
133 static_assert(expectLt(seqSmall, ticMid1), "");
134 static_assert(expectLt(seqSmall, ticBig), "");
135
136 static_assert(expectGt(seqMid0, seqZero), "");
137 static_assert(expectGt(seqMid0, seqSmall), "");
138 static_assert(expectEq(seqMid0, seqMid0), "");
139 static_assert(expectEq(seqMid0, seqMid1), "");
140 static_assert(expectLt(seqMid0, seqBig), "");
141 static_assert(expectLt(seqMid0, ticZero), "");
142 static_assert(expectLt(seqMid0, ticSmall), "");
143 static_assert(expectLt(seqMid0, ticMid0), "");
144 static_assert(expectLt(seqMid0, ticMid1), "");
145 static_assert(expectLt(seqMid0, ticBig), "");
146
147 static_assert(expectGt(seqMid1, seqZero), "");
148 static_assert(expectGt(seqMid1, seqSmall), "");
149 static_assert(expectEq(seqMid1, seqMid0), "");
150 static_assert(expectEq(seqMid1, seqMid1), "");
151 static_assert(expectLt(seqMid1, seqBig), "");
152 static_assert(expectLt(seqMid1, ticZero), "");
153 static_assert(expectLt(seqMid1, ticSmall), "");
154 static_assert(expectLt(seqMid1, ticMid0), "");
155 static_assert(expectLt(seqMid1, ticMid1), "");
156 static_assert(expectLt(seqMid1, ticBig), "");
157
158 static_assert(expectGt(seqBig, seqZero), "");
159 static_assert(expectGt(seqBig, seqSmall), "");
160 static_assert(expectGt(seqBig, seqMid0), "");
161 static_assert(expectGt(seqBig, seqMid1), "");
162 static_assert(expectEq(seqBig, seqBig), "");
163 static_assert(expectLt(seqBig, ticZero), "");
164 static_assert(expectLt(seqBig, ticSmall), "");
165 static_assert(expectLt(seqBig, ticMid0), "");
166 static_assert(expectLt(seqBig, ticMid1), "");
167 static_assert(expectLt(seqBig, ticBig), "");
168
169 static_assert(expectGt(ticZero, seqZero), "");
170 static_assert(expectGt(ticZero, seqSmall), "");
171 static_assert(expectGt(ticZero, seqMid0), "");
172 static_assert(expectGt(ticZero, seqMid1), "");
173 static_assert(expectGt(ticZero, seqBig), "");
174 static_assert(expectEq(ticZero, ticZero), "");
175 static_assert(expectLt(ticZero, ticSmall), "");
176 static_assert(expectLt(ticZero, ticMid0), "");
177 static_assert(expectLt(ticZero, ticMid1), "");
178 static_assert(expectLt(ticZero, ticBig), "");
179
180 static_assert(expectGt(ticSmall, seqZero), "");
181 static_assert(expectGt(ticSmall, seqSmall), "");
182 static_assert(expectGt(ticSmall, seqMid0), "");
183 static_assert(expectGt(ticSmall, seqMid1), "");
184 static_assert(expectGt(ticSmall, seqBig), "");
185 static_assert(expectGt(ticSmall, ticZero), "");
186 static_assert(expectEq(ticSmall, ticSmall), "");
187 static_assert(expectLt(ticSmall, ticMid0), "");
188 static_assert(expectLt(ticSmall, ticMid1), "");
189 static_assert(expectLt(ticSmall, ticBig), "");
190
191 static_assert(expectGt(ticMid0, seqZero), "");
192 static_assert(expectGt(ticMid0, seqSmall), "");
193 static_assert(expectGt(ticMid0, seqMid0), "");
194 static_assert(expectGt(ticMid0, seqMid1), "");
195 static_assert(expectGt(ticMid0, seqBig), "");
196 static_assert(expectGt(ticMid0, ticZero), "");
197 static_assert(expectGt(ticMid0, ticSmall), "");
198 static_assert(expectEq(ticMid0, ticMid0), "");
199 static_assert(expectEq(ticMid0, ticMid1), "");
200 static_assert(expectLt(ticMid0, ticBig), "");
201
202 static_assert(expectGt(ticMid1, seqZero), "");
203 static_assert(expectGt(ticMid1, seqSmall), "");
204 static_assert(expectGt(ticMid1, seqMid0), "");
205 static_assert(expectGt(ticMid1, seqMid1), "");
206 static_assert(expectGt(ticMid1, seqBig), "");
207 static_assert(expectGt(ticMid1, ticZero), "");
208 static_assert(expectGt(ticMid1, ticSmall), "");
209 static_assert(expectEq(ticMid1, ticMid0), "");
210 static_assert(expectEq(ticMid1, ticMid1), "");
211 static_assert(expectLt(ticMid1, ticBig), "");
212
213 static_assert(expectGt(ticBig, seqZero), "");
214 static_assert(expectGt(ticBig, seqSmall), "");
215 static_assert(expectGt(ticBig, seqMid0), "");
216 static_assert(expectGt(ticBig, seqMid1), "");
217 static_assert(expectGt(ticBig, seqBig), "");
218 static_assert(expectGt(ticBig, ticZero), "");
219 static_assert(expectGt(ticBig, ticSmall), "");
220 static_assert(expectGt(ticBig, ticMid0), "");
221 static_assert(expectGt(ticBig, ticMid1), "");
222 static_assert(expectEq(ticBig, ticBig), "");
223
224 // Verify streaming.
225 BEAST_EXPECT(streamTest(seqZero));
226 BEAST_EXPECT(streamTest(seqSmall));
227 BEAST_EXPECT(streamTest(seqMid0));
228 BEAST_EXPECT(streamTest(seqMid1));
229 BEAST_EXPECT(streamTest(seqBig));
230 BEAST_EXPECT(streamTest(ticZero));
231 BEAST_EXPECT(streamTest(ticSmall));
232 BEAST_EXPECT(streamTest(ticMid0));
233 BEAST_EXPECT(streamTest(ticMid1));
234 BEAST_EXPECT(streamTest(ticBig));
235 }
236};
237
238BEAST_DEFINE_TESTSUITE(SeqProxy, protocol, ripple);
239
240} // namespace ripple
A testsuite class.
Definition: suite.h:53
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:26
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)