From 8be827761347b7a0a4ce6e7bdfa6fd4585606b21 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Tue, 26 Feb 2013 12:31:00 -0500 Subject: Rewrite QUtil::int_to_string and QUtil::double_to_string Make them safer by avoiding any internal limits and replacing sprintf with std::ostringstream. --- libtests/qtest/qutil/qutil.out | 9 +++---- libtests/qutil.cc | 57 ++++-------------------------------------- 2 files changed, 9 insertions(+), 57 deletions(-) (limited to 'libtests') diff --git a/libtests/qtest/qutil/qutil.out b/libtests/qtest/qutil/qutil.out index a48e6266..c855e98e 100644 --- a/libtests/qtest/qutil/qutil.out +++ b/libtests/qtest/qutil/qutil.out @@ -4,11 +4,10 @@ 3.141590 3.142 1000.123000 -exception 1: Util::int_to_string has been called with a padding value greater than its internal limit -exception 2: Util::int_to_string has been called with a padding value greater than its internal limit -exception 3: Util::int_to_string has been called with a padding value greater than its internal limit -exception 4: Util::double_to_string has been called with a number and a decimal places specification that would break an internal limit -exception 5: Util::double_to_string has been called with a number and a decimal places specification that would break an internal limit +0.12340 +0.00012 +0.12346 +0.00012 one 7 compare okay diff --git a/libtests/qutil.cc b/libtests/qutil.cc index ad77f3ea..4e8c7362 100644 --- a/libtests/qutil.cc +++ b/libtests/qutil.cc @@ -19,58 +19,11 @@ void string_conversion_test() << QUtil::int_to_string(16059, -7) << std::endl << QUtil::double_to_string(3.14159) << std::endl << QUtil::double_to_string(3.14159, 3) << std::endl - << QUtil::double_to_string(1000.123, -1024) << std::endl; - - try - { - // int_to_string bounds error - std::cout << QUtil::int_to_string(1, 50) << std::endl; - } - catch (std::logic_error &e) - { - std::cout << "exception 1: " << e.what() << std::endl; - } - - try - { - // QUtil::int_to_string bounds error - std::cout << QUtil::int_to_string(1, -50) << std::endl; - } - catch (std::logic_error& e) - { - std::cout << "exception 2: " << e.what() << std::endl; - } - - try - { - // QUtil::int_to_string bounds error - std::cout << QUtil::int_to_string(-1, 49) << std::endl; - } - catch (std::logic_error& e) - { - std::cout << "exception 3: " << e.what() << std::endl; - } - - - try - { - // QUtil::double_to_string bounds error - std::cout << QUtil::double_to_string(3.14159, 1024) << std::endl; - } - catch (std::logic_error& e) - { - std::cout << "exception 4: " << e.what() << std::endl; - } - - try - { - // QUtil::double_to_string bounds error - std::cout << QUtil::double_to_string(1000.0, 95) << std::endl; - } - catch (std::logic_error& e) - { - std::cout << "exception 5: " << e.what() << std::endl; - } + << QUtil::double_to_string(1000.123, -1024) << std::endl + << QUtil::double_to_string(.1234, 5) << std::endl + << QUtil::double_to_string(.0001234, 5) << std::endl + << QUtil::double_to_string(.123456, 5) << std::endl + << QUtil::double_to_string(.000123456, 5) << std::endl; std::string embedded_null = "one"; embedded_null += '\0'; -- cgit v1.2.3-70-g09d2