diff --git a/include/etl/format_spec.h b/include/etl/format_spec.h index d8dca693..74787281 100644 --- a/include/etl/format_spec.h +++ b/include/etl/format_spec.h @@ -50,13 +50,13 @@ namespace etl /// Base = 10 /// Width = 0 /// Upper case (for hex) = true - /// Rifght Justified = true + /// Left Justified = false //*************************************************************************** format_spec() : base_(10) , width_(0) , upper_case_(true) - , right_justified_(true) + , left_justified_(false) , fill_(typename TString::value_type(' ')) { @@ -111,7 +111,7 @@ namespace etl //*************************************************************************** /// Gets the upper case flag. //*************************************************************************** - bool upper_case(const bool u) const + bool upper_case() const { return upper_case_; } @@ -135,21 +135,21 @@ namespace etl } //*************************************************************************** - /// Sets the right justify flag. + /// Sets the left justify flag. /// \return A reference to the format_spec. //*************************************************************************** - format_spec& right_justified(const bool r) + format_spec& left_justified(const bool l) { - right_justified_ = r; + left_justified_ = l; return *this; } //*************************************************************************** - /// Gets the right justify flag. + /// Gets the left justify flag. //*************************************************************************** - bool right_justified() const + bool left_justified() const { - return right_justified_; + return left_justified_; } private: @@ -157,7 +157,7 @@ namespace etl uint_least8_t base_; uint_least8_t width_; bool upper_case_; - bool right_justified_; + bool left_justified_; typename TString::value_type fill_; }; } diff --git a/include/etl/private/to_string_helper.h b/include/etl/private/to_string_helper.h index 903f4e39..a384e1ec 100644 --- a/include/etl/private/to_string_helper.h +++ b/include/etl/private/to_string_helper.h @@ -48,7 +48,7 @@ namespace etl /// Helper function for integrals. //*************************************************************************** template - typename etl::enable_if::value, const TIString&>::type + typename etl::enable_if::value, TIString&>::type to_string_helper(T value, TIString& str, const etl::format_spec& format, @@ -76,7 +76,7 @@ namespace etl while (value != 0) { T remainder = etl::absolute(value % T(format.base())); - str.push_back((remainder > 9) ? type('a' + (remainder - 10)) : type('0' + remainder)); + str.push_back((remainder > 9) ? (format.upper_case() ? type('A' + (remainder - 10)) : type('a' + (remainder - 10))) : type('0' + remainder)); value = value / T(format.base()); } @@ -100,16 +100,16 @@ namespace etl { uint32_t fill_length = format.width() - length; - if (format.right_justified()) - { - // Insert fill characters on the left. - str.insert(start, fill_length, format.fill()); - } - else + if (format.left_justified()) { // Insert fill characters on the right. str.insert(str.end(), fill_length, format.fill()); } + else + { + // Insert fill characters on the left. + str.insert(start, fill_length, format.fill()); + } } } diff --git a/include/etl/to_u16string.h b/include/etl/to_u16string.h index 15e05d7b..09a5ce0f 100644 --- a/include/etl/to_u16string.h +++ b/include/etl/to_u16string.h @@ -47,8 +47,8 @@ namespace etl template typename etl::enable_if::value && etl::is_signed::value && - !etl::is_same::value, const etl::iu16string&>::type - to_string(const T value, etl::iu16string& str, const bool append = false) + !etl::is_same::value, etl::iu16string&>::type + to_u16string(const T value, etl::iu16string& str, const bool append = false) { etl::format_spec format; @@ -61,8 +61,8 @@ namespace etl template typename etl::enable_if::value && etl::is_signed::value && - !etl::is_same::value, const etl::iu16string&>::type - to_string(const T value, etl::iu16string& str, const etl::format_spec& format, const bool append = false) + !etl::is_same::value, etl::iu16string&>::type + to_u16string(const T value, etl::iu16string& str, const etl::format_spec& format, const bool append = false) { return to_string_helper(int32_t(value), str, format, append); } @@ -73,8 +73,8 @@ namespace etl template typename etl::enable_if::value && etl::is_unsigned::value && - !etl::is_same::value, const etl::iu16string&>::type - to_string(const T value, etl::iu16string& str, const bool append = false) + !etl::is_same::value, etl::iu16string&>::type + to_u16string(const T value, etl::iu16string& str, const bool append = false) { etl::format_spec format; @@ -87,8 +87,8 @@ namespace etl template typename etl::enable_if::value && etl::is_unsigned::value && - !etl::is_same::value, const etl::iu16string&>::type - to_string(const T value, etl::iu16string& str, const etl::format_spec& format, const bool append = false) + !etl::is_same::value, etl::iu16string&>::type + to_u16string(const T value, etl::iu16string& str, const etl::format_spec& format, const bool append = false) { return to_string_helper(uint32_t(value), str, format, append); } @@ -99,8 +99,8 @@ namespace etl template typename etl::enable_if::value && etl::is_signed::value && - etl::is_same::value, const etl::iu16string&>::type - to_string(const T value, etl::iu16string& str, const bool append = false) + etl::is_same::value, etl::iu16string&>::type + to_u16string(const T value, etl::iu16string& str, const bool append = false) { etl::format_spec format; @@ -113,8 +113,8 @@ namespace etl template typename etl::enable_if::value && etl::is_signed::value && - etl::is_same::value, const etl::iu16string&>::type - to_string(const T value, etl::iu16string& str, const etl::format_spec& format, const bool append = false) + etl::is_same::value, etl::iu16string&>::type + to_u16string(const T value, etl::iu16string& str, const etl::format_spec& format, const bool append = false) { return to_string_helper(int64_t(value), str, format, append); } @@ -125,8 +125,8 @@ namespace etl template typename etl::enable_if::value && etl::is_unsigned::value && - etl::is_same::value, const etl::iu16string&>::type - to_string(const T value, etl::iu16string& str, const bool append = false) + etl::is_same::value, etl::iu16string&>::type + to_u16string(const T value, etl::iu16string& str, const bool append = false) { etl::format_spec format; @@ -139,8 +139,8 @@ namespace etl template typename etl::enable_if::value && etl::is_unsigned::value && - etl::is_same::value, const etl::iu16string&>::type - to_string(const T value, etl::iu16string& str, const etl::format_spec& format, const bool append = false) + etl::is_same::value, etl::iu16string&>::type + to_u16string(const T value, etl::iu16string& str, const etl::format_spec& format, const bool append = false) { return to_string_helper(uint64_t(value), str, format, append); } diff --git a/include/etl/to_u32string.h b/include/etl/to_u32string.h index d2c7baee..9eaf6110 100644 --- a/include/etl/to_u32string.h +++ b/include/etl/to_u32string.h @@ -47,8 +47,8 @@ namespace etl template typename etl::enable_if::value && etl::is_signed::value && - !etl::is_same::value, const etl::iu32string&>::type - to_string(const T value, etl::iu32string& str, const bool append = false) + !etl::is_same::value, etl::iu32string&>::type + to_u32string(const T value, etl::iu32string& str, const bool append = false) { etl::format_spec format; @@ -61,8 +61,8 @@ namespace etl template typename etl::enable_if::value && etl::is_signed::value && - !etl::is_same::value, const etl::iu32string&>::type - to_string(const T value, etl::iu32string& str, const etl::format_spec& format, const bool append = false) + !etl::is_same::value, etl::iu32string&>::type + to_u32string(const T value, etl::iu32string& str, const etl::format_spec& format, const bool append = false) { return to_string_helper(int32_t(value), str, format, append); } @@ -73,8 +73,8 @@ namespace etl template typename etl::enable_if::value && etl::is_unsigned::value && - !etl::is_same::value, const etl::iu32string&>::type - to_string(const T value, etl::iu32string& str, const bool append = false) + !etl::is_same::value, etl::iu32string&>::type + to_u32string(const T value, etl::iu32string& str, const bool append = false) { etl::format_spec format; @@ -87,8 +87,8 @@ namespace etl template typename etl::enable_if::value && etl::is_unsigned::value && - !etl::is_same::value, const etl::iu32string&>::type - to_string(const T value, etl::iu32string& str, const etl::format_spec& format, const bool append = false) + !etl::is_same::value, etl::iu32string&>::type + to_u32string(const T value, etl::iu32string& str, const etl::format_spec& format, const bool append = false) { return to_string_helper(uint32_t(value), str, format, append); } @@ -99,8 +99,8 @@ namespace etl template typename etl::enable_if::value && etl::is_signed::value && - etl::is_same::value, const etl::iu32string&>::type - to_string(const T value, etl::iu32string& str, const bool append = false) + etl::is_same::value, etl::iu32string&>::type + to_u32string(const T value, etl::iu32string& str, const bool append = false) { etl::format_spec format; @@ -113,8 +113,8 @@ namespace etl template typename etl::enable_if::value && etl::is_signed::value && - etl::is_same::value, const etl::iu32string&>::type - to_string(const T value, etl::iu32string& str, const etl::format_spec& format, const bool append = false) + etl::is_same::value, etl::iu32string&>::type + to_u32string(const T value, etl::iu32string& str, const etl::format_spec& format, const bool append = false) { return to_string_helper(int64_t(value), str, format, append); } @@ -125,8 +125,8 @@ namespace etl template typename etl::enable_if::value && etl::is_unsigned::value && - etl::is_same::value, const etl::iu32string&>::type - to_string(const T value, etl::iu32string& str, const bool append = false) + etl::is_same::value, etl::iu32string&>::type + to_u32string(const T value, etl::iu32string& str, const bool append = false) { etl::format_spec format; @@ -139,8 +139,8 @@ namespace etl template typename etl::enable_if::value && etl::is_unsigned::value && - etl::is_same::value, const etl::iu32string&>::type - to_string(const T value, etl::iu32string& str, const etl::format_spec& format, const bool append = false) + etl::is_same::value, etl::iu32string&>::type + to_u32string(const T value, etl::iu32string& str, const etl::format_spec& format, const bool append = false) { return to_string_helper(uint64_t(value), str, format, append); } diff --git a/include/etl/to_wstring.h b/include/etl/to_wstring.h index f84fe55a..ff55b483 100644 --- a/include/etl/to_wstring.h +++ b/include/etl/to_wstring.h @@ -47,8 +47,8 @@ namespace etl template typename etl::enable_if::value && etl::is_signed::value && - !etl::is_same::value, const etl::iwstring&>::type - to_string(const T value, etl::iwstring& str, const bool append = false) + !etl::is_same::value, etl::iwstring&>::type + to_wstring(const T value, etl::iwstring& str, const bool append = false) { etl::format_spec format; @@ -61,8 +61,8 @@ namespace etl template typename etl::enable_if::value && etl::is_signed::value && - !etl::is_same::value, const etl::iwstring&>::type - to_string(const T value, etl::iwstring& str, const etl::format_spec& format, const bool append = false) + !etl::is_same::value, etl::iwstring&>::type + to_wstring(const T value, etl::iwstring& str, const etl::format_spec& format, const bool append = false) { return to_string_helper(int32_t(value), str, format, append); } @@ -73,8 +73,8 @@ namespace etl template typename etl::enable_if::value && etl::is_unsigned::value && - !etl::is_same::value, const etl::iwstring&>::type - to_string(const T value, etl::iwstring& str, const bool append = false) + !etl::is_same::value, etl::iwstring&>::type + to_wstring(const T value, etl::iwstring& str, const bool append = false) { etl::format_spec format; @@ -87,8 +87,8 @@ namespace etl template typename etl::enable_if::value && etl::is_unsigned::value && - !etl::is_same::value, const etl::iwstring&>::type - to_string(const T value, etl::iwstring& str, const etl::format_spec& format, const bool append = false) + !etl::is_same::value, etl::iwstring&>::type + to_wstring(const T value, etl::iwstring& str, const etl::format_spec& format, const bool append = false) { return to_string_helper(uint32_t(value), str, format, append); } @@ -99,8 +99,8 @@ namespace etl template typename etl::enable_if::value && etl::is_signed::value && - etl::is_same::value, const etl::iwstring&>::type - to_string(const T value, etl::iwstring& str, const bool append = false) + etl::is_same::value, etl::iwstring&>::type + to_wstring(const T value, etl::iwstring& str, const bool append = false) { etl::format_spec format; @@ -113,8 +113,8 @@ namespace etl template typename etl::enable_if::value && etl::is_signed::value && - etl::is_same::value, const etl::iwstring&>::type - to_string(const T value, etl::iwstring& str, const etl::format_spec& format, const bool append = false) + etl::is_same::value, etl::iwstring&>::type + to_wstring(const T value, etl::iwstring& str, const etl::format_spec& format, const bool append = false) { return to_string_helper(int64_t(value), str, format, append); } @@ -125,8 +125,8 @@ namespace etl template typename etl::enable_if::value && etl::is_unsigned::value && - etl::is_same::value, const etl::iwstring&>::type - to_string(const T value, etl::iwstring& str, const bool append = false) + etl::is_same::value, etl::iwstring&>::type + to_wstring(const T value, etl::iwstring& str, const bool append = false) { etl::format_spec format; @@ -139,8 +139,8 @@ namespace etl template typename etl::enable_if::value && etl::is_unsigned::value && - etl::is_same::value, const etl::iwstring&>::type - to_string(const T value, etl::iwstring& str, const etl::format_spec& format, const bool append = false) + etl::is_same::value, etl::iwstring&>::type + to_wstring(const T value, etl::iwstring& str, const etl::format_spec& format, const bool append = false) { return to_string_helper(uint64_t(value), str, format, append); } diff --git a/test/test_to_string.cpp b/test/test_to_string.cpp index 9fa6c684..fc70aded 100644 --- a/test/test_to_string.cpp +++ b/test/test_to_string.cpp @@ -28,7 +28,7 @@ SOFTWARE. #include "UnitTest++.h" -#include +#include #include "etl/to_string.h" #include "etl/cstring.h" @@ -41,32 +41,202 @@ namespace { typedef etl::format_spec Format; - SUITE(test_string_char) + std::ostream& operator << (std::ostream& os, const etl::istring& str) + { + for (auto c : str) + { + os << c; + } + + return os; + } + + SUITE(test_to_string) { //************************************************************************* TEST(test_default_format_no_append) { etl::string<20> str; - CHECK(etl::string<20>(STR("0")) == etl::to_string(uint8_t(0), str)); - CHECK(etl::string<20>(STR("0")) == etl::to_string(uint16_t(0), str)); - CHECK(etl::string<20>(STR("0")) == etl::to_string(uint32_t(0), str)); - CHECK(etl::string<20>(STR("0")) == etl::to_string(uint64_t(0), str)); + CHECK_EQUAL(etl::string<20>(STR("0")), etl::to_string(uint8_t(0), str)); + CHECK_EQUAL(etl::string<20>(STR("0")), etl::to_string(uint16_t(0), str)); + CHECK_EQUAL(etl::string<20>(STR("0")), etl::to_string(uint32_t(0), str)); + CHECK_EQUAL(etl::string<20>(STR("0")), etl::to_string(uint64_t(0), str)); - CHECK(etl::string<20>(STR("128")) == etl::to_string(uint8_t(128), str)); - CHECK(etl::string<20>(STR("32768")) == etl::to_string(uint16_t(32768), str)); - CHECK(etl::string<20>(STR("2147483648")) == etl::to_string(uint32_t(2147483648ul), str)); - CHECK(etl::string<20>(STR("9223372036854775808")) == etl::to_string(uint64_t(9223372036854775808ull), str)); + CHECK_EQUAL(etl::string<20>(STR("128")), etl::to_string(uint8_t(128), str)); + CHECK_EQUAL(etl::string<20>(STR("32768")), etl::to_string(uint16_t(32768), str)); + CHECK_EQUAL(etl::string<20>(STR("2147483648")), etl::to_string(uint32_t(2147483648ul), str)); + CHECK_EQUAL(etl::string<20>(STR("9223372036854775808")), etl::to_string(uint64_t(9223372036854775808ull), str)); - CHECK(etl::string<20>(STR("127")) == etl::to_string(int8_t(127), str)); - CHECK(etl::string<20>(STR("32767")) == etl::to_string(int16_t(32767), str)); - CHECK(etl::string<20>(STR("2147483647")) == etl::to_string(int32_t(2147483647ll), str)); - CHECK(etl::string<20>(STR("9223372036854775807")) == etl::to_string(int64_t(9223372036854775807ll), str)); + CHECK_EQUAL(etl::string<20>(STR("127")), etl::to_string(int8_t(127), str)); + CHECK_EQUAL(etl::string<20>(STR("32767")), etl::to_string(int16_t(32767), str)); + CHECK_EQUAL(etl::string<20>(STR("2147483647")), etl::to_string(int32_t(2147483647ll), str)); + CHECK_EQUAL(etl::string<20>(STR("9223372036854775807")), etl::to_string(int64_t(9223372036854775807ll), str)); - CHECK(etl::string<20>(STR("-128")) == etl::to_string(int8_t(-128), str)); - CHECK(etl::string<20>(STR("-32768")) == etl::to_string(int16_t(-32768), str)); - CHECK(etl::string<20>(STR("-2147483648")) == etl::to_string(int32_t(-2147483648ll), str)); - CHECK(etl::string<20>(STR("-9223372036854775808")) == etl::to_string(int64_t(-9223372036854775808ll), str)); + CHECK_EQUAL(etl::string<20>(STR("-128")), etl::to_string(int8_t(-128), str)); + CHECK_EQUAL(etl::string<20>(STR("-32768")), etl::to_string(int16_t(-32768), str)); + CHECK_EQUAL(etl::string<20>(STR("-2147483648")), etl::to_string(int32_t(-2147483648ll), str)); + CHECK_EQUAL(etl::string<20>(STR("-9223372036854775808")), etl::to_string(int64_t(-9223372036854775808ll), str)); + } + + //************************************************************************* + TEST(test_default_format_append) + { + etl::string<120> str; + + CHECK_EQUAL(etl::string<120>(STR("0")), etl::to_string(uint8_t(0), str, true)); + CHECK_EQUAL(etl::string<120>(STR("00")), etl::to_string(uint16_t(0), str, true)); + CHECK_EQUAL(etl::string<120>(STR("000")), etl::to_string(uint32_t(0), str, true)); + CHECK_EQUAL(etl::string<120>(STR("0000")), etl::to_string(uint64_t(0), str, true)); + + CHECK_EQUAL(etl::string<120>(STR("0000128")), etl::to_string(uint8_t(128), str, true)); + CHECK_EQUAL(etl::string<120>(STR("000012832768")), etl::to_string(uint16_t(32768), str, true)); + CHECK_EQUAL(etl::string<120>(STR("0000128327682147483648")), etl::to_string(uint32_t(2147483648ul), str, true)); + CHECK_EQUAL(etl::string<120>(STR("00001283276821474836489223372036854775808")), etl::to_string(uint64_t(9223372036854775808ull), str, true)); + + CHECK_EQUAL(etl::string<120>(STR("00001283276821474836489223372036854775808127")), etl::to_string(int8_t(127), str, true)); + CHECK_EQUAL(etl::string<120>(STR("0000128327682147483648922337203685477580812732767")), etl::to_string(int16_t(32767), str, true)); + CHECK_EQUAL(etl::string<120>(STR("00001283276821474836489223372036854775808127327672147483647")), etl::to_string(int32_t(2147483647ll), str, true)); + CHECK_EQUAL(etl::string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807")), etl::to_string(int64_t(9223372036854775807ll), str, true)); + + CHECK_EQUAL(etl::string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128")), etl::to_string(int8_t(-128), str, true)); + CHECK_EQUAL(etl::string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768")), etl::to_string(int16_t(-32768), str, true)); + CHECK_EQUAL(etl::string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648")), etl::to_string(int32_t(-2147483648ll), str, true)); + CHECK_EQUAL(etl::string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648-9223372036854775808")), etl::to_string(int64_t(-9223372036854775808ll), str, true)); + } + + //************************************************************************* + TEST(test_format_right_justified_no_append) + { + etl::string<20> str; + + Format format = Format().base(10).width(20).fill(STR('#')); + + 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)); + CHECK_EQUAL(etl::string<20>(STR("###################0")), etl::to_string(uint64_t(0), str, format)); + + CHECK_EQUAL(etl::string<20>(STR("#################128")), etl::to_string(uint8_t(128), str, format)); + CHECK_EQUAL(etl::string<20>(STR("###############32768")), etl::to_string(uint16_t(32768), str, format)); + CHECK_EQUAL(etl::string<20>(STR("##########2147483648")), etl::to_string(uint32_t(2147483648ul), str, format)); + CHECK_EQUAL(etl::string<20>(STR("#9223372036854775808")), etl::to_string(uint64_t(9223372036854775808ull), str, format)); + + CHECK_EQUAL(etl::string<20>(STR("#################127")), etl::to_string(int8_t(127), str, format)); + CHECK_EQUAL(etl::string<20>(STR("###############32767")), etl::to_string(int16_t(32767), str, format)); + CHECK_EQUAL(etl::string<20>(STR("##########2147483647")), etl::to_string(int32_t(2147483647ll), str, format)); + CHECK_EQUAL(etl::string<20>(STR("#9223372036854775807")), etl::to_string(int64_t(9223372036854775807ll), str, format)); + + CHECK_EQUAL(etl::string<20>(STR("################-128")), etl::to_string(int8_t(-128), str, format)); + CHECK_EQUAL(etl::string<20>(STR("##############-32768")), etl::to_string(int16_t(-32768), str, format)); + CHECK_EQUAL(etl::string<20>(STR("#########-2147483648")), etl::to_string(int32_t(-2147483648ll), str, format)); + CHECK_EQUAL(etl::string<20>(STR("-9223372036854775808")), etl::to_string(int64_t(-9223372036854775808ll), str, format)); + } + + //************************************************************************* + TEST(test_format_left_justified_no_append) + { + etl::string<20> str; + + Format format = Format().base(10).width(20).fill(STR('#')).left_justified(true); + + 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)); + CHECK_EQUAL(etl::string<20>(STR("0###################")), etl::to_string(uint64_t(0), str, format)); + + CHECK_EQUAL(etl::string<20>(STR("128#################")), etl::to_string(uint8_t(128), str, format)); + CHECK_EQUAL(etl::string<20>(STR("32768###############")), etl::to_string(uint16_t(32768), str, format)); + CHECK_EQUAL(etl::string<20>(STR("2147483648##########")), etl::to_string(uint32_t(2147483648ul), str, format)); + CHECK_EQUAL(etl::string<20>(STR("9223372036854775808#")), etl::to_string(uint64_t(9223372036854775808ull), str, format)); + + CHECK_EQUAL(etl::string<20>(STR("127#################")), etl::to_string(int8_t(127), str, format)); + CHECK_EQUAL(etl::string<20>(STR("32767###############")), etl::to_string(int16_t(32767), str, format)); + CHECK_EQUAL(etl::string<20>(STR("2147483647##########")), etl::to_string(int32_t(2147483647ll), str, format)); + CHECK_EQUAL(etl::string<20>(STR("9223372036854775807#")), etl::to_string(int64_t(9223372036854775807ll), str, format)); + + CHECK_EQUAL(etl::string<20>(STR("-128################")), etl::to_string(int8_t(-128), str, format)); + CHECK_EQUAL(etl::string<20>(STR("-32768##############")), etl::to_string(int16_t(-32768), str, format)); + CHECK_EQUAL(etl::string<20>(STR("-2147483648#########")), etl::to_string(int32_t(-2147483648ll), str, format)); + CHECK_EQUAL(etl::string<20>(STR("-9223372036854775808")), etl::to_string(int64_t(-9223372036854775808ll), str, format)); + } + + //************************************************************************* + TEST(test_binary_format_no_append) + { + etl::string<64> str; + + CHECK_EQUAL(etl::string<64>(STR("00000000")), etl::to_string(uint8_t(0), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::string<64>(STR("0000000000000000")), etl::to_string(uint16_t(0), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::string<64>(STR("00000000000000000000000000000000")), etl::to_string(uint32_t(0), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::string<64>(STR("0000000000000000000000000000000000000000000000000000000000000000")), etl::to_string(uint64_t(0), str, Format().base(2).width(64).fill(STR('0')))); + + CHECK_EQUAL(etl::string<64>(STR("10000000")), etl::to_string(uint8_t(128), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::string<64>(STR("1000000000000000")), etl::to_string(uint16_t(32768), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::string<64>(STR("10000000000000000000000000000000")), etl::to_string(uint32_t(2147483648ul), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::string<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_string(uint64_t(9223372036854775808ull), str, Format().base(2).width(64).fill(STR('0')))); + + CHECK_EQUAL(etl::string<64>(STR("01111111")), etl::to_string(int8_t(127), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::string<64>(STR("0111111111111111")), etl::to_string(int16_t(32767), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::string<64>(STR("01111111111111111111111111111111")), etl::to_string(int32_t(2147483647ll), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::string<64>(STR("0111111111111111111111111111111111111111111111111111111111111111")), etl::to_string(int64_t(9223372036854775807ll), str, Format().base(2).width(64).fill(STR('0')))); + + CHECK_EQUAL(etl::string<64>(STR("10000000")), etl::to_string(int8_t(-128), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::string<64>(STR("1000000000000000")), etl::to_string(int16_t(-32768), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::string<64>(STR("10000000000000000000000000000000")), etl::to_string(int32_t(-2147483648ll), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::string<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_string(int64_t(-9223372036854775808ll), str, Format().base(2).width(64).fill(STR('0')))); + } + + //************************************************************************* + TEST(test_octal_format_no_append) + { + etl::string<22> str; + + CHECK_EQUAL(etl::string<22>(STR("000")), etl::to_string(uint8_t(0), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::string<22>(STR("000000")), etl::to_string(uint16_t(0), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::string<22>(STR("00000000000")), etl::to_string(uint32_t(0), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::string<22>(STR("0000000000000000000000")), etl::to_string(uint64_t(0), str, Format().base(8).width(22).fill(STR('0')))); + + CHECK_EQUAL(etl::string<22>(STR("200")), etl::to_string(uint8_t(128), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::string<22>(STR("100000")), etl::to_string(uint16_t(32768), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::string<22>(STR("20000000000")), etl::to_string(uint32_t(2147483648ul), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::string<22>(STR("1000000000000000000000")), etl::to_string(uint64_t(9223372036854775808ull), str, Format().base(8).width(22).fill(STR('0')))); + + CHECK_EQUAL(etl::string<22>(STR("177")), etl::to_string(int8_t(127), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::string<22>(STR("077777")), etl::to_string(int16_t(32767), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::string<22>(STR("17777777777")), etl::to_string(int32_t(2147483647ll), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::string<22>(STR("0777777777777777777777")), etl::to_string(int64_t(9223372036854775807ll), str, Format().base(8).width(22).fill(STR('0')))); + + CHECK_EQUAL(etl::string<22>(STR("200")), etl::to_string(int8_t(-128), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::string<22>(STR("100000")), etl::to_string(int16_t(-32768), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::string<22>(STR("20000000000")), etl::to_string(int32_t(-2147483648ll), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::string<22>(STR("1000000000000000000000")), etl::to_string(int64_t(-9223372036854775808ll), str, Format().base(8).width(22).fill(STR('0')))); + } + + //************************************************************************* + TEST(test_hex_format_no_append) + { + etl::string<16> str; + + CHECK_EQUAL(etl::string<16>(STR("00")), etl::to_string(uint8_t(0), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::string<16>(STR("0000")), etl::to_string(uint16_t(0), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::string<16>(STR("00000000")), etl::to_string(uint32_t(0), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::string<16>(STR("0000000000000000")), etl::to_string(uint64_t(0), str, Format().base(16).width(16).fill(STR('0')))); + + CHECK_EQUAL(etl::string<16>(STR("80")), etl::to_string(uint8_t(128), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::string<16>(STR("8000")), etl::to_string(uint16_t(32768), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::string<16>(STR("80000000")), etl::to_string(uint32_t(2147483648ul), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::string<16>(STR("8000000000000000")), etl::to_string(uint64_t(9223372036854775808ull), str, Format().base(16).width(16).fill(STR('0')))); + + CHECK_EQUAL(etl::string<16>(STR("7F")), etl::to_string(int8_t(127), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::string<16>(STR("7FFF")), etl::to_string(int16_t(32767), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::string<16>(STR("7FFFFFFF")), etl::to_string(int32_t(2147483647ll), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::string<16>(STR("7FFFFFFFFFFFFFFF")), etl::to_string(int64_t(9223372036854775807ll), str, Format().base(16).width(16).fill(STR('0')))); + + CHECK_EQUAL(etl::string<16>(STR("80")), etl::to_string(int8_t(-128), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::string<16>(STR("8000")), etl::to_string(int16_t(-32768), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::string<16>(STR("80000000")), etl::to_string(int32_t(-2147483648ll), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::string<16>(STR("8000000000000000")), etl::to_string(int64_t(-9223372036854775808ll), str, Format().base(16).width(16).fill(STR('0')))); } }; } diff --git a/test/test_to_u16string.cpp b/test/test_to_u16string.cpp new file mode 100644 index 00000000..7e62521e --- /dev/null +++ b/test/test_to_u16string.cpp @@ -0,0 +1,242 @@ +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2019 jwellbelove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#include "UnitTest++.h" + +#include + +#include "etl/to_u16string.h" +#include "etl/u16string.h" +#include "etl/format_spec.h" + +#undef STR +#define STR(x) u##x + +namespace +{ + typedef etl::format_spec Format; + + std::ostream& operator << (std::ostream& os, const etl::iu16string& str) + { + for (auto c : str) + { + os << c; + } + + return os; + } + + SUITE(test_to_u16string) + { + //************************************************************************* + TEST(test_default_format_no_append) + { + etl::u16string<20> str; + + CHECK_EQUAL(etl::u16string<20>(STR("0")), etl::to_u16string(uint8_t(0), str)); + CHECK_EQUAL(etl::u16string<20>(STR("0")), etl::to_u16string(uint16_t(0), str)); + CHECK_EQUAL(etl::u16string<20>(STR("0")), etl::to_u16string(uint32_t(0), str)); + CHECK_EQUAL(etl::u16string<20>(STR("0")), etl::to_u16string(uint64_t(0), str)); + + CHECK_EQUAL(etl::u16string<20>(STR("128")), etl::to_u16string(uint8_t(128), str)); + CHECK_EQUAL(etl::u16string<20>(STR("32768")), etl::to_u16string(uint16_t(32768), str)); + CHECK_EQUAL(etl::u16string<20>(STR("2147483648")), etl::to_u16string(uint32_t(2147483648ul), str)); + CHECK_EQUAL(etl::u16string<20>(STR("9223372036854775808")), etl::to_u16string(uint64_t(9223372036854775808ull), str)); + + CHECK_EQUAL(etl::u16string<20>(STR("127")), etl::to_u16string(int8_t(127), str)); + CHECK_EQUAL(etl::u16string<20>(STR("32767")), etl::to_u16string(int16_t(32767), str)); + CHECK_EQUAL(etl::u16string<20>(STR("2147483647")), etl::to_u16string(int32_t(2147483647ll), str)); + CHECK_EQUAL(etl::u16string<20>(STR("9223372036854775807")), etl::to_u16string(int64_t(9223372036854775807ll), str)); + + CHECK_EQUAL(etl::u16string<20>(STR("-128")), etl::to_u16string(int8_t(-128), str)); + CHECK_EQUAL(etl::u16string<20>(STR("-32768")), etl::to_u16string(int16_t(-32768), str)); + CHECK_EQUAL(etl::u16string<20>(STR("-2147483648")), etl::to_u16string(int32_t(-2147483648ll), str)); + CHECK_EQUAL(etl::u16string<20>(STR("-9223372036854775808")), etl::to_u16string(int64_t(-9223372036854775808ll), str)); + } + + //************************************************************************* + TEST(test_default_format_append) + { + etl::u16string<120> str; + + CHECK_EQUAL(etl::u16string<120>(STR("0")), etl::to_u16string(uint8_t(0), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("00")), etl::to_u16string(uint16_t(0), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("000")), etl::to_u16string(uint32_t(0), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("0000")), etl::to_u16string(uint64_t(0), str, true)); + + CHECK_EQUAL(etl::u16string<120>(STR("0000128")), etl::to_u16string(uint8_t(128), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("000012832768")), etl::to_u16string(uint16_t(32768), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("0000128327682147483648")), etl::to_u16string(uint32_t(2147483648ul), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("00001283276821474836489223372036854775808")), etl::to_u16string(uint64_t(9223372036854775808ull), str, true)); + + CHECK_EQUAL(etl::u16string<120>(STR("00001283276821474836489223372036854775808127")), etl::to_u16string(int8_t(127), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("0000128327682147483648922337203685477580812732767")), etl::to_u16string(int16_t(32767), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("00001283276821474836489223372036854775808127327672147483647")), etl::to_u16string(int32_t(2147483647ll), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807")), etl::to_u16string(int64_t(9223372036854775807ll), str, true)); + + CHECK_EQUAL(etl::u16string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128")), etl::to_u16string(int8_t(-128), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768")), etl::to_u16string(int16_t(-32768), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648")), etl::to_u16string(int32_t(-2147483648ll), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648-9223372036854775808")), etl::to_u16string(int64_t(-9223372036854775808ll), str, true)); + } + + //************************************************************************* + TEST(test_format_right_justified_no_append) + { + etl::u16string<20> str; + + Format format = Format().base(10).width(20).fill(STR('#')); + + 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)); + CHECK_EQUAL(etl::u16string<20>(STR("###################0")), etl::to_u16string(uint32_t(0), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("###################0")), etl::to_u16string(uint64_t(0), str, format)); + + CHECK_EQUAL(etl::u16string<20>(STR("#################128")), etl::to_u16string(uint8_t(128), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("###############32768")), etl::to_u16string(uint16_t(32768), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("##########2147483648")), etl::to_u16string(uint32_t(2147483648ul), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("#9223372036854775808")), etl::to_u16string(uint64_t(9223372036854775808ull), str, format)); + + CHECK_EQUAL(etl::u16string<20>(STR("#################127")), etl::to_u16string(int8_t(127), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("###############32767")), etl::to_u16string(int16_t(32767), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("##########2147483647")), etl::to_u16string(int32_t(2147483647ll), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("#9223372036854775807")), etl::to_u16string(int64_t(9223372036854775807ll), str, format)); + + CHECK_EQUAL(etl::u16string<20>(STR("################-128")), etl::to_u16string(int8_t(-128), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("##############-32768")), etl::to_u16string(int16_t(-32768), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("#########-2147483648")), etl::to_u16string(int32_t(-2147483648ll), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("-9223372036854775808")), etl::to_u16string(int64_t(-9223372036854775808ll), str, format)); + } + + //************************************************************************* + TEST(test_format_left_justified_no_append) + { + etl::u16string<20> str; + + Format format = Format().base(10).width(20).fill(STR('#')).left_justified(true); + + 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)); + CHECK_EQUAL(etl::u16string<20>(STR("0###################")), etl::to_u16string(uint32_t(0), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("0###################")), etl::to_u16string(uint64_t(0), str, format)); + + CHECK_EQUAL(etl::u16string<20>(STR("128#################")), etl::to_u16string(uint8_t(128), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("32768###############")), etl::to_u16string(uint16_t(32768), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("2147483648##########")), etl::to_u16string(uint32_t(2147483648ul), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("9223372036854775808#")), etl::to_u16string(uint64_t(9223372036854775808ull), str, format)); + + CHECK_EQUAL(etl::u16string<20>(STR("127#################")), etl::to_u16string(int8_t(127), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("32767###############")), etl::to_u16string(int16_t(32767), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("2147483647##########")), etl::to_u16string(int32_t(2147483647ll), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("9223372036854775807#")), etl::to_u16string(int64_t(9223372036854775807ll), str, format)); + + CHECK_EQUAL(etl::u16string<20>(STR("-128################")), etl::to_u16string(int8_t(-128), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("-32768##############")), etl::to_u16string(int16_t(-32768), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("-2147483648#########")), etl::to_u16string(int32_t(-2147483648ll), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("-9223372036854775808")), etl::to_u16string(int64_t(-9223372036854775808ll), str, format)); + } + + //************************************************************************* + TEST(test_binary_format_no_append) + { + etl::u16string<64> str; + + CHECK_EQUAL(etl::u16string<64>(STR("00000000")), etl::to_u16string(uint8_t(0), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("0000000000000000")), etl::to_u16string(uint16_t(0), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("00000000000000000000000000000000")), etl::to_u16string(uint32_t(0), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("0000000000000000000000000000000000000000000000000000000000000000")), etl::to_u16string(uint64_t(0), str, Format().base(2).width(64).fill(STR('0')))); + + CHECK_EQUAL(etl::u16string<64>(STR("10000000")), etl::to_u16string(uint8_t(128), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("1000000000000000")), etl::to_u16string(uint16_t(32768), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("10000000000000000000000000000000")), etl::to_u16string(uint32_t(2147483648ul), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_u16string(uint64_t(9223372036854775808ull), str, Format().base(2).width(64).fill(STR('0')))); + + CHECK_EQUAL(etl::u16string<64>(STR("01111111")), etl::to_u16string(int8_t(127), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("0111111111111111")), etl::to_u16string(int16_t(32767), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("01111111111111111111111111111111")), etl::to_u16string(int32_t(2147483647ll), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("0111111111111111111111111111111111111111111111111111111111111111")), etl::to_u16string(int64_t(9223372036854775807ll), str, Format().base(2).width(64).fill(STR('0')))); + + CHECK_EQUAL(etl::u16string<64>(STR("10000000")), etl::to_u16string(int8_t(-128), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("1000000000000000")), etl::to_u16string(int16_t(-32768), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("10000000000000000000000000000000")), etl::to_u16string(int32_t(-2147483648ll), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_u16string(int64_t(-9223372036854775808ll), str, Format().base(2).width(64).fill(STR('0')))); + } + + //************************************************************************* + TEST(test_octal_format_no_append) + { + etl::u16string<22> str; + + CHECK_EQUAL(etl::u16string<22>(STR("000")), etl::to_u16string(uint8_t(0), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("000000")), etl::to_u16string(uint16_t(0), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("00000000000")), etl::to_u16string(uint32_t(0), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("0000000000000000000000")), etl::to_u16string(uint64_t(0), str, Format().base(8).width(22).fill(STR('0')))); + + CHECK_EQUAL(etl::u16string<22>(STR("200")), etl::to_u16string(uint8_t(128), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("100000")), etl::to_u16string(uint16_t(32768), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("20000000000")), etl::to_u16string(uint32_t(2147483648ul), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("1000000000000000000000")), etl::to_u16string(uint64_t(9223372036854775808ull), str, Format().base(8).width(22).fill(STR('0')))); + + CHECK_EQUAL(etl::u16string<22>(STR("177")), etl::to_u16string(int8_t(127), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("077777")), etl::to_u16string(int16_t(32767), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("17777777777")), etl::to_u16string(int32_t(2147483647ll), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("0777777777777777777777")), etl::to_u16string(int64_t(9223372036854775807ll), str, Format().base(8).width(22).fill(STR('0')))); + + CHECK_EQUAL(etl::u16string<22>(STR("200")), etl::to_u16string(int8_t(-128), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("100000")), etl::to_u16string(int16_t(-32768), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("20000000000")), etl::to_u16string(int32_t(-2147483648ll), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("1000000000000000000000")), etl::to_u16string(int64_t(-9223372036854775808ll), str, Format().base(8).width(22).fill(STR('0')))); + } + + //************************************************************************* + TEST(test_hex_format_no_append) + { + etl::u16string<16> str; + + CHECK_EQUAL(etl::u16string<16>(STR("00")), etl::to_u16string(uint8_t(0), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("0000")), etl::to_u16string(uint16_t(0), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("00000000")), etl::to_u16string(uint32_t(0), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("0000000000000000")), etl::to_u16string(uint64_t(0), str, Format().base(16).width(16).fill(STR('0')))); + + CHECK_EQUAL(etl::u16string<16>(STR("80")), etl::to_u16string(uint8_t(128), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("8000")), etl::to_u16string(uint16_t(32768), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("80000000")), etl::to_u16string(uint32_t(2147483648ul), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("8000000000000000")), etl::to_u16string(uint64_t(9223372036854775808ull), str, Format().base(16).width(16).fill(STR('0')))); + + CHECK_EQUAL(etl::u16string<16>(STR("7F")), etl::to_u16string(int8_t(127), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("7FFF")), etl::to_u16string(int16_t(32767), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("7FFFFFFF")), etl::to_u16string(int32_t(2147483647ll), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("7FFFFFFFFFFFFFFF")), etl::to_u16string(int64_t(9223372036854775807ll), str, Format().base(16).width(16).fill(STR('0')))); + + CHECK_EQUAL(etl::u16string<16>(STR("80")), etl::to_u16string(int8_t(-128), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("8000")), etl::to_u16string(int16_t(-32768), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("80000000")), etl::to_u16string(int32_t(-2147483648ll), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("8000000000000000")), etl::to_u16string(int64_t(-9223372036854775808ll), str, Format().base(16).width(16).fill(STR('0')))); + } + }; +} diff --git a/test/test_to_u32string.cpp b/test/test_to_u32string.cpp new file mode 100644 index 00000000..ce161691 --- /dev/null +++ b/test/test_to_u32string.cpp @@ -0,0 +1,242 @@ +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2019 jwellbelove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#include "UnitTest++.h" + +#include + +#include "etl/to_u32string.h" +#include "etl/u32string.h" +#include "etl/format_spec.h" + +#undef STR +#define STR(x) U##x + +namespace +{ + typedef etl::format_spec Format; + + std::ostream& operator << (std::ostream& os, const etl::iu32string& str) + { + for (auto c : str) + { + os << c; + } + + return os; + } + + SUITE(test_to_u32string) + { + //************************************************************************* + TEST(test_default_format_no_append) + { + etl::u32string<20> str; + + CHECK_EQUAL(etl::u32string<20>(STR("0")), etl::to_u32string(uint8_t(0), str)); + CHECK_EQUAL(etl::u32string<20>(STR("0")), etl::to_u32string(uint16_t(0), str)); + CHECK_EQUAL(etl::u32string<20>(STR("0")), etl::to_u32string(uint32_t(0), str)); + CHECK_EQUAL(etl::u32string<20>(STR("0")), etl::to_u32string(uint64_t(0), str)); + + CHECK_EQUAL(etl::u32string<20>(STR("128")), etl::to_u32string(uint8_t(128), str)); + CHECK_EQUAL(etl::u32string<20>(STR("32768")), etl::to_u32string(uint16_t(32768), str)); + CHECK_EQUAL(etl::u32string<20>(STR("2147483648")), etl::to_u32string(uint32_t(2147483648ul), str)); + CHECK_EQUAL(etl::u32string<20>(STR("9223372036854775808")), etl::to_u32string(uint64_t(9223372036854775808ull), str)); + + CHECK_EQUAL(etl::u32string<20>(STR("127")), etl::to_u32string(int8_t(127), str)); + CHECK_EQUAL(etl::u32string<20>(STR("32767")), etl::to_u32string(int16_t(32767), str)); + CHECK_EQUAL(etl::u32string<20>(STR("2147483647")), etl::to_u32string(int32_t(2147483647ll), str)); + CHECK_EQUAL(etl::u32string<20>(STR("9223372036854775807")), etl::to_u32string(int64_t(9223372036854775807ll), str)); + + CHECK_EQUAL(etl::u32string<20>(STR("-128")), etl::to_u32string(int8_t(-128), str)); + CHECK_EQUAL(etl::u32string<20>(STR("-32768")), etl::to_u32string(int16_t(-32768), str)); + CHECK_EQUAL(etl::u32string<20>(STR("-2147483648")), etl::to_u32string(int32_t(-2147483648ll), str)); + CHECK_EQUAL(etl::u32string<20>(STR("-9223372036854775808")), etl::to_u32string(int64_t(-9223372036854775808ll), str)); + } + + //************************************************************************* + TEST(test_default_format_append) + { + etl::u32string<120> str; + + CHECK_EQUAL(etl::u32string<120>(STR("0")), etl::to_u32string(uint8_t(0), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("00")), etl::to_u32string(uint16_t(0), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("000")), etl::to_u32string(uint32_t(0), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("0000")), etl::to_u32string(uint64_t(0), str, true)); + + CHECK_EQUAL(etl::u32string<120>(STR("0000128")), etl::to_u32string(uint8_t(128), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("000012832768")), etl::to_u32string(uint16_t(32768), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("0000128327682147483648")), etl::to_u32string(uint32_t(2147483648ul), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("00001283276821474836489223372036854775808")), etl::to_u32string(uint64_t(9223372036854775808ull), str, true)); + + CHECK_EQUAL(etl::u32string<120>(STR("00001283276821474836489223372036854775808127")), etl::to_u32string(int8_t(127), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("0000128327682147483648922337203685477580812732767")), etl::to_u32string(int16_t(32767), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("00001283276821474836489223372036854775808127327672147483647")), etl::to_u32string(int32_t(2147483647ll), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807")), etl::to_u32string(int64_t(9223372036854775807ll), str, true)); + + CHECK_EQUAL(etl::u32string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128")), etl::to_u32string(int8_t(-128), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768")), etl::to_u32string(int16_t(-32768), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648")), etl::to_u32string(int32_t(-2147483648ll), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648-9223372036854775808")), etl::to_u32string(int64_t(-9223372036854775808ll), str, true)); + } + + //************************************************************************* + TEST(test_format_right_justified_no_append) + { + etl::u32string<20> str; + + Format format = Format().base(10).width(20).fill(STR('#')); + + 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)); + CHECK_EQUAL(etl::u32string<20>(STR("###################0")), etl::to_u32string(uint32_t(0), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("###################0")), etl::to_u32string(uint64_t(0), str, format)); + + CHECK_EQUAL(etl::u32string<20>(STR("#################128")), etl::to_u32string(uint8_t(128), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("###############32768")), etl::to_u32string(uint16_t(32768), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("##########2147483648")), etl::to_u32string(uint32_t(2147483648ul), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("#9223372036854775808")), etl::to_u32string(uint64_t(9223372036854775808ull), str, format)); + + CHECK_EQUAL(etl::u32string<20>(STR("#################127")), etl::to_u32string(int8_t(127), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("###############32767")), etl::to_u32string(int16_t(32767), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("##########2147483647")), etl::to_u32string(int32_t(2147483647ll), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("#9223372036854775807")), etl::to_u32string(int64_t(9223372036854775807ll), str, format)); + + CHECK_EQUAL(etl::u32string<20>(STR("################-128")), etl::to_u32string(int8_t(-128), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("##############-32768")), etl::to_u32string(int16_t(-32768), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("#########-2147483648")), etl::to_u32string(int32_t(-2147483648ll), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("-9223372036854775808")), etl::to_u32string(int64_t(-9223372036854775808ll), str, format)); + } + + //************************************************************************* + TEST(test_format_left_justified_no_append) + { + etl::u32string<20> str; + + Format format = Format().base(10).width(20).fill(STR('#')).left_justified(true); + + 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)); + CHECK_EQUAL(etl::u32string<20>(STR("0###################")), etl::to_u32string(uint32_t(0), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("0###################")), etl::to_u32string(uint64_t(0), str, format)); + + CHECK_EQUAL(etl::u32string<20>(STR("128#################")), etl::to_u32string(uint8_t(128), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("32768###############")), etl::to_u32string(uint16_t(32768), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("2147483648##########")), etl::to_u32string(uint32_t(2147483648ul), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("9223372036854775808#")), etl::to_u32string(uint64_t(9223372036854775808ull), str, format)); + + CHECK_EQUAL(etl::u32string<20>(STR("127#################")), etl::to_u32string(int8_t(127), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("32767###############")), etl::to_u32string(int16_t(32767), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("2147483647##########")), etl::to_u32string(int32_t(2147483647ll), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("9223372036854775807#")), etl::to_u32string(int64_t(9223372036854775807ll), str, format)); + + CHECK_EQUAL(etl::u32string<20>(STR("-128################")), etl::to_u32string(int8_t(-128), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("-32768##############")), etl::to_u32string(int16_t(-32768), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("-2147483648#########")), etl::to_u32string(int32_t(-2147483648ll), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("-9223372036854775808")), etl::to_u32string(int64_t(-9223372036854775808ll), str, format)); + } + + //************************************************************************* + TEST(test_binary_format_no_append) + { + etl::u32string<64> str; + + CHECK_EQUAL(etl::u32string<64>(STR("00000000")), etl::to_u32string(uint8_t(0), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("0000000000000000")), etl::to_u32string(uint16_t(0), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("00000000000000000000000000000000")), etl::to_u32string(uint32_t(0), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("0000000000000000000000000000000000000000000000000000000000000000")), etl::to_u32string(uint64_t(0), str, Format().base(2).width(64).fill(STR('0')))); + + CHECK_EQUAL(etl::u32string<64>(STR("10000000")), etl::to_u32string(uint8_t(128), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("1000000000000000")), etl::to_u32string(uint16_t(32768), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("10000000000000000000000000000000")), etl::to_u32string(uint32_t(2147483648ul), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_u32string(uint64_t(9223372036854775808ull), str, Format().base(2).width(64).fill(STR('0')))); + + CHECK_EQUAL(etl::u32string<64>(STR("01111111")), etl::to_u32string(int8_t(127), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("0111111111111111")), etl::to_u32string(int16_t(32767), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("01111111111111111111111111111111")), etl::to_u32string(int32_t(2147483647ll), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("0111111111111111111111111111111111111111111111111111111111111111")), etl::to_u32string(int64_t(9223372036854775807ll), str, Format().base(2).width(64).fill(STR('0')))); + + CHECK_EQUAL(etl::u32string<64>(STR("10000000")), etl::to_u32string(int8_t(-128), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("1000000000000000")), etl::to_u32string(int16_t(-32768), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("10000000000000000000000000000000")), etl::to_u32string(int32_t(-2147483648ll), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_u32string(int64_t(-9223372036854775808ll), str, Format().base(2).width(64).fill(STR('0')))); + } + + //************************************************************************* + TEST(test_octal_format_no_append) + { + etl::u32string<22> str; + + CHECK_EQUAL(etl::u32string<22>(STR("000")), etl::to_u32string(uint8_t(0), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("000000")), etl::to_u32string(uint16_t(0), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("00000000000")), etl::to_u32string(uint32_t(0), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("0000000000000000000000")), etl::to_u32string(uint64_t(0), str, Format().base(8).width(22).fill(STR('0')))); + + CHECK_EQUAL(etl::u32string<22>(STR("200")), etl::to_u32string(uint8_t(128), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("100000")), etl::to_u32string(uint16_t(32768), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("20000000000")), etl::to_u32string(uint32_t(2147483648ul), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("1000000000000000000000")), etl::to_u32string(uint64_t(9223372036854775808ull), str, Format().base(8).width(22).fill(STR('0')))); + + CHECK_EQUAL(etl::u32string<22>(STR("177")), etl::to_u32string(int8_t(127), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("077777")), etl::to_u32string(int16_t(32767), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("17777777777")), etl::to_u32string(int32_t(2147483647ll), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("0777777777777777777777")), etl::to_u32string(int64_t(9223372036854775807ll), str, Format().base(8).width(22).fill(STR('0')))); + + CHECK_EQUAL(etl::u32string<22>(STR("200")), etl::to_u32string(int8_t(-128), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("100000")), etl::to_u32string(int16_t(-32768), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("20000000000")), etl::to_u32string(int32_t(-2147483648ll), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("1000000000000000000000")), etl::to_u32string(int64_t(-9223372036854775808ll), str, Format().base(8).width(22).fill(STR('0')))); + } + + //************************************************************************* + TEST(test_hex_format_no_append) + { + etl::u32string<16> str; + + CHECK_EQUAL(etl::u32string<16>(STR("00")), etl::to_u32string(uint8_t(0), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("0000")), etl::to_u32string(uint16_t(0), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("00000000")), etl::to_u32string(uint32_t(0), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("0000000000000000")), etl::to_u32string(uint64_t(0), str, Format().base(16).width(16).fill(STR('0')))); + + CHECK_EQUAL(etl::u32string<16>(STR("80")), etl::to_u32string(uint8_t(128), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("8000")), etl::to_u32string(uint16_t(32768), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("80000000")), etl::to_u32string(uint32_t(2147483648ul), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("8000000000000000")), etl::to_u32string(uint64_t(9223372036854775808ull), str, Format().base(16).width(16).fill(STR('0')))); + + CHECK_EQUAL(etl::u32string<16>(STR("7F")), etl::to_u32string(int8_t(127), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("7FFF")), etl::to_u32string(int16_t(32767), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("7FFFFFFF")), etl::to_u32string(int32_t(2147483647ll), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("7FFFFFFFFFFFFFFF")), etl::to_u32string(int64_t(9223372036854775807ll), str, Format().base(16).width(16).fill(STR('0')))); + + CHECK_EQUAL(etl::u32string<16>(STR("80")), etl::to_u32string(int8_t(-128), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("8000")), etl::to_u32string(int16_t(-32768), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("80000000")), etl::to_u32string(int32_t(-2147483648ll), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("8000000000000000")), etl::to_u32string(int64_t(-9223372036854775808ll), str, Format().base(16).width(16).fill(STR('0')))); + } + }; +} diff --git a/test/test_to_wstring.cpp b/test/test_to_wstring.cpp new file mode 100644 index 00000000..d3094865 --- /dev/null +++ b/test/test_to_wstring.cpp @@ -0,0 +1,242 @@ +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2019 jwellbelove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#include "UnitTest++.h" + +#include + +#include "etl/to_wstring.h" +#include "etl/wstring.h" +#include "etl/format_spec.h" + +#undef STR +#define STR(x) L##x + +namespace +{ + typedef etl::format_spec Format; + + std::ostream& operator << (std::ostream& os, const etl::iwstring& str) + { + for (auto c : str) + { + os << c; + } + + return os; + } + + SUITE(test_to_wstring) + { + //************************************************************************* + TEST(test_default_format_no_append) + { + etl::wstring<20> str; + + CHECK_EQUAL(etl::wstring<20>(STR("0")), etl::to_wstring(uint8_t(0), str)); + CHECK_EQUAL(etl::wstring<20>(STR("0")), etl::to_wstring(uint16_t(0), str)); + CHECK_EQUAL(etl::wstring<20>(STR("0")), etl::to_wstring(uint32_t(0), str)); + CHECK_EQUAL(etl::wstring<20>(STR("0")), etl::to_wstring(uint64_t(0), str)); + + CHECK_EQUAL(etl::wstring<20>(STR("128")), etl::to_wstring(uint8_t(128), str)); + CHECK_EQUAL(etl::wstring<20>(STR("32768")), etl::to_wstring(uint16_t(32768), str)); + CHECK_EQUAL(etl::wstring<20>(STR("2147483648")), etl::to_wstring(uint32_t(2147483648ul), str)); + CHECK_EQUAL(etl::wstring<20>(STR("9223372036854775808")), etl::to_wstring(uint64_t(9223372036854775808ull), str)); + + CHECK_EQUAL(etl::wstring<20>(STR("127")), etl::to_wstring(int8_t(127), str)); + CHECK_EQUAL(etl::wstring<20>(STR("32767")), etl::to_wstring(int16_t(32767), str)); + CHECK_EQUAL(etl::wstring<20>(STR("2147483647")), etl::to_wstring(int32_t(2147483647ll), str)); + CHECK_EQUAL(etl::wstring<20>(STR("9223372036854775807")), etl::to_wstring(int64_t(9223372036854775807ll), str)); + + CHECK_EQUAL(etl::wstring<20>(STR("-128")), etl::to_wstring(int8_t(-128), str)); + CHECK_EQUAL(etl::wstring<20>(STR("-32768")), etl::to_wstring(int16_t(-32768), str)); + CHECK_EQUAL(etl::wstring<20>(STR("-2147483648")), etl::to_wstring(int32_t(-2147483648ll), str)); + CHECK_EQUAL(etl::wstring<20>(STR("-9223372036854775808")), etl::to_wstring(int64_t(-9223372036854775808ll), str)); + } + + //************************************************************************* + TEST(test_default_format_append) + { + etl::wstring<120> str; + + CHECK_EQUAL(etl::wstring<120>(STR("0")), etl::to_wstring(uint8_t(0), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("00")), etl::to_wstring(uint16_t(0), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("000")), etl::to_wstring(uint32_t(0), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("0000")), etl::to_wstring(uint64_t(0), str, true)); + + CHECK_EQUAL(etl::wstring<120>(STR("0000128")), etl::to_wstring(uint8_t(128), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("000012832768")), etl::to_wstring(uint16_t(32768), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("0000128327682147483648")), etl::to_wstring(uint32_t(2147483648ul), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("00001283276821474836489223372036854775808")), etl::to_wstring(uint64_t(9223372036854775808ull), str, true)); + + CHECK_EQUAL(etl::wstring<120>(STR("00001283276821474836489223372036854775808127")), etl::to_wstring(int8_t(127), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("0000128327682147483648922337203685477580812732767")), etl::to_wstring(int16_t(32767), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("00001283276821474836489223372036854775808127327672147483647")), etl::to_wstring(int32_t(2147483647ll), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807")), etl::to_wstring(int64_t(9223372036854775807ll), str, true)); + + CHECK_EQUAL(etl::wstring<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128")), etl::to_wstring(int8_t(-128), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768")), etl::to_wstring(int16_t(-32768), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648")), etl::to_wstring(int32_t(-2147483648ll), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648-9223372036854775808")), etl::to_wstring(int64_t(-9223372036854775808ll), str, true)); + } + + //************************************************************************* + TEST(test_format_right_justified_no_append) + { + etl::wstring<20> str; + + Format format = Format().base(10).width(20).fill(STR('#')); + + 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)); + CHECK_EQUAL(etl::wstring<20>(STR("###################0")), etl::to_wstring(uint32_t(0), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("###################0")), etl::to_wstring(uint64_t(0), str, format)); + + CHECK_EQUAL(etl::wstring<20>(STR("#################128")), etl::to_wstring(uint8_t(128), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("###############32768")), etl::to_wstring(uint16_t(32768), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("##########2147483648")), etl::to_wstring(uint32_t(2147483648ul), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("#9223372036854775808")), etl::to_wstring(uint64_t(9223372036854775808ull), str, format)); + + CHECK_EQUAL(etl::wstring<20>(STR("#################127")), etl::to_wstring(int8_t(127), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("###############32767")), etl::to_wstring(int16_t(32767), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("##########2147483647")), etl::to_wstring(int32_t(2147483647ll), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("#9223372036854775807")), etl::to_wstring(int64_t(9223372036854775807ll), str, format)); + + CHECK_EQUAL(etl::wstring<20>(STR("################-128")), etl::to_wstring(int8_t(-128), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("##############-32768")), etl::to_wstring(int16_t(-32768), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("#########-2147483648")), etl::to_wstring(int32_t(-2147483648ll), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("-9223372036854775808")), etl::to_wstring(int64_t(-9223372036854775808ll), str, format)); + } + + //************************************************************************* + TEST(test_format_left_justified_no_append) + { + etl::wstring<20> str; + + Format format = Format().base(10).width(20).fill(STR('#')).left_justified(true); + + 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)); + CHECK_EQUAL(etl::wstring<20>(STR("0###################")), etl::to_wstring(uint32_t(0), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("0###################")), etl::to_wstring(uint64_t(0), str, format)); + + CHECK_EQUAL(etl::wstring<20>(STR("128#################")), etl::to_wstring(uint8_t(128), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("32768###############")), etl::to_wstring(uint16_t(32768), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("2147483648##########")), etl::to_wstring(uint32_t(2147483648ul), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("9223372036854775808#")), etl::to_wstring(uint64_t(9223372036854775808ull), str, format)); + + CHECK_EQUAL(etl::wstring<20>(STR("127#################")), etl::to_wstring(int8_t(127), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("32767###############")), etl::to_wstring(int16_t(32767), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("2147483647##########")), etl::to_wstring(int32_t(2147483647ll), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("9223372036854775807#")), etl::to_wstring(int64_t(9223372036854775807ll), str, format)); + + CHECK_EQUAL(etl::wstring<20>(STR("-128################")), etl::to_wstring(int8_t(-128), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("-32768##############")), etl::to_wstring(int16_t(-32768), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("-2147483648#########")), etl::to_wstring(int32_t(-2147483648ll), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("-9223372036854775808")), etl::to_wstring(int64_t(-9223372036854775808ll), str, format)); + } + + //************************************************************************* + TEST(test_binary_format_no_append) + { + etl::wstring<64> str; + + CHECK_EQUAL(etl::wstring<64>(STR("00000000")), etl::to_wstring(uint8_t(0), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("0000000000000000")), etl::to_wstring(uint16_t(0), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("00000000000000000000000000000000")), etl::to_wstring(uint32_t(0), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("0000000000000000000000000000000000000000000000000000000000000000")), etl::to_wstring(uint64_t(0), str, Format().base(2).width(64).fill(STR('0')))); + + CHECK_EQUAL(etl::wstring<64>(STR("10000000")), etl::to_wstring(uint8_t(128), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("1000000000000000")), etl::to_wstring(uint16_t(32768), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("10000000000000000000000000000000")), etl::to_wstring(uint32_t(2147483648ul), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_wstring(uint64_t(9223372036854775808ull), str, Format().base(2).width(64).fill(STR('0')))); + + CHECK_EQUAL(etl::wstring<64>(STR("01111111")), etl::to_wstring(int8_t(127), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("0111111111111111")), etl::to_wstring(int16_t(32767), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("01111111111111111111111111111111")), etl::to_wstring(int32_t(2147483647ll), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("0111111111111111111111111111111111111111111111111111111111111111")), etl::to_wstring(int64_t(9223372036854775807ll), str, Format().base(2).width(64).fill(STR('0')))); + + CHECK_EQUAL(etl::wstring<64>(STR("10000000")), etl::to_wstring(int8_t(-128), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("1000000000000000")), etl::to_wstring(int16_t(-32768), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("10000000000000000000000000000000")), etl::to_wstring(int32_t(-2147483648ll), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_wstring(int64_t(-9223372036854775808ll), str, Format().base(2).width(64).fill(STR('0')))); + } + + //************************************************************************* + TEST(test_octal_format_no_append) + { + etl::wstring<22> str; + + CHECK_EQUAL(etl::wstring<22>(STR("000")), etl::to_wstring(uint8_t(0), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("000000")), etl::to_wstring(uint16_t(0), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("00000000000")), etl::to_wstring(uint32_t(0), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("0000000000000000000000")), etl::to_wstring(uint64_t(0), str, Format().base(8).width(22).fill(STR('0')))); + + CHECK_EQUAL(etl::wstring<22>(STR("200")), etl::to_wstring(uint8_t(128), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("100000")), etl::to_wstring(uint16_t(32768), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("20000000000")), etl::to_wstring(uint32_t(2147483648ul), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("1000000000000000000000")), etl::to_wstring(uint64_t(9223372036854775808ull), str, Format().base(8).width(22).fill(STR('0')))); + + CHECK_EQUAL(etl::wstring<22>(STR("177")), etl::to_wstring(int8_t(127), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("077777")), etl::to_wstring(int16_t(32767), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("17777777777")), etl::to_wstring(int32_t(2147483647ll), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("0777777777777777777777")), etl::to_wstring(int64_t(9223372036854775807ll), str, Format().base(8).width(22).fill(STR('0')))); + + CHECK_EQUAL(etl::wstring<22>(STR("200")), etl::to_wstring(int8_t(-128), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("100000")), etl::to_wstring(int16_t(-32768), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("20000000000")), etl::to_wstring(int32_t(-2147483648ll), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("1000000000000000000000")), etl::to_wstring(int64_t(-9223372036854775808ll), str, Format().base(8).width(22).fill(STR('0')))); + } + + //************************************************************************* + TEST(test_hex_format_no_append) + { + etl::wstring<16> str; + + CHECK_EQUAL(etl::wstring<16>(STR("00")), etl::to_wstring(uint8_t(0), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("0000")), etl::to_wstring(uint16_t(0), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("00000000")), etl::to_wstring(uint32_t(0), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("0000000000000000")), etl::to_wstring(uint64_t(0), str, Format().base(16).width(16).fill(STR('0')))); + + CHECK_EQUAL(etl::wstring<16>(STR("80")), etl::to_wstring(uint8_t(128), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("8000")), etl::to_wstring(uint16_t(32768), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("80000000")), etl::to_wstring(uint32_t(2147483648ul), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("8000000000000000")), etl::to_wstring(uint64_t(9223372036854775808ull), str, Format().base(16).width(16).fill(STR('0')))); + + CHECK_EQUAL(etl::wstring<16>(STR("7F")), etl::to_wstring(int8_t(127), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("7FFF")), etl::to_wstring(int16_t(32767), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("7FFFFFFF")), etl::to_wstring(int32_t(2147483647ll), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("7FFFFFFFFFFFFFFF")), etl::to_wstring(int64_t(9223372036854775807ll), str, Format().base(16).width(16).fill(STR('0')))); + + CHECK_EQUAL(etl::wstring<16>(STR("80")), etl::to_wstring(int8_t(-128), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("8000")), etl::to_wstring(int16_t(-32768), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("80000000")), etl::to_wstring(int32_t(-2147483648ll), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("8000000000000000")), etl::to_wstring(int64_t(-9223372036854775808ll), str, Format().base(16).width(16).fill(STR('0')))); + } + }; +} diff --git a/test/vs2017/etl.vcxproj b/test/vs2017/etl.vcxproj index ac1dcfeb..38450030 100644 --- a/test/vs2017/etl.vcxproj +++ b/test/vs2017/etl.vcxproj @@ -753,6 +753,9 @@ + + + diff --git a/test/vs2017/etl.vcxproj.filters b/test/vs2017/etl.vcxproj.filters index 59beb25d..ce39c495 100644 --- a/test/vs2017/etl.vcxproj.filters +++ b/test/vs2017/etl.vcxproj.filters @@ -1193,6 +1193,15 @@ Source Files + + Source Files + + + Source Files + + + Source Files +