29TEST_CASE(
"Converting strings to numbers",
"[ioutils::stringUtils]" )
31 SECTION(
"a string char" )
33 SECTION(
"string valid, positive, no error check" )
36 REQUIRE(
static_cast<int>( val ) == 5 );
39 SECTION(
"string valid, negative, w/ error check" )
43 REQUIRE(
static_cast<int>( val ) == -5 );
47 SECTION(
"positive overflow, w/ error check" )
51 REQUIRE(
static_cast<int>( val ) == std::numeric_limits<char>::max() );
55 SECTION(
"negative overflow, w/ error check" )
59 REQUIRE(
static_cast<int>( val ) == std::numeric_limits<char>::lowest() );
63 SECTION(
"invalid string, w/ error check" )
67 REQUIRE(
static_cast<int>( val ) == 0 );
71 SECTION(
"string valid w/ leading spaces, negative, w/ error check" )
75 REQUIRE(
static_cast<int>( val ) == -5 );
80 SECTION(
"a string unsigned char" )
82 SECTION(
"string valid, positive, no error check" )
85 REQUIRE(
static_cast<int>( val ) == 200 );
88 SECTION(
"string valid, w/ error check" )
92 REQUIRE(
static_cast<int>( val ) == 100 );
96 SECTION(
"positive overflow, w/ error check" )
100 REQUIRE(
static_cast<int>( val ) == std::numeric_limits<unsigned char>::max() );
104 SECTION(
"invalid string, w/ error check" )
108 REQUIRE(
static_cast<int>( val ) == 0 );
113 SECTION(
"a string short" )
115 SECTION(
"string valid, positive, no error check" )
118 REQUIRE(
static_cast<int>( val ) == 500 );
121 SECTION(
"string valid, negative, w/ error check" )
125 REQUIRE(
static_cast<int>( val ) == -1024 );
129 SECTION(
"positive overflow, w/ error check" )
133 REQUIRE(
static_cast<int>( val ) == std::numeric_limits<short>::max() );
137 SECTION(
"negative overflow, w/ error check" )
141 REQUIRE(
static_cast<int>( val ) == std::numeric_limits<short>::lowest() );
145 SECTION(
"invalid string, w/ error check" )
149 REQUIRE(
static_cast<int>( val ) == 0 );
154 SECTION(
"a string unsigned short" )
156 SECTION(
"string valid, positive, no error check" )
159 REQUIRE(
static_cast<int>( val ) == 20000 );
162 SECTION(
"string valid, w/ error check" )
166 REQUIRE(
static_cast<int>( val ) == 65000 );
170 SECTION(
"positive overflow, w/ error check" )
174 REQUIRE(
static_cast<int>( val ) == std::numeric_limits<unsigned short>::max() );
178 SECTION(
"invalid string, w/ error check" )
182 REQUIRE(
static_cast<int>( val ) == 0 );
187 SECTION(
"a string int" )
189 SECTION(
"string valid, positive, no error check" )
192 REQUIRE(
static_cast<int>( val ) == 1000000 );
195 SECTION(
"string valid, negative, w/ error check" )
198 int val =
stoT<int>(
"-2000000", &errc );
199 REQUIRE(
static_cast<int>( val ) == -2000000 );
203 SECTION(
"positive overflow, w/ error check" )
206 int val =
stoT<int>(
"3000000000", &errc );
207 REQUIRE(
static_cast<int>( val ) == std::numeric_limits<int>::max() );
211 SECTION(
"negative overflow, w/ error check" )
214 int val =
stoT<int>(
"-2147483650", &errc );
215 REQUIRE(
static_cast<int>( val ) == std::numeric_limits<int>::lowest() );
219 SECTION(
"invalid string, w/ error check" )
223 REQUIRE(
static_cast<int>( val ) == 0 );
228 SECTION(
"a string unsigned int" )
230 SECTION(
"string valid, positive, no error check" )
233 REQUIRE(
static_cast<unsigned int>( val ) == 4000000000 );
236 SECTION(
"string valid, w/ error check" )
240 REQUIRE(
static_cast<unsigned int>( val ) == 2 );
244 SECTION(
"positive overflow, w/ error check" )
248 REQUIRE(
static_cast<unsigned int>( val ) == std::numeric_limits<unsigned int>::max() );
252 SECTION(
"invalid string, w/ error check" )
256 REQUIRE(
static_cast<unsigned int>( val ) == 0 );
261 SECTION(
"a string long" )
263 SECTION(
"string valid, positive, no error check" )
266 REQUIRE(
static_cast<long>( val ) == 1000000 );
269 SECTION(
"string valid, negative, w/ error check" )
273 REQUIRE(
static_cast<long>( val ) == -2000000 );
277 SECTION(
"positive overflow, w/ error check" )
280 long val =
stoT<long>(
"9223372036854775808", &errc );
281 REQUIRE(
static_cast<long>( val ) == std::numeric_limits<long>::max() );
285 SECTION(
"negative overflow, w/ error check" )
288 long val =
stoT<long>(
"-9223372036854775809", &errc );
289 REQUIRE(
static_cast<long>( val ) == std::numeric_limits<long>::lowest() );
293 SECTION(
"invalid string, w/ error check" )
297 REQUIRE(
static_cast<long>( val ) == 0 );
302 SECTION(
"a string unsigned long" )
304 SECTION(
"string valid, positive, no error check" )
307 REQUIRE(
static_cast<unsigned long>( val ) == 400000000000 );
310 SECTION(
"string valid, w/ error check" )
314 REQUIRE(
static_cast<unsigned long>( val ) == 16 );
318 SECTION(
"positive overflow, w/ error check" )
322 REQUIRE(
static_cast<unsigned long>( val ) == std::numeric_limits<unsigned long>::max() );
326 SECTION(
"invalid string, w/ error check" )
330 REQUIRE(
static_cast<unsigned long>( val ) == 0 );
335 SECTION(
"a string long long" )
337 SECTION(
"string valid, positive, no error check" )
340 REQUIRE(
static_cast<long long>( val ) == 1000052 );
343 SECTION(
"string valid, negative, w/ error check" )
347 REQUIRE(
static_cast<long long>( val ) == -2300000 );
351 SECTION(
"positive overflow, w/ error check" )
355 REQUIRE(
static_cast<long long>( val ) == std::numeric_limits<long long>::max() );
359 SECTION(
"negative overflow, w/ error check" )
363 REQUIRE(
static_cast<long long>( val ) == std::numeric_limits<long long>::lowest() );
367 SECTION(
"invalid string, w/ error check" )
371 REQUIRE(
static_cast<long long>( val ) == 0 );
376 SECTION(
"a string unsigned long long" )
378 SECTION(
"string valid, positive, no error check" )
381 REQUIRE(
static_cast<unsigned long long>( val ) == 400000000000 );
384 SECTION(
"string valid, w/ error check" )
388 REQUIRE(
static_cast<unsigned long long>( val ) == 16 );
392 SECTION(
"positive overflow, w/ error check" )
396 REQUIRE(
static_cast<unsigned long long>( val ) == std::numeric_limits<unsigned long long>::max() );
400 SECTION(
"invalid string, w/ error check" )
404 REQUIRE(
static_cast<unsigned long long>( val ) == 0 );
409 SECTION(
"a string bool" )
411 SECTION(
"string valid, true, no error check" )
414 REQUIRE(
static_cast<bool>( val ) ==
true );
417 SECTION(
"string valid, false, w/ error check" )
421 REQUIRE(
static_cast<bool>( val ) ==
false );
425 SECTION(
"string valid, t, w/ error check" )
429 REQUIRE(
static_cast<bool>( val ) ==
true );
433 SECTION(
"string valid, f, w/ error check" )
437 REQUIRE(
static_cast<bool>( val ) ==
false );
441 SECTION(
"string valid, 1, w/ error check" )
445 REQUIRE(
static_cast<bool>( val ) ==
true );
449 SECTION(
"string valid, 0, w/ error check" )
453 REQUIRE(
static_cast<bool>( val ) ==
false );
457 SECTION(
"string valid, number > 1, w/ error check" )
461 REQUIRE(
static_cast<bool>( val ) ==
true );
465 SECTION(
"string valid, -0, w/ error check" )
469 REQUIRE(
static_cast<bool>( val ) ==
false );
473 SECTION(
"invalid string, w/ error check" )
477 REQUIRE(
static_cast<bool>( val ) == 0 );
481 SECTION(
"string valid w/ leading spaces, t, w/ error check" )
485 REQUIRE(
static_cast<bool>( val ) ==
true );
490 SECTION(
"a string float" )
492 SECTION(
"string valid, positive, no error check" )
495 REQUIRE_THAT( val, WithinRel(
static_cast<float>( 2.567 ), std::numeric_limits<float>::epsilon() ) );
498 SECTION(
"string valid, negative, w/ error check" )
502 REQUIRE_THAT( val, WithinRel(
static_cast<float>( -2300000.897 ), std::numeric_limits<float>::epsilon() ) );
506 SECTION(
"positive overflow, w/ error check" )
514 SECTION(
"negative overflow, w/ error check" )
522 SECTION(
"invalid string, w/ error check" )
526 REQUIRE(
static_cast<float>( val ) == 0 );
530 SECTION(
"string valid w/ leading spaces, negative, w/ error check" )
534 REQUIRE_THAT( val, WithinRel(
static_cast<float>( -2300000.897 ), std::numeric_limits<float>::epsilon() ) );
539 SECTION(
"a string double" )
541 SECTION(
"string valid, positive, no error check" )
544 REQUIRE_THAT( val, WithinRel(
static_cast<double>( 22.2567 ), std::numeric_limits<double>::epsilon() ) );
547 SECTION(
"string valid, negative, w/ error check" )
552 WithinRel(
static_cast<double>( -2300000.897987 ), std::numeric_limits<double>::epsilon() ) );
556 SECTION(
"positive overflow, w/ error check" )
564 SECTION(
"negative overflow, w/ error check" )
572 SECTION(
"invalid string, w/ error check" )
576 REQUIRE(
static_cast<double>( val ) == 0 );
581 SECTION(
"a string long double" )
585 SECTION(
"string valid, positive, no error check" )
588 REQUIRE_THAT(
static_cast<double>( val ), Catch::Matchers::WithinRel( 22.2567, 1e-9 ) );
591 SECTION(
"string valid, negative, w/ error check" )
595 REQUIRE_THAT(
static_cast<double>( val ), Catch::Matchers::WithinRel( -2300000.897987, 1e-9 ) );
599 SECTION(
"positive overflow, w/ error check" )
607 SECTION(
"negative overflow, w/ error check" )
615 SECTION(
"invalid string, w/ error check" )
619 REQUIRE(
static_cast<long double>( val ) == 0 );
629TEST_CASE(
"Parsing delimited numeric vectors",
"[ioutils::stringUtils::parseStringVector]" )
631 std::vector<int> modes{ 99 };
633 REQUIRE( modes == std::vector<int>{ 5, 10, 15 } );
635 std::vector<double> bounds{ 99.0 };
637 REQUIRE( bounds == std::vector<double>{ -1.5, 0.0, 2.25 } );
639 std::vector<int> defaultDelimiter;
641 REQUIRE( defaultDelimiter == std::vector<int>{ 1, 2, 3 } );
643 std::vector<int> characterDelimiter;
645 REQUIRE( characterDelimiter == std::vector<int>{ 4, 5, 6 } );
647 std::vector<int> delimiterSet;
649 REQUIRE( delimiterSet == std::vector<int>{ 1, 2, 3 } );
652 REQUIRE( delimiterSet == std::vector<int>{ 42 } );
void parseStringVector(std::vector< typeT > &v, const std::string &s, char delim=',')
Parses a string into a vector of tokens delimited by a character.