From 42306e2ff8716ce9a8f57da791122cc88308890c Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Thu, 20 Jun 2019 19:48:53 -0400 Subject: QUtil: add unsigned int/string functions --- libtests/qtest/qutil/qutil.out | 13 +++++++++---- libtests/qutil.cc | 29 ++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 9 deletions(-) (limited to 'libtests') diff --git a/libtests/qtest/qutil/qutil.out b/libtests/qtest/qutil/qutil.out index c35f22e3..4d19617d 100644 --- a/libtests/qtest/qutil/qutil.out +++ b/libtests/qtest/qutil/qutil.out @@ -17,12 +17,17 @@ one compare okay -2147483648 to int: PASSED 2147483647 to int: PASSED -2147483648 to int threw: PASSED --2147483649 to int threw: PASSED -9999999999999999999999999 to int threw: PASSED +2147483648 to int threw (integer out of range converting 2147483648 from a 8-byte signed type to a 4-byte signed type): PASSED +-2147483649 to int threw (integer out of range converting -2147483649 from a 8-byte signed type to a 4-byte signed type): PASSED +9999999999999999999999999 to int threw (overflow/underflow converting 9999999999999999999999999 to 64-bit integer): PASSED 2147483648 to int: PASSED -2147483649 to int: PASSED -99999999999999999999999999999999999999999999999999 to int threw: PASSED +99999999999999999999999999999999999999999999999999 to int threw (overflow/underflow converting 99999999999999999999999999999999999999999999999999 to 64-bit integer): PASSED +16059 to int: PASSED +-16059 to int threw (underflow converting -16059 to 64-bit unsigned integer): PASSED +9999999999 to int threw (integer out of range converting 9999999999 from a 8-byte unsigned type to a 4-byte unsigned type): PASSED +16059 to int: PASSED +-16059 to int threw (underflow converting -16059 to 64-bit unsigned integer): PASSED ---- os wrapper before remove exception: remove file: No such file or directory diff --git a/libtests/qutil.cc b/libtests/qutil.cc index 27881c6e..0e0a063b 100644 --- a/libtests/qutil.cc +++ b/libtests/qutil.cc @@ -23,20 +23,22 @@ void test_to_number(char const* str, int_T wanted, bool error, bool threw = false; bool worked = false; int_T result = 0; + std::string msg; try { result = fn(str); worked = (wanted == result); } - catch (std::runtime_error const&) + catch (std::runtime_error const& e) { threw = true; + msg = e.what(); } if (threw) { if (error) { - std::cout << str << " to int threw: PASSED" << std::endl; + std::cout << str << " to int threw (" << msg << "): PASSED" << std::endl; } else { @@ -67,6 +69,16 @@ void test_to_ll(char const* str, long long wanted, bool error) test_to_number(str, wanted, error, QUtil::string_to_ll); } +void test_to_uint(char const* str, unsigned int wanted, bool error) +{ + test_to_number(str, wanted, error, QUtil::string_to_uint); +} + +void test_to_ull(char const* str, unsigned long long wanted, bool error) +{ + test_to_number(str, wanted, error, QUtil::string_to_ull); +} + void string_conversion_test() { std::cout << QUtil::int_to_string(16059) << std::endl @@ -105,6 +117,8 @@ void string_conversion_test() long long int_min_minus_1 = static_cast(INT_MIN) - 1; std::string int_max_plus_1_str = QUtil::int_to_string(int_max_plus_1); std::string int_min_minus_1_str = QUtil::int_to_string(int_min_minus_1); + std::string small_positive = QUtil::uint_to_string(16059U); + std::string small_negative = QUtil::int_to_string(-16059); test_to_int(int_min_str.c_str(), INT_MIN, false); test_to_int(int_max_str.c_str(), INT_MAX, false); test_to_int(int_max_plus_1_str.c_str(), 0, true); @@ -113,6 +127,11 @@ void string_conversion_test() test_to_ll(int_max_plus_1_str.c_str(), int_max_plus_1, false); test_to_ll(int_min_minus_1_str.c_str(), int_min_minus_1, false); test_to_ll("99999999999999999999999999999999999999999999999999", 0, true); + test_to_uint(small_positive.c_str(), 16059U, false); + test_to_uint(small_negative.c_str(), 0, true); + test_to_uint("9999999999", 0, true); + test_to_ull(small_positive.c_str(), 16059U, false); + test_to_ull(small_negative.c_str(), 0, true); } void os_wrapper_test() @@ -159,7 +178,7 @@ void getenv_test() static void print_utf8(unsigned long val) { std::string result = QUtil::toUTF8(val); - std::cout << "0x" << QUtil::int_to_string_base(val, 16) << " ->"; + std::cout << "0x" << QUtil::uint_to_string_base(val, 16) << " ->"; if (val < 0xfffe) { std::cout << " " << result; @@ -199,7 +218,7 @@ void to_utf8_test() static void print_utf16(unsigned long val) { std::string result = QUtil::toUTF16(val); - std::cout << "0x" << QUtil::int_to_string_base(val, 16) << " ->"; + std::cout << "0x" << QUtil::uint_to_string_base(val, 16) << " ->"; for (std::string::iterator iter = result.begin(); iter != result.end(); ++iter) { @@ -249,7 +268,7 @@ void transcoding_test(std::string (*to_utf8)(std::string const&), std::string back; for (int i = 128; i <= last; ++i) { - in.at(0) = static_cast(i); + in.at(0) = static_cast(static_cast(i)); out = (*to_utf8)(in); std::string wanted = (out == "\xef\xbf\xbd") ? unknown : in; back = (*from_utf8)(out, '?'); -- cgit v1.2.3-54-g00ecf