diff --git a/include/etl/private/to_string_helper.h b/include/etl/private/to_string_helper.h index 04140678..496d56d0 100644 --- a/include/etl/private/to_string_helper.h +++ b/include/etl/private/to_string_helper.h @@ -88,7 +88,7 @@ namespace etl void add_boolean(const bool value, TIString& str, const etl::basic_format_spec& format, - bool append) + const bool append) { typedef typename TIString::value_type type; typedef typename TIString::iterator iterator; @@ -240,8 +240,31 @@ namespace etl /// Helper function for floating point integral and fractional. //*************************************************************************** template - void add_integral_fractional(const uint32_t integral, - const uint32_t fractional, + void add_integral_and_fractional(const uint32_t integral, + const uint32_t fractional, + TIString& str, + const etl::basic_format_spec& integral_format, + const etl::basic_format_spec& fractional_format, + const bool negative) + { + typedef typename TIString::value_type type; + + etl::private_to_string::add_integral(integral, str, integral_format, true, negative); + + if (fractional_format.get_precision() > 0) + { + str.push_back(type('.')); + etl::private_to_string::add_integral(fractional, str, fractional_format, true, false); + } + } + +#if ETL_USING_64BIT_TYPES + //*************************************************************************** + /// Helper function for floating point integral and fractional. + //*************************************************************************** + template + void add_integral_and_fractional(const uint64_t integral, + const uint64_t fractional, TIString& str, const etl::basic_format_spec& integral_format, const etl::basic_format_spec& fractional_format, @@ -257,15 +280,16 @@ namespace etl etl::private_to_string::add_integral(fractional, str, fractional_format, true, false); } } +#endif //*************************************************************************** /// Helper function for floating point. //*************************************************************************** template - void add_floating_point(T value, + void add_floating_point(const T value, TIString& str, const etl::basic_format_spec& format, - bool append) + const bool append) { typedef typename TIString::iterator iterator; typedef typename TIString::value_type type; @@ -313,24 +337,25 @@ namespace etl fractional = 0U; } - etl::private_to_string::add_integral_fractional(integral, fractional, str, integral_format, fractional_format, etl::is_negative(value)); + etl::private_to_string::add_integral_and_fractional(integral, fractional, str, integral_format, fractional_format, etl::is_negative(value)); } etl::private_to_string::add_alignment(str, start, format); } //*************************************************************************** - /// Helper function for divided integers. + /// Helper function for denominated integers. //*************************************************************************** template - void add_integral_denominated(T value, - uint32_t denominator, + void add_integral_denominated(const T value, + const uint32_t denominator_exponent, TIString& str, const etl::basic_format_spec& format, - bool append = false) + const bool append = false) { - typedef typename TIString::iterator iterator; - typedef typename TIString::value_type type; + typedef typename TIString::iterator iterator; + typedef typename TIString::value_type type; + typedef typename etl::make_unsigned::type working_t; if (!append) { @@ -339,20 +364,21 @@ namespace etl iterator start = str.end(); - uint32_t abs_value = etl::absolute(value); + // Caculate the denominator. + working_t denominator = 1U; - // Figure out how many decimal digits we have in the value. - uint32_t original_decimal_digits = 0U; - uint32_t multiplier = 1U; - - while (multiplier < denominator) + for (uint32_t i = 0U; i < denominator_exponent; ++i) { - multiplier *= 10U; - ++original_decimal_digits; + denominator *= 10U; } + working_t abs_value = etl::absolute(value); + + // Figure out how many decimal digits we have in the value. + const uint32_t& original_decimal_digits = denominator_exponent; + // How many decimal digits are we displaying. - uint32_t displayed_decimal_digits = (format.get_precision() > original_decimal_digits) ? original_decimal_digits : format.get_precision(); + const uint32_t displayed_decimal_digits = (format.get_precision() > original_decimal_digits) ? original_decimal_digits : format.get_precision(); // Format for the integral part. etl::basic_format_spec integral_format = format; @@ -362,30 +388,26 @@ namespace etl etl::basic_format_spec fractional_format = integral_format; fractional_format.precision(displayed_decimal_digits).width(displayed_decimal_digits).fill(type('0')).right(); - // We don't need to do this if the fractional part is zero. - if ((abs_value % denominator) != 0U) + // Do we need to check for rounding? + if (original_decimal_digits > displayed_decimal_digits) { - // Do we need to check for rounding? - if (original_decimal_digits > displayed_decimal_digits) + // Which digit to adjust? + uint32_t count = original_decimal_digits - fractional_format.get_width(); + + // The 'round-away-from-zero' value. + uint32_t rounding = 5U; + + while (count-- > 1U) { - // Which digit to adjust? - uint32_t count = original_decimal_digits - fractional_format.get_width(); + rounding *= 10U; + } - // The 'round-away-from-zero' value. - uint32_t rounding = 5U; - - while (count-- > 1U) - { - rounding *= 10U; - } - - abs_value += rounding; - } + abs_value += rounding; } // Split the value into integral and fractional. - uint32_t integral = abs_value / denominator; - uint32_t fractional = abs_value % denominator; + working_t integral = abs_value / denominator; + working_t fractional = abs_value % denominator; // Move the fractional part to the right place. uint32_t count = original_decimal_digits - fractional_format.get_width(); @@ -395,7 +417,7 @@ namespace etl } // Create the string. - etl::private_to_string::add_integral_fractional(integral, fractional, str, integral_format, fractional_format, etl::is_negative(value)); + etl::private_to_string::add_integral_and_fractional(integral, fractional, str, integral_format, fractional_format, etl::is_negative(value)); etl::private_to_string::add_alignment(str, start, format); } @@ -406,7 +428,7 @@ namespace etl void add_pointer(const volatile void* value, TIString& str, const etl::basic_format_spec& format, - bool append) + const bool append) { uintptr_t p = reinterpret_cast(value); @@ -420,7 +442,7 @@ namespace etl void add_string(const TIString& value, TIString& str, const etl::basic_format_spec& format, - bool append) + const bool append) { if (!append) { @@ -441,7 +463,7 @@ namespace etl void add_string_view(const TSringView& value, TIString& str, const etl::basic_format_spec& format, - bool append) + const bool append) { if (!append) { @@ -464,7 +486,7 @@ namespace etl const TIString& to_string(const bool value, TIString& str, const etl::basic_format_spec& format, - bool append = false) + const bool append = false) { etl::private_to_string::add_boolean(value, str, format, append); @@ -478,193 +500,101 @@ namespace etl const TIString& to_string(const volatile void* value, TIString& str, const etl::basic_format_spec& format, - bool append = false) + const bool append = false) { etl::private_to_string::add_pointer(value, str, format, append); return str; } -#if ETL_NOT_USING_64BIT_TYPES - +#if ETL_USING_64BIT_TYPES //*************************************************************************** - /// For signed integrals less than 64 bits. + /// For integrals less than 64 bits. //*************************************************************************** template typename etl::enable_if::value && - etl::is_signed::value && - !etl::is_same::value>::value, const TIString& > ::type - to_string(const T value, TIString& str, const etl::basic_format_spec& format, bool append = false) + !etl::is_same::value && + !etl::is_one_of::value, const TIString&>::type + to_string(const T value, TIString& str, const etl::basic_format_spec& format, const bool append = false) { - etl::private_to_string::add_integral(int32_t(value), str, format, append, etl::is_negative(value)); + typedef typename etl::conditional::value, int32_t, uint32_t>::type type; + + etl::private_to_string::add_integral(type(value), str, format, append, etl::is_negative(value)); return str; } //*************************************************************************** - /// For unsigned integrals less than 64 bits. + /// For 64 bit integrals. //*************************************************************************** template typename etl::enable_if::value && - etl::is_unsigned::value && - !etl::is_same::value>::value, const TIString& > ::type - to_string(const T value, TIString& str, const etl::basic_format_spec& format, bool append = false) + !etl::is_same::value && + etl::is_one_of::value, const TIString&>::type + to_string(const T value, TIString& str, const etl::basic_format_spec& format, const bool append = false) { - etl::private_to_string::add_integral(uint32_t(value), str, format, append, false); + etl::private_to_string::add_integral(value, str, format, append, etl::is_negative(value)); return str; } //*************************************************************************** - /// For signed integrals less than 64 bits. + /// For denominated integrals less than 64 bits. + //*************************************************************************** + template + typename etl::enable_if::value && + !etl::is_same::value && + !etl::is_one_of::value, const TIString&>::type + to_string(const T value, uint32_t denominator_exponant, TIString& str, const etl::basic_format_spec& format, const bool append = false) + { + typedef typename etl::conditional::value, int32_t, uint32_t>::type type; + + etl::private_to_string::add_integral_denominated(type(value), denominator_exponant, str, format, append); + + return str; + } + + //*************************************************************************** + /// For denominated 64 bit integrals. //*************************************************************************** template typename etl::enable_if::value&& - etl::is_signed::value && - !etl::is_same::value>::value, const TIString& > ::type - to_string(const T value, typename etl::make_unsigned::type denominator, TIString& str, const etl::basic_format_spec& format, bool append = false) + !etl::is_same::value&& + etl::is_one_of::value, const TIString&>::type + to_string(const T value, uint32_t denominator_exponant, TIString& str, const etl::basic_format_spec& format, const bool append = false) { - etl::private_to_string::add_integral_denominated(int32_t(value), denominator, str, format, append, etl::is_negative(value)); + etl::private_to_string::add_integral_denominated(value, denominator_exponant, str, format, append); return str; } - - //*************************************************************************** - /// For unsigned integrals less than 64 bits. - //*************************************************************************** - template - typename etl::enable_if::value&& - etl::is_unsigned::value && - !etl::is_same::value>::value, const TIString& > ::type - to_string(const T value, typename etl::make_unsigned::type denominator, TIString& str, const etl::basic_format_spec& format, bool append = false) - { - etl::private_to_string::add_integral_denominated(uint32_t(value), denominator, str, format, append, false); - - return str; - } - #else - //*************************************************************************** - /// For signed integrals less than 64 bits. + /// For integrals less than 64 bits. //*************************************************************************** template typename etl::enable_if::value && - etl::is_signed::value && - !etl::is_same::value && - !etl::is_same::value, const TIString&>::type - to_string(const T value, TIString& str, const etl::basic_format_spec& format, bool append = false) + !etl::is_same::value>::value, const TIString& > ::type + to_string(const T value, TIString& str, const etl::basic_format_spec& format, const bool append = false) { - etl::private_to_string::add_integral(int32_t(value), str, format, append, etl::is_negative(value)); + typedef typename etl::conditional::value, int32_t, uint32_t>::type type; + + etl::private_to_string::add_integral(type(value), str, format, append, false); return str; } //*************************************************************************** - /// For unsigned integrals less than 64 bits. + /// For denominated integrals less than 64 bits. //*************************************************************************** template typename etl::enable_if::value && - etl::is_unsigned::value && - !etl::is_same::value && - !etl::is_same::value, const TIString&>::type - to_string(const T value, TIString& str, const etl::basic_format_spec& format, bool append = false) + !etl::is_same::value>::value, const TIString& > ::type + to_string(const T value, uint32_t denominator_exponant, TIString& str, const etl::basic_format_spec& format, const bool append = false) { - etl::private_to_string::add_integral(uint32_t(value), str, format, append, false); + etl::private_to_string::add_integral_denominated(type(value), denominator_exponant, str, format, append, false); return str; } - - //*************************************************************************** - /// For signed 64 bit integrals. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_signed::value && - !etl::is_same::value && - etl::is_same::value, const TIString&>::type - to_string(const T value, TIString& str, const etl::basic_format_spec& format, bool append = false) - { - etl::private_to_string::add_integral(int64_t(value), str, format, append, etl::is_negative(value)); - - return str; - } - - //*************************************************************************** - /// For unsigned 64 bit integrals. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_unsigned::value && - !etl::is_same::value && - etl::is_same::value, const TIString&>::type - to_string(const T value, TIString& str, const etl::basic_format_spec& format, bool append = false) - { - etl::private_to_string::add_integral(uint64_t(value), str, format, append, false); - - return str; - } - - //*************************************************************************** - /// For signed integrals less than 64 bits. - //*************************************************************************** - template - typename etl::enable_if::value&& - etl::is_signed::value && - !etl::is_same::value && - !etl::is_same::value, const TIString&>::type - to_string(const T value, typename etl::make_unsigned::type denominator, TIString& str, const etl::basic_format_spec& format, bool append = false) - { - etl::private_to_string::add_integral_denominated(int32_t(value), denominator, str, format, append); - - return str; - } - - //*************************************************************************** - /// For unsigned integrals less than 64 bits. - //*************************************************************************** - template - typename etl::enable_if::value&& - etl::is_unsigned::value && - !etl::is_same::value && - !etl::is_same::value, const TIString&>::type - to_string(const T value, typename etl::make_unsigned::type denominator, TIString& str, const etl::basic_format_spec& format, bool append = false) - { - etl::private_to_string::add_integral_denominated(uint32_t(value), denominator, str, format, append); - - return str; - } - - //*************************************************************************** - /// For signed 64 bit integrals. - //*************************************************************************** - template - typename etl::enable_if::value&& - etl::is_signed::value && - !etl::is_same::value&& - etl::is_same::value, const TIString&>::type - to_string(const T value, typename etl::make_unsigned::type denominator, TIString& str, const etl::basic_format_spec& format, bool append = false) - { - etl::private_to_string::add_integral_denominated(int64_t(value), denominator, str, format, append, etl::is_negative(value)); - - return str; - } - - //*************************************************************************** - /// For unsigned 64 bit integrals. - //*************************************************************************** - template - typename etl::enable_if::value&& - etl::is_unsigned::value && - !etl::is_same::value&& - etl::is_same::value, const TIString&>::type - to_string(const T value, typename etl::make_unsigned::type denominator, TIString& str, const etl::basic_format_spec& format, bool append = false) - { - etl::private_to_string::add_integral(uint64_t(value), denominator, str, format, append, false); - - return str; - } - #endif //*************************************************************************** @@ -672,7 +602,7 @@ namespace etl //*************************************************************************** template typename etl::enable_if::value, const TIString&>::type - to_string(const T value, TIString& str, const etl::basic_format_spec& format, bool append = false) + to_string(const T value, TIString& str, const etl::basic_format_spec& format, const bool append = false) { etl::private_to_string::add_floating_point(value, str, format, append); diff --git a/include/etl/to_string.h b/include/etl/to_string.h index de09aec2..274f26ea 100644 --- a/include/etl/to_string.h +++ b/include/etl/to_string.h @@ -71,11 +71,11 @@ namespace etl //*************************************************************************** template typename etl::enable_if::value && !etl::is_same::value, const etl::istring&>::type - to_string(const T value, typename etl::make_unsigned::type denominator, etl::istring& str, bool append = false) + to_string(const T value, uint32_t denominator_exponent, etl::istring& str, bool append = false) { etl::format_spec format; - return private_to_string::to_string(value, denominator, str, format, append); + return private_to_string::to_string(value, denominator_exponent, str, format, append); } //*************************************************************************** @@ -84,9 +84,9 @@ namespace etl //*************************************************************************** template typename etl::enable_if::value && !etl::is_same::value, const etl::istring&>::type - to_string(const T value, typename etl::make_unsigned::type denominator, etl::istring& str, const etl::format_spec& format, bool append = false) + to_string(const T value, uint32_t denominator_exponent, etl::istring& str, const etl::format_spec& format, bool append = false) { - return private_to_string::to_string(value, denominator, str, format, append); + return private_to_string::to_string(value, denominator_exponent, str, format, append); } //*************************************************************************** diff --git a/include/etl/to_u16string.h b/include/etl/to_u16string.h index 9d117aa2..0765f7b8 100644 --- a/include/etl/to_u16string.h +++ b/include/etl/to_u16string.h @@ -71,11 +71,11 @@ namespace etl //*************************************************************************** template typename etl::enable_if::value && !etl::is_same::value, const etl::iu16string&>::type - to_string(const T value, typename etl::make_unsigned::type denominator, etl::iu16string& str, bool append = false) + to_string(const T value, uint32_t denominator_exponent, etl::iu16string& str, bool append = false) { etl::u16format_spec format; - return private_to_string::to_string(value, denominator, str, format, append); + return private_to_string::to_string(value, denominator_exponent, str, format, append); } //*************************************************************************** @@ -84,9 +84,9 @@ namespace etl //*************************************************************************** template typename etl::enable_if::value && !etl::is_same::value, const etl::iu16string&>::type - to_string(const T value, typename etl::make_unsigned::type denominator, etl::iu16string& str, const etl::u16format_spec& format, bool append = false) + to_string(const T value, uint32_t denominator_exponent, etl::iu16string& str, const etl::u16format_spec& format, bool append = false) { - return private_to_string::to_string(value, denominator, str, format, append); + return private_to_string::to_string(value, denominator_exponent, str, format, append); } diff --git a/include/etl/to_u32string.h b/include/etl/to_u32string.h index a17e6ba8..08c6cc06 100644 --- a/include/etl/to_u32string.h +++ b/include/etl/to_u32string.h @@ -71,11 +71,11 @@ namespace etl //*************************************************************************** template typename etl::enable_if::value && !etl::is_same::value, const etl::iu32string&>::type - to_string(const T value, typename etl::make_unsigned::type denominator, etl::iu32string& str, bool append = false) + to_string(const T value, uint32_t denominator_exponent, etl::iu32string& str, bool append = false) { etl::u32format_spec format; - return private_to_string::to_string(value, denominator, str, format, append); + return private_to_string::to_string(value, denominator_exponent, str, format, append); } //*************************************************************************** @@ -84,9 +84,9 @@ namespace etl //*************************************************************************** template typename etl::enable_if::value && !etl::is_same::value, const etl::iu32string&>::type - to_string(const T value, typename etl::make_unsigned::type denominator, etl::iu32string& str, const etl::u32format_spec& format, bool append = false) + to_string(const T value, uint32_t denominator_exponent, etl::iu32string& str, const etl::u32format_spec& format, bool append = false) { - return private_to_string::to_string(value, denominator, str, format, append); + return private_to_string::to_string(value, denominator_exponent, str, format, append); } //*************************************************************************** diff --git a/include/etl/to_wstring.h b/include/etl/to_wstring.h index 7f710674..a6bf4b64 100644 --- a/include/etl/to_wstring.h +++ b/include/etl/to_wstring.h @@ -71,11 +71,11 @@ namespace etl //*************************************************************************** template typename etl::enable_if::value && !etl::is_same::value, const etl::iwstring&>::type - to_string(const T value, typename etl::make_unsigned::type denominator, etl::iwstring& str, bool append = false) + to_string(const T value, uint32_t denominator_exponent, etl::iwstring& str, bool append = false) { etl::wformat_spec format; - return private_to_string::to_string(value, denominator, str, format, append); + return private_to_string::to_string(value, denominator_exponent, str, format, append); } //*************************************************************************** @@ -84,9 +84,9 @@ namespace etl //*************************************************************************** template typename etl::enable_if::value && !etl::is_same::value, const etl::iwstring&>::type - to_string(const T value, typename etl::make_unsigned::type denominator, etl::iwstring& str, const etl::wformat_spec& format, bool append = false) + to_string(const T value, uint32_t denominator_exponent, etl::iwstring& str, const etl::wformat_spec& format, bool append = false) { - return private_to_string::to_string(value, denominator, str, format, append); + return private_to_string::to_string(value, denominator_exponent, str, format, append); } //*************************************************************************** diff --git a/test/test_to_string.cpp b/test/test_to_string.cpp index 25888ba4..440915ca 100644 --- a/test/test_to_string.cpp +++ b/test/test_to_string.cpp @@ -440,9 +440,9 @@ namespace etl::string<20> result; int value = -1234567; - etl::to_string(value, 1000000, result); + etl::to_string(value, 6U, result); - CHECK_EQUAL(etl::string<20>("-1").c_str(), result.c_str()); + CHECK_EQUAL(etl::string<20>(STR("-1")).c_str(), result.c_str()); } //************************************************************************* @@ -453,9 +453,22 @@ namespace Format format = Format().precision(100); - etl::to_string(value, 1000000, result, format); + etl::to_string(value, 6U, result, format); - CHECK_EQUAL(etl::string<20>("-1.234560").c_str(), result.c_str()); + CHECK_EQUAL(etl::string<20>(STR("-1.234560")).c_str(), result.c_str()); + } + + //************************************************************************* + TEST(test_integer_denominator_huge_precision_64bit) + { + etl::string<20> result; + int64_t value = -9223372036854775808LL; + + Format format = Format().precision(100); + + etl::to_string(value, 12U, result, format); + + CHECK_EQUAL(etl::string<20>(STR("-9223372.036854775808")).c_str(), result.c_str()); } //************************************************************************* @@ -469,7 +482,7 @@ namespace Format format = Format().precision(4); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK_EQUAL(etl::string<20>(STR("-1.0000")).c_str(), result_i.c_str()); @@ -487,7 +500,7 @@ namespace Format format = Format().precision(4); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK_EQUAL(etl::string<20>(STR("0.0000")).c_str(), result_i.c_str()); @@ -505,7 +518,7 @@ namespace Format format = Format().precision(4); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK_EQUAL(etl::string<20>(STR("-0.0004")).c_str(), result_i.c_str()); @@ -523,7 +536,7 @@ namespace Format format = Format().precision(4); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK_EQUAL(etl::string<20>(STR("-123.0004")).c_str(), result_i.c_str()); @@ -541,7 +554,7 @@ namespace Format format = Format().precision(4); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK_EQUAL(etl::string<20>(STR("-123.0000")).c_str(), result_i.c_str()); @@ -559,7 +572,7 @@ namespace Format format = Format().precision(4); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK_EQUAL(etl::string<20>(STR("-123.0001")).c_str(), result_i.c_str()); @@ -577,7 +590,7 @@ namespace Format format = Format().precision(4).width(6).right(); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK_EQUAL(etl::string<20>(STR("-123.4568")).c_str(), result_i.c_str()); @@ -595,7 +608,7 @@ namespace Format format = Format().precision(4).width(15).right(); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK_EQUAL(etl::string<20>(STR(" -123.4568")).c_str(), result_i.c_str()); @@ -613,7 +626,7 @@ namespace Format format = Format().precision(4).right(); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK_EQUAL(etl::string<20>(STR("124.0000")).c_str(), result_i.c_str()); @@ -631,7 +644,7 @@ namespace Format format = Format().precision(4).right(); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK_EQUAL(etl::string<20>(STR("-124.0000")).c_str(), result_i.c_str()); diff --git a/test/test_to_u16string.cpp b/test/test_to_u16string.cpp index d8a55476..b8f364de 100644 --- a/test/test_to_u16string.cpp +++ b/test/test_to_u16string.cpp @@ -349,6 +349,61 @@ namespace CHECK(etl::u16string<20>(STR("Result true ")) == to_string(true, str, Format().precision(6).width(10).left().boolalpha(true), true)); } + //************************************************************************* + TEST(test_integer_denominator_default_format) + { + etl::u16string<20> result; + int value = -1234567; + + etl::to_string(value, 6U, result); + + CHECK(etl::u16string<20>(STR("-1")) == result); + } + + //************************************************************************* + TEST(test_integer_denominator_huge_precision) + { + etl::u16string<20> result; + int value = -1234560; + + Format format = Format().precision(100); + + etl::to_string(value, 6U, result, format); + + CHECK(etl::u16string<20>(STR("-1.234560")) == result); + } + + //************************************************************************* + TEST(test_integer_denominator_huge_precision_64bit) + { + etl::u16string<20> result; + int64_t value = -9223372036854775808LL; + + Format format = Format().precision(100); + + etl::to_string(value, 12U, result, format); + + CHECK(etl::u16string<20>(STR("-9223372.036854775808")) == result); + } + + //************************************************************************* + TEST(test_integer_denominator_zero_fractional) + { + etl::u16string<20> result_i; + int value_i = -1000000; + + etl::u16string<20> result_d; + double value_d = -1.000000; + + Format format = Format().precision(4); + + etl::to_string(value_i, 6U, result_i, format); + etl::to_string(value_d, result_d, format); + + CHECK(etl::u16string<20>(STR("-1.0000")) == result_i); + CHECK(result_d == result_i); + } + //************************************************************************* TEST(test_integer_denominator_zero_value) { @@ -360,7 +415,7 @@ namespace Format format = Format().precision(4); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::u16string<20>(STR("0.0000")) == result_i); @@ -378,11 +433,11 @@ namespace Format format = Format().precision(4); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::u16string<20>(STR("-0.0004")) == result_i); - CHECK(result_d == result_i); + CHECK(result_d == result_i); } //************************************************************************* @@ -396,7 +451,7 @@ namespace Format format = Format().precision(4); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::u16string<20>(STR("-123.0004")) == result_i); @@ -414,7 +469,7 @@ namespace Format format = Format().precision(4); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::u16string<20>(STR("-123.0000")) == result_i); @@ -432,7 +487,7 @@ namespace Format format = Format().precision(4); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::u16string<20>(STR("-123.0001")) == result_i); @@ -450,7 +505,7 @@ namespace Format format = Format().precision(4).width(6).right(); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::u16string<20>(STR("-123.4568")) == result_i); @@ -468,7 +523,7 @@ namespace Format format = Format().precision(4).width(15).right(); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::u16string<20>(STR(" -123.4568")) == result_i); @@ -486,7 +541,7 @@ namespace Format format = Format().precision(4).right(); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::u16string<20>(STR("124.0000")) == result_i); @@ -504,10 +559,10 @@ namespace Format format = Format().precision(4).right(); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); - CHECK(etl::u16string<20>(STR("-124.0000")) == result_i.c_str()); + CHECK(etl::u16string<20>(STR("-124.0000")) == result_i); CHECK(result_d == result_i); } }; diff --git a/test/test_to_u32string.cpp b/test/test_to_u32string.cpp index bfa098c9..e8ac328f 100644 --- a/test/test_to_u32string.cpp +++ b/test/test_to_u32string.cpp @@ -354,6 +354,61 @@ namespace CHECK(etl::u32string<20>(STR("Result true ")) == to_string(true, str, Format().precision(6).width(10).left().boolalpha(true), true)); } + //************************************************************************* + TEST(test_integer_denominator_default_format) + { + etl::u32string<20> result; + int value = -1234567; + + etl::to_string(value, 6U, result); + + CHECK(etl::u32string<20>(STR("-1")) == result); + } + + //************************************************************************* + TEST(test_integer_denominator_huge_precision) + { + etl::u32string<20> result; + int value = -1234560; + + Format format = Format().precision(100); + + etl::to_string(value, 6U, result, format); + + CHECK(etl::u32string<20>(STR("-1.234560")) == result); + } + + //************************************************************************* + TEST(test_integer_denominator_huge_precision_64bit) + { + etl::u32string<20> result; + int64_t value = -9223372036854775808LL; + + Format format = Format().precision(100); + + etl::to_string(value, 12U, result, format); + + CHECK(etl::u32string<20>(STR("-9223372.036854775808")) == result); + } + + //************************************************************************* + TEST(test_integer_denominator_zero_fractional) + { + etl::u32string<20> result_i; + int value_i = -1000000; + + etl::u32string<20> result_d; + double value_d = -1.000000; + + Format format = Format().precision(4); + + etl::to_string(value_i, 6U, result_i, format); + etl::to_string(value_d, result_d, format); + + CHECK(etl::u32string<20>(STR("-1.0000")) == result_i); + CHECK(result_d == result_i); + } + //************************************************************************* TEST(test_integer_denominator_zero_value) { @@ -365,7 +420,7 @@ namespace Format format = Format().precision(4); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::u32string<20>(STR("0.0000")) == result_i); @@ -383,7 +438,7 @@ namespace Format format = Format().precision(4); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::u32string<20>(STR("-0.0004")) == result_i); @@ -401,7 +456,7 @@ namespace Format format = Format().precision(4); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::u32string<20>(STR("-123.0004")) == result_i); @@ -419,7 +474,7 @@ namespace Format format = Format().precision(4); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::u32string<20>(STR("-123.0000")) == result_i); @@ -437,7 +492,7 @@ namespace Format format = Format().precision(4); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::u32string<20>(STR("-123.0001")) == result_i); @@ -455,7 +510,7 @@ namespace Format format = Format().precision(4).width(6).right(); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::u32string<20>(STR("-123.4568")) == result_i); @@ -473,7 +528,7 @@ namespace Format format = Format().precision(4).width(15).right(); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::u32string<20>(STR(" -123.4568")) == result_i); @@ -491,7 +546,7 @@ namespace Format format = Format().precision(4).right(); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::u32string<20>(STR("124.0000")) == result_i); @@ -509,10 +564,10 @@ namespace Format format = Format().precision(4).right(); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); - CHECK(etl::u32string<20>(STR("-124.0000")) == result_i.c_str()); + CHECK(etl::u32string<20>(STR("-124.0000")) == result_i); CHECK(result_d == result_i); } }; diff --git a/test/test_to_wstring.cpp b/test/test_to_wstring.cpp index 48c02d84..35ecf78e 100644 --- a/test/test_to_wstring.cpp +++ b/test/test_to_wstring.cpp @@ -416,6 +416,61 @@ namespace CHECK(compare == str); } + //************************************************************************* + TEST(test_integer_denominator_default_format) + { + etl::wstring<20> result; + int value = -1234567; + + etl::to_string(value, 6U, result); + + CHECK(etl::wstring<20>(STR("-1")) == result); + } + + //************************************************************************* + TEST(test_integer_denominator_huge_precision) + { + etl::wstring<20> result; + int value = -1234560; + + Format format = Format().precision(100); + + etl::to_string(value, 6U, result, format); + + CHECK(etl::wstring<20>(STR("-1.234560")) == result); + } + + //************************************************************************* + TEST(test_integer_denominator_huge_precision_64bit) + { + etl::wstring<20> result; + int64_t value = -9223372036854775808LL; + + Format format = Format().precision(100); + + etl::to_string(value, 12U, result, format); + + CHECK(etl::wstring<20>(STR("-9223372.036854775808")) == result); + } + + //************************************************************************* + TEST(test_integer_denominator_zero_fractional) + { + etl::wstring<20> result_i; + int value_i = -1000000; + + etl::wstring<20> result_d; + double value_d = -1.000000; + + Format format = Format().precision(4); + + etl::to_string(value_i, 6U, result_i, format); + etl::to_string(value_d, result_d, format); + + CHECK(etl::wstring<20>(STR("-1.0000")) == result_i); + CHECK(result_d == result_i); + } + //************************************************************************* TEST(test_integer_denominator_zero_value) { @@ -427,7 +482,7 @@ namespace Format format = Format().precision(4); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::wstring<20>(STR("0.0000")) == result_i); @@ -445,7 +500,7 @@ namespace Format format = Format().precision(4); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::wstring<20>(STR("-0.0004")) == result_i); @@ -463,7 +518,7 @@ namespace Format format = Format().precision(4); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::wstring<20>(STR("-123.0004")) == result_i); @@ -481,7 +536,7 @@ namespace Format format = Format().precision(4); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::wstring<20>(STR("-123.0000")) == result_i); @@ -499,7 +554,7 @@ namespace Format format = Format().precision(4); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::wstring<20>(STR("-123.0001")) == result_i); @@ -517,7 +572,7 @@ namespace Format format = Format().precision(4).width(6).right(); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::wstring<20>(STR("-123.4568")) == result_i); @@ -535,7 +590,7 @@ namespace Format format = Format().precision(4).width(15).right(); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::wstring<20>(STR(" -123.4568")) == result_i); @@ -553,7 +608,7 @@ namespace Format format = Format().precision(4).right(); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); CHECK(etl::wstring<20>(STR("124.0000")) == result_i); @@ -571,10 +626,10 @@ namespace Format format = Format().precision(4).right(); - etl::to_string(value_i, 1000000, result_i, format); + etl::to_string(value_i, 6U, result_i, format); etl::to_string(value_d, result_d, format); - CHECK(etl::wstring<20>(STR("-124.0000")) == result_i.c_str()); + CHECK(etl::wstring<20>(STR("-124.0000")) == result_i); CHECK(result_d == result_i); } };