27TEST_CASE(
"Converting strings to numbers",
"[ioutils::stringUtils]" )
29 SECTION(
"a string char" )
31 SECTION(
"string valid, positive, no error check" )
33 char val = stoT<char>(
"5" );
34 REQUIRE(
static_cast<int>( val ) == 5 );
37 SECTION(
"string valid, negative, w/ error check" )
40 char val = stoT<char>(
"-5", &errc );
41 REQUIRE(
static_cast<int>( val ) == -5 );
45 SECTION(
"positive overflow, w/ error check" )
48 char val = stoT<char>(
"128", &errc );
49 REQUIRE(
static_cast<int>( val ) == std::numeric_limits<char>::max() );
53 SECTION(
"negative overflow, w/ error check" )
56 char val = stoT<char>(
"-129", &errc );
57 REQUIRE(
static_cast<int>( val ) == std::numeric_limits<char>::lowest() );
61 SECTION(
"invalid string, w/ error check" )
64 char val = stoT<char>(
"!", &errc );
65 REQUIRE(
static_cast<int>( val ) == 0 );
69 SECTION(
"string valid w/ leading spaces, negative, w/ error check" )
72 char val = stoT<char>(
" -5", &errc );
73 REQUIRE(
static_cast<int>( val ) == -5 );
78 SECTION(
"a string unsigned char" )
80 SECTION(
"string valid, positive, no error check" )
82 unsigned char val = stoT<unsigned char>(
"200" );
83 REQUIRE(
static_cast<int>( val ) == 200 );
86 SECTION(
"string valid, w/ error check" )
89 unsigned char val = stoT<unsigned char>(
"100", &errc );
90 REQUIRE(
static_cast<int>( val ) == 100 );
94 SECTION(
"positive overflow, w/ error check" )
97 unsigned char val = stoT<unsigned char>(
"256", &errc );
98 REQUIRE(
static_cast<int>( val ) == std::numeric_limits<unsigned char>::max() );
102 SECTION(
"invalid string, w/ error check" )
105 unsigned char val = stoT<unsigned char>(
"*", &errc );
106 REQUIRE(
static_cast<int>( val ) == 0 );
111 SECTION(
"a string short" )
113 SECTION(
"string valid, positive, no error check" )
115 short val = stoT<short>(
"500" );
116 REQUIRE(
static_cast<int>( val ) == 500 );
119 SECTION(
"string valid, negative, w/ error check" )
122 short val = stoT<short>(
"-1024", &errc );
123 REQUIRE(
static_cast<int>( val ) == -1024 );
127 SECTION(
"positive overflow, w/ error check" )
130 short val = stoT<short>(
"47000", &errc );
131 REQUIRE(
static_cast<int>( val ) == std::numeric_limits<short>::max() );
135 SECTION(
"negative overflow, w/ error check" )
138 short val = stoT<short>(
"-37000", &errc );
139 REQUIRE(
static_cast<int>( val ) == std::numeric_limits<short>::lowest() );
143 SECTION(
"invalid string, w/ error check" )
146 short val = stoT<short>(
"-", &errc );
147 REQUIRE(
static_cast<int>( val ) == 0 );
152 SECTION(
"a string unsigned short" )
154 SECTION(
"string valid, positive, no error check" )
156 unsigned short val = stoT<unsigned short>(
"20000" );
157 REQUIRE(
static_cast<int>( val ) == 20000 );
160 SECTION(
"string valid, w/ error check" )
163 unsigned short val = stoT<unsigned short>(
"65000", &errc );
164 REQUIRE(
static_cast<int>( val ) == 65000 );
168 SECTION(
"positive overflow, w/ error check" )
171 unsigned short val = stoT<unsigned short>(
"70000", &errc );
172 REQUIRE(
static_cast<int>( val ) == std::numeric_limits<unsigned short>::max() );
176 SECTION(
"invalid string, w/ error check" )
179 unsigned short val = stoT<unsigned short>(
"#", &errc );
180 REQUIRE(
static_cast<int>( val ) == 0 );
185 SECTION(
"a string int" )
187 SECTION(
"string valid, positive, no error check" )
189 int val = stoT<int>(
"1000000" );
190 REQUIRE(
static_cast<int>( val ) == 1000000 );
193 SECTION(
"string valid, negative, w/ error check" )
196 int val = stoT<int>(
"-2000000", &errc );
197 REQUIRE(
static_cast<int>( val ) == -2000000 );
201 SECTION(
"positive overflow, w/ error check" )
204 int val = stoT<int>(
"3000000000", &errc );
205 REQUIRE(
static_cast<int>( val ) == std::numeric_limits<int>::max() );
209 SECTION(
"negative overflow, w/ error check" )
212 int val = stoT<int>(
"-2147483650", &errc );
213 REQUIRE(
static_cast<int>( val ) == std::numeric_limits<int>::lowest() );
217 SECTION(
"invalid string, w/ error check" )
220 int val = stoT<int>(
" w", &errc );
221 REQUIRE(
static_cast<int>( val ) == 0 );
226 SECTION(
"a string unsigned int" )
228 SECTION(
"string valid, positive, no error check" )
230 unsigned int val = stoT<unsigned int>(
"4000000000" );
231 REQUIRE(
static_cast<unsigned int>( val ) == 4000000000 );
234 SECTION(
"string valid, w/ error check" )
237 unsigned int val = stoT<unsigned int>(
"2", &errc );
238 REQUIRE(
static_cast<unsigned int>( val ) == 2 );
242 SECTION(
"positive overflow, w/ error check" )
245 unsigned int val = stoT<unsigned int>(
"6000000000", &errc );
246 REQUIRE(
static_cast<unsigned int>( val ) == std::numeric_limits<unsigned int>::max() );
250 SECTION(
"invalid string, w/ error check" )
253 unsigned int val = stoT<unsigned int>(
"?8", &errc );
254 REQUIRE(
static_cast<unsigned int>( val ) == 0 );
259 SECTION(
"a string long" )
261 SECTION(
"string valid, positive, no error check" )
263 long val = stoT<long>(
"1000000" );
264 REQUIRE(
static_cast<long>( val ) == 1000000 );
267 SECTION(
"string valid, negative, w/ error check" )
270 long val = stoT<long>(
"-2000000", &errc );
271 REQUIRE(
static_cast<long>( val ) == -2000000 );
275 SECTION(
"positive overflow, w/ error check" )
278 long val = stoT<long>(
"9223372036854775808", &errc );
279 REQUIRE(
static_cast<long>( val ) == std::numeric_limits<long>::max() );
283 SECTION(
"negative overflow, w/ error check" )
286 long val = stoT<long>(
"-9223372036854775809", &errc );
287 REQUIRE(
static_cast<long>( val ) == std::numeric_limits<long>::lowest() );
291 SECTION(
"invalid string, w/ error check" )
294 long val = stoT<long>(
" w8", &errc );
295 REQUIRE(
static_cast<long>( val ) == 0 );
300 SECTION(
"a string unsigned long" )
302 SECTION(
"string valid, positive, no error check" )
304 unsigned long val = stoT<unsigned long>(
"400000000000" );
305 REQUIRE(
static_cast<unsigned long>( val ) == 400000000000 );
308 SECTION(
"string valid, w/ error check" )
311 unsigned long val = stoT<unsigned long>(
"16", &errc );
312 REQUIRE(
static_cast<unsigned long>( val ) == 16 );
316 SECTION(
"positive overflow, w/ error check" )
319 unsigned long val = stoT<unsigned long>(
"18523372036854775808", &errc );
320 REQUIRE(
static_cast<unsigned long>( val ) == std::numeric_limits<unsigned long>::max() );
324 SECTION(
"invalid string, w/ error check" )
327 unsigned long val = stoT<unsigned long>(
"?8", &errc );
328 REQUIRE(
static_cast<unsigned long>( val ) == 0 );
333 SECTION(
"a string long long" )
335 SECTION(
"string valid, positive, no error check" )
337 long long val = stoT<long long>(
"1000052" );
338 REQUIRE(
static_cast<long long>( val ) == 1000052 );
341 SECTION(
"string valid, negative, w/ error check" )
344 long long val = stoT<long long>(
"-2300000", &errc );
345 REQUIRE(
static_cast<long long>( val ) == -2300000 );
349 SECTION(
"positive overflow, w/ error check" )
352 long long val = stoT<long long>(
"9223372036854775808", &errc );
353 REQUIRE(
static_cast<long long>( val ) == std::numeric_limits<long long>::max() );
357 SECTION(
"negative overflow, w/ error check" )
360 long long val = stoT<long long>(
"-9223372036854775809", &errc );
361 REQUIRE(
static_cast<long long>( val ) == std::numeric_limits<long long>::lowest() );
365 SECTION(
"invalid string, w/ error check" )
368 long long val = stoT<long long>(
"-..8", &errc );
369 REQUIRE(
static_cast<long long>( val ) == 0 );
374 SECTION(
"a string unsigned long long" )
376 SECTION(
"string valid, positive, no error check" )
378 unsigned long long val = stoT<unsigned long long>(
"400000000000" );
379 REQUIRE(
static_cast<unsigned long long>( val ) == 400000000000 );
382 SECTION(
"string valid, w/ error check" )
385 unsigned long long val = stoT<unsigned long long>(
"16", &errc );
386 REQUIRE(
static_cast<unsigned long long>( val ) == 16 );
390 SECTION(
"positive overflow, w/ error check" )
393 unsigned long long val = stoT<unsigned long long>(
"18523372036854775808", &errc );
394 REQUIRE(
static_cast<unsigned long long>( val ) == std::numeric_limits<unsigned long long>::max() );
398 SECTION(
"invalid string, w/ error check" )
401 unsigned long long val = stoT<unsigned long long>(
"++9.2", &errc );
402 REQUIRE(
static_cast<unsigned long long>( val ) == 0 );
407 SECTION(
"a string bool" )
409 SECTION(
"string valid, true, no error check" )
411 bool val = stoT<bool>(
"true" );
412 REQUIRE(
static_cast<bool>( val ) ==
true );
415 SECTION(
"string valid, false, w/ error check" )
418 bool val = stoT<bool>(
"false", &errc );
419 REQUIRE(
static_cast<bool>( val ) ==
false );
423 SECTION(
"string valid, t, w/ error check" )
426 bool val = stoT<bool>(
"t", &errc );
427 REQUIRE(
static_cast<bool>( val ) ==
true );
431 SECTION(
"string valid, f, w/ error check" )
434 bool val = stoT<bool>(
"false", &errc );
435 REQUIRE(
static_cast<bool>( val ) ==
false );
439 SECTION(
"string valid, 1, w/ error check" )
442 bool val = stoT<bool>(
"1", &errc );
443 REQUIRE(
static_cast<bool>( val ) ==
true );
447 SECTION(
"string valid, 0, w/ error check" )
450 bool val = stoT<bool>(
"0", &errc );
451 REQUIRE(
static_cast<bool>( val ) ==
false );
455 SECTION(
"string valid, number > 1, w/ error check" )
458 bool val = stoT<bool>(
"237", &errc );
459 REQUIRE(
static_cast<bool>( val ) ==
true );
463 SECTION(
"string valid, -0, w/ error check" )
466 bool val = stoT<bool>(
"-0.2", &errc );
467 REQUIRE(
static_cast<bool>( val ) ==
false );
471 SECTION(
"invalid string, w/ error check" )
474 bool val = stoT<bool>(
"Xtrue", &errc );
475 REQUIRE(
static_cast<bool>( val ) == 0 );
479 SECTION(
"string valid w/ leading spaces, t, w/ error check" )
482 bool val = stoT<bool>(
" t", &errc );
483 REQUIRE(
static_cast<bool>( val ) ==
true );
488 SECTION(
"a string float" )
490 SECTION(
"string valid, positive, no error check" )
492 float val = stoT<float>(
"2.567" );
493 REQUIRE_THAT( val, WithinRel(
static_cast<float>( 2.567 ), std::numeric_limits<float>::epsilon() ) );
496 SECTION(
"string valid, negative, w/ error check" )
499 float val = stoT<float>(
"-2300000.897", &errc );
500 REQUIRE_THAT( val, WithinRel(
static_cast<float>( -2300000.897 ), std::numeric_limits<float>::epsilon() ) );
504 SECTION(
"positive overflow, w/ error check" )
507 float val = stoT<float>(
"1e55", &errc );
512 SECTION(
"negative overflow, w/ error check" )
515 float val = stoT<float>(
"-1e55", &errc );
520 SECTION(
"invalid string, w/ error check" )
523 float val = stoT<float>(
"r5", &errc );
524 REQUIRE(
static_cast<float>( val ) == 0 );
528 SECTION(
"string valid w/ leading spaces, negative, w/ error check" )
531 float val = stoT<float>(
" -2300000.897", &errc );
532 REQUIRE_THAT( val, WithinRel(
static_cast<float>( -2300000.897 ), std::numeric_limits<float>::epsilon() ) );
537 SECTION(
"a string double" )
539 SECTION(
"string valid, positive, no error check" )
541 double val = stoT<double>(
"22.2567" );
542 REQUIRE_THAT( val, WithinRel(
static_cast<double>( 22.2567 ), std::numeric_limits<double>::epsilon() ) );
545 SECTION(
"string valid, negative, w/ error check" )
548 double val = stoT<double>(
"-2300000.897987", &errc );
550 WithinRel(
static_cast<double>( -2300000.897987 ), std::numeric_limits<double>::epsilon() ) );
554 SECTION(
"positive overflow, w/ error check" )
557 double val = stoT<double>(
"1e400", &errc );
562 SECTION(
"negative overflow, w/ error check" )
565 double val = stoT<double>(
"-1e400", &errc );
570 SECTION(
"invalid string, w/ error check" )
573 double val = stoT<double>(
"+d", &errc );
574 REQUIRE(
static_cast<double>( val ) == 0 );
579 SECTION(
"a string long double" )
583 SECTION(
"string valid, positive, no error check" )
585 long double val = stoT<long double>(
"22.2567" );
586 REQUIRE_THAT(
static_cast<double>( val ), Catch::Matchers::WithinRel( 22.2567, 1e-9 ) );
589 SECTION(
"string valid, negative, w/ error check" )
592 long double val = stoT<long double>(
"-2300000.897987", &errc );
593 REQUIRE_THAT(
static_cast<double>( val ), Catch::Matchers::WithinRel( -2300000.897987, 1e-9 ) );
597 SECTION(
"positive overflow, w/ error check" )
600 long double val = stoT<long double>(
"1e10000", &errc );
605 SECTION(
"negative overflow, w/ error check" )
608 long double val = stoT<long double>(
"-1e10000", &errc );
613 SECTION(
"invalid string, w/ error check" )
616 long double val = stoT<long double>(
"+d", &errc );
617 REQUIRE(
static_cast<long double>( val ) == 0 );