20 #include <ripple/beast/core/LexicalCast.h>
21 #include <ripple/beast/unit_test.h>
22 #include <ripple/beast/xor_shift_engine.h>
29 template <
class IntType>
32 return static_cast <IntType
> (r());
35 template <
class IntType>
46 template <
class IntType>
52 "random " <<
typeid (IntType).name ();
55 for (
int i = 0; i < 1000; ++i)
57 IntType
const value (nextRandomInt <IntType> (r));
65 "numeric_limits <" <<
typeid (IntType).name () <<
">";
75 testcase(
"pathologies");
78 lexicalCastThrow<int>(
"\xef\xbc\x91\xef\xbc\x90");
95 testcase (
"conversion overflows");
97 tryBadConvert <std::uint64_t> (
"99999999999999999999");
98 tryBadConvert <std::uint32_t> (
"4294967300");
99 tryBadConvert <std::uint16_t> (
"75821");
104 testcase (
"conversion underflows");
106 tryBadConvert <std::uint32_t> (
"-1");
108 tryBadConvert <std::int64_t> (
"-99999999999999999999");
109 tryBadConvert <std::int32_t> (
"-4294967300");
110 tryBadConvert <std::int16_t> (
"-75821");
128 testcase (
"conversion edge cases");
130 expect(tryEdgeCase <std::uint64_t> (
"18446744073709551614"));
131 expect(tryEdgeCase <std::uint64_t> (
"18446744073709551615"));
132 expect(!tryEdgeCase <std::uint64_t> (
"18446744073709551616"));
134 expect(tryEdgeCase <std::int64_t> (
"9223372036854775806"));
135 expect(tryEdgeCase <std::int64_t> (
"9223372036854775807"));
136 expect(!tryEdgeCase <std::int64_t> (
"9223372036854775808"));
138 expect(tryEdgeCase <std::int64_t> (
"-9223372036854775807"));
139 expect(tryEdgeCase <std::int64_t> (
"-9223372036854775808"));
140 expect(!tryEdgeCase <std::int64_t> (
"-9223372036854775809"));
142 expect(tryEdgeCase <std::uint32_t> (
"4294967294"));
143 expect(tryEdgeCase <std::uint32_t> (
"4294967295"));
144 expect(!tryEdgeCase <std::uint32_t> (
"4294967296"));
146 expect(tryEdgeCase <std::int32_t> (
"2147483646"));
147 expect(tryEdgeCase <std::int32_t> (
"2147483647"));
148 expect(!tryEdgeCase <std::int32_t> (
"2147483648"));
150 expect(tryEdgeCase <std::int32_t> (
"-2147483647"));
151 expect(tryEdgeCase <std::int32_t> (
"-2147483648"));
152 expect(!tryEdgeCase <std::int32_t> (
"-2147483649"));
154 expect(tryEdgeCase <std::uint16_t> (
"65534"));
155 expect(tryEdgeCase <std::uint16_t> (
"65535"));
156 expect(!tryEdgeCase <std::uint16_t> (
"65536"));
158 expect(tryEdgeCase <std::int16_t> (
"32766"));
159 expect(tryEdgeCase <std::int16_t> (
"32767"));
160 expect(!tryEdgeCase <std::int16_t> (
"32768"));
162 expect(tryEdgeCase <std::int16_t> (
"-32767"));
163 expect(tryEdgeCase <std::int16_t> (
"-32768"));
164 expect(!tryEdgeCase <std::int16_t> (
"-32769"));
170 bool result = !success;
175 out = lexicalCastThrow <T> (s);
183 expect (result == success, s);
188 testcase (
"throwing conversion");
190 testThrowConvert <std::uint64_t> (
"99999999999999999999",
false);
191 testThrowConvert <std::uint64_t> (
"9223372036854775806",
true);
193 testThrowConvert <std::uint32_t> (
"4294967290",
true);
194 testThrowConvert <std::uint32_t> (
"42949672900",
false);
195 testThrowConvert <std::uint32_t> (
"429496729000",
false);
196 testThrowConvert <std::uint32_t> (
"4294967290000",
false);
198 testThrowConvert <std::int32_t> (
"5294967295",
false);
199 testThrowConvert <std::int32_t> (
"-2147483644",
true);
201 testThrowConvert <std::int16_t> (
"66666",
false);
202 testThrowConvert <std::int16_t> (
"-5711",
true);
207 testcase (
"zero conversion");
228 testcase (
"entire range");
241 expect (result == actual, actual +
" (string to integer)");
243 if (result == actual)
245 auto number = lexicalCast <std::int16_t> (result);
248 expect (
false, actual +
" (integer to string)");
261 testIntegers <int> (r);
262 testIntegers <unsigned int> (r);
263 testIntegers <short> (r);
264 testIntegers <unsigned short> (r);
265 testIntegers <std::int32_t> (r);
266 testIntegers <std::uint32_t> (r);
267 testIntegers <std::int64_t> (r);
268 testIntegers <std::uint64_t> (r);