diff --git a/Makefile b/Makefile index 3439f7c..cf1cd7f 100644 --- a/Makefile +++ b/Makefile @@ -17,9 +17,9 @@ tests: FORCE $(MAKE) -C tests -tests-gcc10: FORCE - env CXX=g++10 CXXFLAGS="-Wl,-rpath=/usr/local/lib/gcc10/ -Wall -pedantic -O0 -g3 -std=c++20 -fmax-errors=1 -I../src -I/usr/local/include" $(MAKE) -C src - env CXX=g++10 CXXFLAGS="-Wl,-rpath=/usr/local/lib/gcc10/ -Wall -pedantic -O0 -g3 -std=c++20 -fmax-errors=1 -I../src -I/usr/local/include" $(MAKE) -C tests +tests-gcc11: FORCE + env CXX=g++11 CXXFLAGS="-Wl,-rpath=/usr/local/lib/gcc11/ -Wall -pedantic -O0 -g3 -std=c++20 -fmax-errors=1 -I../src -I/usr/local/include" $(MAKE) -C src + env CXX=g++11 CXXFLAGS="-Wl,-rpath=/usr/local/lib/gcc11/ -Wall -pedantic -O0 -g3 -std=c++20 -fmax-errors=1 -I../src -I/usr/local/include" $(MAKE) -C tests tests-clang: FORCE diff --git a/src/convert/strtoint.h b/src/convert/strtoint.h index ebff9c6..7052f25 100644 --- a/src/convert/strtoint.h +++ b/src/convert/strtoint.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2017-2021, Tomasz Sowa + * Copyright (c) 2017-2022, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -44,10 +44,8 @@ namespace pt { - - template -unsigned long long Toull(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) +unsigned long long to_ull(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) { unsigned long long res = 0; bool carry = false; @@ -96,8 +94,16 @@ unsigned long long Toull(const CharType * str, int base = 10, const CharType ** } +template +unsigned long long to_ull(const StringType & str, int base = 10, const typename StringType::value_type ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) +{ + return to_ull(str.c_str(), base, after_str, was_overflow, allow_skip_whitechars); +} + + + template -long long Toll(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) +long long to_ll(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) { bool was_sign = false; bool was_overflow_u = false; @@ -113,7 +119,7 @@ long long Toll(const CharType * str, int base = 10, const CharType ** after_str str += 1; } - unsigned long long uval = Toull(str, base, after_str, &was_overflow_u, false); + unsigned long long uval = to_ull(str, base, after_str, &was_overflow_u, false); unsigned long long sign_add = ( was_sign ) ? 1 : 0; if( was_overflow_u ) @@ -138,14 +144,21 @@ long long Toll(const CharType * str, int base = 10, const CharType ** after_str +template +long long to_ll(const StringType & str, int base = 10, const typename StringType::value_type ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) +{ + return to_ll(str.c_str(), base, after_str, was_overflow, allow_skip_whitechars); +} + + template -IntegerType ToUnsignedIntegerType(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) +IntegerType to_unsigned_integer_type(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) { bool was_overflow_ll = false; SetOverflow(was_overflow, false); - unsigned long long val = Toull(str, base, after_str, &was_overflow_ll, allow_skip_whitechars); + unsigned long long val = to_ull(str, base, after_str, &was_overflow_ll, allow_skip_whitechars); if( was_overflow_ll || val > static_cast(std::numeric_limits::max()) ) { @@ -158,28 +171,39 @@ IntegerType ToUnsignedIntegerType(const CharType * str, int base = 10, const Cha template -unsigned long Toul(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) +unsigned long to_ul(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) { - return ToUnsignedIntegerType(str, base, after_str, was_overflow, allow_skip_whitechars); + return to_unsigned_integer_type(str, base, after_str, was_overflow, allow_skip_whitechars); +} + +template +unsigned long to_ul(const StringType & str, int base = 10, const typename StringType::value_type ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) +{ + return to_unsigned_integer_type(str.c_str(), base, after_str, was_overflow, allow_skip_whitechars); } template -unsigned int Toui(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) +unsigned int to_ui(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) { - return ToUnsignedIntegerType(str, base, after_str, was_overflow, allow_skip_whitechars); + return to_unsigned_integer_type(str, base, after_str, was_overflow, allow_skip_whitechars); } +template +unsigned int to_ui(const StringType & str, int base = 10, const typename StringType::value_type ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) +{ + return to_unsigned_integer_type(str.c_str(), base, after_str, was_overflow, allow_skip_whitechars); +} template -IntegerType ToIntegerType(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) +IntegerType to_integer_type(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) { bool was_overflow_ll = false; SetOverflow(was_overflow, false); - long long val = Toll(str, base, after_str, &was_overflow_ll, allow_skip_whitechars); + long long val = to_ll(str, base, after_str, &was_overflow_ll, allow_skip_whitechars); if( was_overflow_ll || val < static_cast(std::numeric_limits::min()) || @@ -195,22 +219,28 @@ IntegerType ToIntegerType(const CharType * str, int base = 10, const CharType ** template -long Tol(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) +long to_l(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) { - return ToIntegerType(str, base, after_str, was_overflow, allow_skip_whitechars); + return to_integer_type(str, base, after_str, was_overflow, allow_skip_whitechars); +} + +template +long to_l(const StringType & str, int base = 10, const typename StringType::value_type ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) +{ + return to_integer_type(str.c_str(), base, after_str, was_overflow, allow_skip_whitechars); } template -int Toi(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) +int to_i(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) { - return ToIntegerType(str, base, after_str, was_overflow, allow_skip_whitechars); + return to_integer_type(str, base, after_str, was_overflow, allow_skip_whitechars); } - - - - - +template +int to_i(const StringType & str, int base = 10, const typename StringType::value_type ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) +{ + return to_integer_type(str.c_str(), base, after_str, was_overflow, allow_skip_whitechars); +} /* @@ -224,7 +254,7 @@ int Toi(const CharType * str, int base = 10, const CharType ** after_str = 0, bo * */ template -unsigned long long Toull_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) +unsigned long long to_ull_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) { if( allow_skip_whitechars ) str = skip_white(str); @@ -249,13 +279,13 @@ unsigned long long Toull_b(const CharType * str, const CharType ** after_str = 0 str += 1; } - return Toull(str, base, after_str, was_overflow, false); + return to_ull(str, base, after_str, was_overflow, false); } template -long long Toll_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) +long long to_ll_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) { bool was_sign = false; bool was_overflow_u = false; @@ -271,7 +301,7 @@ long long Toll_b(const CharType * str, const CharType ** after_str = 0, bool * w str += 1; } - unsigned long long uval = Toull_b(str, after_str, &was_overflow_u, false); + unsigned long long uval = to_ull_b(str, after_str, &was_overflow_u, false); unsigned long long sign_add = ( was_sign ) ? 1 : 0; if( was_overflow_u ) @@ -302,12 +332,12 @@ long long Toll_b(const CharType * str, const CharType ** after_str = 0, bool * w template -IntegerType ToUnsignedIntegerType_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) +IntegerType to_unsigned_integer_type_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) { bool was_overflow_ll = false; SetOverflow(was_overflow, false); - unsigned long long val = Toull_b(str, after_str, &was_overflow_ll, allow_skip_whitechars); + unsigned long long val = to_ull_b(str, after_str, &was_overflow_ll, allow_skip_whitechars); if( was_overflow_ll || val > static_cast(std::numeric_limits::max()) ) { @@ -320,15 +350,15 @@ IntegerType ToUnsignedIntegerType_b(const CharType * str, const CharType ** afte template -unsigned long Toul_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) +unsigned long to_ul_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) { - return ToUnsignedIntegerType_b(str, after_str, was_overflow, allow_skip_whitechars); + return to_unsigned_integer_type_b(str, after_str, was_overflow, allow_skip_whitechars); } template -unsigned int Toui_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) +unsigned int to_ui_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) { - return ToUnsignedIntegerType_b(str, after_str, was_overflow, allow_skip_whitechars); + return to_unsigned_integer_type_b(str, after_str, was_overflow, allow_skip_whitechars); } @@ -336,12 +366,12 @@ unsigned int Toui_b(const CharType * str, const CharType ** after_str = 0, bool template -IntegerType ToIntegerType_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) +IntegerType to_integer_type_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) { bool was_overflow_ll = false; SetOverflow(was_overflow, false); - long long val = Toll_b(str, after_str, &was_overflow_ll, allow_skip_whitechars); + long long val = to_ll_b(str, after_str, &was_overflow_ll, allow_skip_whitechars); if( was_overflow_ll || val < static_cast(std::numeric_limits::min()) || @@ -357,15 +387,15 @@ IntegerType ToIntegerType_b(const CharType * str, const CharType ** after_str = template -long Tol_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) +long to_l_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) { - return ToIntegerType_b(str, after_str, was_overflow, allow_skip_whitechars); + return to_integer_type_b(str, after_str, was_overflow, allow_skip_whitechars); } template -int Toi_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) +int to_i_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true) { - return ToIntegerType_b(str, after_str, was_overflow, allow_skip_whitechars); + return to_integer_type_b(str, after_str, was_overflow, allow_skip_whitechars); } diff --git a/src/space/space.cpp b/src/space/space.cpp index dd0c90c..db2a131 100644 --- a/src/space/space.cpp +++ b/src/space/space.cpp @@ -1072,7 +1072,7 @@ long long Space::convert_string_to_long_long() const { bool was_overflow = false; const char * after_str; - long long val = Toll(value.value_string.c_str(), 10, &after_str, &was_overflow, true); + long long val = to_ll(value.value_string.c_str(), 10, &after_str, &was_overflow, true); return was_overflow ? 0 : val; } @@ -1082,7 +1082,7 @@ long long Space::convert_wstring_to_long_long() const { bool was_overflow = false; const wchar_t * after_str; - long long val = Toll(value.value_wstring.c_str(), 10, &after_str, &was_overflow, true); + long long val = to_ll(value.value_wstring.c_str(), 10, &after_str, &was_overflow, true); return was_overflow ? 0 : val; } @@ -1092,7 +1092,7 @@ unsigned long long Space::convert_string_to_ulong_long() const { bool was_overflow = false; const char * after_str; - unsigned long long val = Toull(value.value_string.c_str(), 10, &after_str, &was_overflow, true); + unsigned long long val = to_ull(value.value_string.c_str(), 10, &after_str, &was_overflow, true); return was_overflow ? 0 : val; } @@ -1102,7 +1102,7 @@ unsigned long long Space::convert_wstring_to_ulong_long() const { bool was_overflow = false; const wchar_t * after_str; - unsigned long long val = Toull(value.value_wstring.c_str(), 10, &after_str, &was_overflow, true); + unsigned long long val = to_ull(value.value_wstring.c_str(), 10, &after_str, &was_overflow, true); return was_overflow ? 0 : val; } diff --git a/src/space/spaceparser.cpp b/src/space/spaceparser.cpp index 546ccfa..7481f00 100644 --- a/src/space/spaceparser.cpp +++ b/src/space/spaceparser.cpp @@ -572,7 +572,7 @@ void SpaceParser::parse_integer_value(Space * space) base = 8; } - long long val = Toll(token.c_str(), base, &after_str, &was_overflow, false); + long long val = to_ll(token.c_str(), base, &after_str, &was_overflow, false); if( was_overflow ) { diff --git a/tests/Makefile.dep b/tests/Makefile.dep index faa24dd..5fd8554 100644 --- a/tests/Makefile.dep +++ b/tests/Makefile.dep @@ -1,15 +1,15 @@ # DO NOT DELETE -./convert.o: convert.h test.h ../src/convert/convert.h -./convert.o: ../src/convert/inttostr.h ../src/convert/patternreplacer.h -./convert.o: ../src/textstream/textstream.h ../src/textstream/stream.h -./convert.o: ../src/space/space.h ../src/textstream/types.h -./convert.o: ../src/convert/inttostr.h ../src/utf8/utf8.h -./convert.o: ../src/textstream/stream.h ../src/utf8/utf8_templates.h -./convert.o: ../src/utf8/utf8_private.h ../src/date/date.h -./convert.o: ../src/membuffer/membuffer.h ../src/textstream/types.h -./convert.o: ../src/convert/strtoint.h ../src/convert/text.h -./convert.o: ../src/convert/misc.h ../src/convert/double.h +./convert.o: convert.h ../src/convert/convert.h ../src/convert/inttostr.h +./convert.o: ../src/convert/patternreplacer.h ../src/textstream/textstream.h +./convert.o: ../src/textstream/stream.h ../src/space/space.h +./convert.o: ../src/textstream/types.h ../src/convert/inttostr.h +./convert.o: ../src/utf8/utf8.h ../src/textstream/stream.h +./convert.o: ../src/utf8/utf8_templates.h ../src/utf8/utf8_private.h +./convert.o: ../src/date/date.h ../src/membuffer/membuffer.h +./convert.o: ../src/textstream/types.h ../src/convert/strtoint.h +./convert.o: ../src/convert/text.h ../src/convert/misc.h +./convert.o: ../src/convert/double.h test.h ./csvparser.o: csvparser.h ../src/csv/csvparser.h ../src/space/space.h ./csvparser.o: ../src/textstream/types.h ../src/convert/inttostr.h ./csvparser.o: ../src/utf8/utf8.h ../src/textstream/stream.h @@ -17,7 +17,16 @@ ./csvparser.o: ../src/convert/baseparser.h ../src/textstream/textstream.h ./csvparser.o: ../src/textstream/stream.h ../src/date/date.h ./csvparser.o: ../src/membuffer/membuffer.h ../src/textstream/types.h test.h -./main.o: convert.h mainoptionsparser.h csvparser.h +./main.o: convert.h ../src/convert/convert.h ../src/convert/inttostr.h +./main.o: ../src/convert/patternreplacer.h ../src/textstream/textstream.h +./main.o: ../src/textstream/stream.h ../src/space/space.h +./main.o: ../src/textstream/types.h ../src/convert/inttostr.h +./main.o: ../src/utf8/utf8.h ../src/textstream/stream.h +./main.o: ../src/utf8/utf8_templates.h ../src/utf8/utf8_private.h +./main.o: ../src/date/date.h ../src/membuffer/membuffer.h +./main.o: ../src/textstream/types.h ../src/convert/strtoint.h +./main.o: ../src/convert/text.h ../src/convert/misc.h ../src/convert/double.h +./main.o: test.h mainoptionsparser.h csvparser.h ./test.o: test.h ./mainoptionsparser.o: mainoptionsparser.h test.h ./mainoptionsparser.o: ../src/mainoptions/mainoptionsparser.h diff --git a/tests/convert.cpp b/tests/convert.cpp index 4abd187..a3ec724 100644 --- a/tests/convert.cpp +++ b/tests/convert.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2021, Tomasz Sowa + * Copyright (c) 2021-2022, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -34,8 +34,6 @@ #include #include "convert.h" -#include "test.h" -#include "convert/convert.h" namespace pt @@ -1919,6 +1917,181 @@ void test_text46() } +void test_text47() +{ + reset_test_counter("to_ull"); + + IntegerHelper helper_tab[] = { + {L"", 0ULL, 10, L"", false, false}, + {L"0", 0ULL, 10, L"", false, false}, + {L"00", 0ULL, 10, L"", false, false}, + {L"1", 1ULL, 10, L"", false, false}, + {L"50", 50ULL, 10, L"", false, false}, + {L"050", 50ULL, 10, L"", false, false}, + {L"100", 100ULL, 10, L"", false, false}, + {L"00100", 100ULL, 10, L"", false, false}, + {L"128", 128ULL, 10, L"", false, false}, + {L"1000", 1000ULL, 10, L"", false, false}, + {L"65535", 65535ULL, 10, L"", false, false}, + {L"65536", 65536ULL, 10, L"", false, false}, + {L"65537", 65537ULL, 10, L"", false, false}, + {L"4294967295", 4294967295ULL, 10, L"", false, false}, + {L"4294967296", 4294967296ULL, 10, L"", false, false}, + {L"4294967297", 4294967297ULL, 10, L"", false, false}, + {L"18446744073709551614", 18446744073709551614ULL, 10, L"", false, false}, + {L"18446744073709551615", 18446744073709551615ULL, 10, L"", false, false}, + {L" 1024", 0ULL, 10, L" 1024", false, false}, + {L" 1024", 1024ULL, 10, L"", false, true}, + {L" \t 1024", 0ULL, 10, L" \t 1024", false, false}, + {L" \t 1024", 1024ULL, 10, L"", false, true}, + {L" 1024foo", 0ULL, 10, L" 1024foo", false, false}, + {L" 1024foo", 1024ULL, 10, L"foo", false, true}, + {L" \t 1024foo", 0ULL, 10, L" \t 1024foo", false, false}, + {L" \t 1024foo", 1024ULL, 10, L"foo", false, true}, + + {L"", 0ULL, 2, L"", false, false}, + {L"0", 0ULL, 2, L"", false, false}, + {L"1", 1ULL, 2, L"", false, false}, + {L"2", 0ULL, 2, L"2", false, false}, + {L"101001011", 0b101001011ULL, 2, L"", false, false}, + {L"001011001101", 0b001011001101ULL, 2, L"", false, false}, + + {L"", 0ULL, 8, L"", false, false}, + {L"0", 0ULL, 8, L"", false, false}, + {L"1", 1ULL, 8, L"", false, false}, + {L"7", 7ULL, 8, L"", false, false}, + {L"8", 0ULL, 8, L"8", false, false}, + {L"54321760123", 054321760123ULL, 8, L"", false, false}, + {L"54012418", 05401241ULL, 8, L"8", false, false}, + {L"01256252foo", 01256252ULL, 8, L"foo", false, false}, + + {L"", 0ULL, 16, L"", false, false}, + {L"0", 0ULL, 16, L"", false, false}, + {L"9", 9ULL, 16, L"", false, false}, + {L"a", 10ULL, 16, L"", false, false}, + {L"f", 15ULL, 16, L"", false, false}, + {L"g", 0ULL, 16, L"g", false, false}, + {L"A", 10ULL, 16, L"", false, false}, + {L"F", 15ULL, 16, L"", false, false}, + {L"G", 0ULL, 16, L"G", false, false}, + {L"FFFF", 0xFFFFULL, 16, L"", false, false}, + {L"54FB91", 0x54FB91ULL, 16, L"", false, false}, + {L"abcdef00123", 0xabcdef00123ULL, 16, L"", false, false}, + {L"0a65bC3aDB1", 0x0a65bC3aDB1ULL, 16, L"", false, false}, + {L"0a65bC3aDB1g", 0x0a65bC3aDB1ULL, 16, L"g", false, false}, + {L"0a65bC3aDB1G", 0x0a65bC3aDB1ULL, 16, L"G", false, false}, + {L"0a65bC3aDB1z", 0x0a65bC3aDB1ULL, 16, L"z", false, false}, + {L"0a65bC3aDB1Z", 0x0a65bC3aDB1ULL, 16, L"Z", false, false}, + }; + + size_t len = sizeof(helper_tab) / sizeof(IntegerHelper); + make_str_to_int_tests(&to_ull, &to_ull, helper_tab, len); +} + + + +void test_text48() +{ + reset_test_counter("to_ll"); + + IntegerHelper helper_tab[] = { + {L"", 0LL, 10, L"", false, false}, + {L"0", 0LL, 10, L"", false, false}, + {L"-0", 0LL, 10, L"", false, false}, + {L"00", 0LL, 10, L"", false, false}, + {L"-00", 0LL, 10, L"", false, false}, + {L"100", 100LL, 10, L"", false, false}, + {L"-100", -100LL, 10, L"", false, false}, + {L"00100", 100LL, 10, L"", false, false}, + {L"-00100", -100LL, 10, L"", false, false}, + {L"128", 128LL, 10, L"", false, false}, + {L"-128", -128LL, 10, L"", false, false}, + {L"1000", 1000LL, 10, L"", false, false}, + {L"-1000", -1000LL, 10, L"", false, false}, + {L"65535", 65535LL, 10, L"", false, false}, + {L"65536", 65536LL, 10, L"", false, false}, + {L"65537", 65537LL, 10, L"", false, false}, + {L"-65535", -65535LL, 10, L"", false, false}, + {L"-65536", -65536LL, 10, L"", false, false}, + {L"-65537", -65537LL, 10, L"", false, false}, + {L"4294967295", 4294967295LL, 10, L"", false, false}, + {L"4294967296", 4294967296LL, 10, L"", false, false}, + {L"4294967297", 4294967297LL, 10, L"", false, false}, + {L"-4294967295", -4294967295LL, 10, L"", false, false}, + {L"-4294967296", -4294967296LL, 10, L"", false, false}, + {L"-4294967297", -4294967297LL, 10, L"", false, false}, + + {L"9223372036854775806", 9223372036854775806LL, 10, L"", false, false}, + {L"9223372036854775807", 9223372036854775807LL, 10, L"", false, false}, + {L"9223372036854775808", 0LL, 10, L"", true, false}, + + {L"-9223372036854775807", -9223372036854775807LL, 10, L"", false, false}, + {L"-9223372036854775808", std::numeric_limits::min(), 10, L"", false, false}, // gcc and clang not allow -9223372036854775808LL as a literal + {L"-9223372036854775809", 0LL, 10, L"", true, false}, + + {L"123456789876543210123", 0LL, 10, L"", true, false}, + {L"-123456789876543210123", 0LL, 10, L"", true, false}, + + {L" -1024", 0LL, 10, L" -1024", false, false}, + {L" -1024", -1024LL, 10, L"", false, true}, + {L" \t -1024", 0LL, 10, L" \t -1024", false, false}, + {L" \t -1024", -1024LL, 10, L"", false, true}, + {L" -1024foo", 0LL, 10, L" -1024foo", false, false}, + {L" -1024foo", -1024LL, 10, L"foo", false, true}, + {L" \t -1024foo", 0LL, 10, L" \t -1024foo", false, false}, + {L" \t -1024foo", -1024LL, 10, L"foo", false, true}, + + {L"", 0LL, 2, L"", false, false}, + {L"0", 0LL, 2, L"", false, false}, + {L"-0", 0LL, 2, L"", false, false}, + {L"1", 1LL, 2, L"", false, false}, + {L"2", 0LL, 2, L"2", false, false}, + {L"-1", -1LL, 2, L"", false, false}, + {L"-2", 0LL, 2, L"2", false, false}, + {L"-101001011", -0b101001011LL, 2, L"", false, false}, + {L"-001011001101", -0b001011001101LL, 2, L"", false, false}, + + {L"", 0LL, 8, L"", false, false}, + {L"0", 0LL, 8, L"", false, false}, + {L"-0", 0LL, 8, L"", false, false}, + {L"1", 1LL, 8, L"", false, false}, + {L"7", 7LL, 8, L"", false, false}, + {L"8", 0LL, 8, L"8", false, false}, + {L"-1", -1LL, 8, L"", false, false}, + {L"-7", -7LL, 8, L"", false, false}, + {L"-8", 0LL, 8, L"8", false, false}, + {L"54321760123", 054321760123LL, 8, L"", false, false}, + {L"54012418", 05401241LL, 8, L"8", false, false}, + {L"01256252foo", 01256252LL, 8, L"foo", false, false}, + {L"-54321760123", -054321760123LL, 8, L"", false, false}, + {L"-54012418", -05401241LL, 8, L"8", false, false}, + {L"-01256252foo", -01256252LL, 8, L"foo", false, false}, + + {L"", 0LL, 16, L"", false, false}, + {L"0", 0LL, 16, L"", false, false}, + {L"-0", 0LL, 16, L"", false, false}, + {L"ab65c", 0xab65cLL, 16, L"", false, false}, + {L"-654FEc23", -0x654FEc23LL, 16, L"", false, false}, + {L"076af12b", 0x76af12bLL, 16, L"", false, false}, + {L"-076de12be", -0x076de12beLL, 16, L"", false, false}, + + {L"7FFFFFFFFFFFFFFF", 0x7FFFFFFFFFFFFFFFLL, 16, L"", false, false}, + {L"8000000000000000", 0LL, 16, L"", true, false}, + {L"8000000000000001", 0LL, 16, L"", true, false}, + {L"abcdef0123456789", 0LL, 16, L"", true, false}, + + {L"-7FFFFFFFFFFFFFFF", -0x7FFFFFFFFFFFFFFFLL, 16, L"", false, false}, + {L"-8000000000000000", std::numeric_limits::min(), 16, L"", false, false}, + {L"-8000000000000001", 0LL, 16, L"", true, false}, + {L"-abcdef0123456789", 0LL, 16, L"", true, false}, + + }; + + size_t len = sizeof(helper_tab) / sizeof(IntegerHelper); + make_str_to_int_tests(&to_ll, &to_ll, helper_tab, len); +} + +// IMPROVEME put integer tests for functions: to_ul(), to_ui(), to_l(), to_i(), to_ull_b(), to_ll_b(), to_ul_b(), to_ui_b(), to_l_b(), to_i_b() void make_tests() @@ -1969,6 +2142,8 @@ void make_tests() test_text44(); test_text45(); test_text46(); + test_text47(); + test_text48(); } diff --git a/tests/convert.h b/tests/convert.h index c8d6e66..0487ae6 100644 --- a/tests/convert.h +++ b/tests/convert.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2021, Tomasz Sowa + * Copyright (c) 2021-2022, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -35,12 +35,53 @@ #ifndef headerfile_pikotools_tests_convert #define headerfile_pikotools_tests_convert +#include "convert/convert.h" +#include "test.h" + + namespace pt { namespace pt_convert_tests { +template +struct IntegerHelper { + const wchar_t * val; + IntegerType val_int; + int base; + const wchar_t * after_str; + bool was_overflow; + bool allow_skip_whitechars; +}; + + + +template +void make_str_to_int_tests( + IntegerType (*convert_function1)(const typename StringType::value_type *, int, const typename StringType::value_type **, bool *, bool allow_skip_whitechars), + IntegerType (*convert_function2)(const StringType &, int, const typename StringType::value_type **, bool *, bool allow_skip_whitechars), + IntegerHelper * helper_tab, + size_t len) +{ + for(size_t i=0 ; i & helper = helper_tab[i]; + std::wstring str1 = helper.val; + const wchar_t * after; + bool was_overflow; + + test(convert_function1(str1.c_str(), helper.base, &after, &was_overflow, helper.allow_skip_whitechars), helper.val_int); + test(helper.after_str, after); + test(helper.was_overflow, was_overflow); + + test(convert_function2(str1, helper.base, &after, &was_overflow, helper.allow_skip_whitechars), helper.val_int); + test(helper.after_str, after); + test(helper.was_overflow, was_overflow); + } +} + + void make_tests();