From 9ef75bd89ec21150817b49bcfab33b5d077bf6cb Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 14 Apr 2019 19:26:55 +0100 Subject: [PATCH] Final unit tests for all string types. --- .vs/.gitignore | 1 + .vs/ProjectSettings.json | 3 + CMakeSettings.json | 17 + include/etl/basic_format_spec.h | 73 +++- include/etl/private/to_string_helper.h | 484 ++++++++++++++++++++++--- include/etl/to_string.h | 98 +---- include/etl/to_u16string.h | 96 +---- include/etl/to_u32string.h | 96 +---- include/etl/to_wstring.h | 96 +---- test/test_to_string.cpp | 135 ++++++- test/test_to_u16string.cpp | 73 +++- test/test_to_u32string.cpp | 69 +++- test/test_to_wstring.cpp | 133 ++++++- 13 files changed, 948 insertions(+), 426 deletions(-) create mode 100644 .vs/.gitignore create mode 100644 .vs/ProjectSettings.json create mode 100644 CMakeSettings.json diff --git a/.vs/.gitignore b/.vs/.gitignore new file mode 100644 index 00000000..35226880 --- /dev/null +++ b/.vs/.gitignore @@ -0,0 +1 @@ +/slnx.sqlite diff --git a/.vs/ProjectSettings.json b/.vs/ProjectSettings.json new file mode 100644 index 00000000..38c5f003 --- /dev/null +++ b/.vs/ProjectSettings.json @@ -0,0 +1,3 @@ +{ + "CurrentProjectSetting": "x86-Debug" +} \ No newline at end of file diff --git a/CMakeSettings.json b/CMakeSettings.json new file mode 100644 index 00000000..9d0eaab8 --- /dev/null +++ b/CMakeSettings.json @@ -0,0 +1,17 @@ +{ + "configurations": [ + { + "name": "x86-Debug", + "generator": "Ninja", + "configurationType": "Debug", + "inheritEnvironments": [ + "msvc_x86" + ], + "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}", + "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}", + "cmakeCommandArgs": "", + "buildCommandArgs": "-v", + "ctestCommandArgs": "" + } + ] +} \ No newline at end of file diff --git a/include/etl/basic_format_spec.h b/include/etl/basic_format_spec.h index 38d48b48..ca3da502 100644 --- a/include/etl/basic_format_spec.h +++ b/include/etl/basic_format_spec.h @@ -55,8 +55,10 @@ namespace etl basic_format_spec() : base_(10) , width_(0) + , precision_(0) , upper_case_(true) , left_justified_(false) + , boolalpha_(false) , fill_(typename TString::value_type(' ')) { @@ -111,10 +113,11 @@ namespace etl base(16); return *this; } + //*************************************************************************** /// Gets the base. //*************************************************************************** - uint32_t base() const + uint32_t get_base() const { return base_; } @@ -132,11 +135,29 @@ namespace etl //*************************************************************************** /// Gets the width. //*************************************************************************** - uint32_t width() const + uint32_t get_width() const { return width_; } + //*************************************************************************** + /// Sets the precision. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& precision(const uint32_t w) + { + precision_ = static_cast(w); + return *this; + } + + //*************************************************************************** + /// Gets the precision. + //*************************************************************************** + uint32_t get_precision() const + { + return precision_; + } + //*************************************************************************** /// Sets the upper case flag. /// \return A reference to the basic_format_spec. @@ -150,7 +171,7 @@ namespace etl //*************************************************************************** /// Gets the upper case flag. //*************************************************************************** - bool upper_case() const + bool is_upper_case() const { return upper_case_; } @@ -168,7 +189,7 @@ namespace etl //*************************************************************************** /// Gets the fill character. //*************************************************************************** - typename TString::value_type fill() const + typename TString::value_type get_fill() const { return fill_; } @@ -177,26 +198,64 @@ namespace etl /// Sets the left justify flag. /// \return A reference to the basic_format_spec. //*************************************************************************** - basic_format_spec& left_justified(const bool l) + basic_format_spec& left() { - left_justified_ = l; + left_justified_ = true; return *this; } //*************************************************************************** /// Gets the left justify flag. //*************************************************************************** - bool left_justified() const + bool is_left() const { return left_justified_; } + //*************************************************************************** + /// Sets the right justify flag. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& right() + { + left_justified_ = false; + return *this; + } + + //*************************************************************************** + /// Gets the right justify flag. + //*************************************************************************** + bool is_right() const + { + return !left_justified_; + } + + //*************************************************************************** + /// Sets the bool alpha flag. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& boolalpha(bool b) + { + boolalpha_ = b; + return *this; + } + + //*************************************************************************** + /// Gets the boolalpha flag. + //*************************************************************************** + bool is_boolalpha() const + { + return boolalpha_; + } + private: uint_least8_t base_; uint_least8_t width_; + uint_least8_t precision_; bool upper_case_; bool left_justified_; + bool boolalpha_; typename TString::value_type fill_; }; } diff --git a/include/etl/private/to_string_helper.h b/include/etl/private/to_string_helper.h index ce4dfd50..76595dfc 100644 --- a/include/etl/private/to_string_helper.h +++ b/include/etl/private/to_string_helper.h @@ -33,87 +33,461 @@ SOFTWARE. ///\ingroup private +#include + #include "../platform.h" #include "../absolute.h" #include "../negative.h" #include "../basic_format_spec.h" #include "../type_traits.h" +#include "../container.h" #include "../stl/algorithm.h" #include "../stl/iterator.h" +#include "../stl/limits.h" namespace etl { - //*************************************************************************** - /// Helper function for integrals. - //*************************************************************************** - template - typename etl::enable_if::value, TIString&>::type - to_string_helper(T value, - TIString& str, - const etl::basic_format_spec& format, - const bool append) + namespace private_to_string { - typedef typename TIString::value_type type; - typedef typename TIString::iterator iterator; - - const bool negative = etl::is_negative(value); - - if (!append) + //*************************************************************************** + /// Helper function for left/right alignment. + //*************************************************************************** + template + void add_alignment(TIString& str, typename TIString::iterator position, const etl::basic_format_spec& format) { - str.clear(); - } + uint32_t length = static_cast(std::distance(position, str.end())); - iterator start = str.end(); - - if (value == 0) - { - str.push_back(type('0')); - } - else - { - // Extract the digits, in reverse order. - while (value != 0) + if (length < format.get_width()) { - T remainder = etl::absolute(value % T(format.base())); - str.push_back((remainder > 9) ? (format.upper_case() ? type('A' + (remainder - 10)) : type('a' + (remainder - 10))) : type('0' + remainder)); - value = value / T(format.base()); - } + uint32_t fill_length = format.get_width() - length; - // If number is negative, append '-' - if ((format.base() == 10) && negative) - { - str.push_back(type('-')); - } - - // Reverse the string we appended. - std::reverse(start, str.end()); - } - - // Do we have a width specification? - if (format.width() != 0) - { - uint32_t length = static_cast(std::distance(start, str.end())); - - // Is the length of the string less than the width? - if (length < format.width()) - { - uint32_t fill_length = format.width() - length; - - if (format.left_justified()) + if (format.is_left()) { // Insert fill characters on the right. - str.insert(str.end(), fill_length, format.fill()); + str.insert(str.end(), fill_length, format.get_fill()); } else { // Insert fill characters on the left. - str.insert(start, fill_length, format.fill()); + str.insert(position, fill_length, format.get_fill()); } } } - return str; + //*************************************************************************** + /// Helper function for booleans. + //*************************************************************************** + template + void add_boolean(const bool value, + TIString& str, + const etl::basic_format_spec& format, + const bool append) + { + typedef typename TIString::value_type type; + typedef typename TIString::iterator iterator; + + static const type t[] = { 't', 'r', 'u', 'e' }; + static const type f[] = { 'f', 'a', 'l', 's', 'e' }; + + if (!append) + { + str.clear(); + } + + iterator start = str.end(); + + if (format.is_boolalpha()) + { + if (value) + { + str.insert(str.end(), etl::begin(t), etl::end(t)); + } + else + { + str.insert(str.end(), etl::begin(f), etl::end(f)); + } + } + else + { + if (value) + { + str.push_back(type('1')); + } + else + { + str.push_back(type('0')); + } + } + + etl::private_to_string::add_alignment(str, start, format); + } + + //*************************************************************************** + /// Helper function for integrals. + //*************************************************************************** + template + void add_integral(T value, + TIString& str, + const etl::basic_format_spec& format, + const bool append) + { + typedef typename TIString::value_type type; + typedef typename TIString::iterator iterator; + + const bool negative = etl::is_negative(value); + + if (!append) + { + str.clear(); + } + + iterator start = str.end(); + + if (value == 0) + { + str.push_back(type('0')); + } + else + { + // Extract the digits, in reverse order. + while (value != 0) + { + T remainder = etl::absolute(value % T(format.get_base())); + str.push_back((remainder > 9) ? (format.is_upper_case() ? type('A' + (remainder - 10)) : type('a' + (remainder - 10))) : type('0' + remainder)); + value = value / T(format.get_base()); + } + + // If number is negative, append '-' + if ((format.get_base() == 10) && negative) + { + str.push_back(type('-')); + } + + // Reverse the string we appended. + std::reverse(start, str.end()); + } + + etl::private_to_string::add_alignment(str, start, format); + } + + //*************************************************************************** + /// Helper function for floating point nan and inf. + //*************************************************************************** + template + void add_nan_inf(const bool not_a_number, + const bool infinity, + TIString& str) + { + typedef typename TIString::value_type type; + + static const type n[] = { 'n', 'a', 'n' }; + static const type i[] = { 'i', 'n', 'f' }; + + if (not_a_number) + { + str.insert(str.end(), etl::begin(n), etl::end(n)); + } + else if (infinity) + { + str.insert(str.end(), etl::begin(i), etl::end(i)); + } + } + + //*************************************************************************** + /// Helper function for floating point integral and fractional. + //*************************************************************************** + template + void add_integral_fractional(const int64_t integral, + const int64_t fractional, + TIString& str, + const etl::basic_format_spec& format) + { + typedef typename TIString::value_type type; + + etl::private_to_string::add_integral(integral, str, format, true); + + if (format.get_precision() > 0) + { + str.push_back(type('.')); + etl::private_to_string::add_integral(fractional, str, format, true); + } + } + + //*************************************************************************** + /// Helper function for floating point. + //*************************************************************************** + template + void add_floating_point(T value, + TIString& str, + const etl::basic_format_spec& format, + const bool append) + { + typedef typename TIString::iterator iterator; + + if (!append) + { + str.clear(); + } + + iterator start = str.end(); + + if (isnan(value) || isinf(value)) + { + etl::private_to_string::add_nan_inf(isnan(value), isinf(value), str); + } + else + { + // Make sure we format the two halves correctly. + uint32_t max_precision = std::numeric_limits::digits10; + + etl::basic_format_spec local_format = format; + local_format.decimal().width(0).precision(format.get_precision() > max_precision ? max_precision : format.get_precision()); + + int64_t multiplier = 1; + + for (uint32_t i = 0; i < local_format.get_precision(); ++i) + { + multiplier *= 10U; + } + + T f_integral = (value < T(0.0) ? ceil(value) : floor(value)); + int64_t integral = static_cast(f_integral); + int64_t fractional = static_cast(round((value - f_integral) * multiplier)); + + etl::private_to_string::add_integral_fractional(integral, fractional, str, local_format); + } + + etl::private_to_string::add_alignment(str, start, format); + } + + //*************************************************************************** + /// Helper function for pointers. + //*************************************************************************** + template + void add_pointer(const volatile void* value, + TIString& str, + const etl::basic_format_spec& format, + const bool append) + { + uintptr_t p = reinterpret_cast(value); + + return etl::private_to_string::add_integral(p, str, format, append); + } + + //********************************************************************************************************* + + //*************************************************************************** + /// For booleans. Default format spec. + //***************************************************************************etl::basic_format_spec + template + const TIString& to_string(const bool value, + TIString& str, + const bool append = false) + { + etl::basic_format_spec format; + + etl::private_to_string::add_boolean(value, str, format, append); + + return str; + } + + //*************************************************************************** + /// For booleans. Supplied format spec. + //*************************************************************************** + template + const TIString& to_string(const bool value, + TIString& str, + const etl::basic_format_spec& format, + const bool append = false) + { + etl::private_to_string::add_boolean(value, str, format, append); + + return str; + } + + //*************************************************************************** + /// For pointers. Default format spec. + //*************************************************************************** + template + const TIString& to_string(const volatile void* value, + TIString& str, + const bool append = false) + { + etl::basic_format_spec format; + + etl::private_to_string::add_pointer(value, str, format, append); + + return str; + } + + //*************************************************************************** + /// For pointers. Supplied format spec. + //*************************************************************************** + template + const TIString& to_string(const volatile void* value, + TIString& str, + const etl::basic_format_spec& format, + const bool append = false) + { + etl::private_to_string::add_pointer(value, str, format, append); + + return str; + } + + //*************************************************************************** + /// For signed integrals less than 64 bits. Default format spec. + //*************************************************************************** + 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 bool append = false) + { + etl::basic_format_spec format; + + etl::private_to_string::add_integral(int32_t(value), str, format, append); + + return str; + } + + //*************************************************************************** + /// For signed integrals less than 64 bits. Supplied format spec. + //*************************************************************************** + 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, const bool append = false) + { + etl::private_to_string::add_integral(int32_t(value), str, format, append); + + return str; + } + + //*************************************************************************** + /// For unsigned integrals less then 64 bits. Default format spec. + //*************************************************************************** + 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 bool append = false) + { + etl::basic_format_spec format; + + etl::private_to_string::add_integral(uint32_t(value), str, format, append); + + return str; + } + + //*************************************************************************** + /// For unsigned integrals less than 64 bits. Supplied format spec. + //*************************************************************************** + 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, const bool append = false) + { + etl::private_to_string::add_integral(uint32_t(value), str, format, append); + + return str; + } + + //*************************************************************************** + /// For signed 64 bit integrals. Default format spec. + //*************************************************************************** + 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 bool append = false) + { + etl::basic_format_spec format; + + etl::private_to_string::add_integral(int64_t(value), str, format, append); + + return str; + } + + //*************************************************************************** + /// For signed 64 bit integrals. Supplied format spec. + //*************************************************************************** + 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, const bool append = false) + { + etl::private_to_string::add_integral(int64_t(value), str, format, append); + + return str; + } + + //*************************************************************************** + /// For unsigned 64 bit integrals. Default format spec. + //*************************************************************************** + 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 bool append = false) + { + etl::basic_format_spec format; + + etl::private_to_string::add_integral(uint64_t(value), str, format, append); + + return str; + } + + //*************************************************************************** + /// For unsigned 64 bit integrals. Supplied format spec. + //*************************************************************************** + 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, const bool append = false) + { + etl::private_to_string::add_integral(uint64_t(value), str, format, append); + + return str; + } + + //*************************************************************************** + /// For floating point. Default format spec. + //*************************************************************************** + template + typename etl::enable_if::value, const TIString&>::type + to_string(const T value, TIString& str, const bool append = false) + { + etl::basic_format_spec format; + + etl::private_to_string::add_floating_point(value, str, format, append); + + return str; + } + + //*************************************************************************** + /// For floating point. Supplied format spec. + //*************************************************************************** + template + typename etl::enable_if::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_floating_point(value, str, format, append); + + return str; + } } } diff --git a/include/etl/to_string.h b/include/etl/to_string.h index 058ac099..55076cfd 100644 --- a/include/etl/to_string.h +++ b/include/etl/to_string.h @@ -39,110 +39,28 @@ SOFTWARE. #include "format_spec.h" #include "private/to_string_helper.h" +#include + namespace etl { //*************************************************************************** - /// For signed integrals less than 64 bits. Default format spec. + /// Default format spec. //*************************************************************************** template - typename etl::enable_if::value && - etl::is_signed::value && - !etl::is_same::value, const etl::istring&>::type - to_string(const T value, etl::istring& str, const bool append = false) + const etl::istring& to_string(const T value, etl::istring& str, const bool append = false) { etl::format_spec format; - return to_string_helper(int32_t(value), str, format, append); + return private_to_string::to_string(value, str, format, append); } //*************************************************************************** - /// For signed integrals less than 64 bits. Supplied format spec. + /// Supplied format spec. //*************************************************************************** template - typename etl::enable_if::value && - etl::is_signed::value && - !etl::is_same::value, const etl::istring&>::type - to_string(const T value, etl::istring& str, const etl::format_spec& format, const bool append = false) + const etl::istring& to_string(const T value, etl::istring& str, const etl::format_spec& format, const bool append = false) { - return to_string_helper(int32_t(value), str, format, append); - } - - //*************************************************************************** - /// For unsigned integrals less then 64 bits. Default format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_unsigned::value && - !etl::is_same::value, const etl::istring&>::type - to_string(const T value, etl::istring& str, const bool append = false) - { - etl::format_spec format; - - return to_string_helper(uint32_t(value), str, format, append); - } - - //*************************************************************************** - /// For unsigned integrals less than 64 bits. Supplied format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_unsigned::value && - !etl::is_same::value, const etl::istring&>::type - to_string(const T value, etl::istring& str, const etl::format_spec& format, const bool append = false) - { - return to_string_helper(uint32_t(value), str, format, append); - } - - //*************************************************************************** - /// For signed 64 bit integrals. Default format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_signed::value && - etl::is_same::value, const etl::istring&>::type - to_string(const T value, etl::istring& str, const bool append = false) - { - etl::format_spec format; - - return to_string_helper(int64_t(value), str, format, append); - } - - //*************************************************************************** - /// For signed 64 bit integrals. Supplied format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_signed::value && - etl::is_same::value, const etl::istring&>::type - to_string(const T value, etl::istring& str, const etl::format_spec& format, const bool append = false) - { - return to_string_helper(int64_t(value), str, format, append); - } - - //*************************************************************************** - /// For unsigned 64 bit integrals. Default format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_unsigned::value && - etl::is_same::value, const etl::istring&>::type - to_string(const T value, etl::istring& str, const bool append = false) - { - etl::format_spec format; - - return to_string_helper(uint64_t(value), str, format, append); - } - - //*************************************************************************** - /// For unsigned 64 bit integrals. Supplied format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_unsigned::value && - etl::is_same::value, const etl::istring&>::type - to_string(const T value, etl::istring& str, const etl::format_spec& format, const bool append = false) - { - return to_string_helper(uint64_t(value), str, format, append); + return private_to_string::to_string(value, str, format, append); } } diff --git a/include/etl/to_u16string.h b/include/etl/to_u16string.h index 46bfbb59..58e47866 100644 --- a/include/etl/to_u16string.h +++ b/include/etl/to_u16string.h @@ -42,107 +42,23 @@ SOFTWARE. namespace etl { //*************************************************************************** - /// For signed integrals less than 64 bits. Default format spec. + /// Default format spec. //*************************************************************************** template - typename etl::enable_if::value && - etl::is_signed::value && - !etl::is_same::value, etl::iu16string&>::type - to_u16string(const T value, etl::iu16string& str, const bool append = false) + const etl::iu16string& to_u16string(const T value, etl::iu16string& str, const bool append = false) { etl::u16format_spec format; - return to_string_helper(int32_t(value), str, format, append); + return etl::private_to_string::to_string(value, str, format, append); } //*************************************************************************** - /// For signed integrals less than 64 bits. Supplied format spec. + /// Supplied format spec. //*************************************************************************** template - typename etl::enable_if::value && - etl::is_signed::value && - !etl::is_same::value, etl::iu16string&>::type - to_u16string(const T value, etl::iu16string& str, const etl::u16format_spec& format, const bool append = false) + const etl::iu16string& to_u16string(const T value, etl::iu16string& str, const etl::u16format_spec& format, const bool append = false) { - return to_string_helper(int32_t(value), str, format, append); - } - - //*************************************************************************** - /// For unsigned integrals less then 64 bits. Default format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_unsigned::value && - !etl::is_same::value, etl::iu16string&>::type - to_u16string(const T value, etl::iu16string& str, const bool append = false) - { - etl::u16format_spec format; - - return to_string_helper(uint32_t(value), str, format, append); - } - - //*************************************************************************** - /// For unsigned integrals less than 64 bits. Supplied format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_unsigned::value && - !etl::is_same::value, etl::iu16string&>::type - to_u16string(const T value, etl::iu16string& str, const etl::u16format_spec& format, const bool append = false) - { - return to_string_helper(uint32_t(value), str, format, append); - } - - //*************************************************************************** - /// For signed 64 bit integrals. Default format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_signed::value && - etl::is_same::value, etl::iu16string&>::type - to_u16string(const T value, etl::iu16string& str, const bool append = false) - { - etl::u16format_spec format; - - return to_string_helper(int64_t(value), str, format, append); - } - - //*************************************************************************** - /// For signed 64 bit integrals. Supplied format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_signed::value && - etl::is_same::value, etl::iu16string&>::type - to_u16string(const T value, etl::iu16string& str, const etl::u16format_spec& format, const bool append = false) - { - return to_string_helper(int64_t(value), str, format, append); - } - - //*************************************************************************** - /// For unsigned 64 bit integrals. Default format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_unsigned::value && - etl::is_same::value, etl::iu16string&>::type - to_u16string(const T value, etl::iu16string& str, const bool append = false) - { - etl::u16format_spec format; - - return to_string_helper(uint64_t(value), str, format, append); - } - - //*************************************************************************** - /// For unsigned 64 bit integrals. Supplied format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_unsigned::value && - etl::is_same::value, etl::iu16string&>::type - to_u16string(const T value, etl::iu16string& str, const etl::u16format_spec& format, const bool append = false) - { - return to_string_helper(uint64_t(value), str, format, append); + return etl::private_to_string::to_string(value, str, format, append); } } diff --git a/include/etl/to_u32string.h b/include/etl/to_u32string.h index 6caa403c..c45898d1 100644 --- a/include/etl/to_u32string.h +++ b/include/etl/to_u32string.h @@ -42,107 +42,23 @@ SOFTWARE. namespace etl { //*************************************************************************** - /// For signed integrals less than 64 bits. Default format spec. + /// Default format spec. //*************************************************************************** template - typename etl::enable_if::value && - etl::is_signed::value && - !etl::is_same::value, etl::iu32string&>::type - to_u32string(const T value, etl::iu32string& str, const bool append = false) + const etl::iu32string& to_u32string(const T value, etl::iu32string& str, const bool append = false) { etl::u32format_spec format; - return to_string_helper(int32_t(value), str, format, append); + return etl::private_to_string::to_string(value, str, format, append); } //*************************************************************************** - /// For signed integrals less than 64 bits. Supplied format spec. + /// Supplied format spec. //*************************************************************************** template - typename etl::enable_if::value && - etl::is_signed::value && - !etl::is_same::value, etl::iu32string&>::type - to_u32string(const T value, etl::iu32string& str, const etl::u32format_spec& format, const bool append = false) + const etl::iu32string& to_u32string(const T value, etl::iu32string& str, const etl::u32format_spec& format, const bool append = false) { - return to_string_helper(int32_t(value), str, format, append); - } - - //*************************************************************************** - /// For unsigned integrals less then 64 bits. Default format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_unsigned::value && - !etl::is_same::value, etl::iu32string&>::type - to_u32string(const T value, etl::iu32string& str, const bool append = false) - { - etl::u32format_spec format; - - return to_string_helper(uint32_t(value), str, format, append); - } - - //*************************************************************************** - /// For unsigned integrals less than 64 bits. Supplied format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_unsigned::value && - !etl::is_same::value, etl::iu32string&>::type - to_u32string(const T value, etl::iu32string& str, const etl::u32format_spec& format, const bool append = false) - { - return to_string_helper(uint32_t(value), str, format, append); - } - - //*************************************************************************** - /// For signed 64 bit integrals. Default format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_signed::value && - etl::is_same::value, etl::iu32string&>::type - to_u32string(const T value, etl::iu32string& str, const bool append = false) - { - etl::u32format_spec format; - - return to_string_helper(int64_t(value), str, format, append); - } - - //*************************************************************************** - /// For signed 64 bit integrals. Supplied format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_signed::value && - etl::is_same::value, etl::iu32string&>::type - to_u32string(const T value, etl::iu32string& str, const etl::u32format_spec& format, const bool append = false) - { - return to_string_helper(int64_t(value), str, format, append); - } - - //*************************************************************************** - /// For unsigned 64 bit integrals. Default format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_unsigned::value && - etl::is_same::value, etl::iu32string&>::type - to_u32string(const T value, etl::iu32string& str, const bool append = false) - { - etl::u32format_spec format; - - return to_string_helper(uint64_t(value), str, format, append); - } - - //*************************************************************************** - /// For unsigned 64 bit integrals. Supplied format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_unsigned::value && - etl::is_same::value, etl::iu32string&>::type - to_u32string(const T value, etl::iu32string& str, const etl::u32format_spec& format, const bool append = false) - { - return to_string_helper(uint64_t(value), str, format, append); + return etl::private_to_string::to_string(value, str, format, append); } } diff --git a/include/etl/to_wstring.h b/include/etl/to_wstring.h index 9bd39944..97cc04b4 100644 --- a/include/etl/to_wstring.h +++ b/include/etl/to_wstring.h @@ -42,107 +42,23 @@ SOFTWARE. namespace etl { //*************************************************************************** - /// For signed integrals less than 64 bits. Default format spec. + /// Default format spec. //*************************************************************************** template - typename etl::enable_if::value && - etl::is_signed::value && - !etl::is_same::value, etl::iwstring&>::type - to_wstring(const T value, etl::iwstring& str, const bool append = false) + const etl::iwstring& to_wstring(const T value, etl::iwstring& str, const bool append = false) { etl::wformat_spec format; - return to_string_helper(int32_t(value), str, format, append); + return etl::private_to_string::to_string(value, str, format, append); } //*************************************************************************** - /// For signed integrals less than 64 bits. Supplied format spec. + /// Supplied format spec. //*************************************************************************** template - typename etl::enable_if::value && - etl::is_signed::value && - !etl::is_same::value, etl::iwstring&>::type - to_wstring(const T value, etl::iwstring& str, const etl::wformat_spec& format, const bool append = false) + const etl::iwstring& to_wstring(const T value, etl::iwstring& str, const etl::wformat_spec& format, const bool append = false) { - return to_string_helper(int32_t(value), str, format, append); - } - - //*************************************************************************** - /// For unsigned integrals less then 64 bits. Default format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_unsigned::value && - !etl::is_same::value, etl::iwstring&>::type - to_wstring(const T value, etl::iwstring& str, const bool append = false) - { - etl::wformat_spec format; - - return to_string_helper(uint32_t(value), str, format, append); - } - - //*************************************************************************** - /// For unsigned integrals less than 64 bits. Supplied format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_unsigned::value && - !etl::is_same::value, etl::iwstring&>::type - to_wstring(const T value, etl::iwstring& str, const etl::wformat_spec& format, const bool append = false) - { - return to_string_helper(uint32_t(value), str, format, append); - } - - //*************************************************************************** - /// For signed 64 bit integrals. Default format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_signed::value && - etl::is_same::value, etl::iwstring&>::type - to_wstring(const T value, etl::iwstring& str, const bool append = false) - { - etl::wformat_spec format; - - return to_string_helper(int64_t(value), str, format, append); - } - - //*************************************************************************** - /// For signed 64 bit integrals. Supplied format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_signed::value && - etl::is_same::value, etl::iwstring&>::type - to_wstring(const T value, etl::iwstring& str, const etl::wformat_spec& format, const bool append = false) - { - return to_string_helper(int64_t(value), str, format, append); - } - - //*************************************************************************** - /// For unsigned 64 bit integrals. Default format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_unsigned::value && - etl::is_same::value, etl::iwstring&>::type - to_wstring(const T value, etl::iwstring& str, const bool append = false) - { - etl::wformat_spec format; - - return to_string_helper(uint64_t(value), str, format, append); - } - - //*************************************************************************** - /// For unsigned 64 bit integrals. Supplied format spec. - //*************************************************************************** - template - typename etl::enable_if::value && - etl::is_unsigned::value && - etl::is_same::value, etl::iwstring&>::type - to_wstring(const T value, etl::iwstring& str, const etl::wformat_spec& format, const bool append = false) - { - return to_string_helper(uint64_t(value), str, format, append); + return etl::private_to_string::to_string(value, str, format, append); } } diff --git a/test/test_to_string.cpp b/test/test_to_string.cpp index de746f60..01a9abe0 100644 --- a/test/test_to_string.cpp +++ b/test/test_to_string.cpp @@ -29,6 +29,8 @@ SOFTWARE. #include "UnitTest++.h" #include +#include +#include #include "etl/to_string.h" #include "etl/cstring.h" @@ -112,6 +114,8 @@ namespace Format format = Format().base(10).width(20).fill(STR('#')); + etl::to_string(uint8_t(0), str, format); + CHECK_EQUAL(etl::string<20>(STR("###################0")), etl::to_string(uint8_t(0), str, format)); CHECK_EQUAL(etl::string<20>(STR("###################0")), etl::to_string(uint16_t(0), str, format)); CHECK_EQUAL(etl::string<20>(STR("###################0")), etl::to_string(uint32_t(0), str, format)); @@ -138,7 +142,7 @@ namespace { etl::string<20> str; - Format format = Format().base(10).width(20).fill(STR('#')).left_justified(true); + Format format = Format().base(10).width(20).fill(STR('#')).left(); CHECK_EQUAL(etl::string<20>(STR("0###################")), etl::to_string(uint8_t(0), str, format)); CHECK_EQUAL(etl::string<20>(STR("0###################")), etl::to_string(uint16_t(0), str, format)); @@ -249,5 +253,134 @@ namespace CHECK_EQUAL(etl::string<17>(STR("123456")), etl::to_string(123456, str, Format().decimal())); CHECK_EQUAL(etl::string<17>(STR("1E240")), etl::to_string(123456, str, Format().hex())); } + + //************************************************************************* + TEST(test_floating_point_no_append) + { + etl::string<20> str; + + CHECK_EQUAL(etl::string<20>(STR(" 12.345678")), etl::to_string(12.345678, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::string<20>(STR("12.345678 ")), etl::to_string(12.345678, str, Format().precision(6).width(10).left())); + } + + //************************************************************************* + TEST(test_floating_point_append) + { + etl::string<20> str; + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::string<20>(STR("Result 12.345678")), etl::to_string(12.345678, str, Format().precision(6).width(10).right(), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::string<20>(STR("Result 12.345678 ")), etl::to_string(12.345678, str, Format().precision(6).width(10).left(), true)); + } + + //************************************************************************* + TEST(test_bool_no_append) + { + etl::string<20> str; + + CHECK_EQUAL(etl::string<20>(STR(" 0")), to_string(false, str, Format().precision(6).width(10).right().boolalpha(false))); + CHECK_EQUAL(etl::string<20>(STR(" 1")), to_string(true, str, Format().precision(6).width(10).right().boolalpha(false))); + CHECK_EQUAL(etl::string<20>(STR("0 ")), to_string(false, str, Format().precision(6).width(10).left().boolalpha(false))); + CHECK_EQUAL(etl::string<20>(STR("1 ")), to_string(true, str, Format().precision(6).width(10).left().boolalpha(false))); + + CHECK_EQUAL(etl::string<20>(STR(" false")), to_string(false, str, Format().precision(6).width(10).right().boolalpha(true))); + CHECK_EQUAL(etl::string<20>(STR(" true")), to_string(true, str, Format().precision(6).width(10).right().boolalpha(true))); + CHECK_EQUAL(etl::string<20>(STR("false ")), to_string(false, str, Format().precision(6).width(10).left().boolalpha(true))); + CHECK_EQUAL(etl::string<20>(STR("true ")), to_string(true, str, Format().precision(6).width(10).left().boolalpha(true))); + } + + //************************************************************************* + TEST(test_bool_append) + { + etl::string<20> str; + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::string<20>(STR("Result 0")), to_string(false, str, Format().precision(6).width(10).right().boolalpha(false), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::string<20>(STR("Result 1")), to_string(true, str, Format().precision(6).width(10).right().boolalpha(false), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::string<20>(STR("Result 0 ")), to_string(false, str, Format().precision(6).width(10).left().boolalpha(false), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::string<20>(STR("Result 1 ")), to_string(true, str, Format().precision(6).width(10).left().boolalpha(false), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::string<20>(STR("Result false")), to_string(false, str, Format().precision(6).width(10).right().boolalpha(true), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::string<20>(STR("Result true")), to_string(true, str, Format().precision(6).width(10).right().boolalpha(true), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::string<20>(STR("Result false ")), to_string(false, str, Format().precision(6).width(10).left().boolalpha(true), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::string<20>(STR("Result true ")), to_string(true, str, Format().precision(6).width(10).left().boolalpha(true), true)); + } + + //************************************************************************* + TEST(test_pointer_no_append) + { + etl::string<20> str; + + static const volatile int cvi = 0; + + std::ostringstream oss; + oss.width(10); + oss.fill(STR('0')); + oss << std::hex << std::uppercase << std::right << uintptr_t(&cvi); + std::string temp(oss.str()); + etl::string<20> compare(temp.begin(), temp.end()); + + to_string(&cvi, str, Format().hex().width(10).right().fill(STR('0'))); + CHECK_EQUAL(compare, str); + + oss.clear(); + oss.str(STR("")); + oss.width(10); + oss.fill(STR('0')); + oss << std::hex << std::uppercase << std::left << uintptr_t(&cvi); + temp = oss.str(); + compare.assign(temp.begin(), temp.end()); + + to_string(&cvi, str, Format().hex().width(10).left().fill(STR('0'))); + CHECK_EQUAL(compare, str); + } + + //************************************************************************* + TEST(test_pointer_append) + { + etl::string<20> str; + + static const volatile int cvi = 0; + + std::ostringstream oss; + oss.width(10); + oss.fill(STR('0')); + oss << std::hex << std::uppercase << std::right << uintptr_t(&cvi); + std::string temp(STR("Result ")); + temp.append(oss.str()); + etl::string<20> compare(temp.begin(), temp.end()); + + str.assign(STR("Result ")); + to_string(&cvi, str, Format().hex().width(10).right().fill(STR('0')), true); + CHECK_EQUAL(compare, str); + + oss.clear(); + oss.str(STR("")); + oss.width(10); + oss.fill(STR('0')); + oss << std::hex << std::uppercase << std::left << uintptr_t(&cvi); + temp = STR("Result "); + temp.append(oss.str()); + compare.assign(temp.begin(), temp.end()); + + str.assign(STR("Result ")); + to_string(&cvi, str, Format().hex().width(10).left().fill(STR('0')), true); + CHECK_EQUAL(compare, str); + } }; } diff --git a/test/test_to_u16string.cpp b/test/test_to_u16string.cpp index 01d92537..8069a7d5 100644 --- a/test/test_to_u16string.cpp +++ b/test/test_to_u16string.cpp @@ -29,6 +29,8 @@ SOFTWARE. #include "UnitTest++.h" #include +#include +#include #include "etl/to_u16string.h" #include "etl/u16string.h" @@ -41,6 +43,8 @@ namespace { typedef etl::u16format_spec Format; + typedef std::basic_ostringstream u16stringstream; + std::ostream& operator << (std::ostream& os, const etl::iu16string& str) { for (auto c : str) @@ -138,7 +142,7 @@ namespace { etl::u16string<20> str; - Format format = Format().base(10).width(20).fill(STR('#')).left_justified(true); + Format format = Format().base(10).width(20).fill(STR('#')).left(); CHECK_EQUAL(etl::u16string<20>(STR("0###################")), etl::to_u16string(uint8_t(0), str, format)); CHECK_EQUAL(etl::u16string<20>(STR("0###################")), etl::to_u16string(uint16_t(0), str, format)); @@ -249,5 +253,72 @@ namespace CHECK_EQUAL(etl::u16string<17>(STR("123456")), etl::to_u16string(123456, str, Format().decimal())); CHECK_EQUAL(etl::u16string<17>(STR("1E240")), etl::to_u16string(123456, str, Format().hex())); } + + //************************************************************************* + TEST(test_floating_point_no_append) + { + etl::u16string<20> str; + + CHECK_EQUAL(etl::u16string<20>(STR(" 12.345678")), etl::to_u16string(12.345678, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::u16string<20>(STR("12.345678 ")), etl::to_u16string(12.345678, str, Format().precision(6).width(10).left())); + } + + //************************************************************************* + TEST(test_floating_point_append) + { + etl::u16string<20> str; + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::u16string<20>(STR("Result 12.345678")), etl::to_u16string(12.345678, str, Format().precision(6).width(10).right(), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::u16string<20>(STR("Result 12.345678 ")), etl::to_u16string(12.345678, str, Format().precision(6).width(10).left(), true)); + } + + //************************************************************************* + TEST(test_bool_no_append) + { + etl::u16string<20> str; + + CHECK_EQUAL(etl::u16string<20>(STR(" 0")), to_u16string(false, str, Format().precision(6).width(10).right().boolalpha(false))); + CHECK_EQUAL(etl::u16string<20>(STR(" 1")), to_u16string(true, str, Format().precision(6).width(10).right().boolalpha(false))); + CHECK_EQUAL(etl::u16string<20>(STR("0 ")), to_u16string(false, str, Format().precision(6).width(10).left().boolalpha(false))); + CHECK_EQUAL(etl::u16string<20>(STR("1 ")), to_u16string(true, str, Format().precision(6).width(10).left().boolalpha(false))); + + CHECK_EQUAL(etl::u16string<20>(STR(" false")), to_u16string(false, str, Format().precision(6).width(10).right().boolalpha(true))); + CHECK_EQUAL(etl::u16string<20>(STR(" true")), to_u16string(true, str, Format().precision(6).width(10).right().boolalpha(true))); + CHECK_EQUAL(etl::u16string<20>(STR("false ")), to_u16string(false, str, Format().precision(6).width(10).left().boolalpha(true))); + CHECK_EQUAL(etl::u16string<20>(STR("true ")), to_u16string(true, str, Format().precision(6).width(10).left().boolalpha(true))); + } + + //************************************************************************* + TEST(test_bool_append) + { + etl::u16string<20> str; + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::u16string<20>(STR("Result 0")), to_u16string(false, str, Format().precision(6).width(10).right().boolalpha(false), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::u16string<20>(STR("Result 1")), to_u16string(true, str, Format().precision(6).width(10).right().boolalpha(false), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::u16string<20>(STR("Result 0 ")), to_u16string(false, str, Format().precision(6).width(10).left().boolalpha(false), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::u16string<20>(STR("Result 1 ")), to_u16string(true, str, Format().precision(6).width(10).left().boolalpha(false), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::u16string<20>(STR("Result false")), to_u16string(false, str, Format().precision(6).width(10).right().boolalpha(true), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::u16string<20>(STR("Result true")), to_u16string(true, str, Format().precision(6).width(10).right().boolalpha(true), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::u16string<20>(STR("Result false ")), to_u16string(false, str, Format().precision(6).width(10).left().boolalpha(true), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::u16string<20>(STR("Result true ")), to_u16string(true, str, Format().precision(6).width(10).left().boolalpha(true), true)); + } }; } diff --git a/test/test_to_u32string.cpp b/test/test_to_u32string.cpp index d83bb332..f900e88d 100644 --- a/test/test_to_u32string.cpp +++ b/test/test_to_u32string.cpp @@ -138,7 +138,7 @@ namespace { etl::u32string<20> str; - Format format = Format().base(10).width(20).fill(STR('#')).left_justified(true); + Format format = Format().base(10).width(20).fill(STR('#')).left(); CHECK_EQUAL(etl::u32string<20>(STR("0###################")), etl::to_u32string(uint8_t(0), str, format)); CHECK_EQUAL(etl::u32string<20>(STR("0###################")), etl::to_u32string(uint16_t(0), str, format)); @@ -250,5 +250,72 @@ namespace CHECK_EQUAL(etl::u32string<17>(STR("123456")), etl::to_u32string(123456, str, Format().decimal())); CHECK_EQUAL(etl::u32string<17>(STR("1E240")), etl::to_u32string(123456, str, Format().hex())); } + + //************************************************************************* + TEST(test_floating_point_no_append) + { + etl::u32string<20> str; + + CHECK_EQUAL(etl::u32string<20>(STR(" 12.345678")), etl::to_u32string(12.345678, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::u32string<20>(STR("12.345678 ")), etl::to_u32string(12.345678, str, Format().precision(6).width(10).left())); + } + + //************************************************************************* + TEST(test_floating_point_append) + { + etl::u32string<20> str; + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::u32string<20>(STR("Result 12.345678")), etl::to_u32string(12.345678, str, Format().precision(6).width(10).right(), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::u32string<20>(STR("Result 12.345678 ")), etl::to_u32string(12.345678, str, Format().precision(6).width(10).left(), true)); + } + + //************************************************************************* + TEST(test_bool_no_append) + { + etl::u32string<20> str; + + CHECK_EQUAL(etl::u32string<20>(STR(" 0")), to_u32string(false, str, Format().precision(6).width(10).right().boolalpha(false))); + CHECK_EQUAL(etl::u32string<20>(STR(" 1")), to_u32string(true, str, Format().precision(6).width(10).right().boolalpha(false))); + CHECK_EQUAL(etl::u32string<20>(STR("0 ")), to_u32string(false, str, Format().precision(6).width(10).left().boolalpha(false))); + CHECK_EQUAL(etl::u32string<20>(STR("1 ")), to_u32string(true, str, Format().precision(6).width(10).left().boolalpha(false))); + + CHECK_EQUAL(etl::u32string<20>(STR(" false")), to_u32string(false, str, Format().precision(6).width(10).right().boolalpha(true))); + CHECK_EQUAL(etl::u32string<20>(STR(" true")), to_u32string(true, str, Format().precision(6).width(10).right().boolalpha(true))); + CHECK_EQUAL(etl::u32string<20>(STR("false ")), to_u32string(false, str, Format().precision(6).width(10).left().boolalpha(true))); + CHECK_EQUAL(etl::u32string<20>(STR("true ")), to_u32string(true, str, Format().precision(6).width(10).left().boolalpha(true))); + } + + //************************************************************************* + TEST(test_bool_append) + { + etl::u32string<20> str; + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::u32string<20>(STR("Result 0")), to_u32string(false, str, Format().precision(6).width(10).right().boolalpha(false), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::u32string<20>(STR("Result 1")), to_u32string(true, str, Format().precision(6).width(10).right().boolalpha(false), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::u32string<20>(STR("Result 0 ")), to_u32string(false, str, Format().precision(6).width(10).left().boolalpha(false), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::u32string<20>(STR("Result 1 ")), to_u32string(true, str, Format().precision(6).width(10).left().boolalpha(false), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::u32string<20>(STR("Result false")), to_u32string(false, str, Format().precision(6).width(10).right().boolalpha(true), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::u32string<20>(STR("Result true")), to_u32string(true, str, Format().precision(6).width(10).right().boolalpha(true), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::u32string<20>(STR("Result false ")), to_u32string(false, str, Format().precision(6).width(10).left().boolalpha(true), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::u32string<20>(STR("Result true ")), to_u32string(true, str, Format().precision(6).width(10).left().boolalpha(true), true)); + } }; } diff --git a/test/test_to_wstring.cpp b/test/test_to_wstring.cpp index 7acf8797..dffbee4a 100644 --- a/test/test_to_wstring.cpp +++ b/test/test_to_wstring.cpp @@ -29,6 +29,8 @@ SOFTWARE. #include "UnitTest++.h" #include +#include +#include #include "etl/to_wstring.h" #include "etl/wstring.h" @@ -138,7 +140,7 @@ namespace { etl::wstring<20> str; - Format format = Format().base(10).width(20).fill(STR('#')).left_justified(true); + Format format = Format().base(10).width(20).fill(STR('#')).left(); CHECK_EQUAL(etl::wstring<20>(STR("0###################")), etl::to_wstring(uint8_t(0), str, format)); CHECK_EQUAL(etl::wstring<20>(STR("0###################")), etl::to_wstring(uint16_t(0), str, format)); @@ -249,5 +251,134 @@ namespace CHECK_EQUAL(etl::wstring<17>(STR("123456")), etl::to_wstring(123456, str, Format().decimal())); CHECK_EQUAL(etl::wstring<17>(STR("1E240")), etl::to_wstring(123456, str, Format().hex())); } + + //************************************************************************* + TEST(test_floating_point_no_append) + { + etl::wstring<20> str; + + CHECK_EQUAL(etl::wstring<20>(STR(" 12.345678")), etl::to_wstring(12.345678, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::wstring<20>(STR("12.345678 ")), etl::to_wstring(12.345678, str, Format().precision(6).width(10).left())); + } + + //************************************************************************* + TEST(test_floating_point_append) + { + etl::wstring<20> str; + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::wstring<20>(STR("Result 12.345678")), etl::to_wstring(12.345678, str, Format().precision(6).width(10).right(), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::wstring<20>(STR("Result 12.345678 ")), etl::to_wstring(12.345678, str, Format().precision(6).width(10).left(), true)); + } + + //************************************************************************* + TEST(test_bool_no_append) + { + etl::wstring<20> str; + + CHECK_EQUAL(etl::wstring<20>(STR(" 0")), to_wstring(false, str, Format().precision(6).width(10).right().boolalpha(false))); + CHECK_EQUAL(etl::wstring<20>(STR(" 1")), to_wstring(true, str, Format().precision(6).width(10).right().boolalpha(false))); + CHECK_EQUAL(etl::wstring<20>(STR("0 ")), to_wstring(false, str, Format().precision(6).width(10).left().boolalpha(false))); + CHECK_EQUAL(etl::wstring<20>(STR("1 ")), to_wstring(true, str, Format().precision(6).width(10).left().boolalpha(false))); + + CHECK_EQUAL(etl::wstring<20>(STR(" false")), to_wstring(false, str, Format().precision(6).width(10).right().boolalpha(true))); + CHECK_EQUAL(etl::wstring<20>(STR(" true")), to_wstring(true, str, Format().precision(6).width(10).right().boolalpha(true))); + CHECK_EQUAL(etl::wstring<20>(STR("false ")), to_wstring(false, str, Format().precision(6).width(10).left().boolalpha(true))); + CHECK_EQUAL(etl::wstring<20>(STR("true ")), to_wstring(true, str, Format().precision(6).width(10).left().boolalpha(true))); + } + + //************************************************************************* + TEST(test_bool_append) + { + etl::wstring<20> str; + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::wstring<20>(STR("Result 0")), to_wstring(false, str, Format().precision(6).width(10).right().boolalpha(false), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::wstring<20>(STR("Result 1")), to_wstring(true, str, Format().precision(6).width(10).right().boolalpha(false), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::wstring<20>(STR("Result 0 ")), to_wstring(false, str, Format().precision(6).width(10).left().boolalpha(false), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::wstring<20>(STR("Result 1 ")), to_wstring(true, str, Format().precision(6).width(10).left().boolalpha(false), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::wstring<20>(STR("Result false")), to_wstring(false, str, Format().precision(6).width(10).right().boolalpha(true), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::wstring<20>(STR("Result true")), to_wstring(true, str, Format().precision(6).width(10).right().boolalpha(true), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::wstring<20>(STR("Result false ")), to_wstring(false, str, Format().precision(6).width(10).left().boolalpha(true), true)); + + str.assign(STR("Result ")); + CHECK_EQUAL(etl::wstring<20>(STR("Result true ")), to_wstring(true, str, Format().precision(6).width(10).left().boolalpha(true), true)); + } + + //************************************************************************* + TEST(test_pointer_no_append) + { + etl::wstring<20> str; + + static const volatile int cvi = 0; + + std::wostringstream oss; + oss.width(10); + oss.fill(STR('0')); + oss << std::hex << std::uppercase << std::right << uintptr_t(&cvi); + std::wstring temp(oss.str()); + etl::wstring<20> compare(temp.begin(), temp.end()); + + to_wstring(&cvi, str, Format().hex().width(10).right().fill(STR('0'))); + CHECK_EQUAL(compare, str); + + oss.clear(); + oss.str(STR("")); + oss.width(10); + oss.fill(STR('0')); + oss << std::hex << std::uppercase << std::left << uintptr_t(&cvi); + temp = oss.str(); + compare.assign(temp.begin(), temp.end()); + + to_wstring(&cvi, str, Format().hex().width(10).left().fill(STR('0'))); + CHECK_EQUAL(compare, str); + } + + //************************************************************************* + TEST(test_pointer_append) + { + etl::wstring<20> str; + + static const volatile int cvi = 0; + + std::wostringstream oss; + oss.width(10); + oss.fill(STR('0')); + oss << std::hex << std::uppercase << std::right << uintptr_t(&cvi); + std::wstring temp(STR("Result ")); + temp.append(oss.str()); + etl::wstring<20> compare(temp.begin(), temp.end()); + + str.assign(STR("Result ")); + to_wstring(&cvi, str, Format().hex().width(10).right().fill(STR('0')), true); + CHECK_EQUAL(compare, str); + + oss.clear(); + oss.str(STR("")); + oss.width(10); + oss.fill(STR('0')); + oss << std::hex << std::uppercase << std::left << uintptr_t(&cvi); + temp = STR("Result "); + temp.append(oss.str()); + compare.assign(temp.begin(), temp.end()); + + str.assign(STR("Result ")); + to_wstring(&cvi, str, Format().hex().width(10).left().fill(STR('0')), true); + CHECK_EQUAL(compare, str); + } }; }