20#include <xrpl/beast/core/LexicalCast.h>
21#include <xrpl/beast/unit_test.h>
22#include <xrpl/beast/xor_shift_engine.h>
29 template <
class IntType>
33 return static_cast<IntType
>(r());
36 template <
class IntType>
48 template <
class IntType>
54 ss <<
"random " <<
typeid(IntType).name();
57 for (
int i = 0; i < 1000; ++i)
59 IntType
const value(nextRandomInt<IntType>(r));
66 ss <<
"numeric_limits <" <<
typeid(IntType).name() <<
">";
80 lexicalCastThrow<int>(
"\xef\xbc\x91\xef\xbc\x90");
101 tryBadConvert<std::uint64_t>(
"99999999999999999999");
102 tryBadConvert<std::uint32_t>(
"4294967300");
103 tryBadConvert<std::uint16_t>(
"75821");
111 tryBadConvert<std::uint32_t>(
"-1");
113 tryBadConvert<std::int64_t>(
"-99999999999999999999");
114 tryBadConvert<std::int32_t>(
"-4294967300");
115 tryBadConvert<std::int16_t>(
"-75821");
137 expect(tryEdgeCase<std::uint64_t>(
"18446744073709551614"));
138 expect(tryEdgeCase<std::uint64_t>(
"18446744073709551615"));
139 expect(!tryEdgeCase<std::uint64_t>(
"18446744073709551616"));
141 expect(tryEdgeCase<std::int64_t>(
"9223372036854775806"));
142 expect(tryEdgeCase<std::int64_t>(
"9223372036854775807"));
143 expect(!tryEdgeCase<std::int64_t>(
"9223372036854775808"));
145 expect(tryEdgeCase<std::int64_t>(
"-9223372036854775807"));
146 expect(tryEdgeCase<std::int64_t>(
"-9223372036854775808"));
147 expect(!tryEdgeCase<std::int64_t>(
"-9223372036854775809"));
149 expect(tryEdgeCase<std::uint32_t>(
"4294967294"));
150 expect(tryEdgeCase<std::uint32_t>(
"4294967295"));
151 expect(!tryEdgeCase<std::uint32_t>(
"4294967296"));
153 expect(tryEdgeCase<std::int32_t>(
"2147483646"));
154 expect(tryEdgeCase<std::int32_t>(
"2147483647"));
155 expect(!tryEdgeCase<std::int32_t>(
"2147483648"));
157 expect(tryEdgeCase<std::int32_t>(
"-2147483647"));
158 expect(tryEdgeCase<std::int32_t>(
"-2147483648"));
159 expect(!tryEdgeCase<std::int32_t>(
"-2147483649"));
161 expect(tryEdgeCase<std::uint16_t>(
"65534"));
162 expect(tryEdgeCase<std::uint16_t>(
"65535"));
163 expect(!tryEdgeCase<std::uint16_t>(
"65536"));
165 expect(tryEdgeCase<std::int16_t>(
"32766"));
166 expect(tryEdgeCase<std::int16_t>(
"32767"));
167 expect(!tryEdgeCase<std::int16_t>(
"32768"));
169 expect(tryEdgeCase<std::int16_t>(
"-32767"));
170 expect(tryEdgeCase<std::int16_t>(
"-32768"));
171 expect(!tryEdgeCase<std::int16_t>(
"-32769"));
178 bool result = !success;
183 out = lexicalCastThrow<T>(s);
191 expect(result == success, s);
199 testThrowConvert<std::uint64_t>(
"99999999999999999999",
false);
200 testThrowConvert<std::uint64_t>(
"9223372036854775806",
true);
202 testThrowConvert<std::uint32_t>(
"4294967290",
true);
203 testThrowConvert<std::uint32_t>(
"42949672900",
false);
204 testThrowConvert<std::uint32_t>(
"429496729000",
false);
205 testThrowConvert<std::uint32_t>(
"4294967290000",
false);
207 testThrowConvert<std::int32_t>(
"5294967295",
false);
208 testThrowConvert<std::int32_t>(
"-2147483644",
true);
210 testThrowConvert<std::int16_t>(
"66666",
false);
211 testThrowConvert<std::int16_t>(
"-5711",
true);
252 expect(result == actual, actual +
" (string to integer)");
254 if (result == actual)
256 auto number = lexicalCast<std::int16_t>(result);
259 expect(
false, actual +
" (integer to string)");
273 testIntegers<int>(r);
274 testIntegers<unsigned int>(r);
275 testIntegers<short>(r);
276 testIntegers<unsigned short>(r);
277 testIntegers<std::int32_t>(r);
278 testIntegers<std::uint32_t>(r);
279 testIntegers<std::int64_t>(r);
280 testIntegers<std::uint64_t>(r);
static IntType nextRandomInt(xor_shift_engine &r)
void testIntegers(xor_shift_engine &r)
void testThrowingConversions()
void run() override
Runs the suite.
void testConversionUnderflows()
void testConversionOverflows()
bool tryEdgeCase(std::string const &s)
void testInteger(IntType in)
void testThrowConvert(std::string const &s, bool success)
void tryBadConvert(std::string const &s)
void pass()
Record a successful test condition.
testcase_t testcase
Memberspace for declaring test cases.
bool expect(Condition const &shouldBeTrue)
Evaluate a test condition.
Out lexicalCast(In in, Out defaultValue=Out())
Convert from one type to another.
bool lexicalCastChecked(Out &out, In in)
Intelligently convert from one type to another.
Thrown when a conversion is not possible with LexicalCast.