From b110a967abee0338a1668bd6921d9d803226c577 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Fri, 1 Mar 2019 21:08:40 +0100 Subject: [PATCH 01/41] First commit --- test/test_to_string.cpp | 108 ++++++++++++++++++++++++++++++++ test/vs2017/etl.vcxproj | 1 + test/vs2017/etl.vcxproj.filters | 3 + 3 files changed, 112 insertions(+) create mode 100644 test/test_to_string.cpp diff --git a/test/test_to_string.cpp b/test/test_to_string.cpp new file mode 100644 index 00000000..ba1e182a --- /dev/null +++ b/test/test_to_string.cpp @@ -0,0 +1,108 @@ +/****************************************************************************** +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/cstring.h" + +#undef STR +#define STR(x) x + +namespace +{ + /* A utility function to reverse a string */ + void reverse(char str[], int length) + { + int start = 0; + int end = length - 1; + while (start < end) + { + std::swap(*(str + start), *(str + end)); + start++; + end--; + } + } + + // Implementation of itoa() + char* itoa(int num, char* str, int base) + { + int i = 0; + bool isNegative = false; + + /* Handle 0 explicitely, otherwise empty string is printed for 0 */ + if (num == 0) + { + str[i++] = '0'; + str[i] = '\0'; + return str; + } + + // In standard itoa(), negative numbers are handled only with + // base 10. Otherwise numbers are considered unsigned. + if (num < 0 && base == 10) + { + isNegative = true; + num = -num; + } + + // Process individual digits + while (num != 0) + { + int rem = num % base; + str[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0'; + num = num / base; + } + + // If number is negative, append '-' + if (isNegative) + str[i++] = '-'; + + str[i] = '\0'; // Append string terminator + + // Reverse the string + reverse(str, i); + + return str; + } + + SUITE(test_string_char) + { + //************************************************************************* + TEST(test_x) + { + char str[100]; + itoa(1567, str, 10); + itoa(-1567, str, 10); + itoa(1567, str, 2); + itoa(1567, str, 8); + itoa(1567, str, 16); + } + }; +} diff --git a/test/vs2017/etl.vcxproj b/test/vs2017/etl.vcxproj index d01f21d8..baaa7f5f 100644 --- a/test/vs2017/etl.vcxproj +++ b/test/vs2017/etl.vcxproj @@ -745,6 +745,7 @@ + diff --git a/test/vs2017/etl.vcxproj.filters b/test/vs2017/etl.vcxproj.filters index bb822b22..37e13f60 100644 --- a/test/vs2017/etl.vcxproj.filters +++ b/test/vs2017/etl.vcxproj.filters @@ -1163,6 +1163,9 @@ Source Files + + Source Files + From b996e52244410116c0e703a0b46cb13e5fcb2ede Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 3 Mar 2019 22:35:19 +0000 Subject: [PATCH 02/41] Initial version --- include/etl/to_string.h | 101 ++++++++++++++++++++++++++++++++++++++++ test/test_to_string.cpp | 33 +++++++------ 2 files changed, 121 insertions(+), 13 deletions(-) create mode 100644 include/etl/to_string.h diff --git a/include/etl/to_string.h b/include/etl/to_string.h new file mode 100644 index 00000000..54c5021b --- /dev/null +++ b/include/etl/to_string.h @@ -0,0 +1,101 @@ +///\file + +/****************************************************************************** +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. +******************************************************************************/ + +#ifndef ETL_TO_STRING_INCLUDED +#define ETL_TO_STRING_INCLUDED + +///\ingroup utilities + +#include "platform.h" +#include "type_traits.h" +#include "stl/algorithm.h" + +namespace etl +{ + template + void to_string_helper(T value, const bool negative, TString& str, const int base, const bool upper_case) + { + typedef typename TString::value_type type; + typedef typename TString::iterator iterator; + + iterator start = str.end(); + + if (value == 0) + { + str.push_back(type('0')); + } + else + { + // Extract the digits, in reverse order. + while (value != 0) + { + T remainder = value % base; + str.push_back((remainder > 9) ? type('a' + (remainder - 10)) : type('0' + remainder)); + value = value / base; + } + + // If number is negative, append '-' + if ((base == 10) && negative) + { + str.push_back(type('-')); + } + + // Reverse the string we appended. + std::reverse(start, str.end()); + } + } + + //*************************************************************************** + /// + //*************************************************************************** + template + typename etl::enable_if::value && etl::is_signed::value, void>::type + to_string(T value, TString& str, const int base = 10, const bool upper_case = true) + { + typedef typename etl::make_unsigned::type Unsigned; + + const bool negative = (value < 0); + const Unsigned uvalue = (negative ? -value : value); + + to_string_helper(uvalue, negative, str, base, upper_case); + } + + //*************************************************************************** + /// + //*************************************************************************** + template + typename etl::enable_if::value && etl::is_unsigned::value, void>::type + to_string(T value, TString& str, const int base = 10, const bool upper_case = true) + { + to_string_helper(value, false, str, base, upper_case); + } +} + +#endif diff --git a/test/test_to_string.cpp b/test/test_to_string.cpp index ba1e182a..5453ab57 100644 --- a/test/test_to_string.cpp +++ b/test/test_to_string.cpp @@ -30,6 +30,7 @@ SOFTWARE. #include +#include "etl/to_string.h" #include "etl/cstring.h" #undef STR @@ -50,7 +51,7 @@ namespace } } - // Implementation of itoa() + // Implementation of itoa() char* itoa(int num, char* str, int base) { int i = 0; @@ -64,15 +65,15 @@ namespace return str; } - // In standard itoa(), negative numbers are handled only with - // base 10. Otherwise numbers are considered unsigned. + // In standard itoa(), negative numbers are handled only with + // base 10. Otherwise numbers are considered unsigned. if (num < 0 && base == 10) { isNegative = true; num = -num; } - // Process individual digits + // Process individual digits while (num != 0) { int rem = num % base; @@ -80,13 +81,13 @@ namespace num = num / base; } - // If number is negative, append '-' + // If number is negative, append '-' if (isNegative) str[i++] = '-'; - str[i] = '\0'; // Append string terminator + str[i] = '\0'; // Append string terminator - // Reverse the string + // Reverse the string reverse(str, i); return str; @@ -97,12 +98,18 @@ namespace //************************************************************************* TEST(test_x) { - char str[100]; - itoa(1567, str, 10); - itoa(-1567, str, 10); - itoa(1567, str, 2); - itoa(1567, str, 8); - itoa(1567, str, 16); + //char str[100]; + + etl::string<10> str; + + etl::to_string(char(127), str, 10); + + str.clear(); + etl::to_string(char(-128), str, 10); + //itoa(-1567, str, 10); + //itoa(1567, str, 2); + //itoa(1567, str, 8); + //itoa(1567, str, 16); } }; } From 310fecdd2607a3648d2cb3ffc0c5f978a9ac2747 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 3 Mar 2019 22:36:22 +0000 Subject: [PATCH 03/41] Initial version --- test/vs2017/etl.vcxproj | 1 + test/vs2017/etl.vcxproj.filters | 3 +++ 2 files changed, 4 insertions(+) diff --git a/test/vs2017/etl.vcxproj b/test/vs2017/etl.vcxproj index baaa7f5f..1e77e967 100644 --- a/test/vs2017/etl.vcxproj +++ b/test/vs2017/etl.vcxproj @@ -434,6 +434,7 @@ + diff --git a/test/vs2017/etl.vcxproj.filters b/test/vs2017/etl.vcxproj.filters index 37e13f60..c1c35c1f 100644 --- a/test/vs2017/etl.vcxproj.filters +++ b/test/vs2017/etl.vcxproj.filters @@ -732,6 +732,9 @@ ETL\Containers + + ETL\Utilities + From fe9a88138836389682f9b410779bdd813b5273c8 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sat, 6 Apr 2019 12:17:15 +0100 Subject: [PATCH 04/41] Merge branch 'development' into feature/to_string # Conflicts: # test/vs2017/etl.vcxproj.filters --- conanfile.py | 27 + include/etl/basic_string.h | 104 +- include/etl/cstring.h | 28 +- include/etl/observer.h | 35 +- include/etl/string_view.h | 10 + include/etl/u16string.h | 30 +- include/etl/u32string.h | 30 +- include/etl/vector.h | 366 +++- include/etl/version.h | 4 +- include/etl/visitor.h | 72 +- include/etl/wstring.h | 30 +- support/Release notes.txt | 28 + test/codeblocks/ETL.cbp | 2 + test/test_string_char.cpp | 676 ++++++- test/test_string_u16.cpp | 778 ++++++-- test/test_string_u32.cpp | 780 ++++++-- test/test_string_view.cpp | 12 + test/test_string_wchar_t.cpp | 783 ++++++-- test/test_vector.cpp | 23 + test/test_vector_external_buffer.cpp | 1128 +++++++++++ test/test_vector_pointer_external_buffer.cpp | 1756 ++++++++++++++++++ test/vs2017/etl.vcxproj | 2 + test/vs2017/etl.vcxproj.filters | 6 + 23 files changed, 6204 insertions(+), 506 deletions(-) create mode 100755 conanfile.py create mode 100644 test/test_vector_external_buffer.cpp create mode 100644 test/test_vector_pointer_external_buffer.cpp diff --git a/conanfile.py b/conanfile.py new file mode 100755 index 00000000..ba16304f --- /dev/null +++ b/conanfile.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from conans import ConanFile + + +class EmbeddedTemplateLibraryConan(ConanFile): + name = "embedded-template-library" + version = "14.11.2" + license = "MIT" + author = "John Wellbelove " + url = "https://github.com/ETLCPP/etl" + description = "A C++ template library for embedded applications" + topics = ("embedded", "template", "container") + no_copy_source = True + scm = { + "type": "git", + "url": "auto", + "revision": "auto" + } + + def package(self): + self.copy("LICENSE", "licenses") + self.copy("*.h", src="include", dst="include") + + def package_id(self): + self.info.header_only() diff --git a/include/etl/basic_string.h b/include/etl/basic_string.h index 54d2d545..f5c13b5d 100644 --- a/include/etl/basic_string.h +++ b/include/etl/basic_string.h @@ -221,6 +221,14 @@ namespace etl return is_truncated; } + //************************************************************************* + /// Clears the 'truncated' flag. + //************************************************************************* + void clear_truncated() + { + is_truncated = false; + } + protected: //************************************************************************* @@ -393,6 +401,11 @@ namespace etl //********************************************************************* void resize(size_t new_size, T value) { + if (new_size > CAPACITY) + { + is_truncated = true; + } + new_size = std::min(new_size, CAPACITY); // Size up? @@ -510,8 +523,12 @@ namespace etl //********************************************************************* void assign(const etl::ibasic_string& other) { - size_t len = std::min(CAPACITY, other.size()); - assign(other.begin(), other.begin() + len); + assign(other.begin(), other.end()); + + if (other.truncated()) + { + is_truncated = true; + } } //********************************************************************* @@ -560,6 +577,8 @@ namespace etl //********************************************************************* void assign(const_pointer other, size_t length_) { + is_truncated = (length_ > CAPACITY); + length_ = std::min(length_, CAPACITY); initialise(); @@ -593,6 +612,8 @@ namespace etl } p_buffer[current_size] = 0; + + is_truncated = (first != last); } //********************************************************************* @@ -605,6 +626,8 @@ namespace etl { initialise(); + is_truncated = (n > CAPACITY); + n = std::min(n, CAPACITY); std::fill_n(begin(), n, value); @@ -630,7 +653,7 @@ namespace etl if (current_size != CAPACITY) { p_buffer[current_size++] = value; - is_truncated = false; + p_buffer[current_size] = 0; } else { @@ -657,6 +680,12 @@ namespace etl ibasic_string& append(const ibasic_string& str) { insert(end(), str.begin(), str.end()); + + if (str.truncated()) + { + is_truncated = true; + } + return *this; } @@ -671,6 +700,7 @@ namespace etl ETL_ASSERT(subposition <= str.size(), ETL_ERROR(string_out_of_bounds)); insert(size(), str, subposition, sublength); + return *this; } @@ -725,8 +755,6 @@ namespace etl //********************************************************************* iterator insert(const_iterator position, T value) { - is_truncated = false; - // Quick hack, as iterators are pointers. iterator insert_position = const_cast(position); @@ -773,8 +801,6 @@ namespace etl //********************************************************************* void insert(const_iterator position, size_t n, T value) { - is_truncated = false; - if (n == 0) { return; @@ -785,15 +811,20 @@ namespace etl const size_t start = std::distance(cbegin(), position); // No effect. - if (start == CAPACITY) + if (start >= CAPACITY) { + is_truncated = true; return; } // Fills the string to the end? if ((start + n) >= CAPACITY) { - is_truncated = ((current_size + n) > CAPACITY); + if ((current_size + n) > CAPACITY) + { + is_truncated = true; + } + current_size = CAPACITY; std::fill(insert_position, end(), value); } @@ -834,8 +865,6 @@ namespace etl template void insert(iterator position, TIterator first, TIterator last) { - is_truncated = false; - if (first == last) { return; @@ -845,15 +874,20 @@ namespace etl const size_t n = std::distance(first, last); // No effect. - if (start == CAPACITY) + if (start >= CAPACITY) { + is_truncated = true; return; } // Fills the string to the end? if ((start + n) >= CAPACITY) { - is_truncated = ((current_size + n) > CAPACITY); + if (((current_size + n) > CAPACITY)) + { + is_truncated = true; + } + current_size = CAPACITY; while (position != end()) @@ -902,6 +936,12 @@ namespace etl ETL_ASSERT(position <= size(), ETL_ERROR(string_out_of_bounds)); insert(begin() + position, str.cbegin(), str.cend()); + + if (str.truncated()) + { + is_truncated = true; + } + return *this; } @@ -923,6 +963,12 @@ namespace etl } insert(begin() + position, str.cbegin() + subposition, str.cbegin() + subposition + sublength); + + if (str.truncated()) + { + is_truncated = true; + } + return *this; } @@ -1031,6 +1077,11 @@ namespace etl //********************************************************************* size_t copy(pointer s, size_t len, size_t pos = 0) { + if ((pos + len > size())) + { + is_truncated = true; + } + size_t endpos = std::min(pos + len, size()); for (size_t i = pos; i < endpos; ++i) @@ -1304,6 +1355,11 @@ namespace etl // Insert the new stuff. insert(first_, str.begin(), str.end()); + if (str.truncated()) + { + is_truncated = true; + } + return *this; } @@ -1855,21 +1911,28 @@ namespace etl { if (&rhs != this) { - assign(rhs.cbegin(), rhs.cend()); + assign(rhs); } return *this; } + //************************************************************************* + /// Assignment operator. + //************************************************************************* + ibasic_string& operator = (const_pointer rhs) + { + assign(rhs); + + return *this; + } + //************************************************************************* /// += operator. //************************************************************************* ibasic_string& operator += (const ibasic_string& rhs) { - if (&rhs != this) - { - append(rhs); - } + append(rhs); return *this; } @@ -1877,7 +1940,7 @@ namespace etl //************************************************************************* /// += operator. //************************************************************************* - ibasic_string& operator += (const T* rhs) + ibasic_string& operator += (const_pointer rhs) { append(rhs); @@ -1918,7 +1981,8 @@ namespace etl void initialise() { current_size = 0; - p_buffer[0] = 0; + p_buffer[0] = 0; + is_truncated = false; } //************************************************************************* diff --git a/include/etl/cstring.h b/include/etl/cstring.h index 3dea5592..8fdd1aaa 100644 --- a/include/etl/cstring.h +++ b/include/etl/cstring.h @@ -75,7 +75,7 @@ namespace etl string(const etl::string& other) : istring(reinterpret_cast(&buffer), MAX_SIZE) { - this->assign(other.begin(), other.end()); + this->assign(other); } //************************************************************************* @@ -85,7 +85,7 @@ namespace etl string(const etl::istring& other) : istring(reinterpret_cast(&buffer), MAX_SIZE) { - this->assign(other.begin(), other.end()); + this->assign(other); } //************************************************************************* @@ -94,15 +94,17 @@ namespace etl ///\param position The position of the first character. ///\param length The number of characters. Default = npos. //************************************************************************* - string(const etl::string& other, size_t position, size_t length_ = npos) + string(const etl::istring& other, size_t position, size_t length_ = npos) : istring(reinterpret_cast(&buffer), MAX_SIZE) { ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds)); - // Set the length to the exact amount. - length_ = (length_ > MAX_SIZE_) ? MAX_SIZE_ : length_; - this->assign(other.begin() + position, other.begin() + position + length_); + + if (other.truncated()) + { + this->is_truncated = true; + } } //************************************************************************* @@ -186,16 +188,26 @@ namespace etl //************************************************************************* /// Assignment operator. //************************************************************************* - string& operator = (const string& rhs) + string& operator = (const istring& rhs) { if (&rhs != this) { - this->assign(rhs.cbegin(), rhs.cend()); + this->assign(rhs); } return *this; } + //************************************************************************* + /// Assignment operator. + //************************************************************************* + string& operator = (const value_type* text) + { + this->assign(text); + + return *this; + } + //************************************************************************* /// Fix the internal pointers after a low level memory copy. //************************************************************************* diff --git a/include/etl/observer.h b/include/etl/observer.h index 06d07dfc..dcc1edb3 100644 --- a/include/etl/observer.h +++ b/include/etl/observer.h @@ -186,17 +186,46 @@ namespace etl } protected: - + ~observable() { } - + private: /// The list of observers. Observer_List observer_list; }; +#if ETL_CPP11_SUPPORTED && !defined(ETL_OBSERVER_FORCE_CPP03) + + //***************************************************************** + /// The observer class for N types. + ///\ingroup observer + //***************************************************************** + template + class observer : public observer, public observer + { + public: + + using observer::notification; + using observer::notification; + }; + + //***************************************************************** + /// The specialised observer class for 1 type. + ///\ingroup observer + //***************************************************************** + template + class observer + { + public: + + virtual void notification(T1) = 0; + }; + +#else + //********************************************************************* /// The observer interface for eight notification types. ///\ingroup observer @@ -355,6 +384,8 @@ namespace etl virtual ~observer() {} virtual void notification(T1) = 0; }; + +#endif } #undef ETL_FILE diff --git a/include/etl/string_view.h b/include/etl/string_view.h index 319ca801..b8ada876 100644 --- a/include/etl/string_view.h +++ b/include/etl/string_view.h @@ -39,6 +39,7 @@ SOFTWARE. #include "char_traits.h" #include "integral_limits.h" #include "hash.h" +#include "basic_string.h" #include "algorithm.h" @@ -124,6 +125,15 @@ namespace etl { } + //************************************************************************* + /// Construct from string. + //************************************************************************* + ETL_CONSTEXPR basic_string_view(const etl::ibasic_string& str) + : mbegin(str.begin()), + mend(str.end()) + { + } + //************************************************************************* /// Construct from T*. //************************************************************************* diff --git a/include/etl/u16string.h b/include/etl/u16string.h index ef864d63..f0d7a08f 100644 --- a/include/etl/u16string.h +++ b/include/etl/u16string.h @@ -75,8 +75,7 @@ namespace etl u16string(const etl::u16string& other) : iu16string(reinterpret_cast(&buffer), MAX_SIZE) { - this->initialise(); - this->assign(other.begin(), other.end()); + this->assign(other); } //************************************************************************* @@ -86,7 +85,7 @@ namespace etl u16string(const etl::iu16string& other) : iu16string(reinterpret_cast(&buffer), MAX_SIZE) { - this->assign(other.begin(), other.end()); + this->assign(other); } //************************************************************************* @@ -95,16 +94,17 @@ namespace etl ///\param position The position of the first character. ///\param length The number of characters. Default = npos. //************************************************************************* - u16string(const etl::u16string& other, size_t position, size_t length_ = npos) + u16string(const etl::iu16string& other, size_t position, size_t length_ = npos) : iu16string(reinterpret_cast(&buffer), MAX_SIZE) { ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds)); - // Set the length to the exact amount. - length_ = (length_ > MAX_SIZE_) ? MAX_SIZE_ : length_; - - this->initialise(); this->assign(other.begin() + position, other.begin() + position + length_); + + if (other.truncated()) + { + this->is_truncated = true; + } } //************************************************************************* @@ -114,7 +114,6 @@ namespace etl u16string(const value_type* text) : iu16string(reinterpret_cast(&buffer), MAX_SIZE) { - this->initialise(); this->assign(text, text + etl::char_traits::length(text)); } @@ -126,7 +125,6 @@ namespace etl u16string(const value_type* text, size_t count) : iu16string(reinterpret_cast(&buffer), MAX_SIZE) { - this->initialise(); this->assign(text, text + count); } @@ -194,12 +192,22 @@ namespace etl { if (&rhs != this) { - this->assign(rhs.cbegin(), rhs.cend()); + this->assign(rhs); } return *this; } + //************************************************************************* + /// Assignment operator. + //************************************************************************* + u16string& operator = (const value_type* text) + { + this->assign(text); + + return *this; + } + //************************************************************************* /// Fix the internal pointers after a low level memory copy. //************************************************************************* diff --git a/include/etl/u32string.h b/include/etl/u32string.h index 7684f9aa..27792fcb 100644 --- a/include/etl/u32string.h +++ b/include/etl/u32string.h @@ -75,8 +75,7 @@ namespace etl u32string(const etl::u32string& other) : iu32string(reinterpret_cast(&buffer), MAX_SIZE) { - this->initialise(); - this->assign(other.begin(), other.end()); + this->assign(other); } //************************************************************************* @@ -86,7 +85,7 @@ namespace etl u32string(const etl::iu32string& other) : iu32string(reinterpret_cast(&buffer), MAX_SIZE) { - this->assign(other.begin(), other.end()); + this->assign(other); } //************************************************************************* @@ -95,16 +94,17 @@ namespace etl ///\param position The position of the first character. ///\param length The number of characters. Default = npos. //************************************************************************* - u32string(const etl::u32string& other, size_t position, size_t length_ = npos) + u32string(const etl::iu32string& other, size_t position, size_t length_ = npos) : iu32string(reinterpret_cast(&buffer), MAX_SIZE) { ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds)); - // Set the length to the exact amount. - length_ = (length_ > MAX_SIZE_) ? MAX_SIZE_ : length_; - - this->initialise(); this->assign(other.begin() + position, other.begin() + position + length_); + + if (other.truncated()) + { + this->is_truncated = true; + } } //************************************************************************* @@ -114,7 +114,6 @@ namespace etl u32string(const value_type* text) : iu32string(reinterpret_cast(&buffer), MAX_SIZE) { - this->initialise(); this->assign(text, text + etl::char_traits::length(text)); } @@ -126,7 +125,6 @@ namespace etl u32string(const value_type* text, size_t count) : iu32string(reinterpret_cast(&buffer), MAX_SIZE) { - this->initialise(); this->assign(text, text + count); } @@ -194,12 +192,22 @@ namespace etl { if (&rhs != this) { - this->assign(rhs.cbegin(), rhs.cend()); + this->assign(rhs); } return *this; } + //************************************************************************* + /// Assignment operator. + //************************************************************************* + u32string& operator = (const value_type* text) + { + this->assign(text); + + return *this; + } + //************************************************************************* /// Fix the internal pointers after a low level memory copy. //************************************************************************* diff --git a/include/etl/vector.h b/include/etl/vector.h index 577241ce..5c3ecc63 100644 --- a/include/etl/vector.h +++ b/include/etl/vector.h @@ -952,7 +952,25 @@ namespace etl etl::destroy(p_buffer, p_end); ETL_SUBTRACT_DEBUG_COUNT(int32_t(std::distance(p_buffer, p_end))) - p_end = p_buffer; + p_end = p_buffer; + } + + //********************************************************************* + /// Initialise the source vector after a move. + //********************************************************************* + void initialise_source_external_buffer_after_move() + { + ETL_SUBTRACT_DEBUG_COUNT(int32_t(std::distance(p_buffer, p_end))) + + p_end = p_buffer; + } + + //********************************************************************* + /// Initialise the destination vector after a move. + //********************************************************************* + void initialise_destination_external_buffer_after_move() + { + ETL_ADD_DEBUG_COUNT(int32_t(std::distance(p_buffer, p_end))) } //************************************************************************* @@ -962,14 +980,14 @@ namespace etl { uintptr_t length = p_end - p_buffer; p_buffer = p_buffer_; - p_end = p_buffer_ + length; + p_end = p_buffer_ + length; } - private: - pointer p_buffer; ///< Pointer to the start of the buffer. pointer p_end; ///< Pointer to one past the last element in the buffer. + private: + //********************************************************************* /// Create a new element with a default value at the back. //********************************************************************* @@ -1281,6 +1299,157 @@ namespace etl typename etl::aligned_storage::value>::type buffer; }; + //*************************************************************************** + /// A vector implementation that uses a fixed size buffer. + /// The buffer is supplied on construction. + ///\tparam T The element type. + ///\ingroup vector + //*************************************************************************** + template + class vector : public etl::ivector + { + public: + + //************************************************************************* + /// Constructor. + //************************************************************************* + vector(void* buffer, size_t max_size) + : etl::ivector(reinterpret_cast(buffer), max_size) + { + this->initialise(); + } + + //************************************************************************* + /// Constructor, with size. + ///\param initial_size The initial size of the vector. + //************************************************************************* + explicit vector(size_t initial_size, void* buffer, size_t max_size) + : etl::ivector(reinterpret_cast(buffer), max_size) + { + this->initialise(); + this->resize(initial_size); + } + + //************************************************************************* + /// Constructor, from initial size and value. + ///\param initial_size The initial size of the vector. + ///\param value The value to fill the vector with. + //************************************************************************* + vector(size_t initial_size, typename etl::ivector::parameter_t value, void* buffer, size_t max_size) + : etl::ivector(reinterpret_cast(buffer), max_size) + { + this->initialise(); + this->resize(initial_size, value); + } + + //************************************************************************* + /// Constructor, from an iterator range. + ///\tparam TIterator The iterator type. + ///\param first The iterator to the first element. + ///\param last The iterator to the last element + 1. + //************************************************************************* + template + vector(TIterator first, TIterator last, void* buffer, size_t max_size) + : etl::ivector(reinterpret_cast(buffer), max_size) + { + this->assign(first, last); + } + +#if ETL_CPP11_SUPPORTED && !defined(ETL_STLPORT) && !defined(ETL_NO_STL) + //************************************************************************* + /// Constructor, from an initializer_list. + //************************************************************************* + vector(std::initializer_list init, void* buffer, size_t max_size) + : etl::ivector(reinterpret_cast(buffer), max_size) + { + this->assign(init.begin(), init.end()); + } +#endif + + //************************************************************************* + /// Copy constructor. + //************************************************************************* + vector(const vector& other, void* buffer, size_t max_size) + : etl::ivector(reinterpret_cast(buffer), max_size) + { + this->assign(other.begin(), other.end()); + } + + //************************************************************************* + /// Assignment operator. + //************************************************************************* + vector& operator = (const vector& rhs) + { + if (&rhs != this) + { + this->assign(rhs.cbegin(), rhs.cend()); + } + + return *this; + } + +#if ETL_CPP11_SUPPORTED + //************************************************************************* + /// Move constructor. + //************************************************************************* + vector(vector&& other, void* buffer, size_t max_size) + : etl::ivector(reinterpret_cast(buffer), max_size) + { + if (this != &other) + { + this->initialise(); + + this->p_buffer = other.p_buffer; + this->p_end = other.p_end; + + this->initialise_destination_external_buffer_after_move(); + other.initialise_source_external_buffer_after_move(); + } + } + + //************************************************************************* + /// Move assignment operator. + //************************************************************************* + vector& operator = (vector&& rhs) + { + if (&rhs != this) + { + this->clear(); + this->p_buffer = rhs.p_buffer; + this->p_end = rhs.p_end; + + this->initialise_destination_external_buffer_after_move(); + rhs.initialise_source_external_buffer_after_move(); + } + + return *this; + } +#endif + + //************************************************************************* + /// Destructor. + //************************************************************************* + ~vector() + { + this->clear(); + } + + //************************************************************************* + /// Fix the internal pointers after a low level memory copy. + //************************************************************************* +#ifdef ETL_IVECTOR_REPAIR_ENABLE + virtual +#endif + void repair() + { +#if ETL_CPP11_TYPE_TRAITS_IS_TRIVIAL_SUPPORTED + ETL_ASSERT(std::is_trivially_copyable::value, ETL_ERROR(etl::vector_incompatible_type)); +#endif + + etl::ivector::repair_buffer(this->p_buffer); + } + }; + //*************************************************************************** /// A vector implementation that uses a fixed size buffer. ///\tparam T The element type. @@ -1375,6 +1544,50 @@ namespace etl return *this; } +#if ETL_CPP11_SUPPORTED + //************************************************************************* + /// Move constructor. + //************************************************************************* + vector(vector&& other) + : etl::ivector(reinterpret_cast(&buffer), MAX_SIZE) + { + if (this != &other) + { + this->initialise(); + + typename etl::ivector::iterator itr = other.begin(); + while (itr != other.end()) + { + this->push_back(std::move(*itr)); + ++itr; + } + + other.initialise_source_external_buffer_after_move(); + } + } + + //************************************************************************* + /// Move assignment operator. + //************************************************************************* + vector& operator = (vector&& rhs) + { + if (&rhs != this) + { + this->clear(); + typename etl::ivector::iterator itr = rhs.begin(); + while (itr != rhs.end()) + { + this->push_back(std::move(*itr)); + ++itr; + } + + rhs.initialise_source_external_buffer_after_move(); + } + + return *this; + } +#endif + //************************************************************************* /// Fix the internal pointers after a low level memory copy. //************************************************************************* @@ -1387,6 +1600,151 @@ namespace etl typename etl::aligned_storage::value>::type buffer; }; + + //*************************************************************************** + /// A vector implementation that uses a fixed size buffer. + /// The buffer is supplied on construction. + ///\tparam T The element type that is pointed to. + ///\ingroup vector + //*************************************************************************** + template + class vector : public etl::ivector + { + public: + + //************************************************************************* + /// Constructor. + //************************************************************************* + vector(void* buffer, size_t max_size) + : etl::ivector(reinterpret_cast(buffer), max_size) + { + this->initialise(); + } + + //************************************************************************* + /// Constructor, with size. + ///\param initial_size The initial size of the vector. + //************************************************************************* + explicit vector(size_t initial_size, void* buffer, size_t max_size) + : etl::ivector(reinterpret_cast(buffer), max_size) + { + this->initialise(); + this->resize(initial_size); + } + + //************************************************************************* + /// Constructor, from initial size and value. + ///\param initial_size The initial size of the vector. + ///\param value The value to fill the vector with. + //************************************************************************* + vector(size_t initial_size, typename etl::ivector::parameter_t value, void* buffer, size_t max_size) + : etl::ivector(reinterpret_cast(buffer), max_size) + { + this->initialise(); + this->resize(initial_size, value); + } + + //************************************************************************* + /// Constructor, from an iterator range. + ///\tparam TIterator The iterator type. + ///\param first The iterator to the first element. + ///\param last The iterator to the last element + 1. + //************************************************************************* + template + vector(TIterator first, TIterator last, void* buffer, size_t max_size) + : etl::ivector(reinterpret_cast(buffer), max_size) + { + this->assign(first, last); + } + +#if ETL_CPP11_SUPPORTED && !defined(ETL_STLPORT) && !defined(ETL_NO_STL) + //************************************************************************* + /// Constructor, from an initializer_list. + //************************************************************************* + vector(std::initializer_list init, void* buffer, size_t max_size) + : etl::ivector(reinterpret_cast(buffer), max_size) + { + this->assign(init.begin(), init.end()); + } +#endif + + //************************************************************************* + /// Copy constructor. + //************************************************************************* + vector(const vector& other, void* buffer, size_t max_size) + : etl::ivector(reinterpret_cast(buffer), max_size) + { + this->assign(other.begin(), other.end()); + } + + //************************************************************************* + /// Assignment operator. + //************************************************************************* + vector& operator = (const vector& rhs) + { + if (&rhs != this) + { + this->assign(rhs.cbegin(), rhs.cend()); + } + + return *this; + } + +#if ETL_CPP11_SUPPORTED + //************************************************************************* + /// Move constructor. + //************************************************************************* + vector(vector&& other, void* buffer, size_t max_size) + : etl::ivector(reinterpret_cast(buffer), max_size) + { + if (this != &other) + { + this->p_buffer = other.p_buffer; + this->p_end = other.p_end; + + this->initialise_destination_external_buffer_after_move(); + other.initialise_source_external_buffer_after_move(); + } + } + + //************************************************************************* + /// Move assignment operator. + //************************************************************************* + vector& operator = (vector&& rhs) + { + if (&rhs != this) + { + this->clear(); + this->p_buffer = rhs.p_buffer; + this->p_end = rhs.p_end; + + this->initialise_destination_external_buffer_after_move(); + rhs.initialise_source_external_buffer_after_move(); + } + + return *this; + } +#endif + + //************************************************************************* + /// Destructor. + //************************************************************************* + ~vector() + { + this->clear(); + } + + //************************************************************************* + /// Fix the internal pointers after a low level memory copy. + //************************************************************************* +#ifdef ETL_IVECTOR_REPAIR_ENABLE + virtual +#endif + void repair() + { + etl::ivector::repair_buffer(this->p_buffer); + } + }; } #ifdef ETL_COMPILER_GCC diff --git a/include/etl/version.h b/include/etl/version.h index 2f7aebb4..85d56f97 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -38,8 +38,8 @@ SOFTWARE. ///\ingroup utilities #define ETL_VERSION_MAJOR 14 -#define ETL_VERSION_MINOR 13 -#define ETL_VERSION_PATCH 1 +#define ETL_VERSION_MINOR 17 +#define ETL_VERSION_PATCH 0 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) ETL_STRINGIFY(ETL_VERSION_MINOR) ETL_STRINGIFY(ETL_VERSION_PATCH) #define ETL_VERSION_W ETL_WIDE_STRING(ETL_CONCAT(ETL_CONCAT(ETL_VERSION_MAJOR, ETL_VERSION_MINOR), ETL_VERSION_PATCH)) diff --git a/include/etl/visitor.h b/include/etl/visitor.h index 70e58252..3273eb68 100644 --- a/include/etl/visitor.h +++ b/include/etl/visitor.h @@ -40,16 +40,45 @@ SOFTWARE. /// structure on which it operates. A practical result of this separation is the /// ability to add new operations to existing object structures without modifying /// those structures. It is one way to easily follow the open/closed principle. -/// In essence, the visitor allows one to add new virtual functions to a family -/// of classes without modifying the classes themselves; instead, one creates a +/// In essence, the visitor allows one to add new virtual functions to a family +/// of classes without modifying the classes themselves; instead, one creates a /// visitor class that implements all of the appropriate specialisations of the -/// virtual function. The visitor takes the instance as input, and implements +/// virtual function. The visitor takes the instance as input, and implements /// the goal through double dispatch.
/// \ingroup patterns //***************************************************************************** namespace etl { +#if ETL_CPP11_SUPPORTED && !defined(ETL_VISITOR_FORCE_CPP03) + + //***************************************************************** + /// The visitable class for N types. + ///\ingroup visitor + //***************************************************************** + template + class visitable : public visitable, public visitable + { + public: + + using visitable::accept; + using visitable::accept; + }; + + //***************************************************************** + /// The specialised visitable class for 1 type. + ///\ingroup visitor + //***************************************************************** + template + class visitable + { + public: + + virtual void accept(T1&) = 0; + }; + +#else + //***************************************************************** /// The visitable base class for four visitor types. /// Derive visitable classes from this. @@ -108,6 +137,37 @@ namespace etl virtual void accept(T1&) = 0; }; +#endif + +#if ETL_CPP11_SUPPORTED && !defined(ETL_VISITOR_FORCE_CPP03) + + //***************************************************************** + /// The visitor class for N types. + ///\ingroup visitor + //***************************************************************** + template + class visitor : public visitor, public visitor + { + public: + + using visitor::visit; + using visitor::visit; + }; + + //***************************************************************** + /// The specialised visitor class for 1 type. + ///\ingroup visitor + //***************************************************************** + template + class visitor + { + public: + + virtual void visit(T1&) = 0; + }; + +#else + //***************************************************************** /// The visitor base class for sixteen types. /// Derive visitors from this. @@ -115,7 +175,7 @@ namespace etl //***************************************************************** template class visitor { @@ -323,7 +383,7 @@ namespace etl virtual void visit(T8&) = 0; virtual void visit(T9&) = 0; }; - + //***************************************************************** /// The visitor base class for eight types. /// Derive visitors from this. @@ -459,6 +519,8 @@ namespace etl virtual void visit(T1&) = 0; }; + +#endif } #endif diff --git a/include/etl/wstring.h b/include/etl/wstring.h index 8272953c..388525c7 100644 --- a/include/etl/wstring.h +++ b/include/etl/wstring.h @@ -75,8 +75,7 @@ namespace etl wstring(const etl::wstring& other) : iwstring(reinterpret_cast(&buffer), MAX_SIZE) { - wstring::initialise(); - wstring::assign(other.begin(), other.end()); + this->assign(other); } //************************************************************************* @@ -86,7 +85,7 @@ namespace etl wstring(const etl::iwstring& other) : iwstring(reinterpret_cast(&buffer), MAX_SIZE) { - this->assign(other.begin(), other.end()); + this->assign(other); } @@ -96,16 +95,17 @@ namespace etl ///\param position The position of the first character. ///\param length The number of characters. Default = npos. //************************************************************************* - wstring(const etl::wstring& other, size_t position, size_t length_ = npos) + wstring(const etl::iwstring& other, size_t position, size_t length_ = npos) : iwstring(reinterpret_cast(&buffer), MAX_SIZE) { ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds)); - // Set the length to the exact amount. - length_ = (length_ > MAX_SIZE_) ? MAX_SIZE_ : length_; - - this->initialise(); this->assign(other.begin() + position, other.begin() + position + length_); + + if (other.truncated()) + { + this->is_truncated = true; + } } //************************************************************************* @@ -115,7 +115,6 @@ namespace etl wstring(const value_type* text) : iwstring(reinterpret_cast(&buffer), MAX_SIZE) { - this->initialise(); this->assign(text, text + etl::char_traits::length(text)); } @@ -127,7 +126,6 @@ namespace etl wstring(const value_type* text, size_t count) : iwstring(reinterpret_cast(&buffer), MAX_SIZE) { - this->initialise(); this->assign(text, text + count); } @@ -195,12 +193,22 @@ namespace etl { if (&rhs != this) { - this->assign(rhs.cbegin(), rhs.cend()); + this->assign(rhs); } return *this; } + //************************************************************************* + /// Assignment operator. + //************************************************************************* + wstring& operator = (const value_type* text) + { + this->assign(text); + + return *this; + } + //************************************************************************* /// Fix the internal pointers after a low level memory copy. //************************************************************************* diff --git a/support/Release notes.txt b/support/Release notes.txt index 77f8c9fe..1ef4d2dc 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,31 @@ +=============================================================================== +14.17.0 +Added C++11 variadic templates for etl::observer. + +=============================================================================== +14.16.0 +Added C++11 variadic templates for etl::visitable and etl::visitor. + +=============================================================================== +14.15.0 +Added external buffer support for vector. + +=============================================================================== +14.14.2 +Modified 'truncated' to only be cleared on 'clear()' or 'assign()'. +Added assignment from zero terminated string pointer. + +=============================================================================== +14.14.1 +Fixed bug where 'truncated' was not always set correctly for strings. + +=============================================================================== +14.14.0 +Fixed string push_back() bug where the internal terminator was not updated. +Basic Conan package added. +Added const ref istring constructors. +Updated profiles for C++17. + =============================================================================== 14.13.1 Modified etl::queue_spsc_locked parameters to 'const'. diff --git a/test/codeblocks/ETL.cbp b/test/codeblocks/ETL.cbp index 17829a76..daf6c462 100644 --- a/test/codeblocks/ETL.cbp +++ b/test/codeblocks/ETL.cbp @@ -441,8 +441,10 @@ + + diff --git a/test/test_string_char.cpp b/test/test_string_char.cpp index 91a5ef05..b40a4ae4 100644 --- a/test/test_string_char.cpp +++ b/test/test_string_char.cpp @@ -3,7 +3,7 @@ The MIT License(MIT) Embedded Template Library. https://github.com/ETLCPP/etl -http://www.etlcpp.com +https://www.etlcpp.com Copyright(c) 2016 jwellbelove @@ -56,6 +56,7 @@ namespace typedef std::string Compare_Text; typedef Text::value_type value_t; typedef etl::string<52> TextL; + typedef etl::string<4> TextS; Compare_Text initial_text; Compare_Text less_text; @@ -102,6 +103,7 @@ namespace CHECK_EQUAL(text.capacity(), SIZE); CHECK_EQUAL(text.max_size(), SIZE); CHECK(text.begin() == text.end()); + CHECK(!text.truncated()); } //************************************************************************* @@ -113,6 +115,7 @@ namespace CHECK(text.cbegin() == text.cend()); CHECK(text.rbegin() == text.rend()); CHECK(text.crbegin() == text.crend()); + CHECK(!text.truncated()); } //************************************************************************* @@ -130,6 +133,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -138,6 +142,7 @@ namespace Text text(SIZE + 1, STR('A')); CHECK_EQUAL(SIZE, text.size()); + CHECK(text.truncated()); } //************************************************************************* @@ -151,6 +156,21 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_constructor_char_pointer_excess) + { + Compare_Text compare_text(initial_text.c_str()); + + Text text(longer_text.c_str()); + + CHECK(!text.empty()); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -164,6 +184,21 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_constructor_char_pointer_size_excess) + { + Compare_Text compare_text(SIZE, STR('A')); + + Text text(SIZE + 1, STR('A')); + + CHECK(!text.empty()); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -177,6 +212,21 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_constructor_size_char_excess) + { + Compare_Text compare_text(initial_text.c_str(), initial_text.size()); + + Text text(longer_text.c_str(), longer_text.size()); + + CHECK(!text.empty()); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -188,19 +238,19 @@ namespace CHECK(text.size() == SIZE); CHECK(!text.empty()); + CHECK(!text.truncated()); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_constructor_range_excess) { - Text text; - - text.assign(longer_text.begin(), longer_text.end()); + Text text(longer_text.begin(), longer_text.end()); bool is_equal = Equal(initial_text, text); CHECK(is_equal); CHECK(text.size() == SIZE); CHECK(!text.empty()); + CHECK(text.truncated()); } //************************************************************************* @@ -209,6 +259,35 @@ namespace Text text(initial_text.c_str()); Text text2(text); CHECK(text2 == text); + CHECK(!text2.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_copy_constructor_i) + { + Text text(initial_text.c_str()); + IText& itext = text; + Text text2(itext); + CHECK(text2 == text); + CHECK(!text2.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_copy_constructor_excess) + { + Text text(initial_text.c_str()); + TextL textl(longer_text.c_str()); + Text text2(textl); + CHECK(text2 == text); + CHECK(text2.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_copy_constructor_from_truncated) + { + Text text(longer_text.c_str()); + Text text2(text); + CHECK(text2.truncated()); } //************************************************************************* @@ -222,6 +301,21 @@ namespace bool is_equal = Equal(compare_text2, text2); CHECK(is_equal); + CHECK(!text2.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_construct_position_length_excess) + { + Compare_Text compare_text(longer_text.c_str()); + Compare_Text compare_text2(compare_text, 2, 11); + + TextL textl(longer_text.c_str()); + Text text2(textl, 2, 12); + + bool is_equal = Equal(compare_text2, text2); + CHECK(is_equal); + CHECK(text2.truncated()); } //************************************************************************* @@ -232,6 +326,21 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_construct_initializer_list_excess) + { + Compare_Text compare_text = { STR('H'), STR('e'), STR('l'), STR('l'), STR('o'), STR(' '), + STR('W'), STR('o'), STR('r'), STR('l'), STR('d') }; + Text text = { STR('H'), STR('e'), STR('l'), STR('l'), STR('o'), STR(' '), + STR('W'), STR('o'), STR('r'), STR('l'), STR('d'), STR(' '), + STR('T'), STR('h'), STR('e'), STR('r'), STR('e') }; + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -244,6 +353,22 @@ namespace bool is_equal = Equal(text, other_text); CHECK(is_equal); + CHECK(!text.truncated()); + CHECK(!other_text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_excess) + { + Text text(longer_text.begin(), longer_text.end()); + Text other_text; + + other_text = text; + + bool is_equal = Equal(text, other_text); + CHECK(is_equal); + CHECK(text.truncated()); + CHECK(other_text.truncated()); } //************************************************************************* @@ -260,8 +385,27 @@ namespace bool is_equal = Equal(text1, text2); CHECK(is_equal); + CHECK(!text1.truncated()); + CHECK(!text2.truncated()); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_iterface_excess) + { + Text text1(longer_text.begin(), longer_text.end()); + Text text2; + + IText& itext1 = text1; + IText& itext2 = text2; + + itext2 = itext1; + + bool is_equal = Equal(text1, text2); + + CHECK(is_equal); + CHECK(text1.truncated()); + CHECK(text2.truncated()); + } //************************************************************************* TEST_FIXTURE(SetupFixture, test_self_assignment) @@ -274,6 +418,73 @@ namespace bool is_equal = Equal(text, other_text); CHECK(is_equal); + CHECK(!text.truncated()); + CHECK(!other_text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_self_assignment_excess) + { + Text text(longer_text.begin(), longer_text.end()); + Text other_text(text); + + other_text = other_text; + + bool is_equal = Equal(text, other_text); + + CHECK(is_equal); + CHECK(text.truncated()); + CHECK(other_text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_from_literal) + { + Text text; + + text = STR("Hello World"); + + bool is_equal = Equal(std::string(STR("Hello World")), text); + CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_from_literal_excess) + { + Text text; + + text = STR("Hello World There"); + + bool is_equal = Equal(std::string(STR("Hello World")), text); + CHECK(is_equal); + CHECK(text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_from_literal_via_interface) + { + Text text; + IText& itext = text; + + itext = STR("Hello World"); + + bool is_equal = Equal(std::string(STR("Hello World")), itext); + CHECK(is_equal); + CHECK(!itext.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_from_literal_via_interface_excess) + { + Text text; + IText& itext = text; + + itext = STR("Hello World There"); + + bool is_equal = Equal(std::string(STR("Hello World")), itext); + CHECK(is_equal); + CHECK(itext.truncated()); } //************************************************************************* @@ -307,6 +518,7 @@ namespace text.resize(NEW_SIZE); CHECK_EQUAL(text.size(), NEW_SIZE); + CHECK(!text.truncated()); } //************************************************************************* @@ -323,8 +535,8 @@ namespace compare_text.fill(INITIAL_VALUE); bool is_equal = Equal(compare_text, text); - CHECK(is_equal); + CHECK(!text.truncated()); } @@ -337,6 +549,7 @@ namespace Text text(INITIAL_SIZE, STR('A')); text.resize(NEW_SIZE, STR('A')); CHECK_EQUAL(SIZE, text.size()); + CHECK(text.truncated()); } //************************************************************************* @@ -349,6 +562,7 @@ namespace text.resize(NEW_SIZE); CHECK_EQUAL(text.size(), NEW_SIZE); + CHECK(!text.truncated()); } //************************************************************************* @@ -368,26 +582,66 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* - TEST_FIXTURE(SetupFixture, test_empty) + TEST_FIXTURE(SetupFixture, test_empty_full) + { + Text text; + text.resize(text.max_size(), STR('A')); + + CHECK(!text.empty()); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_empty_half) + { + Text text; + text.resize(text.max_size() / 2, STR('A')); + + CHECK(!text.empty()); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_empty_empty) + { + Text text; + + CHECK(text.empty()); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_full_full) { Text text; text.resize(text.max_size(), STR('A')); CHECK(text.full()); - CHECK(!text.empty()); + CHECK(!text.truncated()); } //************************************************************************* - TEST_FIXTURE(SetupFixture, test_full) + TEST_FIXTURE(SetupFixture, test_full_half) + { + Text text; + text.resize(text.max_size() / 2, STR('A')); + + CHECK(!text.full()); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_full_empty) { Text text; CHECK(!text.full()); - CHECK(text.empty()); + CHECK(!text.truncated()); } //************************************************************************* @@ -400,30 +654,36 @@ namespace { CHECK_EQUAL(text[i], compare_text[i]); } + + CHECK(!text.truncated()); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_index_const) { - const Compare_Text compare_text(initial_text.c_str()); - const Text text(initial_text.c_str()); + const Compare_Text compare_text(initial_text.c_str()); + const Text text(initial_text.c_str()); - for (size_t i = 0; i < text.size(); ++i) - { - CHECK_EQUAL(text[i], compare_text[i]); - } + for (size_t i = 0; i < text.size(); ++i) + { + CHECK_EQUAL(text[i], compare_text[i]); + } + + CHECK(!text.truncated()); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_at) { - Compare_Text compare_text(initial_text.c_str()); - Text text(initial_text.c_str()); + Compare_Text compare_text(initial_text.c_str()); + Text text(initial_text.c_str()); - for (size_t i = 0; i < text.size(); ++i) - { - CHECK_EQUAL(text.at(i), compare_text.at(i)); - } + for (size_t i = 0; i < text.size(); ++i) + { + CHECK_EQUAL(text.at(i), compare_text.at(i)); + } + + CHECK(!text.truncated()); CHECK_THROW(text.at(text.size()), etl::string_out_of_bounds); } @@ -431,15 +691,17 @@ namespace //************************************************************************* TEST_FIXTURE(SetupFixture, test_at_const) { - const Compare_Text compare_text(initial_text.c_str()); - const Text text(initial_text.c_str()); + const Compare_Text compare_text(initial_text.c_str()); + const Text text(initial_text.c_str()); - for (size_t i = 0; i < text.size(); ++i) - { - CHECK_EQUAL(text.at(i), compare_text.at(i)); - } + for (size_t i = 0; i < text.size(); ++i) + { + CHECK_EQUAL(text.at(i), compare_text.at(i)); + } - CHECK_THROW(text.at(text.size()), etl::string_out_of_bounds); + CHECK(!text.truncated()); + + CHECK_THROW(text.at(text.size()), etl::string_out_of_bounds); } //************************************************************************* @@ -449,6 +711,7 @@ namespace Text text(initial_text.c_str()); CHECK(text.front() == compare_text.front()); + CHECK(!text.truncated()); } //************************************************************************* @@ -458,6 +721,7 @@ namespace const Text text(initial_text.c_str()); CHECK(text.front() == compare_text.front()); + CHECK(!text.truncated()); } //************************************************************************* @@ -467,6 +731,7 @@ namespace Text text(initial_text.c_str()); CHECK(text.back() == compare_text.back()); + CHECK(!text.truncated()); } //************************************************************************* @@ -476,6 +741,7 @@ namespace const Text text(initial_text.c_str()); CHECK(text.back() == compare_text.back()); + CHECK(!text.truncated()); } //************************************************************************* @@ -490,6 +756,7 @@ namespace compare_text.begin()); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -504,6 +771,41 @@ namespace compare_text.begin()); CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assign_string) + { + Compare_Text compare_input(initial_text.c_str()); + Text input(initial_text.c_str()); + + Compare_Text compare_text; + Text text; + + compare_text.assign(compare_input); + text.assign(input); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assign_string_excess) + { + Compare_Text compare_input(initial_text.c_str()); + TextL input(longer_text.c_str()); + + Compare_Text compare_text; + Text text; + + compare_text.assign(compare_input); + text.assign(input); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -512,20 +814,24 @@ namespace Compare_Text compare_text(initial_text.c_str()); Text text; - - text.assign(compare_text.c_str()); + text.assign(initial_text.c_str()); bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); + } - compare_text.assign(longer_text.c_str()); - compare_text.resize(std::min(compare_text.size(), SIZE)); + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assign_pointer_excess) + { + Compare_Text compare_text(initial_text.c_str()); - text.assign(compare_text.c_str()); - - is_equal = Equal(compare_text, text); + Text text; + text.assign(longer_text.c_str()); + bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -538,8 +844,8 @@ namespace text.assign(compare_text.begin(), compare_text.end()); bool is_equal = Equal(compare_text, text); - CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -551,11 +857,9 @@ namespace CHECK_EQUAL(initial_text.size(), text.size()); - bool is_equal = std::equal(text.begin(), - text.end(), - initial_text.begin()); - + bool is_equal = Equal(initial_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -573,8 +877,8 @@ namespace CHECK(text.size() == INITIAL_SIZE); bool is_equal = Equal(compare_text, text); - CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -591,10 +895,9 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } - - //************************************************************************* TEST_FIXTURE(SetupFixture, test_push_back) { @@ -611,17 +914,25 @@ namespace text.push_back(STR('A') + value_t(i)); } + CHECK_EQUAL(etl::strlen(compare_text.data()), etl::strlen(text.data())); CHECK_EQUAL(compare_text.size(), text.size()); bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_push_back_excess) { + Compare_Text compare_text; Text text; + for (size_t i = 0; i < SIZE; ++i) + { + compare_text.push_back(STR('A') + value_t(i)); + } + for (size_t i = 0; i < SIZE; ++i) { text.push_back(STR('A') + value_t(i)); @@ -630,8 +941,12 @@ namespace text.push_back(STR('A') + value_t(SIZE)); + CHECK_EQUAL(etl::strlen(compare_text.data()), etl::strlen(text.data())); + CHECK_EQUAL(compare_text.size(), text.size()); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); CHECK(text.truncated()); - CHECK_EQUAL(SIZE, text.size()); } //************************************************************************* @@ -796,7 +1111,7 @@ namespace text.assign(initial_text.begin(), initial_text.end()); text.insert(text.begin() + offset, INSERT_SIZE, INSERT_VALUE); - CHECK(!text.truncated()); + CHECK(text.truncated()); is_equal = Equal(compare_text, text); CHECK(is_equal); @@ -890,6 +1205,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } } @@ -908,6 +1224,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } } @@ -926,6 +1243,28 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); + } + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_insert_size_t_position_string_from_truncated) + { + for (size_t offset = 0; offset <= short_text.size(); ++offset) + { + Compare_Text compare_text(short_text.begin(), short_text.end()); + Text text(short_text.begin(), short_text.end()); + Text insert(longer_text.begin(), longer_text.end()); + insert.erase(insert.begin(), insert.end()); + insert.append(insert_text.begin(), insert_text.end()); + + text.insert(offset, insert); + compare_text.insert(offset, insert_text); + compare_text.resize(std::min(compare_text.size(), SIZE)); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } } @@ -942,6 +1281,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); compare_text.assign(short_text.begin(), short_text.end()); text.assign(short_text.begin(), short_text.end()); @@ -954,6 +1294,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); compare_text.assign(short_text.begin(), short_text.end()); text.assign(short_text.begin(), short_text.end()); @@ -964,6 +1305,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -979,6 +1321,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(short_text.c_str()); @@ -991,6 +1334,45 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_append_truncated_string) + { + Text text(short_text.c_str()); + TextS append(short_text.c_str()); + CHECK(append.truncated()); + + text.append(append); + CHECK(text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_append_string_to_self) + { + Compare_Text compare_text(short_text.c_str()); + Text text(short_text.c_str()); + + // Non-overflow. + compare_text.append(compare_text); + text.append(text); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(!text.truncated()); + + // Overflow. + compare_text.assign(shorter_text.c_str()); + text.assign(shorter_text.c_str()); + + compare_text.append(compare_text); + compare_text.resize(std::min(compare_text.size(), SIZE)); + text.append(text); + + is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1006,6 +1388,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Partial string. compare_text.assign(short_text.c_str()); @@ -1018,6 +1401,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(short_text.c_str()); @@ -1030,6 +1414,18 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_append_truncated_string_subpos_sublen) + { + Text text(short_text.c_str()); + TextS append(short_text.c_str()); + CHECK(append.truncated()); + + text.append(append, 1, 2); + CHECK(text.truncated()); } //************************************************************************* @@ -1045,6 +1441,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(short_text.c_str()); @@ -1056,6 +1453,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1071,6 +1469,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(short_text.c_str()); @@ -1082,6 +1481,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1097,6 +1497,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(short_text.c_str()); @@ -1109,6 +1510,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1124,6 +1526,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow short text. compare_text.assign(short_text.c_str()); @@ -1135,6 +1538,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1146,6 +1550,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1157,17 +1562,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(2, 2, Compare_Text(STR("Replace"))); + compare_text.replace(2, 7, Compare_Text(STR("Replace"))); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(2, 2, Text(STR("Replace"))); + text.replace(2, 7, Text(STR("Replace"))); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1179,6 +1586,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1190,6 +1598,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1205,6 +1614,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1216,17 +1626,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 4, Compare_Text(STR("Replace"))); + compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 9, Compare_Text(STR("Replace"))); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(text.begin() + 2, text.begin() + 4, Text(STR("Replace"))); + text.replace(text.begin() + 2, text.begin() + 9, Text(STR("Replace"))); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1238,6 +1650,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1253,6 +1666,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1264,6 +1678,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1275,6 +1690,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1286,17 +1702,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(2, 4, Compare_Text(STR("Replace")), 1, 5); + compare_text.replace(2, 7, Compare_Text(STR("Replace")), 1, 5); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(2, 4, Text(STR("Replace")), 1, 5); + text.replace(2, 7, Text(STR("Replace")), 1, 5); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1308,6 +1726,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1319,6 +1738,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1330,6 +1750,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1345,6 +1766,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1356,6 +1778,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1367,6 +1790,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1378,17 +1802,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(2, 4, Compare_Text(STR("Replace")).c_str()); + compare_text.replace(2, 7, Compare_Text(STR("Replace")).c_str()); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(2, 4, Text(STR("Replace")).c_str()); + text.replace(2, 7, Text(STR("Replace")).c_str()); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1400,6 +1826,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1411,6 +1838,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1422,6 +1850,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1437,6 +1866,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1448,17 +1878,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 4, Compare_Text(STR("Replace")).c_str()); + compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 9, Compare_Text(STR("Replace")).c_str()); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(text.begin() + 2, text.begin() + 4, Text(STR("Replace")).c_str()); + text.replace(text.begin() + 2, text.begin() + 9, Text(STR("Replace")).c_str()); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1470,6 +1902,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1485,6 +1918,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1496,6 +1930,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1507,6 +1942,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1518,17 +1954,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(2, 4, Compare_Text(STR("Replace")).c_str(), 5); + compare_text.replace(2, 7, Compare_Text(STR("Replace")).c_str(), 5); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(2, 4, Text(STR("Replace")).c_str(), 5); + text.replace(2, 7, Text(STR("Replace")).c_str(), 5); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1540,6 +1978,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1551,6 +1990,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1562,6 +2002,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1577,6 +2018,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1588,17 +2030,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 4, Compare_Text(STR("Replace")).c_str(), 5); + compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 9, Compare_Text(STR("Replace")).c_str(), 5); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(text.begin() + 2, text.begin() + 4, Text(STR("Replace")).c_str(), 5); + text.replace(text.begin() + 2, text.begin() + 9, Text(STR("Replace")).c_str(), 5); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1610,6 +2054,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1625,6 +2070,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1636,6 +2082,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1647,6 +2094,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1658,17 +2106,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(2, 4, 7, STR('A')); + compare_text.replace(2, 7, 7, STR('A')); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(2, 4, 7, STR('A')); + text.replace(2, 7, 7, STR('A')); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1680,6 +2130,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1691,6 +2142,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1702,6 +2154,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1717,6 +2170,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1728,17 +2182,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 4, 7, STR('A')); + compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 9, 7, STR('A')); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(text.begin() + 2, text.begin() + 4, 7, STR('A')); + text.replace(text.begin() + 2, text.begin() + 9, 7, STR('A')); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1750,6 +2206,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1768,6 +2225,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1779,17 +2237,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 4, replace.begin() + 1, replace.begin() + 5); + compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 7, replace.begin() + 1, replace.begin() + 5); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(text.begin() + 2, text.begin() + 4, replace_long.begin() + 1, replace_long.begin() + 5); + text.replace(text.begin() + 2, text.begin() + 7, replace_long.begin() + 1, replace_long.begin() + 5); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1801,6 +2261,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1814,6 +2275,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -1828,6 +2290,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -1837,6 +2300,7 @@ namespace text.clear(); CHECK_EQUAL(text.size(), size_t(0)); + CHECK(!text.truncated()); } //************************************************************************* @@ -1845,8 +2309,9 @@ namespace Compare_Text compare_text(initial_text.c_str()); Text text(initial_text.c_str()); - bool is_equal = Equal(compare_text, text); + bool is_equal = std::equal(text.begin(), text.end(), compare_text.begin()); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -1855,8 +2320,9 @@ namespace Compare_Text compare_text(initial_text.c_str()); Text text(initial_text.c_str()); - bool is_equal = Equal(compare_text, text); + bool is_equal = std::equal(text.cbegin(), text.cend(), compare_text.cbegin()); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -1865,8 +2331,9 @@ namespace Compare_Text compare_text(initial_text.c_str()); Text text(initial_text.c_str()); - bool is_equal = Equal(compare_text, text); + bool is_equal = std::equal(text.rbegin(), text.rend(), compare_text.rbegin()); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -1875,8 +2342,9 @@ namespace Compare_Text compare_text(initial_text.c_str()); Text text(initial_text.c_str()); - bool is_equal = Equal(compare_text, text); + bool is_equal = std::equal(text.crbegin(), text.crend(), compare_text.crbegin()); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -2069,6 +2537,7 @@ namespace buffer2[length2] = STR('\0'); CHECK_EQUAL(length1, length2); + CHECK(!text.truncated()); bool is_equal = std::equal(buffer1, buffer1 + length1, @@ -3065,6 +3534,7 @@ namespace CHECK_EQUAL(compare_hash, hash); } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_memcpy_repair) { Text text; @@ -3113,5 +3583,73 @@ namespace is_equal = Equal(text, itext); CHECK(!is_equal); } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_truncate_over_many_operations) + { + Text text(short_text.c_str()); + CHECK(!text.truncated()); + + text.insert(3, initial_text.c_str()); + CHECK(text.truncated()); + + while (text.size() != 0) + { + text.pop_back(); + CHECK(text.truncated()); + } + + text.clear(); + CHECK(!text.truncated()); + + text.assign(longer_text.c_str()); + CHECK(text.truncated()); + + text.assign(short_text.c_str()); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_add_from_truncated) + { + Text text1(short_text.c_str()); + TextS text2(short_text.c_str()); + + CHECK(!text1.truncated()); + CHECK(text2.truncated()); + + // text2 has the truncate flag set. + text1 += text2; + + CHECK(text1.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_add_to_truncated) + { + Text text1(longer_text.c_str()); + Text text2(short_text.c_str()); + + CHECK(text1.truncated()); + CHECK(!text2.truncated()); + + // Clear text but not the truncate flag. + text1.erase(text1.begin(), text1.end()); + + // text1 still has the truncate flag set. + text1 += text2; + + CHECK(text1.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_clear_truncated) + { + Text text(longer_text.c_str()); + CHECK(text.truncated()); + + text.clear_truncated(); + CHECK(!text.truncated()); + } }; } diff --git a/test/test_string_u16.cpp b/test/test_string_u16.cpp index 3d409062..ba4c94af 100644 --- a/test/test_string_u16.cpp +++ b/test/test_string_u16.cpp @@ -3,7 +3,7 @@ The MIT License(MIT) Embedded Template Library. https://github.com/ETLCPP/etl -http://www.etlcpp.com +https://www.etlcpp.com Copyright(c) 2016 jwellbelove @@ -55,6 +55,7 @@ namespace typedef std::u16string Compare_Text; typedef Text::value_type value_t; typedef etl::u16string<52> TextL; + typedef etl::u16string<4> TextS; Compare_Text initial_text; Compare_Text less_text; @@ -101,6 +102,7 @@ namespace CHECK_EQUAL(text.capacity(), SIZE); CHECK_EQUAL(text.max_size(), SIZE); CHECK(text.begin() == text.end()); + CHECK(!text.truncated()); } //************************************************************************* @@ -108,10 +110,11 @@ namespace { Text text; - CHECK(text.begin() == text.end()); - CHECK(text.cbegin() == text.cend()); - CHECK(text.rbegin() == text.rend()); + CHECK(text.begin() == text.end()); + CHECK(text.cbegin() == text.cend()); + CHECK(text.rbegin() == text.rend()); CHECK(text.crbegin() == text.crend()); + CHECK(!text.truncated()); } //************************************************************************* @@ -129,6 +132,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -137,6 +141,7 @@ namespace Text text(SIZE + 1, STR('A')); CHECK_EQUAL(SIZE, text.size()); + CHECK(text.truncated()); } //************************************************************************* @@ -150,6 +155,21 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_constructor_char_pointer_excess) + { + Compare_Text compare_text(initial_text.c_str()); + + Text text(longer_text.c_str()); + + CHECK(!text.empty()); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -163,6 +183,21 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_constructor_char_pointer_size_excess) + { + Compare_Text compare_text(SIZE, STR('A')); + + Text text(SIZE + 1, STR('A')); + + CHECK(!text.empty()); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -176,6 +211,21 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_constructor_size_char_excess) + { + Compare_Text compare_text(initial_text.c_str(), initial_text.size()); + + Text text(longer_text.c_str(), longer_text.size()); + + CHECK(!text.empty()); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -187,19 +237,19 @@ namespace CHECK(text.size() == SIZE); CHECK(!text.empty()); + CHECK(!text.truncated()); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_constructor_range_excess) { - Text text; - - text.assign(longer_text.begin(), longer_text.end()); + Text text(longer_text.begin(), longer_text.end()); bool is_equal = Equal(initial_text, text); CHECK(is_equal); CHECK(text.size() == SIZE); CHECK(!text.empty()); + CHECK(text.truncated()); } //************************************************************************* @@ -208,6 +258,35 @@ namespace Text text(initial_text.c_str()); Text text2(text); CHECK(text2 == text); + CHECK(!text2.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_copy_constructor_i) + { + Text text(initial_text.c_str()); + IText& itext = text; + Text text2(itext); + CHECK(text2 == text); + CHECK(!text2.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_copy_constructor_excess) + { + Text text(initial_text.c_str()); + TextL textl(longer_text.c_str()); + Text text2(textl); + CHECK(text2 == text); + CHECK(text2.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_copy_constructor_from_truncated) + { + Text text(longer_text.c_str()); + Text text2(text); + CHECK(text2.truncated()); } //************************************************************************* @@ -221,6 +300,21 @@ namespace bool is_equal = Equal(compare_text2, text2); CHECK(is_equal); + CHECK(!text2.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_construct_position_length_excess) + { + Compare_Text compare_text(longer_text.c_str()); + Compare_Text compare_text2(compare_text, 2, 11); + + TextL textl(longer_text.c_str()); + Text text2(textl, 2, 12); + + bool is_equal = Equal(compare_text2, text2); + CHECK(is_equal); + CHECK(text2.truncated()); } //************************************************************************* @@ -231,6 +325,21 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_construct_initializer_list_excess) + { + Compare_Text compare_text = { STR('H'), STR('e'), STR('l'), STR('l'), STR('o'), STR(' '), + STR('W'), STR('o'), STR('r'), STR('l'), STR('d') }; + Text text = { STR('H'), STR('e'), STR('l'), STR('l'), STR('o'), STR(' '), + STR('W'), STR('o'), STR('r'), STR('l'), STR('d'), STR(' '), + STR('T'), STR('h'), STR('e'), STR('r'), STR('e') }; + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -243,6 +352,22 @@ namespace bool is_equal = Equal(text, other_text); CHECK(is_equal); + CHECK(!text.truncated()); + CHECK(!other_text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_excess) + { + Text text(longer_text.begin(), longer_text.end()); + Text other_text; + + other_text = text; + + bool is_equal = Equal(text, other_text); + CHECK(is_equal); + CHECK(text.truncated()); + CHECK(other_text.truncated()); } //************************************************************************* @@ -259,8 +384,27 @@ namespace bool is_equal = Equal(text1, text2); CHECK(is_equal); + CHECK(!text1.truncated()); + CHECK(!text2.truncated()); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_iterface_excess) + { + Text text1(longer_text.begin(), longer_text.end()); + Text text2; + + IText& itext1 = text1; + IText& itext2 = text2; + + itext2 = itext1; + + bool is_equal = Equal(text1, text2); + + CHECK(is_equal); + CHECK(text1.truncated()); + CHECK(text2.truncated()); + } //************************************************************************* TEST_FIXTURE(SetupFixture, test_self_assignment) @@ -273,6 +417,73 @@ namespace bool is_equal = Equal(text, other_text); CHECK(is_equal); + CHECK(!text.truncated()); + CHECK(!other_text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_self_assignment_excess) + { + Text text(longer_text.begin(), longer_text.end()); + Text other_text(text); + + other_text = other_text; + + bool is_equal = Equal(text, other_text); + + CHECK(is_equal); + CHECK(text.truncated()); + CHECK(other_text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_from_literal) + { + Text text; + + text = STR("Hello World"); + + bool is_equal = Equal(std::u16string(STR("Hello World")), text); + CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_from_literal_excess) + { + Text text; + + text = STR("Hello World There"); + + bool is_equal = Equal(std::u16string(STR("Hello World")), text); + CHECK(is_equal); + CHECK(text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_from_literal_via_interface) + { + Text text; + IText& itext = text; + + itext = STR("Hello World"); + + bool is_equal = Equal(std::u16string(STR("Hello World")), itext); + CHECK(is_equal); + CHECK(!itext.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_from_literal_via_interface_excess) + { + Text text; + IText& itext = text; + + itext = STR("Hello World There"); + + bool is_equal = Equal(std::u16string(STR("Hello World")), itext); + CHECK(is_equal); + CHECK(itext.truncated()); } //************************************************************************* @@ -281,7 +492,7 @@ namespace Text text(initial_text.c_str()); const Text constText(initial_text.c_str()); - CHECK_EQUAL(&text[0], text.begin()); + CHECK_EQUAL(&text[0], text.begin()); CHECK_EQUAL(&constText[0], constText.begin()); } @@ -292,7 +503,7 @@ namespace Text text(initial_text.c_str()); const Text constText(initial_text.c_str()); - CHECK_EQUAL(&text[initial_text.size()], text.end()); + CHECK_EQUAL(&text[initial_text.size()], text.end()); CHECK_EQUAL(&constText[initial_text.size()], constText.end()); } @@ -306,6 +517,7 @@ namespace text.resize(NEW_SIZE); CHECK_EQUAL(text.size(), NEW_SIZE); + CHECK(!text.truncated()); } //************************************************************************* @@ -322,8 +534,8 @@ namespace compare_text.fill(INITIAL_VALUE); bool is_equal = Equal(compare_text, text); - CHECK(is_equal); + CHECK(!text.truncated()); } @@ -336,6 +548,7 @@ namespace Text text(INITIAL_SIZE, STR('A')); text.resize(NEW_SIZE, STR('A')); CHECK_EQUAL(SIZE, text.size()); + CHECK(text.truncated()); } //************************************************************************* @@ -348,6 +561,7 @@ namespace text.resize(NEW_SIZE); CHECK_EQUAL(text.size(), NEW_SIZE); + CHECK(!text.truncated()); } //************************************************************************* @@ -367,26 +581,66 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* - TEST_FIXTURE(SetupFixture, test_empty) + TEST_FIXTURE(SetupFixture, test_empty_full) + { + Text text; + text.resize(text.max_size(), STR('A')); + + CHECK(!text.empty()); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_empty_half) + { + Text text; + text.resize(text.max_size() / 2, STR('A')); + + CHECK(!text.empty()); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_empty_empty) + { + Text text; + + CHECK(text.empty()); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_full_full) { Text text; text.resize(text.max_size(), STR('A')); CHECK(text.full()); - CHECK(!text.empty()); + CHECK(!text.truncated()); } //************************************************************************* - TEST_FIXTURE(SetupFixture, test_full) + TEST_FIXTURE(SetupFixture, test_full_half) + { + Text text; + text.resize(text.max_size() / 2, STR('A')); + + CHECK(!text.full()); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_full_empty) { Text text; CHECK(!text.full()); - CHECK(text.empty()); + CHECK(!text.truncated()); } //************************************************************************* @@ -399,6 +653,8 @@ namespace { CHECK_EQUAL(text[i], compare_text[i]); } + + CHECK(!text.truncated()); } //************************************************************************* @@ -411,6 +667,8 @@ namespace { CHECK_EQUAL(text[i], compare_text[i]); } + + CHECK(!text.truncated()); } //************************************************************************* @@ -424,6 +682,8 @@ namespace CHECK_EQUAL(text.at(i), compare_text.at(i)); } + CHECK(!text.truncated()); + CHECK_THROW(text.at(text.size()), etl::string_out_of_bounds); } @@ -438,6 +698,8 @@ namespace CHECK_EQUAL(text.at(i), compare_text.at(i)); } + CHECK(!text.truncated()); + CHECK_THROW(text.at(text.size()), etl::string_out_of_bounds); } @@ -448,6 +710,7 @@ namespace Text text(initial_text.c_str()); CHECK(text.front() == compare_text.front()); + CHECK(!text.truncated()); } //************************************************************************* @@ -457,6 +720,7 @@ namespace const Text text(initial_text.c_str()); CHECK(text.front() == compare_text.front()); + CHECK(!text.truncated()); } //************************************************************************* @@ -466,6 +730,7 @@ namespace Text text(initial_text.c_str()); CHECK(text.back() == compare_text.back()); + CHECK(!text.truncated()); } //************************************************************************* @@ -475,6 +740,7 @@ namespace const Text text(initial_text.c_str()); CHECK(text.back() == compare_text.back()); + CHECK(!text.truncated()); } //************************************************************************* @@ -489,6 +755,7 @@ namespace compare_text.begin()); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -503,6 +770,41 @@ namespace compare_text.begin()); CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assign_string) + { + Compare_Text compare_input(initial_text.c_str()); + Text input(initial_text.c_str()); + + Compare_Text compare_text; + Text text; + + compare_text.assign(compare_input); + text.assign(input); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assign_string_excess) + { + Compare_Text compare_input(initial_text.c_str()); + TextL input(longer_text.c_str()); + + Compare_Text compare_text; + Text text; + + compare_text.assign(compare_input); + text.assign(input); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -511,20 +813,24 @@ namespace Compare_Text compare_text(initial_text.c_str()); Text text; - - text.assign(compare_text.c_str()); + text.assign(initial_text.c_str()); bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); + } - compare_text.assign(longer_text.c_str()); - compare_text.resize(std::min(compare_text.size(), SIZE)); + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assign_pointer_excess) + { + Compare_Text compare_text(initial_text.c_str()); - text.assign(compare_text.c_str()); - - is_equal = Equal(compare_text, text); + Text text; + text.assign(longer_text.c_str()); + bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -537,8 +843,8 @@ namespace text.assign(compare_text.begin(), compare_text.end()); bool is_equal = Equal(compare_text, text); - CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -550,11 +856,9 @@ namespace CHECK_EQUAL(initial_text.size(), text.size()); - bool is_equal = std::equal(text.begin(), - text.end(), - initial_text.begin()); - + bool is_equal = Equal(initial_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -572,8 +876,8 @@ namespace CHECK(text.size() == INITIAL_SIZE); bool is_equal = Equal(compare_text, text); - CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -590,10 +894,9 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } - - //************************************************************************* TEST_FIXTURE(SetupFixture, test_push_back) { @@ -610,17 +913,25 @@ namespace text.push_back(STR('A') + value_t(i)); } + CHECK_EQUAL(etl::strlen(compare_text.data()), etl::strlen(text.data())); CHECK_EQUAL(compare_text.size(), text.size()); bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_push_back_excess) { + Compare_Text compare_text; Text text; + for (size_t i = 0; i < SIZE; ++i) + { + compare_text.push_back(STR('A') + value_t(i)); + } + for (size_t i = 0; i < SIZE; ++i) { text.push_back(STR('A') + value_t(i)); @@ -629,8 +940,12 @@ namespace text.push_back(STR('A') + value_t(SIZE)); + CHECK_EQUAL(etl::strlen(compare_text.data()), etl::strlen(text.data())); + CHECK_EQUAL(compare_text.size(), text.size()); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); CHECK(text.truncated()); - CHECK_EQUAL(SIZE, text.size()); } //************************************************************************* @@ -656,7 +971,7 @@ namespace TEST_FIXTURE(SetupFixture, test_insert_position_value) { const size_t INITIAL_SIZE = 5; - const value_t INITIAL_VALUE = STR('A'); + const value_t INITIAL_VALUE = STR('A'); for (size_t offset = 0; offset <= INITIAL_SIZE; ++offset) { @@ -719,15 +1034,15 @@ namespace //************************************************************************* TEST_FIXTURE(SetupFixture, test_insert_position_n_value) { - const size_t INITIAL_SIZE = 5; - const size_t INSERT_SIZE = 3; - const value_t INITIAL_VALUE = STR('A'); + Compare_Text compare_text; + Text text; + + const size_t INITIAL_SIZE = 5; + const size_t INSERT_SIZE = 3; + const value_t INITIAL_VALUE = STR('A'); for (size_t offset = 0; offset <= INITIAL_SIZE; ++offset) { - Compare_Text compare_text; - Text text; - text.assign(initial_text.begin(), initial_text.begin() + INITIAL_SIZE); compare_text.assign(initial_text.begin(), initial_text.begin() + INITIAL_SIZE); text.insert(text.begin() + offset, INSERT_SIZE, INITIAL_VALUE); @@ -747,7 +1062,7 @@ namespace Text text; const size_t INSERT_SIZE = 4; - const value_t INSERT_VALUE = STR('A'); + const value_t INSERT_VALUE = STR('A'); size_t offset = 0; compare_text.assign(initial_text.begin(), initial_text.end()); @@ -795,7 +1110,7 @@ namespace text.assign(initial_text.begin(), initial_text.end()); text.insert(text.begin() + offset, INSERT_SIZE, INSERT_VALUE); - CHECK(!text.truncated()); + CHECK(text.truncated()); is_equal = Equal(compare_text, text); CHECK(is_equal); @@ -827,7 +1142,7 @@ namespace TEST_FIXTURE(SetupFixture, test_insert_position_range_excess) { const size_t INITIAL_SIZE = 5; - const value_t INITIAL_VALUE = STR('A'); + const value_t INITIAL_VALUE = STR('A'); Compare_Text compare_text; Text text; @@ -889,6 +1204,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } } @@ -907,6 +1223,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } } @@ -925,6 +1242,28 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); + } + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_insert_size_t_position_string_from_truncated) + { + for (size_t offset = 0; offset <= short_text.size(); ++offset) + { + Compare_Text compare_text(short_text.begin(), short_text.end()); + Text text(short_text.begin(), short_text.end()); + Text insert(longer_text.begin(), longer_text.end()); + insert.erase(insert.begin(), insert.end()); + insert.append(insert_text.begin(), insert_text.end()); + + text.insert(offset, insert); + compare_text.insert(offset, insert_text); + compare_text.resize(std::min(compare_text.size(), SIZE)); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } } @@ -941,6 +1280,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); compare_text.assign(short_text.begin(), short_text.end()); text.assign(short_text.begin(), short_text.end()); @@ -953,6 +1293,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); compare_text.assign(short_text.begin(), short_text.end()); text.assign(short_text.begin(), short_text.end()); @@ -963,6 +1304,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -978,6 +1320,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(short_text.c_str()); @@ -990,6 +1333,45 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_append_truncated_string) + { + Text text(short_text.c_str()); + TextS append(short_text.c_str()); + CHECK(append.truncated()); + + text.append(append); + CHECK(text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_append_string_to_self) + { + Compare_Text compare_text(short_text.c_str()); + Text text(short_text.c_str()); + + // Non-overflow. + compare_text.append(compare_text); + text.append(text); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(!text.truncated()); + + // Overflow. + compare_text.assign(shorter_text.c_str()); + text.assign(shorter_text.c_str()); + + compare_text.append(compare_text); + compare_text.resize(std::min(compare_text.size(), SIZE)); + text.append(text); + + is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1001,10 +1383,11 @@ namespace // Whole string. compare_text.append(insert_text, 0, std::u16string::npos); - text.append(append, 0, etl::iu16string::npos); + text.append(append, 0, Text::npos); bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Partial string. compare_text.assign(short_text.c_str()); @@ -1017,6 +1400,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(short_text.c_str()); @@ -1029,6 +1413,18 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_append_truncated_string_subpos_sublen) + { + Text text(short_text.c_str()); + TextS append(short_text.c_str()); + CHECK(append.truncated()); + + text.append(append, 1, 2); + CHECK(text.truncated()); } //************************************************************************* @@ -1044,6 +1440,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(short_text.c_str()); @@ -1055,6 +1452,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1070,6 +1468,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(short_text.c_str()); @@ -1081,6 +1480,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1096,6 +1496,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(short_text.c_str()); @@ -1108,6 +1509,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1123,6 +1525,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow short text. compare_text.assign(short_text.c_str()); @@ -1134,6 +1537,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1145,6 +1549,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1156,17 +1561,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(2, 2, Compare_Text(STR("Replace"))); + compare_text.replace(2, 7, Compare_Text(STR("Replace"))); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(2, 2, Text(STR("Replace"))); + text.replace(2, 7, Text(STR("Replace"))); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1178,6 +1585,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1189,6 +1597,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1204,6 +1613,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1215,17 +1625,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 4, Compare_Text(STR("Replace"))); + compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 9, Compare_Text(STR("Replace"))); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(text.begin() + 2, text.begin() + 4, Text(STR("Replace"))); + text.replace(text.begin() + 2, text.begin() + 9, Text(STR("Replace"))); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1237,6 +1649,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1252,6 +1665,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1263,6 +1677,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1274,6 +1689,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1285,17 +1701,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(2, 4, Compare_Text(STR("Replace")), 1, 5); + compare_text.replace(2, 7, Compare_Text(STR("Replace")), 1, 5); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(2, 4, Text(STR("Replace")), 1, 5); + text.replace(2, 7, Text(STR("Replace")), 1, 5); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1307,6 +1725,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1318,6 +1737,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1329,6 +1749,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1344,6 +1765,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1355,6 +1777,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1366,6 +1789,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1377,17 +1801,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(2, 4, Compare_Text(STR("Replace")).c_str()); + compare_text.replace(2, 7, Compare_Text(STR("Replace")).c_str()); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(2, 4, Text(STR("Replace")).c_str()); + text.replace(2, 7, Text(STR("Replace")).c_str()); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1399,6 +1825,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1410,6 +1837,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1421,6 +1849,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1436,6 +1865,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1447,17 +1877,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 4, Compare_Text(STR("Replace")).c_str()); + compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 9, Compare_Text(STR("Replace")).c_str()); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(text.begin() + 2, text.begin() + 4, Text(STR("Replace")).c_str()); + text.replace(text.begin() + 2, text.begin() + 9, Text(STR("Replace")).c_str()); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1469,6 +1901,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1484,6 +1917,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1495,6 +1929,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1506,6 +1941,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1517,17 +1953,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(2, 4, Compare_Text(STR("Replace")).c_str(), 5); + compare_text.replace(2, 7, Compare_Text(STR("Replace")).c_str(), 5); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(2, 4, Text(STR("Replace")).c_str(), 5); + text.replace(2, 7, Text(STR("Replace")).c_str(), 5); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1539,6 +1977,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1550,6 +1989,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1561,6 +2001,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1576,6 +2017,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1587,17 +2029,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 4, Compare_Text(STR("Replace")).c_str(), 5); + compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 9, Compare_Text(STR("Replace")).c_str(), 5); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(text.begin() + 2, text.begin() + 4, Text(STR("Replace")).c_str(), 5); + text.replace(text.begin() + 2, text.begin() + 9, Text(STR("Replace")).c_str(), 5); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1609,6 +2053,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1624,6 +2069,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1635,6 +2081,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1646,6 +2093,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1657,17 +2105,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(2, 4, 7, STR('A')); + compare_text.replace(2, 7, 7, STR('A')); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(2, 4, 7, STR('A')); + text.replace(2, 7, 7, STR('A')); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1679,6 +2129,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1690,6 +2141,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1701,6 +2153,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1716,6 +2169,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1727,17 +2181,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 4, 7, STR('A')); + compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 9, 7, STR('A')); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(text.begin() + 2, text.begin() + 4, 7, STR('A')); + text.replace(text.begin() + 2, text.begin() + 9, 7, STR('A')); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1749,6 +2205,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1767,6 +2224,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1778,17 +2236,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 4, replace.begin() + 1, replace.begin() + 5); + compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 7, replace.begin() + 1, replace.begin() + 5); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(text.begin() + 2, text.begin() + 4, replace_long.begin() + 1, replace_long.begin() + 5); + text.replace(text.begin() + 2, text.begin() + 7, replace_long.begin() + 1, replace_long.begin() + 5); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1800,6 +2260,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1813,6 +2274,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -1827,6 +2289,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -1836,6 +2299,7 @@ namespace text.clear(); CHECK_EQUAL(text.size(), size_t(0)); + CHECK(!text.truncated()); } //************************************************************************* @@ -1844,8 +2308,9 @@ namespace Compare_Text compare_text(initial_text.c_str()); Text text(initial_text.c_str()); - bool is_equal = Equal(compare_text, text); + bool is_equal = std::equal(text.begin(), text.end(), compare_text.begin()); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -1854,8 +2319,9 @@ namespace Compare_Text compare_text(initial_text.c_str()); Text text(initial_text.c_str()); - bool is_equal = Equal(compare_text, text); + bool is_equal = std::equal(text.cbegin(), text.cend(), compare_text.cbegin()); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -1864,8 +2330,9 @@ namespace Compare_Text compare_text(initial_text.c_str()); Text text(initial_text.c_str()); - bool is_equal = Equal(compare_text, text); + bool is_equal = std::equal(text.rbegin(), text.rend(), compare_text.rbegin()); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -1874,8 +2341,9 @@ namespace Compare_Text compare_text(initial_text.c_str()); Text text(initial_text.c_str()); - bool is_equal = Equal(compare_text, text); + bool is_equal = std::equal(text.crbegin(), text.crend(), compare_text.crbegin()); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -1935,16 +2403,16 @@ namespace // String-Pointer Pointer-String CHECK((less < pinitial_text) == (less_text < pinitial_text)); - CHECK((pinitial_text < less) == (pinitial_text < less_text)); + CHECK((pinitial_text < less) == (pinitial_text < less_text)); CHECK((greater < pinitial_text) == (greater_text < pinitial_text)); - CHECK((pinitial_text < greater) == (pinitial_text < greater_text)); + CHECK((pinitial_text < greater) == (pinitial_text < greater_text)); CHECK((shorter < pinitial_text) == (shorter_text < pinitial_text)); - CHECK((pinitial_text < shorter) == (pinitial_text < shorter_text)); + CHECK((pinitial_text < shorter) == (pinitial_text < shorter_text)); CHECK((initial < pinitial_text) == (initial_text < pinitial_text)); - CHECK((pinitial_text < initial) == (pinitial_text < initial_text)); + CHECK((pinitial_text < initial) == (pinitial_text < initial_text)); } //************************************************************************* @@ -1954,8 +2422,8 @@ namespace const Text initial(initial_text.c_str()); // String-String - CHECK((less <= initial) == (less_text <= initial_text)); - CHECK((initial <= less) == (initial_text <= less_text)); + CHECK((less <= initial) == (less_text <= initial_text)); + CHECK((initial <= less) == (initial_text <= less_text)); const Text greater(greater_text.c_str()); CHECK((greater <= initial) == (greater_text <= initial_text)); @@ -1969,17 +2437,17 @@ namespace CHECK((initial <= initial) == (initial_text <= initial_text)); // String-Pointer Pointer-String - CHECK((less <= pinitial_text) == (less_text <= pinitial_text)); - CHECK((pinitial_text <= less) == (pinitial_text <= less_text)); + CHECK((less <= pinitial_text) == (less_text <= pinitial_text)); + CHECK((pinitial_text <= less) == (pinitial_text <= less_text)); - CHECK((greater <= pinitial_text) == (greater_text <= pinitial_text)); - CHECK((pinitial_text <= greater) == (pinitial_text <= greater_text)); + CHECK((greater <= pinitial_text) == (greater_text <= pinitial_text)); + CHECK((pinitial_text <= greater) == (pinitial_text <= greater_text)); - CHECK((shorter <= pinitial_text) == (shorter_text <= pinitial_text)); - CHECK((pinitial_text <= shorter) == (pinitial_text <= shorter_text)); + CHECK((shorter <= pinitial_text) == (shorter_text <= pinitial_text)); + CHECK((pinitial_text <= shorter) == (pinitial_text <= shorter_text)); - CHECK((initial <= pinitial_text) == (initial_text <= pinitial_text)); - CHECK((pinitial_text <= initial) == (pinitial_text <= initial_text)); + CHECK((initial <= pinitial_text) == (initial_text <= pinitial_text)); + CHECK((pinitial_text <= initial) == (pinitial_text <= initial_text)); } //************************************************************************* @@ -2005,16 +2473,16 @@ namespace // String-Pointer Pointer-String CHECK((less > pinitial_text) == (less_text > pinitial_text)); - CHECK((pinitial_text > less) == (pinitial_text > less_text)); + CHECK((pinitial_text > less) == (pinitial_text > less_text)); CHECK((greater > pinitial_text) == (greater_text > pinitial_text)); - CHECK((pinitial_text > greater) == (pinitial_text > greater_text)); + CHECK((pinitial_text > greater) == (pinitial_text > greater_text)); CHECK((shorter > pinitial_text) == (shorter_text > pinitial_text)); - CHECK((pinitial_text > shorter) == (pinitial_text > shorter_text)); + CHECK((pinitial_text > shorter) == (pinitial_text > shorter_text)); CHECK((initial > pinitial_text) == (initial_text > pinitial_text)); - CHECK((pinitial_text > initial) == (pinitial_text > initial_text)); + CHECK((pinitial_text > initial) == (pinitial_text > initial_text)); } //************************************************************************* @@ -2024,8 +2492,8 @@ namespace const Text initial(initial_text.begin(), initial_text.end()); // String-String - CHECK((less >= initial) == (less_text >= initial_text)); - CHECK((initial >= less) == (initial_text >= less_text)); + CHECK((less >= initial) == (less_text >= initial_text)); + CHECK((initial >= less) == (initial_text >= less_text)); const Text greater(greater_text.begin(), greater_text.end()); CHECK((greater >= initial) == (greater_text >= initial_text)); @@ -2039,17 +2507,17 @@ namespace CHECK((initial >= initial) == (initial_text >= initial_text)); // String-Pointer Pointer-String - CHECK((less >= pinitial_text) == (less_text >= pinitial_text)); - CHECK((pinitial_text >= less) == (pinitial_text >= less_text)); + CHECK((less >= pinitial_text) == (less_text >= pinitial_text)); + CHECK((pinitial_text >= less) == (pinitial_text >= less_text)); - CHECK((greater >= pinitial_text) == (greater_text >= pinitial_text)); - CHECK((pinitial_text >= greater) == (pinitial_text >= greater_text)); + CHECK((greater >= pinitial_text) == (greater_text >= pinitial_text)); + CHECK((pinitial_text >= greater) == (pinitial_text >= greater_text)); - CHECK((shorter >= pinitial_text) == (shorter_text >= pinitial_text)); - CHECK((pinitial_text >= shorter) == (pinitial_text >= shorter_text)); + CHECK((shorter >= pinitial_text) == (shorter_text >= pinitial_text)); + CHECK((pinitial_text >= shorter) == (pinitial_text >= shorter_text)); - CHECK((initial >= pinitial_text) == (initial_text >= pinitial_text)); - CHECK((pinitial_text >= initial) == (pinitial_text >= initial_text)); + CHECK((initial >= pinitial_text) == (initial_text >= pinitial_text)); + CHECK((pinitial_text >= initial) == (pinitial_text >= initial_text)); } //************************************************************************* @@ -2064,10 +2532,11 @@ namespace size_t length1 = compare_text.copy(buffer1, 5, 2); buffer1[length1] = STR('\0'); - size_t length2 = compare_text.copy(buffer2, 5, 2); + size_t length2 = text.copy(buffer2, 5, 2); buffer2[length2] = STR('\0'); CHECK_EQUAL(length1, length2); + CHECK(!text.truncated()); bool is_equal = std::equal(buffer1, buffer1 + length1, @@ -2078,7 +2547,7 @@ namespace //************************************************************************* TEST_FIXTURE(SetupFixture, test_find_string) { - const value_t *the_haystack = STR("A haystack with a needle and another needle"); + const value_t* the_haystack = STR("A haystack with a needle and another needle"); std::u16string compare_needle(STR("needle")); etl::u16string<50> needle(STR("needle")); @@ -2098,7 +2567,7 @@ namespace CHECK_EQUAL(position1, position2); position2 = haystack.find(needle, position2 + 1); - CHECK_EQUAL(etl::iu16string::npos, position2); + CHECK_EQUAL(etl::u16string<50>::npos, position2); etl::u16string<50> pin(STR("pin")); position2 = haystack.find(pin); @@ -2106,9 +2575,9 @@ namespace } //************************************************************************* - TEST_FIXTURE(SetupFixture, test_find_char_pointer) + TEST_FIXTURE(SetupFixture, test_find_pointer) { - const value_t *the_haystack = STR("A haystack with a needle and another needle"); + const value_t* the_haystack = STR("A haystack with a needle and another needle"); const value_t* needle = STR("needle"); @@ -2137,7 +2606,7 @@ namespace //************************************************************************* TEST_FIXTURE(SetupFixture, test_find_char_pointer_n) { - const value_t *the_haystack = STR("A haystack with a needle and another needle"); + const value_t* the_haystack = STR("A haystack with a needle and another needle"); const value_t* needle = STR("needle"); @@ -2273,7 +2742,7 @@ namespace Compare_Text compare_result; Text result; - // Empty string. + // Equal. compare_result = compare_text.substr(compare_text.size()); result = text.substr(text.size()); CHECK(Equal(compare_result, result)); @@ -2624,8 +3093,8 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_first_of(STR('C'), 5); - position2 = text.find_first_of(STR('C'), 5); + position1 = compare_text.find_first_of(STR('C'), compare_text.size()); + position2 = text.find_first_of(STR('C'), text.size()); CHECK_EQUAL(position1, position2); @@ -2651,13 +3120,13 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(Compare_Text(STR("ZCXE")), 3); - position2 = text.find_last_of(Text(STR("ZCXE")), 3); + position1 = compare_text.find_last_of(Compare_Text(STR("ZCXE")), 5); + position2 = text.find_last_of(Text(STR("ZCXE")), 5); CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(Compare_Text(STR("ZCXE")), 10); - position2 = text.find_last_of(Text(STR("ZCXE")), 10); + position1 = compare_text.find_last_of(Compare_Text(STR("ZCXE")), compare_text.size()); + position2 = text.find_last_of(Text(STR("ZCXE")), text.size()); CHECK_EQUAL(position1, position2); @@ -2683,8 +3152,8 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(STR("ZCXE"), 3); - position2 = text.find_last_of(STR("ZCXE"), 3); + position1 = compare_text.find_last_of(STR("ZCXE"), 5); + position2 = text.find_last_of(STR("ZCXE"), 5); CHECK_EQUAL(position1, position2); @@ -2693,8 +3162,8 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(STR("ZCXE"), 10); - position2 = text.find_last_of(STR("ZCXE"), 10); + position1 = compare_text.find_last_of(STR("ZCXE"), compare_text.size()); + position2 = text.find_last_of(STR("ZCXE"), text.size()); CHECK_EQUAL(position1, position2); @@ -2710,8 +3179,8 @@ namespace Compare_Text compare_text(STR("ABCDEFABCDE")); Text text(STR("ABCDEFABCDE")); - size_t position1 = compare_text.find_last_of(STR("ZCXE"), 11, 4); - size_t position2 = text.find_last_of(STR("ZCXE"), 11, 4); + size_t position1 = compare_text.find_last_of(STR("AZCXE"), 0, 4); + size_t position2 = text.find_last_of(STR("AZCXE"), 0, 4); CHECK_EQUAL(position1, position2); @@ -2720,8 +3189,8 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(STR("ZCXE"), 4, 3); - position2 = text.find_last_of(STR("ZCXE"), 4, 3); + position1 = compare_text.find_last_of(STR("ZCXE"), 5, 3); + position2 = text.find_last_of(STR("ZCXE"), 5, 3); CHECK_EQUAL(position1, position2); @@ -2730,8 +3199,8 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(STR("ZCXE"), 10, 4); - position2 = text.find_last_of(STR("ZCXE"), 10, 4); + position1 = compare_text.find_last_of(STR("ZCXE"), compare_text.size(), 4); + position2 = text.find_last_of(STR("ZCXE"), text.size(), 4); CHECK_EQUAL(position1, position2); @@ -2757,8 +3226,8 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(STR('F'), 5); - position2 = text.find_last_of(STR('F'), 5); + position1 = compare_text.find_last_of(STR('F'), compare_text.size()); + position2 = text.find_last_of(STR('F'), text.size()); CHECK_EQUAL(position1, position2); @@ -2767,8 +3236,8 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(STR('C'), 5); - position2 = text.find_last_of(STR('C'), 5); + position1 = compare_text.find_last_of(STR('C'), compare_text.size()); + position2 = text.find_last_of(STR('C'), text.size()); CHECK_EQUAL(position1, position2); @@ -3064,6 +3533,7 @@ namespace CHECK_EQUAL(compare_hash, hash); } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_memcpy_repair) { Text text; @@ -3112,5 +3582,73 @@ namespace is_equal = Equal(text, itext); CHECK(!is_equal); } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_truncate_over_many_operations) + { + Text text(short_text.c_str()); + CHECK(!text.truncated()); + + text.insert(3, initial_text.c_str()); + CHECK(text.truncated()); + + while (text.size() != 0) + { + text.pop_back(); + CHECK(text.truncated()); + } + + text.clear(); + CHECK(!text.truncated()); + + text.assign(longer_text.c_str()); + CHECK(text.truncated()); + + text.assign(short_text.c_str()); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_add_from_truncated) + { + Text text1(short_text.c_str()); + TextS text2(short_text.c_str()); + + CHECK(!text1.truncated()); + CHECK(text2.truncated()); + + // text2 has the truncate flag set. + text1 += text2; + + CHECK(text1.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_add_to_truncated) + { + Text text1(longer_text.c_str()); + Text text2(short_text.c_str()); + + CHECK(text1.truncated()); + CHECK(!text2.truncated()); + + // Clear text but not the truncate flag. + text1.erase(text1.begin(), text1.end()); + + // text1 still has the truncate flag set. + text1 += text2; + + CHECK(text1.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_clear_truncated) + { + Text text(longer_text.c_str()); + CHECK(text.truncated()); + + text.clear_truncated(); + CHECK(!text.truncated()); + } }; } diff --git a/test/test_string_u32.cpp b/test/test_string_u32.cpp index 6e6c7f03..4acfeb8a 100644 --- a/test/test_string_u32.cpp +++ b/test/test_string_u32.cpp @@ -3,7 +3,7 @@ The MIT License(MIT) Embedded Template Library. https://github.com/ETLCPP/etl -http://www.etlcpp.com +https://www.etlcpp.com Copyright(c) 2016 jwellbelove @@ -55,6 +55,7 @@ namespace typedef std::u32string Compare_Text; typedef Text::value_type value_t; typedef etl::u32string<52> TextL; + typedef etl::u32string<4> TextS; Compare_Text initial_text; Compare_Text less_text; @@ -101,6 +102,7 @@ namespace CHECK_EQUAL(text.capacity(), SIZE); CHECK_EQUAL(text.max_size(), SIZE); CHECK(text.begin() == text.end()); + CHECK(!text.truncated()); } //************************************************************************* @@ -108,10 +110,11 @@ namespace { Text text; - CHECK(text.begin() == text.end()); - CHECK(text.cbegin() == text.cend()); - CHECK(text.rbegin() == text.rend()); + CHECK(text.begin() == text.end()); + CHECK(text.cbegin() == text.cend()); + CHECK(text.rbegin() == text.rend()); CHECK(text.crbegin() == text.crend()); + CHECK(!text.truncated()); } //************************************************************************* @@ -129,6 +132,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -137,6 +141,7 @@ namespace Text text(SIZE + 1, STR('A')); CHECK_EQUAL(SIZE, text.size()); + CHECK(text.truncated()); } //************************************************************************* @@ -150,6 +155,21 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_constructor_char_pointer_excess) + { + Compare_Text compare_text(initial_text.c_str()); + + Text text(longer_text.c_str()); + + CHECK(!text.empty()); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -163,6 +183,21 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_constructor_char_pointer_size_excess) + { + Compare_Text compare_text(SIZE, STR('A')); + + Text text(SIZE + 1, STR('A')); + + CHECK(!text.empty()); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -176,6 +211,21 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_constructor_size_char_excess) + { + Compare_Text compare_text(initial_text.c_str(), initial_text.size()); + + Text text(longer_text.c_str(), longer_text.size()); + + CHECK(!text.empty()); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -187,19 +237,19 @@ namespace CHECK(text.size() == SIZE); CHECK(!text.empty()); + CHECK(!text.truncated()); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_constructor_range_excess) { - Text text; - - text.assign(longer_text.begin(), longer_text.end()); + Text text(longer_text.begin(), longer_text.end()); bool is_equal = Equal(initial_text, text); CHECK(is_equal); CHECK(text.size() == SIZE); CHECK(!text.empty()); + CHECK(text.truncated()); } //************************************************************************* @@ -208,6 +258,35 @@ namespace Text text(initial_text.c_str()); Text text2(text); CHECK(text2 == text); + CHECK(!text2.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_copy_constructor_i) + { + Text text(initial_text.c_str()); + IText& itext = text; + Text text2(itext); + CHECK(text2 == text); + CHECK(!text2.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_copy_constructor_excess) + { + Text text(initial_text.c_str()); + TextL textl(longer_text.c_str()); + Text text2(textl); + CHECK(text2 == text); + CHECK(text2.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_copy_constructor_from_truncated) + { + Text text(longer_text.c_str()); + Text text2(text); + CHECK(text2.truncated()); } //************************************************************************* @@ -221,6 +300,21 @@ namespace bool is_equal = Equal(compare_text2, text2); CHECK(is_equal); + CHECK(!text2.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_construct_position_length_excess) + { + Compare_Text compare_text(longer_text.c_str()); + Compare_Text compare_text2(compare_text, 2, 11); + + TextL textl(longer_text.c_str()); + Text text2(textl, 2, 12); + + bool is_equal = Equal(compare_text2, text2); + CHECK(is_equal); + CHECK(text2.truncated()); } //************************************************************************* @@ -231,6 +325,21 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_construct_initializer_list_excess) + { + Compare_Text compare_text = { STR('H'), STR('e'), STR('l'), STR('l'), STR('o'), STR(' '), + STR('W'), STR('o'), STR('r'), STR('l'), STR('d') }; + Text text = { STR('H'), STR('e'), STR('l'), STR('l'), STR('o'), STR(' '), + STR('W'), STR('o'), STR('r'), STR('l'), STR('d'), STR(' '), + STR('T'), STR('h'), STR('e'), STR('r'), STR('e') }; + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -243,6 +352,22 @@ namespace bool is_equal = Equal(text, other_text); CHECK(is_equal); + CHECK(!text.truncated()); + CHECK(!other_text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_excess) + { + Text text(longer_text.begin(), longer_text.end()); + Text other_text; + + other_text = text; + + bool is_equal = Equal(text, other_text); + CHECK(is_equal); + CHECK(text.truncated()); + CHECK(other_text.truncated()); } //************************************************************************* @@ -259,8 +384,27 @@ namespace bool is_equal = Equal(text1, text2); CHECK(is_equal); + CHECK(!text1.truncated()); + CHECK(!text2.truncated()); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_iterface_excess) + { + Text text1(longer_text.begin(), longer_text.end()); + Text text2; + + IText& itext1 = text1; + IText& itext2 = text2; + + itext2 = itext1; + + bool is_equal = Equal(text1, text2); + + CHECK(is_equal); + CHECK(text1.truncated()); + CHECK(text2.truncated()); + } //************************************************************************* TEST_FIXTURE(SetupFixture, test_self_assignment) @@ -273,6 +417,73 @@ namespace bool is_equal = Equal(text, other_text); CHECK(is_equal); + CHECK(!text.truncated()); + CHECK(!other_text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_self_assignment_excess) + { + Text text(longer_text.begin(), longer_text.end()); + Text other_text(text); + + other_text = other_text; + + bool is_equal = Equal(text, other_text); + + CHECK(is_equal); + CHECK(text.truncated()); + CHECK(other_text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_from_literal) + { + Text text; + + text = STR("Hello World"); + + bool is_equal = Equal(std::u32string(STR("Hello World")), text); + CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_from_literal_excess) + { + Text text; + + text = STR("Hello World There"); + + bool is_equal = Equal(std::u32string(STR("Hello World")), text); + CHECK(is_equal); + CHECK(text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_from_literal_via_interface) + { + Text text; + IText& itext = text; + + itext = STR("Hello World"); + + bool is_equal = Equal(std::u32string(STR("Hello World")), itext); + CHECK(is_equal); + CHECK(!itext.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_from_literal_via_interface_excess) + { + Text text; + IText& itext = text; + + itext = STR("Hello World There"); + + bool is_equal = Equal(std::u32string(STR("Hello World")), itext); + CHECK(is_equal); + CHECK(itext.truncated()); } //************************************************************************* @@ -281,7 +492,7 @@ namespace Text text(initial_text.c_str()); const Text constText(initial_text.c_str()); - CHECK_EQUAL(&text[0], text.begin()); + CHECK_EQUAL(&text[0], text.begin()); CHECK_EQUAL(&constText[0], constText.begin()); } @@ -292,7 +503,7 @@ namespace Text text(initial_text.c_str()); const Text constText(initial_text.c_str()); - CHECK_EQUAL(&text[initial_text.size()], text.end()); + CHECK_EQUAL(&text[initial_text.size()], text.end()); CHECK_EQUAL(&constText[initial_text.size()], constText.end()); } @@ -306,6 +517,7 @@ namespace text.resize(NEW_SIZE); CHECK_EQUAL(text.size(), NEW_SIZE); + CHECK(!text.truncated()); } //************************************************************************* @@ -322,8 +534,8 @@ namespace compare_text.fill(INITIAL_VALUE); bool is_equal = Equal(compare_text, text); - CHECK(is_equal); + CHECK(!text.truncated()); } @@ -336,6 +548,7 @@ namespace Text text(INITIAL_SIZE, STR('A')); text.resize(NEW_SIZE, STR('A')); CHECK_EQUAL(SIZE, text.size()); + CHECK(text.truncated()); } //************************************************************************* @@ -348,6 +561,7 @@ namespace text.resize(NEW_SIZE); CHECK_EQUAL(text.size(), NEW_SIZE); + CHECK(!text.truncated()); } //************************************************************************* @@ -367,26 +581,66 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* - TEST_FIXTURE(SetupFixture, test_empty) + TEST_FIXTURE(SetupFixture, test_empty_full) + { + Text text; + text.resize(text.max_size(), STR('A')); + + CHECK(!text.empty()); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_empty_half) + { + Text text; + text.resize(text.max_size() / 2, STR('A')); + + CHECK(!text.empty()); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_empty_empty) + { + Text text; + + CHECK(text.empty()); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_full_full) { Text text; text.resize(text.max_size(), STR('A')); CHECK(text.full()); - CHECK(!text.empty()); + CHECK(!text.truncated()); } //************************************************************************* - TEST_FIXTURE(SetupFixture, test_full) + TEST_FIXTURE(SetupFixture, test_full_half) + { + Text text; + text.resize(text.max_size() / 2, STR('A')); + + CHECK(!text.full()); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_full_empty) { Text text; CHECK(!text.full()); - CHECK(text.empty()); + CHECK(!text.truncated()); } //************************************************************************* @@ -399,6 +653,8 @@ namespace { CHECK_EQUAL(text[i], compare_text[i]); } + + CHECK(!text.truncated()); } //************************************************************************* @@ -411,6 +667,8 @@ namespace { CHECK_EQUAL(text[i], compare_text[i]); } + + CHECK(!text.truncated()); } //************************************************************************* @@ -424,6 +682,8 @@ namespace CHECK_EQUAL(text.at(i), compare_text.at(i)); } + CHECK(!text.truncated()); + CHECK_THROW(text.at(text.size()), etl::string_out_of_bounds); } @@ -438,6 +698,8 @@ namespace CHECK_EQUAL(text.at(i), compare_text.at(i)); } + CHECK(!text.truncated()); + CHECK_THROW(text.at(text.size()), etl::string_out_of_bounds); } @@ -448,6 +710,7 @@ namespace Text text(initial_text.c_str()); CHECK(text.front() == compare_text.front()); + CHECK(!text.truncated()); } //************************************************************************* @@ -457,6 +720,7 @@ namespace const Text text(initial_text.c_str()); CHECK(text.front() == compare_text.front()); + CHECK(!text.truncated()); } //************************************************************************* @@ -466,6 +730,7 @@ namespace Text text(initial_text.c_str()); CHECK(text.back() == compare_text.back()); + CHECK(!text.truncated()); } //************************************************************************* @@ -475,6 +740,7 @@ namespace const Text text(initial_text.c_str()); CHECK(text.back() == compare_text.back()); + CHECK(!text.truncated()); } //************************************************************************* @@ -489,6 +755,7 @@ namespace compare_text.begin()); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -503,6 +770,41 @@ namespace compare_text.begin()); CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assign_string) + { + Compare_Text compare_input(initial_text.c_str()); + Text input(initial_text.c_str()); + + Compare_Text compare_text; + Text text; + + compare_text.assign(compare_input); + text.assign(input); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assign_string_excess) + { + Compare_Text compare_input(initial_text.c_str()); + TextL input(longer_text.c_str()); + + Compare_Text compare_text; + Text text; + + compare_text.assign(compare_input); + text.assign(input); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -511,20 +813,24 @@ namespace Compare_Text compare_text(initial_text.c_str()); Text text; - - text.assign(compare_text.c_str()); + text.assign(initial_text.c_str()); bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); + } - compare_text.assign(longer_text.c_str()); - compare_text.resize(std::min(compare_text.size(), SIZE)); + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assign_pointer_excess) + { + Compare_Text compare_text(initial_text.c_str()); - text.assign(compare_text.c_str()); - - is_equal = Equal(compare_text, text); + Text text; + text.assign(longer_text.c_str()); + bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -537,8 +843,8 @@ namespace text.assign(compare_text.begin(), compare_text.end()); bool is_equal = Equal(compare_text, text); - CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -550,11 +856,9 @@ namespace CHECK_EQUAL(initial_text.size(), text.size()); - bool is_equal = std::equal(text.begin(), - text.end(), - initial_text.begin()); - + bool is_equal = Equal(initial_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -572,8 +876,8 @@ namespace CHECK(text.size() == INITIAL_SIZE); bool is_equal = Equal(compare_text, text); - CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -590,6 +894,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -608,17 +913,25 @@ namespace text.push_back(STR('A') + value_t(i)); } + CHECK_EQUAL(etl::strlen(compare_text.data()), etl::strlen(text.data())); CHECK_EQUAL(compare_text.size(), text.size()); bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_push_back_excess) { + Compare_Text compare_text; Text text; + for (size_t i = 0; i < SIZE; ++i) + { + compare_text.push_back(STR('A') + value_t(i)); + } + for (size_t i = 0; i < SIZE; ++i) { text.push_back(STR('A') + value_t(i)); @@ -627,8 +940,12 @@ namespace text.push_back(STR('A') + value_t(SIZE)); + CHECK_EQUAL(etl::strlen(compare_text.data()), etl::strlen(text.data())); + CHECK_EQUAL(compare_text.size(), text.size()); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); CHECK(text.truncated()); - CHECK_EQUAL(SIZE, text.size()); } //************************************************************************* @@ -654,7 +971,7 @@ namespace TEST_FIXTURE(SetupFixture, test_insert_position_value) { const size_t INITIAL_SIZE = 5; - const value_t INITIAL_VALUE = STR('A'); + const value_t INITIAL_VALUE = STR('A'); for (size_t offset = 0; offset <= INITIAL_SIZE; ++offset) { @@ -717,15 +1034,15 @@ namespace //************************************************************************* TEST_FIXTURE(SetupFixture, test_insert_position_n_value) { - const size_t INITIAL_SIZE = 5; - const size_t INSERT_SIZE = 3; - const value_t INITIAL_VALUE = STR('A'); + Compare_Text compare_text; + Text text; + + const size_t INITIAL_SIZE = 5; + const size_t INSERT_SIZE = 3; + const value_t INITIAL_VALUE = STR('A'); for (size_t offset = 0; offset <= INITIAL_SIZE; ++offset) { - Compare_Text compare_text; - Text text; - text.assign(initial_text.begin(), initial_text.begin() + INITIAL_SIZE); compare_text.assign(initial_text.begin(), initial_text.begin() + INITIAL_SIZE); text.insert(text.begin() + offset, INSERT_SIZE, INITIAL_VALUE); @@ -745,7 +1062,7 @@ namespace Text text; const size_t INSERT_SIZE = 4; - const value_t INSERT_VALUE = STR('A'); + const value_t INSERT_VALUE = STR('A'); size_t offset = 0; compare_text.assign(initial_text.begin(), initial_text.end()); @@ -793,7 +1110,7 @@ namespace text.assign(initial_text.begin(), initial_text.end()); text.insert(text.begin() + offset, INSERT_SIZE, INSERT_VALUE); - CHECK(!text.truncated()); + CHECK(text.truncated()); is_equal = Equal(compare_text, text); CHECK(is_equal); @@ -825,7 +1142,7 @@ namespace TEST_FIXTURE(SetupFixture, test_insert_position_range_excess) { const size_t INITIAL_SIZE = 5; - const value_t INITIAL_VALUE = STR('A'); + const value_t INITIAL_VALUE = STR('A'); Compare_Text compare_text; Text text; @@ -887,6 +1204,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } } @@ -905,6 +1223,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } } @@ -923,6 +1242,28 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); + } + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_insert_size_t_position_string_from_truncated) + { + for (size_t offset = 0; offset <= short_text.size(); ++offset) + { + Compare_Text compare_text(short_text.begin(), short_text.end()); + Text text(short_text.begin(), short_text.end()); + Text insert(longer_text.begin(), longer_text.end()); + insert.erase(insert.begin(), insert.end()); + insert.append(insert_text.begin(), insert_text.end()); + + text.insert(offset, insert); + compare_text.insert(offset, insert_text); + compare_text.resize(std::min(compare_text.size(), SIZE)); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } } @@ -939,6 +1280,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); compare_text.assign(short_text.begin(), short_text.end()); text.assign(short_text.begin(), short_text.end()); @@ -951,6 +1293,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); compare_text.assign(short_text.begin(), short_text.end()); text.assign(short_text.begin(), short_text.end()); @@ -961,6 +1304,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -976,6 +1320,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(short_text.c_str()); @@ -988,6 +1333,45 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_append_truncated_string) + { + Text text(short_text.c_str()); + TextS append(short_text.c_str()); + CHECK(append.truncated()); + + text.append(append); + CHECK(text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_append_string_to_self) + { + Compare_Text compare_text(short_text.c_str()); + Text text(short_text.c_str()); + + // Non-overflow. + compare_text.append(compare_text); + text.append(text); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(!text.truncated()); + + // Overflow. + compare_text.assign(shorter_text.c_str()); + text.assign(shorter_text.c_str()); + + compare_text.append(compare_text); + compare_text.resize(std::min(compare_text.size(), SIZE)); + text.append(text); + + is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -999,10 +1383,11 @@ namespace // Whole string. compare_text.append(insert_text, 0, std::u32string::npos); - text.append(append, 0, etl::iu32string::npos); + text.append(append, 0, Text::npos); bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Partial string. compare_text.assign(short_text.c_str()); @@ -1015,6 +1400,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(short_text.c_str()); @@ -1027,6 +1413,18 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_append_truncated_string_subpos_sublen) + { + Text text(short_text.c_str()); + TextS append(short_text.c_str()); + CHECK(append.truncated()); + + text.append(append, 1, 2); + CHECK(text.truncated()); } //************************************************************************* @@ -1042,6 +1440,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(short_text.c_str()); @@ -1053,6 +1452,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1068,6 +1468,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(short_text.c_str()); @@ -1079,6 +1480,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1094,6 +1496,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(short_text.c_str()); @@ -1106,6 +1509,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1121,6 +1525,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow short text. compare_text.assign(short_text.c_str()); @@ -1132,6 +1537,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1143,6 +1549,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1154,17 +1561,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(2, 2, Compare_Text(STR("Replace"))); + compare_text.replace(2, 7, Compare_Text(STR("Replace"))); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(2, 2, Text(STR("Replace"))); + text.replace(2, 7, Text(STR("Replace"))); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1176,6 +1585,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1187,6 +1597,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1202,6 +1613,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1213,17 +1625,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 4, Compare_Text(STR("Replace"))); + compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 9, Compare_Text(STR("Replace"))); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(text.begin() + 2, text.begin() + 4, Text(STR("Replace"))); + text.replace(text.begin() + 2, text.begin() + 9, Text(STR("Replace"))); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1235,6 +1649,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1250,6 +1665,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1261,6 +1677,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1272,6 +1689,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1283,17 +1701,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(2, 4, Compare_Text(STR("Replace")), 1, 5); + compare_text.replace(2, 7, Compare_Text(STR("Replace")), 1, 5); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(2, 4, Text(STR("Replace")), 1, 5); + text.replace(2, 7, Text(STR("Replace")), 1, 5); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1305,6 +1725,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1316,6 +1737,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1327,6 +1749,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1342,6 +1765,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1353,6 +1777,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1364,6 +1789,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1375,17 +1801,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(2, 4, Compare_Text(STR("Replace")).c_str()); + compare_text.replace(2, 7, Compare_Text(STR("Replace")).c_str()); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(2, 4, Text(STR("Replace")).c_str()); + text.replace(2, 7, Text(STR("Replace")).c_str()); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1397,6 +1825,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1408,6 +1837,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1419,6 +1849,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1434,6 +1865,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1445,17 +1877,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 4, Compare_Text(STR("Replace")).c_str()); + compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 9, Compare_Text(STR("Replace")).c_str()); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(text.begin() + 2, text.begin() + 4, Text(STR("Replace")).c_str()); + text.replace(text.begin() + 2, text.begin() + 9, Text(STR("Replace")).c_str()); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1467,6 +1901,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1482,6 +1917,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1493,6 +1929,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1504,6 +1941,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1515,17 +1953,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(2, 4, Compare_Text(STR("Replace")).c_str(), 5); + compare_text.replace(2, 7, Compare_Text(STR("Replace")).c_str(), 5); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(2, 4, Text(STR("Replace")).c_str(), 5); + text.replace(2, 7, Text(STR("Replace")).c_str(), 5); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1537,6 +1977,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1548,6 +1989,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1559,6 +2001,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1574,6 +2017,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1585,17 +2029,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 4, Compare_Text(STR("Replace")).c_str(), 5); + compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 9, Compare_Text(STR("Replace")).c_str(), 5); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(text.begin() + 2, text.begin() + 4, Text(STR("Replace")).c_str(), 5); + text.replace(text.begin() + 2, text.begin() + 9, Text(STR("Replace")).c_str(), 5); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1607,6 +2053,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1622,6 +2069,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1633,6 +2081,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1644,6 +2093,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1655,17 +2105,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(2, 4, 7, STR('A')); + compare_text.replace(2, 7, 7, STR('A')); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(2, 4, 7, STR('A')); + text.replace(2, 7, 7, STR('A')); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1677,6 +2129,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1688,6 +2141,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1699,6 +2153,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1714,6 +2169,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1725,17 +2181,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 4, 7, STR('A')); + compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 9, 7, STR('A')); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(text.begin() + 2, text.begin() + 4, 7, STR('A')); + text.replace(text.begin() + 2, text.begin() + 9, 7, STR('A')); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1747,6 +2205,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1765,6 +2224,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1776,17 +2236,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 4, replace.begin() + 1, replace.begin() + 5); + compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 7, replace.begin() + 1, replace.begin() + 5); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(text.begin() + 2, text.begin() + 4, replace_long.begin() + 1, replace_long.begin() + 5); + text.replace(text.begin() + 2, text.begin() + 7, replace_long.begin() + 1, replace_long.begin() + 5); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1798,6 +2260,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1811,6 +2274,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -1825,6 +2289,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -1834,6 +2299,7 @@ namespace text.clear(); CHECK_EQUAL(text.size(), size_t(0)); + CHECK(!text.truncated()); } //************************************************************************* @@ -1842,8 +2308,9 @@ namespace Compare_Text compare_text(initial_text.c_str()); Text text(initial_text.c_str()); - bool is_equal = Equal(compare_text, text); + bool is_equal = std::equal(text.begin(), text.end(), compare_text.begin()); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -1852,8 +2319,9 @@ namespace Compare_Text compare_text(initial_text.c_str()); Text text(initial_text.c_str()); - bool is_equal = Equal(compare_text, text); + bool is_equal = std::equal(text.cbegin(), text.cend(), compare_text.cbegin()); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -1862,8 +2330,9 @@ namespace Compare_Text compare_text(initial_text.c_str()); Text text(initial_text.c_str()); - bool is_equal = Equal(compare_text, text); + bool is_equal = std::equal(text.rbegin(), text.rend(), compare_text.rbegin()); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -1872,8 +2341,9 @@ namespace Compare_Text compare_text(initial_text.c_str()); Text text(initial_text.c_str()); - bool is_equal = Equal(compare_text, text); + bool is_equal = std::equal(text.crbegin(), text.crend(), compare_text.crbegin()); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -1933,16 +2403,16 @@ namespace // String-Pointer Pointer-String CHECK((less < pinitial_text) == (less_text < pinitial_text)); - CHECK((pinitial_text < less) == (pinitial_text < less_text)); + CHECK((pinitial_text < less) == (pinitial_text < less_text)); CHECK((greater < pinitial_text) == (greater_text < pinitial_text)); - CHECK((pinitial_text < greater) == (pinitial_text < greater_text)); + CHECK((pinitial_text < greater) == (pinitial_text < greater_text)); CHECK((shorter < pinitial_text) == (shorter_text < pinitial_text)); - CHECK((pinitial_text < shorter) == (pinitial_text < shorter_text)); + CHECK((pinitial_text < shorter) == (pinitial_text < shorter_text)); CHECK((initial < pinitial_text) == (initial_text < pinitial_text)); - CHECK((pinitial_text < initial) == (pinitial_text < initial_text)); + CHECK((pinitial_text < initial) == (pinitial_text < initial_text)); } //************************************************************************* @@ -1952,8 +2422,8 @@ namespace const Text initial(initial_text.c_str()); // String-String - CHECK((less <= initial) == (less_text <= initial_text)); - CHECK((initial <= less) == (initial_text <= less_text)); + CHECK((less <= initial) == (less_text <= initial_text)); + CHECK((initial <= less) == (initial_text <= less_text)); const Text greater(greater_text.c_str()); CHECK((greater <= initial) == (greater_text <= initial_text)); @@ -1967,17 +2437,17 @@ namespace CHECK((initial <= initial) == (initial_text <= initial_text)); // String-Pointer Pointer-String - CHECK((less <= pinitial_text) == (less_text <= pinitial_text)); - CHECK((pinitial_text <= less) == (pinitial_text <= less_text)); + CHECK((less <= pinitial_text) == (less_text <= pinitial_text)); + CHECK((pinitial_text <= less) == (pinitial_text <= less_text)); - CHECK((greater <= pinitial_text) == (greater_text <= pinitial_text)); - CHECK((pinitial_text <= greater) == (pinitial_text <= greater_text)); + CHECK((greater <= pinitial_text) == (greater_text <= pinitial_text)); + CHECK((pinitial_text <= greater) == (pinitial_text <= greater_text)); - CHECK((shorter <= pinitial_text) == (shorter_text <= pinitial_text)); - CHECK((pinitial_text <= shorter) == (pinitial_text <= shorter_text)); + CHECK((shorter <= pinitial_text) == (shorter_text <= pinitial_text)); + CHECK((pinitial_text <= shorter) == (pinitial_text <= shorter_text)); - CHECK((initial <= pinitial_text) == (initial_text <= pinitial_text)); - CHECK((pinitial_text <= initial) == (pinitial_text <= initial_text)); + CHECK((initial <= pinitial_text) == (initial_text <= pinitial_text)); + CHECK((pinitial_text <= initial) == (pinitial_text <= initial_text)); } //************************************************************************* @@ -2003,16 +2473,16 @@ namespace // String-Pointer Pointer-String CHECK((less > pinitial_text) == (less_text > pinitial_text)); - CHECK((pinitial_text > less) == (pinitial_text > less_text)); + CHECK((pinitial_text > less) == (pinitial_text > less_text)); CHECK((greater > pinitial_text) == (greater_text > pinitial_text)); - CHECK((pinitial_text > greater) == (pinitial_text > greater_text)); + CHECK((pinitial_text > greater) == (pinitial_text > greater_text)); CHECK((shorter > pinitial_text) == (shorter_text > pinitial_text)); - CHECK((pinitial_text > shorter) == (pinitial_text > shorter_text)); + CHECK((pinitial_text > shorter) == (pinitial_text > shorter_text)); CHECK((initial > pinitial_text) == (initial_text > pinitial_text)); - CHECK((pinitial_text > initial) == (pinitial_text > initial_text)); + CHECK((pinitial_text > initial) == (pinitial_text > initial_text)); } //************************************************************************* @@ -2022,8 +2492,8 @@ namespace const Text initial(initial_text.begin(), initial_text.end()); // String-String - CHECK((less >= initial) == (less_text >= initial_text)); - CHECK((initial >= less) == (initial_text >= less_text)); + CHECK((less >= initial) == (less_text >= initial_text)); + CHECK((initial >= less) == (initial_text >= less_text)); const Text greater(greater_text.begin(), greater_text.end()); CHECK((greater >= initial) == (greater_text >= initial_text)); @@ -2037,17 +2507,17 @@ namespace CHECK((initial >= initial) == (initial_text >= initial_text)); // String-Pointer Pointer-String - CHECK((less >= pinitial_text) == (less_text >= pinitial_text)); - CHECK((pinitial_text >= less) == (pinitial_text >= less_text)); + CHECK((less >= pinitial_text) == (less_text >= pinitial_text)); + CHECK((pinitial_text >= less) == (pinitial_text >= less_text)); - CHECK((greater >= pinitial_text) == (greater_text >= pinitial_text)); - CHECK((pinitial_text >= greater) == (pinitial_text >= greater_text)); + CHECK((greater >= pinitial_text) == (greater_text >= pinitial_text)); + CHECK((pinitial_text >= greater) == (pinitial_text >= greater_text)); - CHECK((shorter >= pinitial_text) == (shorter_text >= pinitial_text)); - CHECK((pinitial_text >= shorter) == (pinitial_text >= shorter_text)); + CHECK((shorter >= pinitial_text) == (shorter_text >= pinitial_text)); + CHECK((pinitial_text >= shorter) == (pinitial_text >= shorter_text)); - CHECK((initial >= pinitial_text) == (initial_text >= pinitial_text)); - CHECK((pinitial_text >= initial) == (pinitial_text >= initial_text)); + CHECK((initial >= pinitial_text) == (initial_text >= pinitial_text)); + CHECK((pinitial_text >= initial) == (pinitial_text >= initial_text)); } //************************************************************************* @@ -2062,21 +2532,22 @@ namespace size_t length1 = compare_text.copy(buffer1, 5, 2); buffer1[length1] = STR('\0'); - size_t length2 = compare_text.copy(buffer2, 5, 2); + size_t length2 = text.copy(buffer2, 5, 2); buffer2[length2] = STR('\0'); CHECK_EQUAL(length1, length2); + CHECK(!text.truncated()); bool is_equal = std::equal(buffer1, - buffer1 + length1, - buffer2); + buffer1 + length1, + buffer2); CHECK(is_equal); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_find_string) { - const value_t *the_haystack = STR("A haystack with a needle and another needle"); + const value_t* the_haystack = STR("A haystack with a needle and another needle"); std::u32string compare_needle(STR("needle")); etl::u32string<50> needle(STR("needle")); @@ -2096,7 +2567,7 @@ namespace CHECK_EQUAL(position1, position2); position2 = haystack.find(needle, position2 + 1); - CHECK_EQUAL(etl::iu32string::npos, position2); + CHECK_EQUAL(etl::u32string<50>::npos, position2); etl::u32string<50> pin(STR("pin")); position2 = haystack.find(pin); @@ -2104,9 +2575,9 @@ namespace } //************************************************************************* - TEST_FIXTURE(SetupFixture, test_find_char_pointer) + TEST_FIXTURE(SetupFixture, test_find_pointer) { - const value_t *the_haystack = STR("A haystack with a needle and another needle"); + const value_t* the_haystack = STR("A haystack with a needle and another needle"); const value_t* needle = STR("needle"); @@ -2135,7 +2606,7 @@ namespace //************************************************************************* TEST_FIXTURE(SetupFixture, test_find_char_pointer_n) { - const value_t *the_haystack = STR("A haystack with a needle and another needle"); + const value_t* the_haystack = STR("A haystack with a needle and another needle"); const value_t* needle = STR("needle"); @@ -2271,7 +2742,7 @@ namespace Compare_Text compare_result; Text result; - // Empty string. + // Equal. compare_result = compare_text.substr(compare_text.size()); result = text.substr(text.size()); CHECK(Equal(compare_result, result)); @@ -2622,8 +3093,8 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_first_of(STR('C'), 5); - position2 = text.find_first_of(STR('C'), 5); + position1 = compare_text.find_first_of(STR('C'), compare_text.size()); + position2 = text.find_first_of(STR('C'), text.size()); CHECK_EQUAL(position1, position2); @@ -2649,13 +3120,13 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(Compare_Text(STR("ZCXE")), 3); - position2 = text.find_last_of(Text(STR("ZCXE")), 3); + position1 = compare_text.find_last_of(Compare_Text(STR("ZCXE")), 5); + position2 = text.find_last_of(Text(STR("ZCXE")), 5); CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(Compare_Text(STR("ZCXE")), 10); - position2 = text.find_last_of(Text(STR("ZCXE")), 10); + position1 = compare_text.find_last_of(Compare_Text(STR("ZCXE")), compare_text.size()); + position2 = text.find_last_of(Text(STR("ZCXE")), text.size()); CHECK_EQUAL(position1, position2); @@ -2681,8 +3152,8 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(STR("ZCXE"), 3); - position2 = text.find_last_of(STR("ZCXE"), 3); + position1 = compare_text.find_last_of(STR("ZCXE"), 5); + position2 = text.find_last_of(STR("ZCXE"), 5); CHECK_EQUAL(position1, position2); @@ -2691,8 +3162,8 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(STR("ZCXE"), 10); - position2 = text.find_last_of(STR("ZCXE"), 10); + position1 = compare_text.find_last_of(STR("ZCXE"), compare_text.size()); + position2 = text.find_last_of(STR("ZCXE"), text.size()); CHECK_EQUAL(position1, position2); @@ -2708,8 +3179,8 @@ namespace Compare_Text compare_text(STR("ABCDEFABCDE")); Text text(STR("ABCDEFABCDE")); - size_t position1 = compare_text.find_last_of(STR("ZCXE"), 11, 4); - size_t position2 = text.find_last_of(STR("ZCXE"), 11, 4); + size_t position1 = compare_text.find_last_of(STR("AZCXE"), 0, 4); + size_t position2 = text.find_last_of(STR("AZCXE"), 0, 4); CHECK_EQUAL(position1, position2); @@ -2718,8 +3189,8 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(STR("ZCXE"), 4, 3); - position2 = text.find_last_of(STR("ZCXE"), 4, 3); + position1 = compare_text.find_last_of(STR("ZCXE"), 5, 3); + position2 = text.find_last_of(STR("ZCXE"), 5, 3); CHECK_EQUAL(position1, position2); @@ -2728,8 +3199,8 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(STR("ZCXE"), 10, 4); - position2 = text.find_last_of(STR("ZCXE"), 10, 4); + position1 = compare_text.find_last_of(STR("ZCXE"), compare_text.size(), 4); + position2 = text.find_last_of(STR("ZCXE"), text.size(), 4); CHECK_EQUAL(position1, position2); @@ -2755,8 +3226,8 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(STR('F'), 5); - position2 = text.find_last_of(STR('F'), 5); + position1 = compare_text.find_last_of(STR('F'), compare_text.size()); + position2 = text.find_last_of(STR('F'), text.size()); CHECK_EQUAL(position1, position2); @@ -2765,8 +3236,8 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(STR('C'), 5); - position2 = text.find_last_of(STR('C'), 5); + position1 = compare_text.find_last_of(STR('C'), compare_text.size()); + position2 = text.find_last_of(STR('C'), text.size()); CHECK_EQUAL(position1, position2); @@ -3062,6 +3533,7 @@ namespace CHECK_EQUAL(compare_hash, hash); } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_memcpy_repair) { Text text; @@ -3110,5 +3582,73 @@ namespace is_equal = Equal(text, itext); CHECK(!is_equal); } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_truncate_over_many_operations) + { + Text text(short_text.c_str()); + CHECK(!text.truncated()); + + text.insert(3, initial_text.c_str()); + CHECK(text.truncated()); + + while (text.size() != 0) + { + text.pop_back(); + CHECK(text.truncated()); + } + + text.clear(); + CHECK(!text.truncated()); + + text.assign(longer_text.c_str()); + CHECK(text.truncated()); + + text.assign(short_text.c_str()); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_add_from_truncated) + { + Text text1(short_text.c_str()); + TextS text2(short_text.c_str()); + + CHECK(!text1.truncated()); + CHECK(text2.truncated()); + + // text2 has the truncate flag set. + text1 += text2; + + CHECK(text1.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_add_to_truncated) + { + Text text1(longer_text.c_str()); + Text text2(short_text.c_str()); + + CHECK(text1.truncated()); + CHECK(!text2.truncated()); + + // Clear text but not the truncate flag. + text1.erase(text1.begin(), text1.end()); + + // text1 still has the truncate flag set. + text1 += text2; + + CHECK(text1.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_clear_truncated) + { + Text text(longer_text.c_str()); + CHECK(text.truncated()); + + text.clear_truncated(); + CHECK(!text.truncated()); + } }; } diff --git a/test/test_string_view.cpp b/test/test_string_view.cpp index 353f8031..7ae49af7 100644 --- a/test/test_string_view.cpp +++ b/test/test_string_view.cpp @@ -86,6 +86,18 @@ namespace CHECK(isEqual); } + //************************************************************************* + TEST(test_constructor_etl_string) + { + View view(etltext); + + CHECK_EQUAL(etltext.size(), view.size()); + CHECK_EQUAL(etltext.size(), view.max_size()); + + bool isEqual = std::equal(view.begin(), view.end(), etltext.begin()); + CHECK(isEqual); + } + //************************************************************************* TEST(test_constructor_pointer_size) { diff --git a/test/test_string_wchar_t.cpp b/test/test_string_wchar_t.cpp index 0c43fe33..0aba10d1 100644 --- a/test/test_string_wchar_t.cpp +++ b/test/test_string_wchar_t.cpp @@ -3,7 +3,7 @@ The MIT License(MIT) Embedded Template Library. https://github.com/ETLCPP/etl -http://www.etlcpp.com +https://www.etlcpp.com Copyright(c) 2016 jwellbelove @@ -55,6 +55,7 @@ namespace typedef std::wstring Compare_Text; typedef Text::value_type value_t; typedef etl::wstring<52> TextL; + typedef etl::wstring<4> TextS; Compare_Text initial_text; Compare_Text less_text; @@ -101,6 +102,7 @@ namespace CHECK_EQUAL(text.capacity(), SIZE); CHECK_EQUAL(text.max_size(), SIZE); CHECK(text.begin() == text.end()); + CHECK(!text.truncated()); } //************************************************************************* @@ -108,10 +110,11 @@ namespace { Text text; - CHECK(text.begin() == text.end()); - CHECK(text.cbegin() == text.cend()); - CHECK(text.rbegin() == text.rend()); + CHECK(text.begin() == text.end()); + CHECK(text.cbegin() == text.cend()); + CHECK(text.rbegin() == text.rend()); CHECK(text.crbegin() == text.crend()); + CHECK(!text.truncated()); } //************************************************************************* @@ -129,6 +132,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -137,6 +141,7 @@ namespace Text text(SIZE + 1, STR('A')); CHECK_EQUAL(SIZE, text.size()); + CHECK(text.truncated()); } //************************************************************************* @@ -150,6 +155,21 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_constructor_char_pointer_excess) + { + Compare_Text compare_text(initial_text.c_str()); + + Text text(longer_text.c_str()); + + CHECK(!text.empty()); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -163,6 +183,21 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_constructor_char_pointer_size_excess) + { + Compare_Text compare_text(SIZE, STR('A')); + + Text text(SIZE + 1, STR('A')); + + CHECK(!text.empty()); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -176,6 +211,21 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_constructor_size_char_excess) + { + Compare_Text compare_text(initial_text.c_str(), initial_text.size()); + + Text text(longer_text.c_str(), longer_text.size()); + + CHECK(!text.empty()); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -187,19 +237,19 @@ namespace CHECK(text.size() == SIZE); CHECK(!text.empty()); + CHECK(!text.truncated()); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_constructor_range_excess) { - Text text; - - text.assign(longer_text.begin(), longer_text.end()); + Text text(longer_text.begin(), longer_text.end()); bool is_equal = Equal(initial_text, text); CHECK(is_equal); CHECK(text.size() == SIZE); CHECK(!text.empty()); + CHECK(text.truncated()); } //************************************************************************* @@ -208,6 +258,35 @@ namespace Text text(initial_text.c_str()); Text text2(text); CHECK(text2 == text); + CHECK(!text2.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_copy_constructor_i) + { + Text text(initial_text.c_str()); + IText& itext = text; + Text text2(itext); + CHECK(text2 == text); + CHECK(!text2.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_copy_constructor_excess) + { + Text text(initial_text.c_str()); + TextL textl(longer_text.c_str()); + Text text2(textl); + CHECK(text2 == text); + CHECK(text2.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_copy_constructor_from_truncated) + { + Text text(longer_text.c_str()); + Text text2(text); + CHECK(text2.truncated()); } //************************************************************************* @@ -221,6 +300,21 @@ namespace bool is_equal = Equal(compare_text2, text2); CHECK(is_equal); + CHECK(!text2.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_construct_position_length_excess) + { + Compare_Text compare_text(longer_text.c_str()); + Compare_Text compare_text2(compare_text, 2, 11); + + TextL textl(longer_text.c_str()); + Text text2(textl, 2, 12); + + bool is_equal = Equal(compare_text2, text2); + CHECK(is_equal); + CHECK(text2.truncated()); } //************************************************************************* @@ -231,6 +325,21 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_construct_initializer_list_excess) + { + Compare_Text compare_text = { STR('H'), STR('e'), STR('l'), STR('l'), STR('o'), STR(' '), + STR('W'), STR('o'), STR('r'), STR('l'), STR('d') }; + Text text = { STR('H'), STR('e'), STR('l'), STR('l'), STR('o'), STR(' '), + STR('W'), STR('o'), STR('r'), STR('l'), STR('d'), STR(' '), + STR('T'), STR('h'), STR('e'), STR('r'), STR('e') }; + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -243,6 +352,22 @@ namespace bool is_equal = Equal(text, other_text); CHECK(is_equal); + CHECK(!text.truncated()); + CHECK(!other_text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_excess) + { + Text text(longer_text.begin(), longer_text.end()); + Text other_text; + + other_text = text; + + bool is_equal = Equal(text, other_text); + CHECK(is_equal); + CHECK(text.truncated()); + CHECK(other_text.truncated()); } //************************************************************************* @@ -259,8 +384,27 @@ namespace bool is_equal = Equal(text1, text2); CHECK(is_equal); + CHECK(!text1.truncated()); + CHECK(!text2.truncated()); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_iterface_excess) + { + Text text1(longer_text.begin(), longer_text.end()); + Text text2; + + IText& itext1 = text1; + IText& itext2 = text2; + + itext2 = itext1; + + bool is_equal = Equal(text1, text2); + + CHECK(is_equal); + CHECK(text1.truncated()); + CHECK(text2.truncated()); + } //************************************************************************* TEST_FIXTURE(SetupFixture, test_self_assignment) @@ -273,6 +417,73 @@ namespace bool is_equal = Equal(text, other_text); CHECK(is_equal); + CHECK(!text.truncated()); + CHECK(!other_text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_self_assignment_excess) + { + Text text(longer_text.begin(), longer_text.end()); + Text other_text(text); + + other_text = other_text; + + bool is_equal = Equal(text, other_text); + + CHECK(is_equal); + CHECK(text.truncated()); + CHECK(other_text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_from_literal) + { + Text text; + + text = STR("Hello World"); + + bool is_equal = Equal(std::wstring(STR("Hello World")), text); + CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_from_literal_excess) + { + Text text; + + text = STR("Hello World There"); + + bool is_equal = Equal(std::wstring(STR("Hello World")), text); + CHECK(is_equal); + CHECK(text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_from_literal_via_interface) + { + Text text; + IText& itext = text; + + itext = STR("Hello World"); + + bool is_equal = Equal(std::wstring(STR("Hello World")), itext); + CHECK(is_equal); + CHECK(!itext.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_from_literal_via_interface_excess) + { + Text text; + IText& itext = text; + + itext = STR("Hello World There"); + + bool is_equal = Equal(std::wstring(STR("Hello World")), itext); + CHECK(is_equal); + CHECK(itext.truncated()); } //************************************************************************* @@ -281,7 +492,7 @@ namespace Text text(initial_text.c_str()); const Text constText(initial_text.c_str()); - CHECK_EQUAL(&text[0], text.begin()); + CHECK_EQUAL(&text[0], text.begin()); CHECK_EQUAL(&constText[0], constText.begin()); } @@ -292,7 +503,7 @@ namespace Text text(initial_text.c_str()); const Text constText(initial_text.c_str()); - CHECK_EQUAL(&text[initial_text.size()], text.end()); + CHECK_EQUAL(&text[initial_text.size()], text.end()); CHECK_EQUAL(&constText[initial_text.size()], constText.end()); } @@ -306,6 +517,7 @@ namespace text.resize(NEW_SIZE); CHECK_EQUAL(text.size(), NEW_SIZE); + CHECK(!text.truncated()); } //************************************************************************* @@ -322,8 +534,8 @@ namespace compare_text.fill(INITIAL_VALUE); bool is_equal = Equal(compare_text, text); - CHECK(is_equal); + CHECK(!text.truncated()); } @@ -336,6 +548,7 @@ namespace Text text(INITIAL_SIZE, STR('A')); text.resize(NEW_SIZE, STR('A')); CHECK_EQUAL(SIZE, text.size()); + CHECK(text.truncated()); } //************************************************************************* @@ -348,6 +561,7 @@ namespace text.resize(NEW_SIZE); CHECK_EQUAL(text.size(), NEW_SIZE); + CHECK(!text.truncated()); } //************************************************************************* @@ -367,26 +581,66 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* - TEST_FIXTURE(SetupFixture, test_empty) + TEST_FIXTURE(SetupFixture, test_empty_full) + { + Text text; + text.resize(text.max_size(), STR('A')); + + CHECK(!text.empty()); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_empty_half) + { + Text text; + text.resize(text.max_size() / 2, STR('A')); + + CHECK(!text.empty()); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_empty_empty) + { + Text text; + + CHECK(text.empty()); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_full_full) { Text text; text.resize(text.max_size(), STR('A')); CHECK(text.full()); - CHECK(!text.empty()); + CHECK(!text.truncated()); } //************************************************************************* - TEST_FIXTURE(SetupFixture, test_full) + TEST_FIXTURE(SetupFixture, test_full_half) + { + Text text; + text.resize(text.max_size() / 2, STR('A')); + + CHECK(!text.full()); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_full_empty) { Text text; CHECK(!text.full()); - CHECK(text.empty()); + CHECK(!text.truncated()); } //************************************************************************* @@ -399,6 +653,8 @@ namespace { CHECK_EQUAL(text[i], compare_text[i]); } + + CHECK(!text.truncated()); } //************************************************************************* @@ -411,6 +667,8 @@ namespace { CHECK_EQUAL(text[i], compare_text[i]); } + + CHECK(!text.truncated()); } //************************************************************************* @@ -424,6 +682,8 @@ namespace CHECK_EQUAL(text.at(i), compare_text.at(i)); } + CHECK(!text.truncated()); + CHECK_THROW(text.at(text.size()), etl::string_out_of_bounds); } @@ -438,6 +698,8 @@ namespace CHECK_EQUAL(text.at(i), compare_text.at(i)); } + CHECK(!text.truncated()); + CHECK_THROW(text.at(text.size()), etl::string_out_of_bounds); } @@ -448,6 +710,7 @@ namespace Text text(initial_text.c_str()); CHECK(text.front() == compare_text.front()); + CHECK(!text.truncated()); } //************************************************************************* @@ -457,6 +720,7 @@ namespace const Text text(initial_text.c_str()); CHECK(text.front() == compare_text.front()); + CHECK(!text.truncated()); } //************************************************************************* @@ -466,6 +730,7 @@ namespace Text text(initial_text.c_str()); CHECK(text.back() == compare_text.back()); + CHECK(!text.truncated()); } //************************************************************************* @@ -475,6 +740,7 @@ namespace const Text text(initial_text.c_str()); CHECK(text.back() == compare_text.back()); + CHECK(!text.truncated()); } //************************************************************************* @@ -489,6 +755,7 @@ namespace compare_text.begin()); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -503,6 +770,41 @@ namespace compare_text.begin()); CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assign_string) + { + Compare_Text compare_input(initial_text.c_str()); + Text input(initial_text.c_str()); + + Compare_Text compare_text; + Text text; + + compare_text.assign(compare_input); + text.assign(input); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assign_string_excess) + { + Compare_Text compare_input(initial_text.c_str()); + TextL input(longer_text.c_str()); + + Compare_Text compare_text; + Text text; + + compare_text.assign(compare_input); + text.assign(input); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -511,20 +813,24 @@ namespace Compare_Text compare_text(initial_text.c_str()); Text text; - - text.assign(compare_text.c_str()); + text.assign(initial_text.c_str()); bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); + } - compare_text.assign(longer_text.c_str()); - compare_text.resize(std::min(compare_text.size(), SIZE)); + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assign_pointer_excess) + { + Compare_Text compare_text(initial_text.c_str()); - text.assign(compare_text.c_str()); - - is_equal = Equal(compare_text, text); + Text text; + text.assign(longer_text.c_str()); + bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -537,8 +843,8 @@ namespace text.assign(compare_text.begin(), compare_text.end()); bool is_equal = Equal(compare_text, text); - CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -550,11 +856,9 @@ namespace CHECK_EQUAL(initial_text.size(), text.size()); - bool is_equal = std::equal(text.begin(), - text.end(), - initial_text.begin()); - + bool is_equal = Equal(initial_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -572,8 +876,8 @@ namespace CHECK(text.size() == INITIAL_SIZE); bool is_equal = Equal(compare_text, text); - CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -590,10 +894,9 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } - - //************************************************************************* TEST_FIXTURE(SetupFixture, test_push_back) { @@ -610,17 +913,25 @@ namespace text.push_back(STR('A') + value_t(i)); } + CHECK_EQUAL(etl::strlen(compare_text.data()), etl::strlen(text.data())); CHECK_EQUAL(compare_text.size(), text.size()); bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_push_back_excess) { + Compare_Text compare_text; Text text; + for (size_t i = 0; i < SIZE; ++i) + { + compare_text.push_back(STR('A') + value_t(i)); + } + for (size_t i = 0; i < SIZE; ++i) { text.push_back(STR('A') + value_t(i)); @@ -629,8 +940,12 @@ namespace text.push_back(STR('A') + value_t(SIZE)); + CHECK_EQUAL(etl::strlen(compare_text.data()), etl::strlen(text.data())); + CHECK_EQUAL(compare_text.size(), text.size()); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); CHECK(text.truncated()); - CHECK_EQUAL(SIZE, text.size()); } //************************************************************************* @@ -656,7 +971,7 @@ namespace TEST_FIXTURE(SetupFixture, test_insert_position_value) { const size_t INITIAL_SIZE = 5; - const value_t INITIAL_VALUE = STR('A'); + const value_t INITIAL_VALUE = STR('A'); for (size_t offset = 0; offset <= INITIAL_SIZE; ++offset) { @@ -719,15 +1034,15 @@ namespace //************************************************************************* TEST_FIXTURE(SetupFixture, test_insert_position_n_value) { - const size_t INITIAL_SIZE = 5; - const size_t INSERT_SIZE = 3; - const value_t INITIAL_VALUE = STR('A'); + Compare_Text compare_text; + Text text; + + const size_t INITIAL_SIZE = 5; + const size_t INSERT_SIZE = 3; + const value_t INITIAL_VALUE = STR('A'); for (size_t offset = 0; offset <= INITIAL_SIZE; ++offset) { - Compare_Text compare_text; - Text text; - text.assign(initial_text.begin(), initial_text.begin() + INITIAL_SIZE); compare_text.assign(initial_text.begin(), initial_text.begin() + INITIAL_SIZE); text.insert(text.begin() + offset, INSERT_SIZE, INITIAL_VALUE); @@ -747,7 +1062,7 @@ namespace Text text; const size_t INSERT_SIZE = 4; - const value_t INSERT_VALUE = STR('A'); + const value_t INSERT_VALUE = STR('A'); size_t offset = 0; compare_text.assign(initial_text.begin(), initial_text.end()); @@ -795,7 +1110,7 @@ namespace text.assign(initial_text.begin(), initial_text.end()); text.insert(text.begin() + offset, INSERT_SIZE, INSERT_VALUE); - CHECK(!text.truncated()); + CHECK(text.truncated()); is_equal = Equal(compare_text, text); CHECK(is_equal); @@ -827,7 +1142,7 @@ namespace TEST_FIXTURE(SetupFixture, test_insert_position_range_excess) { const size_t INITIAL_SIZE = 5; - const value_t INITIAL_VALUE = STR('A'); + const value_t INITIAL_VALUE = STR('A'); Compare_Text compare_text; Text text; @@ -889,6 +1204,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } } @@ -907,6 +1223,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } } @@ -925,6 +1242,28 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); + } + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_insert_size_t_position_string_from_truncated) + { + for (size_t offset = 0; offset <= short_text.size(); ++offset) + { + Compare_Text compare_text(short_text.begin(), short_text.end()); + Text text(short_text.begin(), short_text.end()); + Text insert(longer_text.begin(), longer_text.end()); + insert.erase(insert.begin(), insert.end()); + insert.append(insert_text.begin(), insert_text.end()); + + text.insert(offset, insert); + compare_text.insert(offset, insert_text); + compare_text.resize(std::min(compare_text.size(), SIZE)); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } } @@ -941,6 +1280,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); compare_text.assign(short_text.begin(), short_text.end()); text.assign(short_text.begin(), short_text.end()); @@ -953,6 +1293,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); compare_text.assign(short_text.begin(), short_text.end()); text.assign(short_text.begin(), short_text.end()); @@ -963,6 +1304,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -978,6 +1320,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(short_text.c_str()); @@ -990,6 +1333,45 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_append_truncated_string) + { + Text text(short_text.c_str()); + TextS append(short_text.c_str()); + CHECK(append.truncated()); + + text.append(append); + CHECK(text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_append_string_to_self) + { + Compare_Text compare_text(short_text.c_str()); + Text text(short_text.c_str()); + + // Non-overflow. + compare_text.append(compare_text); + text.append(text); + + bool is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(!text.truncated()); + + // Overflow. + compare_text.assign(shorter_text.c_str()); + text.assign(shorter_text.c_str()); + + compare_text.append(compare_text); + compare_text.resize(std::min(compare_text.size(), SIZE)); + text.append(text); + + is_equal = Equal(compare_text, text); + CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1001,10 +1383,11 @@ namespace // Whole string. compare_text.append(insert_text, 0, std::wstring::npos); - text.append(append, 0, etl::iwstring::npos); + text.append(append, 0, Text::npos); bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Partial string. compare_text.assign(short_text.c_str()); @@ -1017,6 +1400,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(short_text.c_str()); @@ -1029,6 +1413,18 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_append_truncated_string_subpos_sublen) + { + Text text(short_text.c_str()); + TextS append(short_text.c_str()); + CHECK(append.truncated()); + + text.append(append, 1, 2); + CHECK(text.truncated()); } //************************************************************************* @@ -1044,6 +1440,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(short_text.c_str()); @@ -1055,6 +1452,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1070,6 +1468,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(short_text.c_str()); @@ -1081,6 +1480,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1096,6 +1496,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(short_text.c_str()); @@ -1108,6 +1509,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1123,6 +1525,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow short text. compare_text.assign(short_text.c_str()); @@ -1134,6 +1537,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1145,6 +1549,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1156,17 +1561,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(2, 2, Compare_Text(STR("Replace"))); + compare_text.replace(2, 7, Compare_Text(STR("Replace"))); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(2, 2, Text(STR("Replace"))); + text.replace(2, 7, Text(STR("Replace"))); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1178,6 +1585,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1189,6 +1597,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1204,6 +1613,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1215,17 +1625,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 4, Compare_Text(STR("Replace"))); + compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 9, Compare_Text(STR("Replace"))); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(text.begin() + 2, text.begin() + 4, Text(STR("Replace"))); + text.replace(text.begin() + 2, text.begin() + 9, Text(STR("Replace"))); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1237,6 +1649,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1252,6 +1665,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1263,6 +1677,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1274,6 +1689,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1285,17 +1701,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(2, 4, Compare_Text(STR("Replace")), 1, 5); + compare_text.replace(2, 7, Compare_Text(STR("Replace")), 1, 5); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(2, 4, Text(STR("Replace")), 1, 5); + text.replace(2, 7, Text(STR("Replace")), 1, 5); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1307,6 +1725,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1318,6 +1737,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1329,6 +1749,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1344,6 +1765,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1355,6 +1777,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1366,6 +1789,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1377,17 +1801,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(2, 4, Compare_Text(STR("Replace")).c_str()); + compare_text.replace(2, 7, Compare_Text(STR("Replace")).c_str()); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(2, 4, Text(STR("Replace")).c_str()); + text.replace(2, 7, Text(STR("Replace")).c_str()); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1399,6 +1825,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1410,6 +1837,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1421,6 +1849,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1436,6 +1865,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1447,17 +1877,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 4, Compare_Text(STR("Replace")).c_str()); + compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 9, Compare_Text(STR("Replace")).c_str()); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(text.begin() + 2, text.begin() + 4, Text(STR("Replace")).c_str()); + text.replace(text.begin() + 2, text.begin() + 9, Text(STR("Replace")).c_str()); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1469,6 +1901,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1484,6 +1917,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1495,6 +1929,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1506,6 +1941,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1517,17 +1953,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(2, 4, Compare_Text(STR("Replace")).c_str(), 5); + compare_text.replace(2, 7, Compare_Text(STR("Replace")).c_str(), 5); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(2, 4, Text(STR("Replace")).c_str(), 5); + text.replace(2, 7, Text(STR("Replace")).c_str(), 5); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1539,6 +1977,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1550,6 +1989,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1561,6 +2001,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1576,6 +2017,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1587,17 +2029,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 4, Compare_Text(STR("Replace")).c_str(), 5); + compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 9, Compare_Text(STR("Replace")).c_str(), 5); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(text.begin() + 2, text.begin() + 4, Text(STR("Replace")).c_str(), 5); + text.replace(text.begin() + 2, text.begin() + 9, Text(STR("Replace")).c_str(), 5); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1609,6 +2053,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1624,6 +2069,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1635,6 +2081,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1646,6 +2093,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow short text, npos. compare_text.assign(short_text.c_str()); @@ -1657,17 +2105,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(2, 4, 7, STR('A')); + compare_text.replace(2, 7, 7, STR('A')); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(2, 4, 7, STR('A')); + text.replace(2, 7, 7, STR('A')); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Non-overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1679,6 +2129,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1690,6 +2141,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Overflow, npos. compare_text.assign(initial_text.c_str()); @@ -1701,6 +2153,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1716,6 +2169,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1727,17 +2181,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 4, 7, STR('A')); + compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 9, 7, STR('A')); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(text.begin() + 2, text.begin() + 4, 7, STR('A')); + text.replace(text.begin() + 2, text.begin() + 9, 7, STR('A')); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1749,6 +2205,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1767,6 +2224,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow short text. compare_text.assign(short_text.c_str()); @@ -1778,17 +2236,19 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); // Non-overflow. compare_text.assign(initial_text.c_str()); text.assign(initial_text.c_str()); - compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 4, replace.begin() + 1, replace.begin() + 5); + compare_text.replace(compare_text.begin() + 2, compare_text.begin() + 7, replace.begin() + 1, replace.begin() + 5); compare_text.resize(std::min(compare_text.size(), SIZE)); - text.replace(text.begin() + 2, text.begin() + 4, replace_long.begin() + 1, replace_long.begin() + 5); + text.replace(text.begin() + 2, text.begin() + 7, replace_long.begin() + 1, replace_long.begin() + 5); is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); // Overflow. compare_text.assign(initial_text.c_str()); @@ -1800,6 +2260,7 @@ namespace is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(text.truncated()); } //************************************************************************* @@ -1813,6 +2274,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -1827,6 +2289,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -1836,6 +2299,7 @@ namespace text.clear(); CHECK_EQUAL(text.size(), size_t(0)); + CHECK(!text.truncated()); } //************************************************************************* @@ -1844,8 +2308,9 @@ namespace Compare_Text compare_text(initial_text.c_str()); Text text(initial_text.c_str()); - bool is_equal = Equal(compare_text, text); + bool is_equal = std::equal(text.begin(), text.end(), compare_text.begin()); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -1854,8 +2319,9 @@ namespace Compare_Text compare_text(initial_text.c_str()); Text text(initial_text.c_str()); - bool is_equal = Equal(compare_text, text); + bool is_equal = std::equal(text.cbegin(), text.cend(), compare_text.cbegin()); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -1864,8 +2330,9 @@ namespace Compare_Text compare_text(initial_text.c_str()); Text text(initial_text.c_str()); - bool is_equal = Equal(compare_text, text); + bool is_equal = std::equal(text.rbegin(), text.rend(), compare_text.rbegin()); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -1874,8 +2341,9 @@ namespace Compare_Text compare_text(initial_text.c_str()); Text text(initial_text.c_str()); - bool is_equal = Equal(compare_text, text); + bool is_equal = std::equal(text.crbegin(), text.crend(), compare_text.crbegin()); CHECK(is_equal); + CHECK(!text.truncated()); } //************************************************************************* @@ -1935,16 +2403,16 @@ namespace // String-Pointer Pointer-String CHECK((less < pinitial_text) == (less_text < pinitial_text)); - CHECK((pinitial_text < less) == (pinitial_text < less_text)); + CHECK((pinitial_text < less) == (pinitial_text < less_text)); CHECK((greater < pinitial_text) == (greater_text < pinitial_text)); - CHECK((pinitial_text < greater) == (pinitial_text < greater_text)); + CHECK((pinitial_text < greater) == (pinitial_text < greater_text)); CHECK((shorter < pinitial_text) == (shorter_text < pinitial_text)); - CHECK((pinitial_text < shorter) == (pinitial_text < shorter_text)); + CHECK((pinitial_text < shorter) == (pinitial_text < shorter_text)); CHECK((initial < pinitial_text) == (initial_text < pinitial_text)); - CHECK((pinitial_text < initial) == (pinitial_text < initial_text)); + CHECK((pinitial_text < initial) == (pinitial_text < initial_text)); } //************************************************************************* @@ -1954,8 +2422,8 @@ namespace const Text initial(initial_text.c_str()); // String-String - CHECK((less <= initial) == (less_text <= initial_text)); - CHECK((initial <= less) == (initial_text <= less_text)); + CHECK((less <= initial) == (less_text <= initial_text)); + CHECK((initial <= less) == (initial_text <= less_text)); const Text greater(greater_text.c_str()); CHECK((greater <= initial) == (greater_text <= initial_text)); @@ -1969,17 +2437,17 @@ namespace CHECK((initial <= initial) == (initial_text <= initial_text)); // String-Pointer Pointer-String - CHECK((less <= pinitial_text) == (less_text <= pinitial_text)); - CHECK((pinitial_text <= less) == (pinitial_text <= less_text)); + CHECK((less <= pinitial_text) == (less_text <= pinitial_text)); + CHECK((pinitial_text <= less) == (pinitial_text <= less_text)); - CHECK((greater <= pinitial_text) == (greater_text <= pinitial_text)); - CHECK((pinitial_text <= greater) == (pinitial_text <= greater_text)); + CHECK((greater <= pinitial_text) == (greater_text <= pinitial_text)); + CHECK((pinitial_text <= greater) == (pinitial_text <= greater_text)); - CHECK((shorter <= pinitial_text) == (shorter_text <= pinitial_text)); - CHECK((pinitial_text <= shorter) == (pinitial_text <= shorter_text)); + CHECK((shorter <= pinitial_text) == (shorter_text <= pinitial_text)); + CHECK((pinitial_text <= shorter) == (pinitial_text <= shorter_text)); - CHECK((initial <= pinitial_text) == (initial_text <= pinitial_text)); - CHECK((pinitial_text <= initial) == (pinitial_text <= initial_text)); + CHECK((initial <= pinitial_text) == (initial_text <= pinitial_text)); + CHECK((pinitial_text <= initial) == (pinitial_text <= initial_text)); } //************************************************************************* @@ -2005,16 +2473,16 @@ namespace // String-Pointer Pointer-String CHECK((less > pinitial_text) == (less_text > pinitial_text)); - CHECK((pinitial_text > less) == (pinitial_text > less_text)); + CHECK((pinitial_text > less) == (pinitial_text > less_text)); CHECK((greater > pinitial_text) == (greater_text > pinitial_text)); - CHECK((pinitial_text > greater) == (pinitial_text > greater_text)); + CHECK((pinitial_text > greater) == (pinitial_text > greater_text)); CHECK((shorter > pinitial_text) == (shorter_text > pinitial_text)); - CHECK((pinitial_text > shorter) == (pinitial_text > shorter_text)); + CHECK((pinitial_text > shorter) == (pinitial_text > shorter_text)); CHECK((initial > pinitial_text) == (initial_text > pinitial_text)); - CHECK((pinitial_text > initial) == (pinitial_text > initial_text)); + CHECK((pinitial_text > initial) == (pinitial_text > initial_text)); } //************************************************************************* @@ -2024,8 +2492,8 @@ namespace const Text initial(initial_text.begin(), initial_text.end()); // String-String - CHECK((less >= initial) == (less_text >= initial_text)); - CHECK((initial >= less) == (initial_text >= less_text)); + CHECK((less >= initial) == (less_text >= initial_text)); + CHECK((initial >= less) == (initial_text >= less_text)); const Text greater(greater_text.begin(), greater_text.end()); CHECK((greater >= initial) == (greater_text >= initial_text)); @@ -2039,17 +2507,17 @@ namespace CHECK((initial >= initial) == (initial_text >= initial_text)); // String-Pointer Pointer-String - CHECK((less >= pinitial_text) == (less_text >= pinitial_text)); - CHECK((pinitial_text >= less) == (pinitial_text >= less_text)); + CHECK((less >= pinitial_text) == (less_text >= pinitial_text)); + CHECK((pinitial_text >= less) == (pinitial_text >= less_text)); - CHECK((greater >= pinitial_text) == (greater_text >= pinitial_text)); - CHECK((pinitial_text >= greater) == (pinitial_text >= greater_text)); + CHECK((greater >= pinitial_text) == (greater_text >= pinitial_text)); + CHECK((pinitial_text >= greater) == (pinitial_text >= greater_text)); - CHECK((shorter >= pinitial_text) == (shorter_text >= pinitial_text)); - CHECK((pinitial_text >= shorter) == (pinitial_text >= shorter_text)); + CHECK((shorter >= pinitial_text) == (shorter_text >= pinitial_text)); + CHECK((pinitial_text >= shorter) == (pinitial_text >= shorter_text)); - CHECK((initial >= pinitial_text) == (initial_text >= pinitial_text)); - CHECK((pinitial_text >= initial) == (pinitial_text >= initial_text)); + CHECK((initial >= pinitial_text) == (initial_text >= pinitial_text)); + CHECK((pinitial_text >= initial) == (pinitial_text >= initial_text)); } //************************************************************************* @@ -2064,21 +2532,22 @@ namespace size_t length1 = compare_text.copy(buffer1, 5, 2); buffer1[length1] = STR('\0'); - size_t length2 = compare_text.copy(buffer2, 5, 2); + size_t length2 = text.copy(buffer2, 5, 2); buffer2[length2] = STR('\0'); CHECK_EQUAL(length1, length2); + CHECK(!text.truncated()); bool is_equal = std::equal(buffer1, - buffer1 + length1, - buffer2); + buffer1 + length1, + buffer2); CHECK(is_equal); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_find_string) { - const value_t *the_haystack = STR("A haystack with a needle and another needle"); + const value_t* the_haystack = STR("A haystack with a needle and another needle"); std::wstring compare_needle(STR("needle")); etl::wstring<50> needle(STR("needle")); @@ -2098,7 +2567,7 @@ namespace CHECK_EQUAL(position1, position2); position2 = haystack.find(needle, position2 + 1); - CHECK_EQUAL(etl::iwstring::npos, position2); + CHECK_EQUAL(etl::wstring<50>::npos, position2); etl::wstring<50> pin(STR("pin")); position2 = haystack.find(pin); @@ -2106,9 +2575,9 @@ namespace } //************************************************************************* - TEST_FIXTURE(SetupFixture, test_find_char_pointer) + TEST_FIXTURE(SetupFixture, test_find_pointer) { - const value_t *the_haystack = STR("A haystack with a needle and another needle"); + const value_t* the_haystack = STR("A haystack with a needle and another needle"); const value_t* needle = STR("needle"); @@ -2137,7 +2606,7 @@ namespace //************************************************************************* TEST_FIXTURE(SetupFixture, test_find_char_pointer_n) { - const value_t *the_haystack = STR("A haystack with a needle and another needle"); + const value_t* the_haystack = STR("A haystack with a needle and another needle"); const value_t* needle = STR("needle"); @@ -2273,7 +2742,7 @@ namespace Compare_Text compare_result; Text result; - // Empty string. + // Equal. compare_result = compare_text.substr(compare_text.size()); result = text.substr(text.size()); CHECK(Equal(compare_result, result)); @@ -2624,8 +3093,8 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_first_of(STR('C'), 5); - position2 = text.find_first_of(STR('C'), 5); + position1 = compare_text.find_first_of(STR('C'), compare_text.size()); + position2 = text.find_first_of(STR('C'), text.size()); CHECK_EQUAL(position1, position2); @@ -2651,13 +3120,13 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(Compare_Text(STR("ZCXE")), 3); - position2 = text.find_last_of(Text(STR("ZCXE")), 3); + position1 = compare_text.find_last_of(Compare_Text(STR("ZCXE")), 5); + position2 = text.find_last_of(Text(STR("ZCXE")), 5); CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(Compare_Text(STR("ZCXE")), 10); - position2 = text.find_last_of(Text(STR("ZCXE")), 10); + position1 = compare_text.find_last_of(Compare_Text(STR("ZCXE")), compare_text.size()); + position2 = text.find_last_of(Text(STR("ZCXE")), text.size()); CHECK_EQUAL(position1, position2); @@ -2683,8 +3152,8 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(STR("ZCXE"), 3); - position2 = text.find_last_of(STR("ZCXE"), 3); + position1 = compare_text.find_last_of(STR("ZCXE"), 5); + position2 = text.find_last_of(STR("ZCXE"), 5); CHECK_EQUAL(position1, position2); @@ -2693,8 +3162,8 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(STR("ZCXE"), 10); - position2 = text.find_last_of(STR("ZCXE"), 10); + position1 = compare_text.find_last_of(STR("ZCXE"), compare_text.size()); + position2 = text.find_last_of(STR("ZCXE"), text.size()); CHECK_EQUAL(position1, position2); @@ -2710,8 +3179,8 @@ namespace Compare_Text compare_text(STR("ABCDEFABCDE")); Text text(STR("ABCDEFABCDE")); - size_t position1 = compare_text.find_last_of(STR("ZCXE"), 11, 4); - size_t position2 = text.find_last_of(STR("ZCXE"), 11, 4); + size_t position1 = compare_text.find_last_of(STR("AZCXE"), 0, 4); + size_t position2 = text.find_last_of(STR("AZCXE"), 0, 4); CHECK_EQUAL(position1, position2); @@ -2720,8 +3189,8 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(STR("ZCXE"), 4, 3); - position2 = text.find_last_of(STR("ZCXE"), 4, 3); + position1 = compare_text.find_last_of(STR("ZCXE"), 5, 3); + position2 = text.find_last_of(STR("ZCXE"), 5, 3); CHECK_EQUAL(position1, position2); @@ -2730,8 +3199,8 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(STR("ZCXE"), 10, 4); - position2 = text.find_last_of(STR("ZCXE"), 10, 4); + position1 = compare_text.find_last_of(STR("ZCXE"), compare_text.size(), 4); + position2 = text.find_last_of(STR("ZCXE"), text.size(), 4); CHECK_EQUAL(position1, position2); @@ -2757,8 +3226,8 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(STR('F'), 5); - position2 = text.find_last_of(STR('F'), 5); + position1 = compare_text.find_last_of(STR('F'), compare_text.size()); + position2 = text.find_last_of(STR('F'), text.size()); CHECK_EQUAL(position1, position2); @@ -2767,8 +3236,8 @@ namespace CHECK_EQUAL(position1, position2); - position1 = compare_text.find_last_of(STR('C'), 5); - position2 = text.find_last_of(STR('C'), 5); + position1 = compare_text.find_last_of(STR('C'), compare_text.size()); + position2 = text.find_last_of(STR('C'), text.size()); CHECK_EQUAL(position1, position2); @@ -3113,5 +3582,73 @@ namespace is_equal = Equal(text, itext); CHECK(!is_equal); } - }; + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_truncate_over_many_operations) + { + Text text(short_text.c_str()); + CHECK(!text.truncated()); + + text.insert(3, initial_text.c_str()); + CHECK(text.truncated()); + + while (text.size() != 0) + { + text.pop_back(); + CHECK(text.truncated()); + } + + text.clear(); + CHECK(!text.truncated()); + + text.assign(longer_text.c_str()); + CHECK(text.truncated()); + + text.assign(short_text.c_str()); + CHECK(!text.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_add_from_truncated) + { + Text text1(short_text.c_str()); + TextS text2(short_text.c_str()); + + CHECK(!text1.truncated()); + CHECK(text2.truncated()); + + // text2 has the truncate flag set. + text1 += text2; + + CHECK(text1.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_add_to_truncated) + { + Text text1(longer_text.c_str()); + Text text2(short_text.c_str()); + + CHECK(text1.truncated()); + CHECK(!text2.truncated()); + + // Clear text but not the truncate flag. + text1.erase(text1.begin(), text1.end()); + + // text1 still has the truncate flag set. + text1 += text2; + + CHECK(text1.truncated()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_clear_truncated) + { + Text text(longer_text.c_str()); + CHECK(text.truncated()); + + text.clear_truncated(); + CHECK(!text.truncated()); + } +}; } diff --git a/test/test_vector.cpp b/test/test_vector.cpp index a2dbb213..cb7ab2d1 100644 --- a/test/test_vector.cpp +++ b/test/test_vector.cpp @@ -163,6 +163,16 @@ namespace CHECK(data2 != data); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_move_constructor) + { + Data data(initial_data.begin(), initial_data.end()); + Data data2(std::move(data)); + CHECK(data.size() == 0); + CHECK(data2.size() == initial_data.size()); + CHECK(data2 != data); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_assignment) { @@ -178,6 +188,19 @@ namespace CHECK(is_equal); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_move_assignment) + { + Data data(initial_data.begin(), initial_data.end()); + Data other_data; + + other_data = std::move(data); + + CHECK(data.size() == 0); + CHECK(other_data.size() == initial_data.size()); + CHECK(data != other_data); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_assignment_iterface) { diff --git a/test/test_vector_external_buffer.cpp b/test/test_vector_external_buffer.cpp new file mode 100644 index 00000000..97f6fff3 --- /dev/null +++ b/test/test_vector_external_buffer.cpp @@ -0,0 +1,1128 @@ +/****************************************************************************** +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 +#include +#include + +#include "etl/vector.h" + +namespace +{ + static const size_t SIZE = 10; + + int buffer1[SIZE]; + int buffer2[SIZE]; + int buffer3[SIZE]; + int buffer4[SIZE]; + int buffer5[SIZE]; + + SUITE(test_vector) + { + typedef etl::vector Data; + typedef etl::ivector IData; + typedef std::vector Compare_Data; + + Compare_Data initial_data; + Compare_Data less_data; + Compare_Data greater_data; + Compare_Data shorter_data; + Compare_Data different_data; + Compare_Data insert_data; + + //************************************************************************* + struct SetupFixture + { + SetupFixture() + { + int n[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int n_insert[] = { 11, 12, 13 }; + int n_less[] = { 0, 1, 2, 3, 3, 5, 6, 7, 8, 9 }; + int n_greater[] = { 0, 1, 2, 4, 4, 5, 6, 7, 8, 9 }; + + initial_data.assign(std::begin(n), std::end(n)); + insert_data.assign(std::begin(n_insert), std::end(n_insert)); + less_data.assign(std::begin(n_less), std::end(n_less)); + greater_data.assign(std::begin(n_greater), std::end(n_greater)); + shorter_data.assign(std::begin(n_greater), std::end(n_greater) - 1); + different_data.assign(initial_data.rbegin(), initial_data.rend()); + + std::fill_n(buffer1, SIZE, -1); + std::fill_n(buffer2, SIZE, -1); + std::fill_n(buffer3, SIZE, -1); + std::fill_n(buffer4, SIZE, -1); + std::fill_n(buffer5, SIZE, -1); + } + }; + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_default_constructor) + { + Data data(buffer1, SIZE); + + CHECK_EQUAL(data.size(), size_t(0)); + CHECK(data.empty()); + CHECK_EQUAL(data.capacity(), SIZE); + CHECK_EQUAL(data.max_size(), SIZE); + CHECK(data.begin() == data.end()); + } + + //************************************************************************* + TEST(test_iterator_comparison_empty) + { + Data data(buffer1, SIZE); + + CHECK(data.begin() == data.end()); + CHECK(data.cbegin() == data.cend()); + CHECK(data.rbegin() == data.rend()); + CHECK(data.crbegin() == data.crend()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_constructor_size) + { + const size_t INITIAL_SIZE = 5; + Data data(INITIAL_SIZE, buffer1, SIZE); + + CHECK(data.size() == INITIAL_SIZE); + CHECK(!data.empty()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_constructor_size_value) + { + const size_t INITIAL_SIZE = 5; + const int INITIAL_VALUE = 1; + + std::array compare_data; + compare_data.fill(INITIAL_VALUE); + + Data data(INITIAL_SIZE, INITIAL_VALUE, buffer1, SIZE); + + CHECK(data.size() == INITIAL_SIZE); + CHECK(!data.empty()); + + bool is_equal = std::equal(data.begin(), + data.end(), + compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_constructor_size_excess) + { + CHECK_THROW(Data data(SIZE + 1, buffer1, SIZE), etl::vector_full); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_constructor_range) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + Data data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + CHECK(data.size() == SIZE); + CHECK(!data.empty()); + } + + //************************************************************************* + TEST(test_constructor_initializer_list) + { + Compare_Data compare_data = { 0, 1, 2, 3 }; + std::initializer_list il = { 0, 1, 2, 3 }; + Data data(il, buffer1, SIZE); + + CHECK_EQUAL(compare_data.size(), data.size()); + CHECK(std::equal(compare_data.begin(), compare_data.end(), data.begin())); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_copy_constructor) + { + Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + Data data2(data, buffer2, SIZE); + CHECK(data2 == data); + + data2[2] = -1; + CHECK(data2 != data); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_move_constructor) + { + Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + Data data2(std::move(data), buffer2, SIZE); + CHECK(data.size() == 0); + CHECK(data2.size() == initial_data.size()); + CHECK(data2 != data); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment) + { + Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + Data other_data(buffer2, SIZE); + + other_data = data; + + bool is_equal = std::equal(data.begin(), + data.end(), + other_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_move_assignment) + { + Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + Data other_data(buffer2, SIZE); + + other_data = std::move(data); + + CHECK(data.size() == 0); + CHECK(other_data.size() == initial_data.size()); + CHECK(data != other_data); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_iterface) + { + Data data1(initial_data.begin(), initial_data.end(), buffer1, SIZE); + Data data2(buffer2, SIZE); + + IData& idata1 = data1; + IData& idata2 = data2; + + idata2 = idata1; + + bool is_equal = std::equal(data1.begin(), + data1.end(), + data2.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_self_assignment) + { + Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + Data other_data(data, buffer1, SIZE); + + other_data = other_data; + + bool is_equal = std::equal(data.begin(), + data.end(), + other_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_begin) + { + Data data(10, buffer1, SIZE); + const Data constData(10, buffer1, SIZE); + + CHECK_EQUAL(&data[0], data.begin()); + CHECK_EQUAL(&constData[0], constData.begin()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_end) + { + Data data(10, buffer1, SIZE); + const Data constData(10, buffer1, SIZE); + + CHECK_EQUAL(&data[10], data.end()); + CHECK_EQUAL(&constData[10], constData.end()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_up) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = 8; + + Data data(INITIAL_SIZE, buffer1, SIZE); + data.resize(NEW_SIZE); + + CHECK_EQUAL(data.size(), NEW_SIZE); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_up_value) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = 8; + const int INITIAL_VALUE = 1; + + Data data(INITIAL_SIZE, INITIAL_VALUE, buffer1, SIZE); + data.resize(NEW_SIZE, INITIAL_VALUE); + + std::array compare_data; + compare_data.fill(INITIAL_VALUE); + + CHECK_EQUAL(compare_data.size(), data.size()); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_excess) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = SIZE + 1; + + Data data(INITIAL_SIZE, buffer1, SIZE); + + CHECK_THROW(data.resize(NEW_SIZE), etl::vector_full); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_down) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = 2; + + Data data(INITIAL_SIZE, buffer1, SIZE); + data.resize(NEW_SIZE); + + CHECK_EQUAL(data.size(), NEW_SIZE); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_down_value) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = 2; + const int INITIAL_VALUE = 1; + + Data data(INITIAL_SIZE, buffer1, SIZE); + data.resize(NEW_SIZE, INITIAL_VALUE); + + CHECK_EQUAL(data.size(), NEW_SIZE); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_empty) + { + Data data(buffer1, SIZE); + data.resize(data.max_size()); + + CHECK(data.full()); + CHECK(!data.empty()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_full) + { + Data data(buffer1, SIZE); + + CHECK(!data.full()); + CHECK(data.empty()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_index) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + Data data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + for (size_t i = 0; i < data.size(); ++i) + { + CHECK_EQUAL(data[i], compare_data[i]); + } + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_index_const) + { + const Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + const Data data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + for (size_t i = 0; i < data.size(); ++i) + { + CHECK_EQUAL(data[i], compare_data[i]); + } + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_at) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + for (size_t i = 0; i < data.size(); ++i) + { + CHECK_EQUAL(data.at(i), compare_data.at(i)); + } + + CHECK_THROW(data.at(data.size()), etl::vector_out_of_bounds); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_at_const) + { + const Compare_Data compare_data(initial_data.begin(), initial_data.end()); + const Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + for (size_t i = 0; i < data.size(); ++i) + { + CHECK_EQUAL(data.at(i), compare_data.at(i)); + } + + CHECK_THROW(data.at(data.size()), etl::vector_out_of_bounds); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_front) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + CHECK(data.front() == compare_data.front()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_front_const) + { + const Compare_Data compare_data(initial_data.begin(), initial_data.end()); + const Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + CHECK(data.front() == compare_data.front()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_back) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + CHECK(data.back() == compare_data.back()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_back_const) + { + const Compare_Data compare_data(initial_data.begin(), initial_data.end()); + const Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + CHECK(data.back() == compare_data.back()); + } + + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_data) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + Data data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + bool is_equal = std::equal(data.data(), + data.data() + data.size(), + compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_data_const) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + const Data data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + bool is_equal = std::equal(data.data(), + data.data() + data.size(), + compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assign_range) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + Data data(buffer1, SIZE); + + data.assign(compare_data.begin(), compare_data.end()); + + CHECK_EQUAL(compare_data.size(), data.size()); + + bool is_equal = std::equal(data.begin(), + data.end(), + compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assign_size_value) + { + const size_t INITIAL_SIZE = 5; + const int INITIAL_VALUE = 1; + std::array compare_data; + compare_data.fill(INITIAL_VALUE); + + Data data(buffer1, SIZE); + data.assign(INITIAL_SIZE, INITIAL_VALUE); + + CHECK_EQUAL(compare_data.size(), data.size()); + + bool is_equal = std::equal(data.begin(), + data.end(), + compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assign_size_value_excess) + { + const size_t INITIAL_SIZE = SIZE; + const size_t EXCESS_SIZE = SIZE + 1; + const int INITIAL_VALUE = 1; + std::array compare_data; + compare_data.fill(INITIAL_VALUE); + + Data data(buffer1, SIZE); + + CHECK_THROW(data.assign(EXCESS_SIZE, INITIAL_VALUE), etl::vector_full); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_push_back) + { + Compare_Data compare_data; + Data data(buffer1, SIZE); + + for (int i = 0; i < int(SIZE); ++i) + { + compare_data.push_back(i); + } + + for (int i = 0; i < int(SIZE); ++i) + { + data.push_back(i); + } + + CHECK_EQUAL(compare_data.size(), data.size()); + + bool is_equal = std::equal(data.begin(), + data.end(), + compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_push_back_literal) + { + Compare_Data compare_data; + Data data(buffer1, SIZE); + + compare_data.push_back(1); + compare_data.push_back(2); + compare_data.push_back(3); + compare_data.push_back(4); + + data.push_back(1); + data.push_back(2); + data.push_back(3); + data.push_back(4); + + CHECK_EQUAL(compare_data.size(), data.size()); + + bool is_equal = std::equal(data.begin(), + data.end(), + compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_push_back_excess) + { + Data data(buffer1, SIZE); + + for (int i = 0; i < int(SIZE); ++i) + { + data.push_back(i); + } + + CHECK_THROW(data.push_back(SIZE), etl::vector_full); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_pop_back) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + compare_data.pop_back(); + compare_data.pop_back(); + + data.pop_back(); + data.pop_back(); + + CHECK_EQUAL(compare_data.size(), data.size()); + + bool is_equal = std::equal(data.begin(), + data.end(), + compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_pop_back_exception) + { + Data data(buffer1, SIZE); + + data.resize(2); + + data.pop_back(); + data.pop_back(); + + CHECK_THROW(data.pop_back(), etl::vector_empty); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_insert_position_value) + { + const size_t INITIAL_SIZE = 5; + const int INITIAL_VALUE = 1; + + for (size_t offset = 0; offset <= INITIAL_SIZE; ++offset) + { + Compare_Data compare_data; + Data data(buffer1, SIZE); + + data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + compare_data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + + data.insert(data.begin() + offset, INITIAL_VALUE); + compare_data.insert(compare_data.begin() + offset, INITIAL_VALUE); + + CHECK_EQUAL(compare_data.size(), data.size()); + + bool is_equal = std::equal(data.begin(), + data.end(), + compare_data.begin()); + + CHECK(is_equal); + } + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_insert_position_value_excess) + { + const size_t INITIAL_SIZE = SIZE; + const int INITIAL_VALUE = 1; + + Data data(INITIAL_SIZE, INITIAL_VALUE, buffer1, SIZE); + + size_t offset = 2; + + CHECK_THROW(data.insert(data.begin() + offset, INITIAL_VALUE), etl::vector_full); + + offset = 0; + + CHECK_THROW(data.insert(data.begin() + offset, INITIAL_VALUE), etl::vector_full); + + offset = data.size(); + + CHECK_THROW(data.insert(data.begin() + offset, INITIAL_VALUE), etl::vector_full); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_insert_position_n_value) + { + const size_t INITIAL_SIZE = 5; + const size_t INSERT_SIZE = 3; + const int INITIAL_VALUE = 11; + + for (size_t offset = 0; offset <= INITIAL_SIZE; ++offset) + { + Compare_Data compare_data; + Data data(buffer1, SIZE); + + data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + compare_data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + data.insert(data.begin() + offset, INSERT_SIZE, INITIAL_VALUE); + compare_data.insert(compare_data.begin() + offset, INSERT_SIZE, INITIAL_VALUE); + + CHECK_EQUAL(compare_data.size(), data.size()); + + bool is_equal = std::equal(data.begin(), + data.end(), + compare_data.begin()); + + CHECK(is_equal); + } + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_insert_position_n_value_excess) + { + const size_t INITIAL_SIZE = SIZE; + const size_t INSERT_SIZE = 4; + const int INITIAL_VALUE = 1; + + Data data(INITIAL_SIZE, INITIAL_VALUE, buffer1, SIZE); + + size_t offset = 0; + + CHECK_THROW(data.insert(data.begin() + offset, INSERT_SIZE, INITIAL_VALUE), etl::vector_full); + + offset = 2; + + CHECK_THROW(data.insert(data.begin() + offset, INSERT_SIZE, INITIAL_VALUE), etl::vector_full); + + offset = 4; + + CHECK_THROW(data.insert(data.begin() + offset, INSERT_SIZE, INITIAL_VALUE), etl::vector_full); + + offset = data.size(); + + CHECK_THROW(data.insert(data.begin() + offset, INSERT_SIZE, INITIAL_VALUE), etl::vector_full); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_insert_position_range) + { + const size_t INITIAL_SIZE = 5; + + for (size_t offset = 0; offset <= INITIAL_SIZE; ++offset) + { + Compare_Data compare_data; + Data data(buffer1, SIZE); + + data.resize(SIZE, -1); + + data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + compare_data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + data.insert(data.begin() + offset, insert_data.begin(), insert_data.end()); + compare_data.insert(compare_data.begin() + offset, insert_data.begin(), insert_data.end()); + + CHECK_EQUAL(compare_data.size(), data.size()); + + bool is_equal = std::equal(data.begin(), + data.end(), + compare_data.begin()); + + CHECK(is_equal); + } + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_insert_position_range_excess) + { + const size_t INITIAL_SIZE = 5; + const int INITIAL_VALUE = 1; + + Data data(INITIAL_SIZE, INITIAL_VALUE, buffer1, SIZE); + + size_t offset = 0; + + CHECK_THROW(data.insert(data.begin() + offset, initial_data.begin(), initial_data.end()), etl::vector_full); + + offset = 2; + + CHECK_THROW(data.insert(data.begin() + offset, initial_data.begin(), initial_data.end()), etl::vector_full); + + offset = 4; + + CHECK_THROW(data.insert(data.begin() + offset, initial_data.begin(), initial_data.end()), etl::vector_full); + + offset = data.size(); + + CHECK_THROW(data.insert(data.begin() + offset, initial_data.begin(), initial_data.end()), etl::vector_full); + } + + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_erase_single) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + compare_data.erase(compare_data.begin() + 2); + + data.erase(data.begin() + 2); + + CHECK_EQUAL(compare_data.size(), data.size()); + + bool is_equal = std::equal(data.begin(), + data.end(), + compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_erase_range) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + compare_data.erase(compare_data.begin() + 2, compare_data.begin() + 4); + + data.erase(data.begin() + 2, data.begin() + 4); + + CHECK_EQUAL(compare_data.size(), data.size()); + + bool is_equal = std::equal(data.begin(), + data.end(), + compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_clear) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + Data data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + data.clear(); + + CHECK_EQUAL(data.size(), size_t(0)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_iterator) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + Data data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + bool is_equal = std::equal(data.begin(), + data.end(), + compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_iterator) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + Data data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + bool is_equal = std::equal(data.cbegin(), + data.cend(), + compare_data.cbegin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_reverse_iterator) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + Data data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + bool is_equal = std::equal(data.rbegin(), + data.rend(), + compare_data.rbegin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_reverse_iterator) + { + const Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + const Data data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + bool is_equal = std::equal(data.crbegin(), + data.crend(), + compare_data.crbegin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_reverse_iterator2) + { + const Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + const Data data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + bool is_equal = std::equal(data.rbegin(), + data.rend(), + compare_data.rbegin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_equal) + { + const Data initial1(initial_data.begin(), initial_data.end(), buffer1, SIZE); + const Data initial2(initial_data.begin(), initial_data.end(), buffer2, SIZE); + + CHECK(initial1 == initial2); + + const Data different(different_data.begin(), different_data.end(), buffer3, SIZE); + + CHECK(!(initial1 == different)); + + const Data shorter(shorter_data.begin(), shorter_data.end(), buffer4, SIZE); + + CHECK(!(shorter == initial1)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_not_equal) + { + const Data initial1(initial_data.begin(), initial_data.end(), buffer1, SIZE); + const Data initial2(initial_data.begin(), initial_data.end(), buffer2, SIZE); + + CHECK(!(initial1 != initial2)); + + const Data different(different_data.begin(), different_data.end(), buffer3, SIZE); + + CHECK(initial1 != different); + + const Data shorter(shorter_data.begin(), shorter_data.end(), buffer4, SIZE); + + CHECK(shorter != initial1); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_less_than) + { + const Data less(less_data.begin(), less_data.end(), buffer1, SIZE); + const Data initial(initial_data.begin(), initial_data.end(), buffer2, SIZE); + + CHECK((less < initial) == (less_data < initial_data)); + + const Data greater(greater_data.begin(), greater_data.end(), buffer3, SIZE); + + CHECK((greater < initial) == (greater_data < initial_data)); + + const Data shorter(shorter_data.begin(), shorter_data.end(), buffer4, SIZE); + + CHECK((shorter < initial) == (shorter_data < initial_data)); + CHECK((initial < shorter) == (initial_data < shorter_data)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_less_than_or_equal) + { + const Data less(less_data.begin(), less_data.end(), buffer1, SIZE); + const Data initial(initial_data.begin(), initial_data.end(), buffer2, SIZE); + + CHECK((less <= initial) == (less_data <= initial_data)); + + const Data greater(greater_data.begin(), greater_data.end(), buffer3, SIZE); + + CHECK((greater <= initial) == (greater_data <= initial_data)); + + const Data shorter(shorter_data.begin(), shorter_data.end(), buffer4, SIZE); + + CHECK((shorter <= initial) == (shorter_data <= initial_data)); + CHECK((initial <= shorter) == (initial_data <= shorter_data)); + + const Data initial2(initial_data.begin(), initial_data.end(), buffer5, SIZE); + CHECK((initial <= initial2) == (initial_data <= initial_data)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_greater_than) + { + const Data less(less_data.begin(), less_data.end(), buffer1, SIZE); + const Data initial(initial_data.begin(), initial_data.end(), buffer2, SIZE); + + CHECK((less > initial) == (less_data > initial_data)); + + const Data greater(greater_data.begin(), greater_data.end(), buffer3, SIZE); + + CHECK((greater > initial) == (greater_data > initial_data)); + + const Data shorter(shorter_data.begin(), shorter_data.end(), buffer4, SIZE); + + CHECK((shorter > initial) == (shorter_data > initial_data)); + CHECK((initial > shorter) == (initial_data > shorter_data)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_greater_than_or_equal) + { + const Data less(less_data.begin(), less_data.end(), buffer1, SIZE); + const Data initial(initial_data.begin(), initial_data.end(), buffer2, SIZE); + + CHECK((less >= initial) == (less_data >= initial_data)); + + const Data greater(greater_data.begin(), greater_data.end(), buffer3, SIZE); + + CHECK((greater >= initial) == (greater_data >= initial_data)); + + const Data shorter(shorter_data.begin(), shorter_data.end(), buffer4, SIZE); + + CHECK((shorter >= initial) == (shorter_data >= initial_data)); + CHECK((initial >= shorter) == (initial_data > shorter_data)); + + const Data initial2(initial_data.begin(), initial_data.end(), buffer5, SIZE); + CHECK((initial >= initial2) == (initial_data >= initial_data)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_memcpy_repair) + { + Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + char buffer[sizeof(Data)]; + + memcpy(&buffer, &data, sizeof(data)); + + Data& rdata(*reinterpret_cast(buffer)); + rdata.repair(); + + // Check that the memcpy'd vector is the same. + CHECK_EQUAL(data.size(), rdata.size()); + CHECK(!rdata.empty()); + CHECK(rdata.full()); + + bool is_equal = std::equal(rdata.begin(), + rdata.end(), + data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_memcpy_repair_virtual) + { + Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + char buffer[sizeof(Data)]; + + memcpy(&buffer, &data, sizeof(data)); + + IData& idata(*reinterpret_cast(buffer)); + idata.repair(); + + // Check that the memcpy'd vector is the same. + CHECK_EQUAL(data.size(), idata.size()); + CHECK(!idata.empty()); + CHECK(idata.full()); + + bool is_equal = std::equal(idata.begin(), + idata.end(), + data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST(test_insert_bug) + { + struct S + { + virtual ~S() + { + + } + + S(int i): i(i){} + + int i; + }; + + S sbuffer1[SIZE] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; + S sbuffer2[SIZE] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; + + const S raw[6] = { 1, 2, 3, 4, 5, 6 }; + + etl::vector dest(etl::begin(raw), etl::end(raw), sbuffer1, SIZE); + etl::vector src((size_t) 2, S(8), sbuffer2, SIZE); + + dest.insert(dest.begin(), src.begin(), src.end()); + + CHECK(dest.size() == 8); + CHECK_EQUAL(src[0].i, dest[0].i); + CHECK_EQUAL(src[1].i, dest[1].i); + CHECK_EQUAL(raw[0].i, dest[2].i); + CHECK_EQUAL(raw[1].i, dest[3].i); + CHECK_EQUAL(raw[2].i, dest[4].i); + CHECK_EQUAL(raw[3].i, dest[5].i); + CHECK_EQUAL(raw[4].i, dest[6].i); + CHECK_EQUAL(raw[5].i, dest[7].i); + } + + //************************************************************************* + TEST(test_insert_n_bug) + { + struct S + { + virtual ~S() + { + + } + + S(int i): i(i){} + + int i; + }; + + S sbuffer1[SIZE] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; + + const S raw[6] = { 1, 2, 3, 4, 5, 6 }; + + etl::vector dest(etl::begin(raw), etl::end(raw), sbuffer1, SIZE); + + dest.insert(dest.begin(), 2, S(8)); + + CHECK(dest.size() == 8); + CHECK_EQUAL(8, dest[0].i); + CHECK_EQUAL(8, dest[1].i); + CHECK_EQUAL(raw[0].i, dest[2].i); + CHECK_EQUAL(raw[1].i, dest[3].i); + CHECK_EQUAL(raw[2].i, dest[4].i); + CHECK_EQUAL(raw[3].i, dest[5].i); + CHECK_EQUAL(raw[4].i, dest[6].i); + CHECK_EQUAL(raw[5].i, dest[7].i); + } + }; +} diff --git a/test/test_vector_pointer_external_buffer.cpp b/test/test_vector_pointer_external_buffer.cpp new file mode 100644 index 00000000..5cdbdd07 --- /dev/null +++ b/test/test_vector_pointer_external_buffer.cpp @@ -0,0 +1,1756 @@ +/****************************************************************************** +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 +#include +#include + +#include "etl/vector.h" + +namespace +{ + static const size_t SIZE = 10; + + int* buffer1[SIZE]; + int* buffer2[SIZE]; + int* buffer3[SIZE]; + int* buffer4[SIZE]; + int* buffer5[SIZE]; + + SUITE(test_vector_pointer) + { + typedef etl::vector Data; + typedef etl::vector CData; + typedef etl::ivector IData; + typedef etl::ivector CIData; + typedef std::vector Compare_Data; + typedef std::vector CCompare_Data; + + Compare_Data initial_data; + Compare_Data less_data; + Compare_Data greater_data; + Compare_Data shorter_data; + Compare_Data different_data; + Compare_Data insert_data; + + //************************************************************************* + struct SetupFixture + { + SetupFixture() + { + int n0 = 0; + int n1 = 1; + int n2 = 2; + int n3 = 3; + int n4 = 4; + int n5 = 5; + int n6 = 6; + int n7 = 7; + int n8 = 8; + int n9 = 9; + int n11 = 11; + int n12 = 12; + int n13 = 13; + + + int* n[] = { &n0, &n1, &n2, &n3, &n4, &n5, &n6, &n7, &n8, &n9 }; + int* n_insert[] = { &n11, &n12, &n13 }; + int* n_less[] = { &n0, &n1, &n2, &n3, &n3, &n5, &n6, &n7, &n8, &n9 }; + int* n_greater[] = { &n0, &n1, &n2, &n4, &n4, &n5, &n6, &n7, &n8, &n9 }; + + initial_data.assign(std::begin(n), std::end(n)); + insert_data.assign(std::begin(n_insert), std::end(n_insert)); + less_data.assign(std::begin(n_less), std::end(n_less)); + greater_data.assign(std::begin(n_greater), std::end(n_greater)); + shorter_data.assign(std::begin(n_greater), std::end(n_greater) - 1); + different_data.assign(initial_data.rbegin(), initial_data.rend()); + + std::fill_n(buffer1, SIZE, nullptr); + std::fill_n(buffer2, SIZE, nullptr); + } + }; + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_default_constructor) + { + Data data(buffer1, SIZE); + + CHECK_EQUAL(data.size(), size_t(0)); + CHECK(data.empty()); + CHECK_EQUAL(data.capacity(), SIZE); + CHECK_EQUAL(data.max_size(), SIZE); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_default_constructor) + { + CData data(buffer1, SIZE); + + CHECK_EQUAL(data.size(), size_t(0)); + CHECK(data.empty()); + CHECK_EQUAL(data.capacity(), SIZE); + CHECK_EQUAL(data.max_size(), SIZE); + } + + //************************************************************************* + TEST(test_iterator_comparison_empty) + { + Data data(buffer1, SIZE); + + CHECK(data.begin() == data.end()); + CHECK(data.cbegin() == data.cend()); + CHECK(data.rbegin() == data.rend()); + CHECK(data.crbegin() == data.crend()); + } + + //************************************************************************* + TEST(test_const_iterator_comparison_empty) + { + CData data(buffer1, SIZE); + + CHECK(data.begin() == data.end()); + CHECK(data.cbegin() == data.cend()); + CHECK(data.rbegin() == data.rend()); + CHECK(data.crbegin() == data.crend()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_constructor_size) + { + const size_t INITIAL_SIZE = 5; + Data data(INITIAL_SIZE, buffer1, SIZE); + + CHECK(data.size() == INITIAL_SIZE); + CHECK(!data.empty()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_constructor_size) + { + const size_t INITIAL_SIZE = 5; + CData data(INITIAL_SIZE, buffer1, SIZE); + + CHECK(data.size() == INITIAL_SIZE); + CHECK(!data.empty()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_constructor_size_value) + { + const size_t INITIAL_SIZE = 5; + int INITIAL_VALUE = 1; + + std::array compare_data; + compare_data.fill(&INITIAL_VALUE); + + Data data(INITIAL_SIZE, &INITIAL_VALUE, buffer1, SIZE); + + CHECK(data.size() == INITIAL_SIZE); + CHECK(!data.empty()); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_constructor_size_value) + { + const size_t INITIAL_SIZE = 5; + int INITIAL_VALUE = 1; + + std::array compare_data; + compare_data.fill(&INITIAL_VALUE); + + CData data(INITIAL_SIZE, &INITIAL_VALUE, buffer1, SIZE); + + CHECK(data.size() == INITIAL_SIZE); + CHECK(!data.empty()); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_constructor_size_excess) + { + CHECK_THROW(Data data(SIZE + 1, buffer1, SIZE), etl::vector_full); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_constructor_size_excess) + { + CHECK_THROW(CData data(SIZE + 1, buffer1, SIZE), etl::vector_full); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_constructor_range) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + Data data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + CHECK(data.size() == SIZE); + CHECK(!data.empty()); + CHECK(std::equal(compare_data.begin(), compare_data.end(), data.begin())); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_constructor_range) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + + CData data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + CHECK(data.size() == SIZE); + CHECK(!data.empty()); + CHECK(std::equal(compare_data.begin(), compare_data.end(), data.begin())); + } + + //************************************************************************* + TEST(test_constructor_initializer_list) + { + int a = 0; + int b = 1; + int c = 3; + int d = 4; + + Compare_Data compare_data = { &a, &b, &c, &d}; + Data data({ &a, &b, &c, &d }, buffer1, SIZE); + + CHECK_EQUAL(compare_data.size(), data.size()); + CHECK(std::equal(compare_data.begin(), compare_data.end(), data.begin())); + } + + //************************************************************************* + TEST(test_const_constructor_initializer_list) + { + int a = 0; + int b = 1; + int c = 3; + int d = 4; + + CCompare_Data compare_data = { &a, &b, &c, &d }; + CData data({ &a, &b, &c, &d }, buffer1, SIZE); + + CHECK_EQUAL(compare_data.size(), data.size()); + CHECK(std::equal(compare_data.begin(), compare_data.end(), data.begin())); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_copy_constructor) + { + Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + Data data2(data, buffer2, SIZE); + CHECK(data2 == data); + + data2[2] = nullptr; + CHECK(data2 != data); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_copy_constructor) + { + CData data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + CData data2(data, buffer2, SIZE); + CHECK(data2 == data); + + data2[2] = nullptr; + CHECK(data2 != data); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment) + { + Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + Data other_data(buffer2, SIZE); + + other_data = data; + + bool is_equal = std::equal(data.begin(), data.end(), other_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_assignment) + { + CData data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + CData other_data(buffer2, SIZE); + + other_data = data; + + bool is_equal = std::equal(data.begin(), data.end(), other_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_iterface) + { + Data data1(initial_data.begin(), initial_data.end(), buffer1, SIZE); + Data data2(buffer2, SIZE); + + IData& idata1 = data1; + IData& idata2 = data2; + + idata2 = idata1; + + bool is_equal = std::equal(data1.begin(), data1.end(), data2.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_assignment_iterface) + { + CData data1(initial_data.begin(), initial_data.end(), buffer1, SIZE); + CData data2(buffer2, SIZE); + + CIData& idata1 = data1; + CIData& idata2 = data2; + + idata2 = idata1; + + bool is_equal = std::equal(data1.begin(), data1.end(), data2.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_self_assignment) + { + Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + Data other_data(data, buffer2, SIZE); + + other_data = other_data; + + bool is_equal = std::equal(data.begin(), data.end(), other_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_self_assignment) + { + CData data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + CData other_data(data, buffer2, SIZE); + + other_data = other_data; + + bool is_equal = std::equal(data.begin(), data.end(), other_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_begin) + { + Data data(10, buffer1, SIZE); + const Data constData(10, buffer2, SIZE); + + CHECK_EQUAL(&data[0], data.begin()); + CHECK_EQUAL(&constData[0], constData.begin()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_begin) + { + CData data(10, buffer1, SIZE); + const CData constData(10, buffer2, SIZE); + + CHECK_EQUAL(&data[0], data.begin()); + CHECK_EQUAL(&constData[0], constData.begin()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_end) + { + Data data(10, buffer1, SIZE); + const Data constData(10, buffer2, SIZE); + + CHECK_EQUAL(&data[10], data.end()); + CHECK_EQUAL(&constData[10], constData.end()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_end) + { + CData data(10, buffer1, SIZE); + const CData constData(10, buffer2, SIZE); + + CHECK_EQUAL(&data[10], data.end()); + CHECK_EQUAL(&constData[10], constData.end()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_up) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = 8; + + Data data(INITIAL_SIZE, buffer1, SIZE); + data.resize(NEW_SIZE); + + CHECK_EQUAL(data.size(), NEW_SIZE); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_resize_up) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = 8; + + CData data(INITIAL_SIZE, buffer1, SIZE); + data.resize(NEW_SIZE); + + CHECK_EQUAL(data.size(), NEW_SIZE); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_up_value) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = 8; + int INITIAL_VALUE = 1; + + Data data(INITIAL_SIZE, &INITIAL_VALUE, buffer1, SIZE); + data.resize(NEW_SIZE, &INITIAL_VALUE); + + std::array compare_data; + compare_data.fill(&INITIAL_VALUE); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_resize_up_value) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = 8; + int INITIAL_VALUE = 1; + + CData data(INITIAL_SIZE, &INITIAL_VALUE, buffer1, SIZE); + data.resize(NEW_SIZE, &INITIAL_VALUE); + + std::array compare_data; + compare_data.fill(&INITIAL_VALUE); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_excess) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = SIZE + 1; + + Data data(INITIAL_SIZE, buffer1, SIZE); + + CHECK_THROW(data.resize(NEW_SIZE), etl::vector_full); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_resize_excess) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = SIZE + 1; + + CData data(INITIAL_SIZE, buffer1, SIZE); + + CHECK_THROW(data.resize(NEW_SIZE), etl::vector_full); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_down) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = 2; + + Data data(INITIAL_SIZE, buffer1, SIZE); + data.resize(NEW_SIZE); + + CHECK_EQUAL(data.size(), NEW_SIZE); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_resize_down) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = 2; + + CData data(INITIAL_SIZE, buffer1, SIZE); + data.resize(NEW_SIZE); + + CHECK_EQUAL(data.size(), NEW_SIZE); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_down_value) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = 2; + int INITIAL_VALUE = 1; + + Data data(INITIAL_SIZE, buffer1, SIZE); + data.resize(NEW_SIZE, &INITIAL_VALUE); + + CHECK_EQUAL(data.size(), NEW_SIZE); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_resize_down_value) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = 2; + int INITIAL_VALUE = 1; + + CData data(INITIAL_SIZE, buffer1, SIZE); + data.resize(NEW_SIZE, &INITIAL_VALUE); + + CHECK_EQUAL(data.size(), NEW_SIZE); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_empty) + { + Data data(buffer1, SIZE); + data.resize(data.max_size()); + + CHECK(data.full()); + CHECK(!data.empty()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_empty) + { + CData data(buffer1, SIZE); + data.resize(data.max_size()); + + CHECK(data.full()); + CHECK(!data.empty()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_full) + { + Data data(buffer1, SIZE); + + CHECK(!data.full()); + CHECK(data.empty()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_full) + { + CData data(buffer1, SIZE); + + CHECK(!data.full()); + CHECK(data.empty()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_index) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + Data data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + for (size_t i = 0; i < data.size(); ++i) + { + CHECK_EQUAL(data[i], compare_data[i]); + } + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_index) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + + CData data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + for (size_t i = 0; i < data.size(); ++i) + { + CHECK_EQUAL(data[i], compare_data[i]); + } + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_index_const) + { + const Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + const Data data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + for (size_t i = 0; i < data.size(); ++i) + { + CHECK_EQUAL(data[i], compare_data[i]); + } + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_index_const) + { + const CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + + const CData data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + for (size_t i = 0; i < data.size(); ++i) + { + CHECK_EQUAL(data[i], compare_data[i]); + } + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_at) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + for (size_t i = 0; i < data.size(); ++i) + { + CHECK_EQUAL(data.at(i), compare_data.at(i)); + } + + CHECK_THROW(data.at(data.size()), etl::vector_out_of_bounds); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_at) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + CData data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + for (size_t i = 0; i < data.size(); ++i) + { + CHECK_EQUAL(data.at(i), compare_data.at(i)); + } + + CHECK_THROW(data.at(data.size()), etl::vector_out_of_bounds); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_at_const) + { + const Compare_Data compare_data(initial_data.begin(), initial_data.end()); + const Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + for (size_t i = 0; i < data.size(); ++i) + { + CHECK_EQUAL(data.at(i), compare_data.at(i)); + } + + CHECK_THROW(data.at(data.size()), etl::vector_out_of_bounds); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_at_const) + { + const CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + const CData data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + for (size_t i = 0; i < data.size(); ++i) + { + CHECK_EQUAL(data.at(i), compare_data.at(i)); + } + + CHECK_THROW(data.at(data.size()), etl::vector_out_of_bounds); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_front) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + CHECK(data.front() == compare_data.front()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_front) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + CData data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + CHECK(data.front() == compare_data.front()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_front_const) + { + const Compare_Data compare_data(initial_data.begin(), initial_data.end()); + const Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + CHECK(data.front() == compare_data.front()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_front_const) + { + const CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + const CData data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + CHECK(data.front() == compare_data.front()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_back) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + CHECK(data.back() == compare_data.back()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_back) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + CData data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + CHECK(data.back() == compare_data.back()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_back_const) + { + const Compare_Data compare_data(initial_data.begin(), initial_data.end()); + const Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + CHECK(data.back() == compare_data.back()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_back_const) + { + const CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + const CData data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + CHECK(data.back() == compare_data.back()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_data) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + Data data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + bool is_equal = std::equal(data.data(), data.data() + data.size(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_data) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + + CData data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + bool is_equal = std::equal(data.data(), data.data() + data.size(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_data_const) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + const Data data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + bool is_equal = std::equal(data.data(), data.data() + data.size(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_data_const) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + + const CData data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + bool is_equal = std::equal(data.data(), data.data() + data.size(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assign_range) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + Data data(buffer1, SIZE); + + data.assign(compare_data.begin(), compare_data.end()); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_assign_range) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + + CData data(buffer1, SIZE); + + data.assign(compare_data.begin(), compare_data.end()); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assign_size_value) + { + const size_t INITIAL_SIZE = 5; + int INITIAL_VALUE = 1; + std::array compare_data; + compare_data.fill(&INITIAL_VALUE); + + Data data(buffer1, SIZE); + data.assign(INITIAL_SIZE, &INITIAL_VALUE); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_assign_size_value) + { + const size_t INITIAL_SIZE = 5; + int INITIAL_VALUE = 1; + std::array compare_data; + compare_data.fill(&INITIAL_VALUE); + + CData data(buffer1, SIZE); + data.assign(INITIAL_SIZE, &INITIAL_VALUE); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assign_size_value_excess) + { + const size_t INITIAL_SIZE = SIZE; + const size_t EXCESS_SIZE = SIZE + 1; + int INITIAL_VALUE = 1; + std::array compare_data; + compare_data.fill(&INITIAL_VALUE); + + Data data(buffer1, SIZE); + + CHECK_THROW(data.assign(EXCESS_SIZE, &INITIAL_VALUE), etl::vector_full); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_assign_size_value_excess) + { + const size_t INITIAL_SIZE = SIZE; + const size_t EXCESS_SIZE = SIZE + 1; + int INITIAL_VALUE = 1; + std::array compare_data; + compare_data.fill(&INITIAL_VALUE); + + CData data(buffer1, SIZE); + + CHECK_THROW(data.assign(EXCESS_SIZE, &INITIAL_VALUE), etl::vector_full); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_push_back) + { + Compare_Data compare_data; + Data data(buffer1, SIZE); + + int d; + + for (size_t i = 0; i < SIZE; ++i) + { + compare_data.push_back(&d); + } + + for (size_t i = 0; i < SIZE; ++i) + { + data.push_back(&d); + } + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_push_back) + { + CCompare_Data compare_data; + CData data(buffer1, SIZE); + + const int d = 0; + + for (size_t i = 0; i < SIZE; ++i) + { + compare_data.push_back(&d); + } + + for (size_t i = 0; i < SIZE; ++i) + { + data.push_back(&d); + } + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_push_back_excess) + { + Data data(buffer1, SIZE); + + int d; + + for (size_t i = 0; i < SIZE; ++i) + { + data.push_back(&d); + } + + CHECK_THROW(data.push_back(&d), etl::vector_full); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_push_back_excess) + { + CData data(buffer1, SIZE); + + const int d = 0; + + for (size_t i = 0; i < SIZE; ++i) + { + data.push_back(&d); + } + + CHECK_THROW(data.push_back(&d), etl::vector_full); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_pop_back) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + compare_data.pop_back(); + compare_data.pop_back(); + + data.pop_back(); + data.pop_back(); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_pop_back) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + CData data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + compare_data.pop_back(); + compare_data.pop_back(); + + data.pop_back(); + data.pop_back(); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_pop_back_exception) + { + Data data(buffer1, SIZE); + + data.resize(2); + + data.pop_back(); + data.pop_back(); + + CHECK_THROW(data.pop_back(), etl::vector_empty); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_pop_back_exception) + { + CData data(buffer1, SIZE); + + data.resize(2); + + data.pop_back(); + data.pop_back(); + + CHECK_THROW(data.pop_back(), etl::vector_empty); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_insert_position_value) + { + const size_t INITIAL_SIZE = 5; + int INITIAL_VALUE = 1; + + for (size_t offset = 0; offset <= INITIAL_SIZE; ++offset) + { + Compare_Data compare_data; + Data data(buffer1, SIZE); + + data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + compare_data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + + data.insert(data.begin() + offset, &INITIAL_VALUE); + compare_data.insert(compare_data.begin() + offset, &INITIAL_VALUE); + + CHECK_EQUAL(compare_data.size(), data.size()); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_insert_position_value) + { + const size_t INITIAL_SIZE = 5; + int INITIAL_VALUE = 1; + + for (size_t offset = 0; offset <= INITIAL_SIZE; ++offset) + { + CCompare_Data compare_data; + CData data(buffer1, SIZE); + + data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + compare_data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + + data.insert(data.begin() + offset, &INITIAL_VALUE); + compare_data.insert(compare_data.begin() + offset, &INITIAL_VALUE); + + CHECK_EQUAL(compare_data.size(), data.size()); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_insert_position_value_excess) + { + const size_t INITIAL_SIZE = SIZE; + int INITIAL_VALUE = 1; + + Data data(INITIAL_SIZE, &INITIAL_VALUE, buffer1, SIZE); + + size_t offset = 2; + + CHECK_THROW(data.insert(data.begin() + offset, &INITIAL_VALUE), etl::vector_full); + + offset = 0; + + CHECK_THROW(data.insert(data.begin() + offset, &INITIAL_VALUE), etl::vector_full); + + offset = data.size(); + + CHECK_THROW(data.insert(data.begin() + offset, &INITIAL_VALUE), etl::vector_full); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_insert_position_n_value) + { + const size_t INITIAL_SIZE = 5; + const size_t INSERT_SIZE = 3; + int INITIAL_VALUE = 11; + + for (size_t offset = 0; offset <= INITIAL_SIZE; ++offset) + { + Compare_Data compare_data; + Data data(buffer1, SIZE); + + data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + compare_data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + data.insert(data.begin() + offset, INSERT_SIZE, &INITIAL_VALUE); + compare_data.insert(compare_data.begin() + offset, INSERT_SIZE, &INITIAL_VALUE); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_insert_position_n_value) + { + const size_t INITIAL_SIZE = 5; + const size_t INSERT_SIZE = 3; + int INITIAL_VALUE = 11; + + for (size_t offset = 0; offset <= INITIAL_SIZE; ++offset) + { + CCompare_Data compare_data; + CData data(buffer1, SIZE); + + data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + compare_data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + data.insert(data.begin() + offset, INSERT_SIZE, &INITIAL_VALUE); + compare_data.insert(compare_data.begin() + offset, INSERT_SIZE, &INITIAL_VALUE); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_insert_position_n_value_excess) + { + const size_t INITIAL_SIZE = SIZE; + const size_t INSERT_SIZE = 4; + int INITIAL_VALUE = 1; + + Data data(INITIAL_SIZE, &INITIAL_VALUE, buffer1, SIZE); + + size_t offset = 0; + + CHECK_THROW(data.insert(data.begin() + offset, INSERT_SIZE, &INITIAL_VALUE), etl::vector_full); + + offset = 2; + + CHECK_THROW(data.insert(data.begin() + offset, INSERT_SIZE, &INITIAL_VALUE), etl::vector_full); + + offset = 4; + + CHECK_THROW(data.insert(data.begin() + offset, INSERT_SIZE, &INITIAL_VALUE), etl::vector_full); + + offset = data.size(); + + CHECK_THROW(data.insert(data.begin() + offset, INSERT_SIZE, &INITIAL_VALUE), etl::vector_full); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_insert_position_n_value_excess) + { + const size_t INITIAL_SIZE = SIZE; + const size_t INSERT_SIZE = 4; + const int INITIAL_VALUE = 1; + + CData data(INITIAL_SIZE, &INITIAL_VALUE, buffer1, SIZE); + + size_t offset = 0; + + CHECK_THROW(data.insert(data.begin() + offset, INSERT_SIZE, &INITIAL_VALUE), etl::vector_full); + + offset = 2; + + CHECK_THROW(data.insert(data.begin() + offset, INSERT_SIZE, &INITIAL_VALUE), etl::vector_full); + + offset = 4; + + CHECK_THROW(data.insert(data.begin() + offset, INSERT_SIZE, &INITIAL_VALUE), etl::vector_full); + + offset = data.size(); + + CHECK_THROW(data.insert(data.begin() + offset, INSERT_SIZE, &INITIAL_VALUE), etl::vector_full); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_insert_position_range) + { + const size_t INITIAL_SIZE = 5; + + for (size_t offset = 0; offset <= INITIAL_SIZE; ++offset) + { + Compare_Data compare_data; + Data data(buffer1, SIZE); + + data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + compare_data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + data.insert(data.begin() + offset, insert_data.begin(), insert_data.end()); + compare_data.insert(compare_data.begin() + offset, insert_data.begin(), insert_data.end()); + + bool is_equal = std::equal(data.begin(), + data.end(), + compare_data.begin()); + + CHECK(is_equal); + } + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_insert_position_range) + { + const size_t INITIAL_SIZE = 5; + + for (size_t offset = 0; offset <= INITIAL_SIZE; ++offset) + { + CCompare_Data compare_data; + CData data(buffer1, SIZE); + + data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + compare_data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + data.insert(data.begin() + offset, insert_data.begin(), insert_data.end()); + compare_data.insert(compare_data.begin() + offset, insert_data.begin(), insert_data.end()); + + bool is_equal = std::equal(data.begin(), + data.end(), + compare_data.begin()); + + CHECK(is_equal); + } + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_insert_position_range_excess) + { + const size_t INITIAL_SIZE = 5; + int INITIAL_VALUE = 1; + + Data data(INITIAL_SIZE, &INITIAL_VALUE, buffer1, SIZE); + + size_t offset = 0; + + CHECK_THROW(data.insert(data.begin() + offset, initial_data.begin(), initial_data.end()), etl::vector_full); + + offset = 2; + + CHECK_THROW(data.insert(data.begin() + offset, initial_data.begin(), initial_data.end()), etl::vector_full); + + offset = 4; + + CHECK_THROW(data.insert(data.begin() + offset, initial_data.begin(), initial_data.end()), etl::vector_full); + + offset = data.size(); + + CHECK_THROW(data.insert(data.begin() + offset, initial_data.begin(), initial_data.end()), etl::vector_full); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_insert_position_range_excess) + { + const size_t INITIAL_SIZE = 5; + const int INITIAL_VALUE = 1; + + CData data(INITIAL_SIZE, &INITIAL_VALUE, buffer1, SIZE); + + size_t offset = 0; + + CHECK_THROW(data.insert(data.begin() + offset, initial_data.begin(), initial_data.end()), etl::vector_full); + + offset = 2; + + CHECK_THROW(data.insert(data.begin() + offset, initial_data.begin(), initial_data.end()), etl::vector_full); + + offset = 4; + + CHECK_THROW(data.insert(data.begin() + offset, initial_data.begin(), initial_data.end()), etl::vector_full); + + offset = data.size(); + + CHECK_THROW(data.insert(data.begin() + offset, initial_data.begin(), initial_data.end()), etl::vector_full); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_erase_single) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + compare_data.erase(compare_data.begin() + 2); + + data.erase(data.begin() + 2); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_erase_single) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + CData data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + compare_data.erase(compare_data.begin() + 2); + + data.erase(data.begin() + 2); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_erase_range) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + Data data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + compare_data.erase(compare_data.begin() + 2, compare_data.begin() + 4); + + data.erase(data.begin() + 2, data.begin() + 4); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_erase_range) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + CData data(initial_data.begin(), initial_data.end(), buffer1, SIZE); + + compare_data.erase(compare_data.begin() + 2, compare_data.begin() + 4); + + data.erase(data.begin() + 2, data.begin() + 4); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_clear) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + Data data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + data.clear(); + + CHECK_EQUAL(data.size(), size_t(0)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_clear) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + + CData data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + data.clear(); + + CHECK_EQUAL(data.size(), size_t(0)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_iterator) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + Data data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_iterator_const) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + + CData data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_iterator) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + Data data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + bool is_equal = std::equal(data.cbegin(), data.cend(), compare_data.cbegin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_iterator_const) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + + CData data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + bool is_equal = std::equal(data.cbegin(), data.cend(), compare_data.cbegin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_reverse_iterator) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + Data data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + bool is_equal = std::equal(data.rbegin(), data.rend(), compare_data.rbegin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_reverse_iterator_const) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + + CData data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + bool is_equal = std::equal(data.rbegin(), data.rend(), compare_data.rbegin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_reverse_iterator) + { + const Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + const Data data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + bool is_equal = std::equal(data.crbegin(), data.crend(), compare_data.crbegin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_reverse_iterator_const) + { + const CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + + const CData data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + bool is_equal = std::equal(data.crbegin(), data.crend(), compare_data.crbegin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_reverse_iterator2) + { + const Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + const Data data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + bool is_equal = std::equal(data.rbegin(), data.rend(), compare_data.rbegin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_reverse_iterator2_const) + { + const CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + + const CData data(compare_data.begin(), compare_data.end(), buffer1, SIZE); + + bool is_equal = std::equal(data.rbegin(), data.rend(), compare_data.rbegin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_equal) + { + const Data initial1(initial_data.begin(), initial_data.end(), buffer1, SIZE); + const Data initial2(initial_data.begin(), initial_data.end(), buffer2, SIZE); + + CHECK(initial1 == initial2); + + const Data different(different_data.begin(), different_data.end(), buffer3, SIZE); + + CHECK(!(initial1 == different)); + + const Data shorter(shorter_data.begin(), shorter_data.end(), buffer4, SIZE); + + CHECK(!(shorter == initial1)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_equal) + { + const CData initial1(initial_data.begin(), initial_data.end(), buffer1, SIZE); + const CData initial2(initial_data.begin(), initial_data.end(), buffer2, SIZE); + + CHECK(initial1 == initial2); + + const CData different(different_data.begin(), different_data.end(), buffer3, SIZE); + + CHECK(!(initial1 == different)); + + const CData shorter(shorter_data.begin(), shorter_data.end(), buffer4, SIZE); + + CHECK(!(shorter == initial1)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_not_equal) + { + const Data initial1(initial_data.begin(), initial_data.end(), buffer1, SIZE); + const Data initial2(initial_data.begin(), initial_data.end(), buffer2, SIZE); + + CHECK(!(initial1 != initial2)); + + const Data different(different_data.begin(), different_data.end(), buffer3, SIZE); + + CHECK(initial1 != different); + + const Data shorter(shorter_data.begin(), shorter_data.end(), buffer4, SIZE); + + CHECK(shorter != initial1); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_not_equal) + { + const CData initial1(initial_data.begin(), initial_data.end(), buffer1, SIZE); + const CData initial2(initial_data.begin(), initial_data.end(), buffer2, SIZE); + + CHECK(!(initial1 != initial2)); + + const CData different(different_data.begin(), different_data.end(), buffer3, SIZE); + + CHECK(initial1 != different); + + const CData shorter(shorter_data.begin(), shorter_data.end(), buffer4, SIZE); + + CHECK(shorter != initial1); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_less_than) + { + const Data less(less_data.begin(), less_data.end(), buffer1, SIZE); + const Data initial(initial_data.begin(), initial_data.end(), buffer2, SIZE); + + CHECK((less < initial) == (less_data < initial_data)); + + const Data greater(greater_data.begin(), greater_data.end(), buffer3, SIZE); + + CHECK((greater < initial) == (greater_data < initial_data)); + + const Data shorter(shorter_data.begin(), shorter_data.end(), buffer4, SIZE); + + CHECK((shorter < initial) == (shorter_data < initial_data)); + CHECK((initial < shorter) == (initial_data < shorter_data)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_less_than) + { + const CData less(less_data.begin(), less_data.end(), buffer1, SIZE); + const CData initial(initial_data.begin(), initial_data.end(), buffer2, SIZE); + + CHECK((less < initial) == (less_data < initial_data)); + + const CData greater(greater_data.begin(), greater_data.end(), buffer3, SIZE); + + CHECK((greater < initial) == (greater_data < initial_data)); + + const CData shorter(shorter_data.begin(), shorter_data.end(), buffer4, SIZE); + + CHECK((shorter < initial) == (shorter_data < initial_data)); + CHECK((initial < shorter) == (initial_data < shorter_data)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_less_than_or_equal) + { + const Data less(less_data.begin(), less_data.end(), buffer1, SIZE); + const Data initial(initial_data.begin(), initial_data.end(), buffer2, SIZE); + + CHECK((less <= initial) == (less_data <= initial_data)); + + const Data greater(greater_data.begin(), greater_data.end(), buffer3, SIZE); + + CHECK((greater <= initial) == (greater_data <= initial_data)); + + const Data shorter(shorter_data.begin(), shorter_data.end(), buffer4, SIZE); + + CHECK((shorter <= initial) == (shorter_data <= initial_data)); + CHECK((initial <= shorter) == (initial_data <= shorter_data)); + + const Data initial2(initial_data.begin(), initial_data.end(), buffer5, SIZE); + CHECK((initial <= initial2) == (initial_data <= initial_data)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_less_than_or_equal) + { + const CData less(less_data.begin(), less_data.end(), buffer1, SIZE); + const CData initial(initial_data.begin(), initial_data.end(), buffer2, SIZE); + + CHECK((less <= initial) == (less_data <= initial_data)); + + const CData greater(greater_data.begin(), greater_data.end(), buffer3, SIZE); + + CHECK((greater <= initial) == (greater_data <= initial_data)); + + const CData shorter(shorter_data.begin(), shorter_data.end(), buffer4, SIZE); + + CHECK((shorter <= initial) == (shorter_data <= initial_data)); + CHECK((initial <= shorter) == (initial_data <= shorter_data)); + + const CData initial2(initial_data.begin(), initial_data.end(), buffer5, SIZE); + CHECK((initial <= initial2) == (initial_data <= initial_data)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_greater_than) + { + const Data less(less_data.begin(), less_data.end(), buffer1, SIZE); + const Data initial(initial_data.begin(), initial_data.end(), buffer2, SIZE); + + CHECK((less > initial) == (less_data > initial_data)); + + const Data greater(greater_data.begin(), greater_data.end(), buffer3, SIZE); + + CHECK((greater > initial) == (greater_data > initial_data)); + + const Data shorter(shorter_data.begin(), shorter_data.end(), buffer4, SIZE); + + CHECK((shorter > initial) == (shorter_data > initial_data)); + CHECK((initial > shorter) == (initial_data > shorter_data)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_greater_than) + { + const CData less(less_data.begin(), less_data.end(), buffer1, SIZE); + const CData initial(initial_data.begin(), initial_data.end(), buffer2, SIZE); + + CHECK((less > initial) == (less_data > initial_data)); + + const CData greater(greater_data.begin(), greater_data.end(), buffer3, SIZE); + + CHECK((greater > initial) == (greater_data > initial_data)); + + const CData shorter(shorter_data.begin(), shorter_data.end(), buffer4, SIZE); + + CHECK((shorter > initial) == (shorter_data > initial_data)); + CHECK((initial > shorter) == (initial_data > shorter_data)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_greater_than_or_equal) + { + const Data less(less_data.begin(), less_data.end(), buffer1, SIZE); + const Data initial(initial_data.begin(), initial_data.end(), buffer2, SIZE); + + CHECK((less >= initial) == (less_data >= initial_data)); + + const Data greater(greater_data.begin(), greater_data.end(), buffer3, SIZE); + + CHECK((greater >= initial) == (greater_data >= initial_data)); + + const Data shorter(shorter_data.begin(), shorter_data.end(), buffer4, SIZE); + + CHECK((shorter >= initial) == (shorter_data >= initial_data)); + CHECK((initial >= shorter) == (initial_data > shorter_data)); + + const Data initial2(initial_data.begin(), initial_data.end(), buffer5, SIZE); + CHECK((initial >= initial2) == (initial_data >= initial_data)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_greater_than_or_equal) + { + const CData less(less_data.begin(), less_data.end(), buffer1, SIZE); + const CData initial(initial_data.begin(), initial_data.end(), buffer2, SIZE); + + CHECK((less >= initial) == (less_data >= initial_data)); + + const CData greater(greater_data.begin(), greater_data.end(), buffer3, SIZE); + + CHECK((greater >= initial) == (greater_data >= initial_data)); + + const CData shorter(shorter_data.begin(), shorter_data.end(), buffer4, SIZE); + + CHECK((shorter >= initial) == (shorter_data >= initial_data)); + CHECK((initial >= shorter) == (initial_data > shorter_data)); + + const CData initial2(initial_data.begin(), initial_data.end(), buffer5, SIZE); + CHECK((initial >= initial2) == (initial_data >= initial_data)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_ivector_of_pointer_to_pointer) + { + int i1 = 1; + etl::vector consttest(buffer1, SIZE); + consttest.push_back(&i1); + const etl::ivector& ct = consttest; + + int* i2 = ct[0]; + + CHECK(i1 == *i2); + CHECK(&i1 == i2); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_ivector_of_pointer_to_const_pointer) + { + int i1 = 1; + etl::vector consttest(buffer1, SIZE); + consttest.push_back(&i1); + const etl::ivector& ct = consttest; + + const int* i2 = ct[0]; + + CHECK(i1 == *i2); + CHECK(&i1 == i2); + } + }; +} diff --git a/test/vs2017/etl.vcxproj b/test/vs2017/etl.vcxproj index 1e77e967..7da9d676 100644 --- a/test/vs2017/etl.vcxproj +++ b/test/vs2017/etl.vcxproj @@ -760,8 +760,10 @@ + + diff --git a/test/vs2017/etl.vcxproj.filters b/test/vs2017/etl.vcxproj.filters index c1c35c1f..c70fac98 100644 --- a/test/vs2017/etl.vcxproj.filters +++ b/test/vs2017/etl.vcxproj.filters @@ -1169,6 +1169,12 @@ Source Files + + Source Files + + + Source Files +
From 7feb3c1cc4dc456fedc96e7160b7cac15bc757b9 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sat, 6 Apr 2019 21:10:20 +0100 Subject: [PATCH 05/41] Created integral to_string. Started etl::string tests. --- include/etl/cstring.h | 2 + include/etl/format_spec.h | 165 +++++++++++++++++++++++++ include/etl/negative.h | 60 +++++++++ include/etl/private/to_string_helper.h | 120 ++++++++++++++++++ include/etl/to_string.h | 144 ++++++++++++++------- include/etl/to_u16string.h | 149 ++++++++++++++++++++++ include/etl/to_u32string.h | 149 ++++++++++++++++++++++ include/etl/to_wstring.h | 149 ++++++++++++++++++++++ include/etl/u16string.h | 2 + include/etl/u32string.h | 2 + include/etl/wstring.h | 2 + test/codeblocks/ETL.cbp | 7 ++ test/test_to_string.cpp | 85 ++++--------- test/vs2017/etl.vcxproj | 6 + test/vs2017/etl.vcxproj.filters | 18 +++ 15 files changed, 948 insertions(+), 112 deletions(-) create mode 100644 include/etl/format_spec.h create mode 100644 include/etl/negative.h create mode 100644 include/etl/private/to_string_helper.h create mode 100644 include/etl/to_u16string.h create mode 100644 include/etl/to_u32string.h create mode 100644 include/etl/to_wstring.h diff --git a/include/etl/cstring.h b/include/etl/cstring.h index 8fdd1aaa..147d3c4c 100644 --- a/include/etl/cstring.h +++ b/include/etl/cstring.h @@ -55,6 +55,8 @@ namespace etl { public: + typedef istring base_type; + typedef istring::value_type value_type; static const size_t MAX_SIZE = MAX_SIZE_; diff --git a/include/etl/format_spec.h b/include/etl/format_spec.h new file mode 100644 index 00000000..d8dca693 --- /dev/null +++ b/include/etl/format_spec.h @@ -0,0 +1,165 @@ +///\file + +/****************************************************************************** +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. +******************************************************************************/ + +#ifndef ETL_FORMAT_SPEC_INCLUDED +#define ETL_FORMAT_SPEC_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "type_traits.h" +#include "static_assert.h" + +namespace etl +{ + template + class format_spec + { + public: + + //*************************************************************************** + /// Default constructor. + /// Sets:- + /// Base = 10 + /// Width = 0 + /// Upper case (for hex) = true + /// Rifght Justified = true + //*************************************************************************** + format_spec() + : base_(10) + , width_(0) + , upper_case_(true) + , right_justified_(true) + , fill_(typename TString::value_type(' ')) + { + + } + + //*************************************************************************** + /// Sets the base. + /// \return A reference to the format_spec. + //*************************************************************************** + format_spec& base(const uint32_t b) + { + base_ = static_cast(b); + return *this; + } + + //*************************************************************************** + /// Gets the base. + //*************************************************************************** + uint32_t base() const + { + return base_; + } + + //*************************************************************************** + /// Sets the width. + /// \return A reference to the format_spec. + //*************************************************************************** + format_spec& width(const uint32_t w) + { + width_ = static_cast(w); + return *this; + } + + //*************************************************************************** + /// Gets the width. + //*************************************************************************** + uint32_t width() const + { + return width_; + } + + //*************************************************************************** + /// Sets the upper case flag. + /// \return A reference to the format_spec. + //*************************************************************************** + format_spec& upper_case(const bool u) + { + upper_case_ = u; + return *this; + } + + //*************************************************************************** + /// Gets the upper case flag. + //*************************************************************************** + bool upper_case(const bool u) const + { + return upper_case_; + } + + //*************************************************************************** + /// Sets the fill character. + /// \return A reference to the format_spec. + //*************************************************************************** + format_spec& fill(const typename TString::value_type c) + { + fill_ = c; + return *this; + } + + //*************************************************************************** + /// Gets the fill character. + //*************************************************************************** + typename TString::value_type fill() const + { + return fill_; + } + + //*************************************************************************** + /// Sets the right justify flag. + /// \return A reference to the format_spec. + //*************************************************************************** + format_spec& right_justified(const bool r) + { + right_justified_ = r; + return *this; + } + + //*************************************************************************** + /// Gets the right justify flag. + //*************************************************************************** + bool right_justified() const + { + return right_justified_; + } + + private: + + uint_least8_t base_; + uint_least8_t width_; + bool upper_case_; + bool right_justified_; + typename TString::value_type fill_; + }; +} + +#endif diff --git a/include/etl/negative.h b/include/etl/negative.h new file mode 100644 index 00000000..7b2a6b33 --- /dev/null +++ b/include/etl/negative.h @@ -0,0 +1,60 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2018 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. +******************************************************************************/ + +#ifndef ETL_NEGATIVE_INCLUDED +#define ETL_NEGATIVE_INCLUDED + +#include "type_traits.h" + +namespace etl +{ + //*************************************************************************** + // For signed types. + //*************************************************************************** + template + typename etl::enable_if::value, bool>::type + is_negative(const T value) + { + return (value < T(0)); + } + + //*************************************************************************** + // For unsigned types. + //*************************************************************************** + template + typename etl::enable_if::value, T>::type + is_negative(const T) + { + return false; + } +} + +#endif + diff --git a/include/etl/private/to_string_helper.h b/include/etl/private/to_string_helper.h new file mode 100644 index 00000000..903f4e39 --- /dev/null +++ b/include/etl/private/to_string_helper.h @@ -0,0 +1,120 @@ +///\file + +/****************************************************************************** +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. +******************************************************************************/ + +#ifndef ETL_TO_STRING_HELPER_INCLUDED +#define ETL_TO_STRING_HELPER_INCLUDED + +///\ingroup private + +#include "../platform.h" +#include "../absolute.h" +#include "../negative.h" +#include "../format_spec.h" +#include "../type_traits.h" + +#include "../stl/algorithm.h" +#include "../stl/iterator.h" + +namespace etl +{ + //*************************************************************************** + /// Helper function for integrals. + //*************************************************************************** + template + typename etl::enable_if::value, const TIString&>::type + to_string_helper(T value, + TIString& str, + const etl::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.base())); + str.push_back((remainder > 9) ? type('a' + (remainder - 10)) : type('0' + remainder)); + value = value / T(format.base()); + } + + // 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.right_justified()) + { + // Insert fill characters on the left. + str.insert(start, fill_length, format.fill()); + } + else + { + // Insert fill characters on the right. + str.insert(str.end(), fill_length, format.fill()); + } + } + } + + return str; + } +} + +#endif diff --git a/include/etl/to_string.h b/include/etl/to_string.h index 54c5021b..51f1991b 100644 --- a/include/etl/to_string.h +++ b/include/etl/to_string.h @@ -28,73 +28,121 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ -#ifndef ETL_TO_STRING_INCLUDED -#define ETL_TO_STRING_INCLUDED +#ifndef ETL_TO_U32STRING_INCLUDED +#define ETL_TO_U32STRING_INCLUDED -///\ingroup utilities +///\ingroup string #include "platform.h" #include "type_traits.h" -#include "stl/algorithm.h" +#include "cstring.h" +#include "format_spec.h" +#include "private/to_string_helper.h" namespace etl { - template - void to_string_helper(T value, const bool negative, TString& str, const int base, const bool upper_case) + //*************************************************************************** + /// For signed integrals less than 64 bits. 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) { - typedef typename TString::value_type type; - typedef typename TString::iterator iterator; + etl::format_spec format; - iterator start = str.end(); - - if (value == 0) - { - str.push_back(type('0')); - } - else - { - // Extract the digits, in reverse order. - while (value != 0) - { - T remainder = value % base; - str.push_back((remainder > 9) ? type('a' + (remainder - 10)) : type('0' + remainder)); - value = value / base; - } - - // If number is negative, append '-' - if ((base == 10) && negative) - { - str.push_back(type('-')); - } - - // Reverse the string we appended. - std::reverse(start, str.end()); - } + return to_string_helper(int32_t(value), str, format, append); } //*************************************************************************** - /// + /// For signed integrals less than 64 bits. Supplied format spec. //*************************************************************************** - template - typename etl::enable_if::value && etl::is_signed::value, void>::type - to_string(T value, TString& str, const int base = 10, const bool upper_case = true) + 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) { - typedef typename etl::make_unsigned::type Unsigned; - - const bool negative = (value < 0); - const Unsigned uvalue = (negative ? -value : value); - - to_string_helper(uvalue, negative, str, base, upper_case); + 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, void>::type - to_string(T value, TString& str, const int base = 10, const bool upper_case = true) + 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) { - to_string_helper(value, false, str, base, upper_case); + 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); } } diff --git a/include/etl/to_u16string.h b/include/etl/to_u16string.h new file mode 100644 index 00000000..15e05d7b --- /dev/null +++ b/include/etl/to_u16string.h @@ -0,0 +1,149 @@ +///\file + +/****************************************************************************** +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. +******************************************************************************/ + +#ifndef ETL_TO_U16STRING_INCLUDED +#define ETL_TO_U16STRING_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "type_traits.h" +#include "u16string.h" +#include "format_spec.h" +#include "private/to_string_helper.h" + +namespace etl +{ + //*************************************************************************** + /// For signed integrals less than 64 bits. Default format spec. + //*************************************************************************** + 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::format_spec format; + + return to_string_helper(int32_t(value), str, format, append); + } + + //*************************************************************************** + /// For signed integrals less than 64 bits. Supplied format spec. + //*************************************************************************** + 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) + { + 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::iu16string&>::type + to_string(const T value, etl::iu16string& 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::iu16string&>::type + to_string(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); + } + + //*************************************************************************** + /// For signed 64 bit integrals. Default format spec. + //*************************************************************************** + 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::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::iu16string&>::type + to_string(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); + } + + //*************************************************************************** + /// For unsigned 64 bit integrals. Default format spec. + //*************************************************************************** + 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::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::iu16string&>::type + to_string(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); + } +} + +#endif diff --git a/include/etl/to_u32string.h b/include/etl/to_u32string.h new file mode 100644 index 00000000..d2c7baee --- /dev/null +++ b/include/etl/to_u32string.h @@ -0,0 +1,149 @@ +///\file + +/****************************************************************************** +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. +******************************************************************************/ + +#ifndef ETL_TO_U32STRING_INCLUDED +#define ETL_TO_U32STRING_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "type_traits.h" +#include "u32string.h" +#include "format_spec.h" +#include "private/to_string_helper.h" + +namespace etl +{ + //*************************************************************************** + /// For signed integrals less than 64 bits. Default format spec. + //*************************************************************************** + 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::format_spec format; + + return to_string_helper(int32_t(value), str, format, append); + } + + //*************************************************************************** + /// For signed integrals less than 64 bits. Supplied format spec. + //*************************************************************************** + 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) + { + 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::iu32string&>::type + to_string(const T value, etl::iu32string& 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::iu32string&>::type + to_string(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); + } + + //*************************************************************************** + /// For signed 64 bit integrals. Default format spec. + //*************************************************************************** + 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::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::iu32string&>::type + to_string(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); + } + + //*************************************************************************** + /// For unsigned 64 bit integrals. Default format spec. + //*************************************************************************** + 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::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::iu32string&>::type + to_string(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); + } +} + +#endif diff --git a/include/etl/to_wstring.h b/include/etl/to_wstring.h new file mode 100644 index 00000000..f84fe55a --- /dev/null +++ b/include/etl/to_wstring.h @@ -0,0 +1,149 @@ +///\file + +/****************************************************************************** +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. +******************************************************************************/ + +#ifndef ETL_TO_WSTRING_INCLUDED +#define ETL_TO_WSTRING_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "type_traits.h" +#include "wstring.h" +#include "format_spec.h" +#include "private/to_string_helper.h" + +namespace etl +{ + //*************************************************************************** + /// For signed integrals less than 64 bits. Default format spec. + //*************************************************************************** + 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::format_spec format; + + return to_string_helper(int32_t(value), str, format, append); + } + + //*************************************************************************** + /// For signed integrals less than 64 bits. Supplied format spec. + //*************************************************************************** + 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) + { + 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::iwstring&>::type + to_string(const T value, etl::iwstring& 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::iwstring&>::type + to_string(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); + } + + //*************************************************************************** + /// For signed 64 bit integrals. Default format spec. + //*************************************************************************** + 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::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::iwstring&>::type + to_string(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); + } + + //*************************************************************************** + /// For unsigned 64 bit integrals. Default format spec. + //*************************************************************************** + 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::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::iwstring&>::type + to_string(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); + } +} + +#endif diff --git a/include/etl/u16string.h b/include/etl/u16string.h index f0d7a08f..0a8d271c 100644 --- a/include/etl/u16string.h +++ b/include/etl/u16string.h @@ -55,6 +55,8 @@ namespace etl { public: + typedef iu16string base_type; + typedef iu16string::value_type value_type; static const size_t MAX_SIZE = MAX_SIZE_; diff --git a/include/etl/u32string.h b/include/etl/u32string.h index 27792fcb..3ad04ae6 100644 --- a/include/etl/u32string.h +++ b/include/etl/u32string.h @@ -55,6 +55,8 @@ namespace etl { public: + typedef iu32string base_type; + typedef iu32string::value_type value_type; static const size_t MAX_SIZE = MAX_SIZE_; diff --git a/include/etl/wstring.h b/include/etl/wstring.h index 388525c7..44ba1798 100644 --- a/include/etl/wstring.h +++ b/include/etl/wstring.h @@ -55,6 +55,8 @@ namespace etl { public: + typedef iwstring base_type; + typedef iwstring::value_type value_type; static const size_t MAX_SIZE = MAX_SIZE_; diff --git a/test/codeblocks/ETL.cbp b/test/codeblocks/ETL.cbp index daf6c462..05f44e6b 100644 --- a/test/codeblocks/ETL.cbp +++ b/test/codeblocks/ETL.cbp @@ -197,6 +197,7 @@ + @@ -247,6 +248,7 @@ + @@ -297,6 +299,10 @@ + + + + @@ -428,6 +434,7 @@ + diff --git a/test/test_to_string.cpp b/test/test_to_string.cpp index 5453ab57..9fa6c684 100644 --- a/test/test_to_string.cpp +++ b/test/test_to_string.cpp @@ -32,84 +32,41 @@ SOFTWARE. #include "etl/to_string.h" #include "etl/cstring.h" +#include "etl/format_spec.h" #undef STR #define STR(x) x namespace { - /* A utility function to reverse a string */ - void reverse(char str[], int length) - { - int start = 0; - int end = length - 1; - while (start < end) - { - std::swap(*(str + start), *(str + end)); - start++; - end--; - } - } - - // Implementation of itoa() - char* itoa(int num, char* str, int base) - { - int i = 0; - bool isNegative = false; - - /* Handle 0 explicitely, otherwise empty string is printed for 0 */ - if (num == 0) - { - str[i++] = '0'; - str[i] = '\0'; - return str; - } - - // In standard itoa(), negative numbers are handled only with - // base 10. Otherwise numbers are considered unsigned. - if (num < 0 && base == 10) - { - isNegative = true; - num = -num; - } - - // Process individual digits - while (num != 0) - { - int rem = num % base; - str[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0'; - num = num / base; - } - - // If number is negative, append '-' - if (isNegative) - str[i++] = '-'; - - str[i] = '\0'; // Append string terminator - - // Reverse the string - reverse(str, i); - - return str; - } + typedef etl::format_spec Format; SUITE(test_string_char) { //************************************************************************* - TEST(test_x) + TEST(test_default_format_no_append) { - //char str[100]; + etl::string<20> str; - etl::string<10> 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)); - etl::to_string(char(127), str, 10); + 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)); - str.clear(); - etl::to_string(char(-128), str, 10); - //itoa(-1567, str, 10); - //itoa(1567, str, 2); - //itoa(1567, str, 8); - //itoa(1567, str, 16); + 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(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)); } }; } diff --git a/test/vs2017/etl.vcxproj b/test/vs2017/etl.vcxproj index 7da9d676..ac1dcfeb 100644 --- a/test/vs2017/etl.vcxproj +++ b/test/vs2017/etl.vcxproj @@ -371,12 +371,15 @@ + + + @@ -435,6 +438,9 @@ + + + diff --git a/test/vs2017/etl.vcxproj.filters b/test/vs2017/etl.vcxproj.filters index c70fac98..59beb25d 100644 --- a/test/vs2017/etl.vcxproj.filters +++ b/test/vs2017/etl.vcxproj.filters @@ -735,6 +735,24 @@ ETL\Utilities + + ETL\Utilities + + + ETL\Utilities + + + ETL\Utilities + + + ETL\Utilities + + + ETL\Utilities + + + ETL\Private + From 5220837979998b4082b8a98323fa1511b36dbab8 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 7 Apr 2019 15:37:20 +0100 Subject: [PATCH 06/41] Completed unit tests --- include/etl/format_spec.h | 20 +- include/etl/private/to_string_helper.h | 16 +- include/etl/to_u16string.h | 32 ++-- include/etl/to_u32string.h | 32 ++-- include/etl/to_wstring.h | 32 ++-- test/test_to_string.cpp | 206 +++++++++++++++++++-- test/test_to_u16string.cpp | 242 +++++++++++++++++++++++++ test/test_to_u32string.cpp | 242 +++++++++++++++++++++++++ test/test_to_wstring.cpp | 242 +++++++++++++++++++++++++ test/vs2017/etl.vcxproj | 3 + test/vs2017/etl.vcxproj.filters | 9 + 11 files changed, 992 insertions(+), 84 deletions(-) create mode 100644 test/test_to_u16string.cpp create mode 100644 test/test_to_u32string.cpp create mode 100644 test/test_to_wstring.cpp 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 + From e122b380a35c6ce974bc449856dc81a1519ce8ad Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Mon, 8 Apr 2019 13:58:48 +0100 Subject: [PATCH 07/41] Added named base settings --- include/etl/format_spec.h | 39 ++++++++++++++++++++++++++++++++++++++ test/test_to_string.cpp | 11 +++++++++++ test/test_to_u16string.cpp | 11 +++++++++++ test/test_to_u32string.cpp | 12 ++++++++++++ test/test_to_wstring.cpp | 11 +++++++++++ 5 files changed, 84 insertions(+) diff --git a/include/etl/format_spec.h b/include/etl/format_spec.h index 74787281..238bc721 100644 --- a/include/etl/format_spec.h +++ b/include/etl/format_spec.h @@ -72,6 +72,45 @@ namespace etl return *this; } + //*************************************************************************** + /// Sets the base to binary. + /// \return A reference to the format_spec. + //*************************************************************************** + format_spec& binary() + { + base(2); + return *this; + } + + //*************************************************************************** + /// Sets the base to octal. + /// \return A reference to the format_spec. + //*************************************************************************** + format_spec& octal() + { + base(8); + return *this; + } + + //*************************************************************************** + /// Sets the base to decimal. + /// \return A reference to the format_spec. + //*************************************************************************** + format_spec& decimal() + { + base(10); + return *this; + } + + //*************************************************************************** + /// Sets the base to hex. + /// \return A reference to the format_spec. + //*************************************************************************** + format_spec& hex() + { + base(16); + return *this; + } //*************************************************************************** /// Gets the base. //*************************************************************************** diff --git a/test/test_to_string.cpp b/test/test_to_string.cpp index fc70aded..462dc7f2 100644 --- a/test/test_to_string.cpp +++ b/test/test_to_string.cpp @@ -238,5 +238,16 @@ namespace 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')))); } + + //************************************************************************* + TEST(test_named_format_no_append) + { + etl::string<17> str; + + CHECK_EQUAL(etl::string<17>(STR("11110001001000000")), etl::to_string(123456, str, Format().binary())); + CHECK_EQUAL(etl::string<17>(STR("361100")), etl::to_string(123456, str, Format().octal())); + 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())); + } }; } diff --git a/test/test_to_u16string.cpp b/test/test_to_u16string.cpp index 7e62521e..ed3a8e4b 100644 --- a/test/test_to_u16string.cpp +++ b/test/test_to_u16string.cpp @@ -238,5 +238,16 @@ namespace 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')))); } + + //************************************************************************* + TEST(test_named_format_no_append) + { + etl::u16string<17> str; + + CHECK_EQUAL(etl::u16string<17>(STR("11110001001000000")), etl::to_u16string(123456, str, Format().binary())); + CHECK_EQUAL(etl::u16string<17>(STR("361100")), etl::to_u16string(123456, str, Format().octal())); + 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())); + } }; } diff --git a/test/test_to_u32string.cpp b/test/test_to_u32string.cpp index ce161691..88482fce 100644 --- a/test/test_to_u32string.cpp +++ b/test/test_to_u32string.cpp @@ -238,5 +238,17 @@ namespace 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')))); } + + + //************************************************************************* + TEST(test_named_format_no_append) + { + etl::u32string<17> str; + + CHECK_EQUAL(etl::u32string<17>(STR("11110001001000000")), etl::to_u32string(123456, str, Format().binary())); + CHECK_EQUAL(etl::u32string<17>(STR("361100")), etl::to_u32string(123456, str, Format().octal())); + 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())); + } }; } diff --git a/test/test_to_wstring.cpp b/test/test_to_wstring.cpp index d3094865..53cfca93 100644 --- a/test/test_to_wstring.cpp +++ b/test/test_to_wstring.cpp @@ -238,5 +238,16 @@ namespace 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')))); } + + //************************************************************************* + TEST(test_named_format_no_append) + { + etl::wstring<17> str; + + CHECK_EQUAL(etl::wstring<17>(STR("11110001001000000")), etl::to_wstring(123456, str, Format().binary())); + CHECK_EQUAL(etl::wstring<17>(STR("361100")), etl::to_wstring(123456, str, Format().octal())); + 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())); + } }; } From 4b79628bae93e84f88b781a1a853f3897dfc7547 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Mon, 8 Apr 2019 19:32:50 +0100 Subject: [PATCH 08/41] Added etl::to_string, etl::to_wstring, etl::to_u16string and etl::to_u32string plus formating support for integrals. Updated version number. --- include/etl/version.h | 2 +- support/Release notes.txt | 5 +++++ test/codeblocks/ETL.cbp | 3 +++ test/test_to_string.cpp | 14 +++++++------- test/test_to_u16string.cpp | 14 +++++++------- test/test_to_u32string.cpp | 14 +++++++------- test/test_to_wstring.cpp | 14 +++++++------- 7 files changed, 37 insertions(+), 29 deletions(-) diff --git a/include/etl/version.h b/include/etl/version.h index 85d56f97..073313ad 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -38,7 +38,7 @@ SOFTWARE. ///\ingroup utilities #define ETL_VERSION_MAJOR 14 -#define ETL_VERSION_MINOR 17 +#define ETL_VERSION_MINOR 18 #define ETL_VERSION_PATCH 0 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) ETL_STRINGIFY(ETL_VERSION_MINOR) ETL_STRINGIFY(ETL_VERSION_PATCH) diff --git a/support/Release notes.txt b/support/Release notes.txt index 1ef4d2dc..60355b97 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,8 @@ +=============================================================================== +14.18.0 +Added etl::to_string, etl::to_wstring, etl::to_u16string and etl::to_u32string +plus formating support for integrals. + =============================================================================== 14.17.0 Added C++11 variadic templates for etl::observer. diff --git a/test/codeblocks/ETL.cbp b/test/codeblocks/ETL.cbp index 05f44e6b..8cd1b63a 100644 --- a/test/codeblocks/ETL.cbp +++ b/test/codeblocks/ETL.cbp @@ -435,6 +435,9 @@ + + + diff --git a/test/test_to_string.cpp b/test/test_to_string.cpp index 462dc7f2..c6564a89 100644 --- a/test/test_to_string.cpp +++ b/test/test_to_string.cpp @@ -76,7 +76,7 @@ namespace 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)); + CHECK_EQUAL(etl::string<20>(STR("-9223372036854775808")), etl::to_string(int64_t(-9223372036854775807ll - 1), str)); } //************************************************************************* @@ -102,7 +102,7 @@ namespace 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)); + CHECK_EQUAL(etl::string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648-9223372036854775808")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, true)); } //************************************************************************* @@ -130,7 +130,7 @@ namespace 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)); + CHECK_EQUAL(etl::string<20>(STR("-9223372036854775808")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, format)); } //************************************************************************* @@ -158,7 +158,7 @@ namespace 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)); + CHECK_EQUAL(etl::string<20>(STR("-9223372036854775808")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, format)); } //************************************************************************* @@ -184,7 +184,7 @@ namespace 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')))); + CHECK_EQUAL(etl::string<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, Format().base(2).width(64).fill(STR('0')))); } //************************************************************************* @@ -210,7 +210,7 @@ namespace 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')))); + CHECK_EQUAL(etl::string<22>(STR("1000000000000000000000")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, Format().base(8).width(22).fill(STR('0')))); } //************************************************************************* @@ -236,7 +236,7 @@ namespace 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')))); + CHECK_EQUAL(etl::string<16>(STR("8000000000000000")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, Format().base(16).width(16).fill(STR('0')))); } //************************************************************************* diff --git a/test/test_to_u16string.cpp b/test/test_to_u16string.cpp index ed3a8e4b..facd3363 100644 --- a/test/test_to_u16string.cpp +++ b/test/test_to_u16string.cpp @@ -76,7 +76,7 @@ namespace 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)); + CHECK_EQUAL(etl::u16string<20>(STR("-9223372036854775808")), etl::to_u16string(int64_t(-9223372036854775807ll - 1), str)); } //************************************************************************* @@ -102,7 +102,7 @@ namespace 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)); + CHECK_EQUAL(etl::u16string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648-9223372036854775808")), etl::to_u16string(int64_t(-9223372036854775807ll - 1), str, true)); } //************************************************************************* @@ -130,7 +130,7 @@ namespace 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)); + CHECK_EQUAL(etl::u16string<20>(STR("-9223372036854775808")), etl::to_u16string(int64_t(-9223372036854775807ll - 1), str, format)); } //************************************************************************* @@ -158,7 +158,7 @@ namespace 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)); + CHECK_EQUAL(etl::u16string<20>(STR("-9223372036854775808")), etl::to_u16string(int64_t(-9223372036854775807ll - 1), str, format)); } //************************************************************************* @@ -184,7 +184,7 @@ namespace 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')))); + CHECK_EQUAL(etl::u16string<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_u16string(int64_t(-9223372036854775807ll - 1), str, Format().base(2).width(64).fill(STR('0')))); } //************************************************************************* @@ -210,7 +210,7 @@ namespace 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')))); + CHECK_EQUAL(etl::u16string<22>(STR("1000000000000000000000")), etl::to_u16string(int64_t(-9223372036854775807ll - 1), str, Format().base(8).width(22).fill(STR('0')))); } //************************************************************************* @@ -236,7 +236,7 @@ namespace 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')))); + CHECK_EQUAL(etl::u16string<16>(STR("8000000000000000")), etl::to_u16string(int64_t(-9223372036854775807ll - 1), str, Format().base(16).width(16).fill(STR('0')))); } //************************************************************************* diff --git a/test/test_to_u32string.cpp b/test/test_to_u32string.cpp index 88482fce..c3f34abd 100644 --- a/test/test_to_u32string.cpp +++ b/test/test_to_u32string.cpp @@ -76,7 +76,7 @@ namespace 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)); + CHECK_EQUAL(etl::u32string<20>(STR("-9223372036854775808")), etl::to_u32string(int64_t(-9223372036854775807ll - 1), str)); } //************************************************************************* @@ -102,7 +102,7 @@ namespace 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)); + CHECK_EQUAL(etl::u32string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648-9223372036854775808")), etl::to_u32string(int64_t(-9223372036854775807ll - 1), str, true)); } //************************************************************************* @@ -130,7 +130,7 @@ namespace 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)); + CHECK_EQUAL(etl::u32string<20>(STR("-9223372036854775808")), etl::to_u32string(int64_t(-9223372036854775807ll - 1), str, format)); } //************************************************************************* @@ -158,7 +158,7 @@ namespace 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)); + CHECK_EQUAL(etl::u32string<20>(STR("-9223372036854775808")), etl::to_u32string(int64_t(-9223372036854775807ll - 1), str, format)); } //************************************************************************* @@ -184,7 +184,7 @@ namespace 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')))); + CHECK_EQUAL(etl::u32string<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_u32string(int64_t(-9223372036854775807ll - 1), str, Format().base(2).width(64).fill(STR('0')))); } //************************************************************************* @@ -210,7 +210,7 @@ namespace 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')))); + CHECK_EQUAL(etl::u32string<22>(STR("1000000000000000000000")), etl::to_u32string(int64_t(-9223372036854775807ll - 1), str, Format().base(8).width(22).fill(STR('0')))); } //************************************************************************* @@ -236,7 +236,7 @@ namespace 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')))); + CHECK_EQUAL(etl::u32string<16>(STR("8000000000000000")), etl::to_u32string(int64_t(-9223372036854775807ll - 1), str, Format().base(16).width(16).fill(STR('0')))); } diff --git a/test/test_to_wstring.cpp b/test/test_to_wstring.cpp index 53cfca93..a97f3c13 100644 --- a/test/test_to_wstring.cpp +++ b/test/test_to_wstring.cpp @@ -76,7 +76,7 @@ namespace 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)); + CHECK_EQUAL(etl::wstring<20>(STR("-9223372036854775808")), etl::to_wstring(int64_t(-9223372036854775807ll - 1), str)); } //************************************************************************* @@ -102,7 +102,7 @@ namespace 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)); + CHECK_EQUAL(etl::wstring<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648-9223372036854775808")), etl::to_wstring(int64_t(-9223372036854775807ll - 1), str, true)); } //************************************************************************* @@ -130,7 +130,7 @@ namespace 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)); + CHECK_EQUAL(etl::wstring<20>(STR("-9223372036854775808")), etl::to_wstring(int64_t(-9223372036854775807ll - 1), str, format)); } //************************************************************************* @@ -158,7 +158,7 @@ namespace 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)); + CHECK_EQUAL(etl::wstring<20>(STR("-9223372036854775808")), etl::to_wstring(int64_t(-9223372036854775807ll - 1), str, format)); } //************************************************************************* @@ -184,7 +184,7 @@ namespace 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')))); + CHECK_EQUAL(etl::wstring<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_wstring(int64_t(-9223372036854775807ll - 1), str, Format().base(2).width(64).fill(STR('0')))); } //************************************************************************* @@ -210,7 +210,7 @@ namespace 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')))); + CHECK_EQUAL(etl::wstring<22>(STR("1000000000000000000000")), etl::to_wstring(int64_t(-9223372036854775807ll - 1), str, Format().base(8).width(22).fill(STR('0')))); } //************************************************************************* @@ -236,7 +236,7 @@ namespace 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')))); + CHECK_EQUAL(etl::wstring<16>(STR("8000000000000000")), etl::to_wstring(int64_t(-9223372036854775807ll - 1), str, Format().base(16).width(16).fill(STR('0')))); } //************************************************************************* From 11da8e133f5562809e4b22f7ce7efdda40e78bbe Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Tue, 9 Apr 2019 06:30:46 +0100 Subject: [PATCH 09/41] Changed etl::format_sepc template to etl::basic_firmat_spec. Created individual format_spec typedefs for each string type. --- include/etl/basic_format_spec.h | 204 +++++++++++++++++++++++++ include/etl/format_spec.h | 165 +------------------- include/etl/private/to_string_helper.h | 4 +- include/etl/to_string.h | 20 +-- include/etl/to_u16string.h | 18 +-- include/etl/to_u32string.h | 18 +-- include/etl/to_wstring.h | 18 +-- include/etl/u16format_spec.h | 45 ++++++ include/etl/u32format_spec.h | 45 ++++++ include/etl/version.h | 2 +- include/etl/wformat_spec.h | 45 ++++++ support/Release notes.txt | 5 + test/codeblocks/ETL.cbp | 4 + test/test_to_string.cpp | 2 +- test/test_to_u16string.cpp | 2 +- test/test_to_u32string.cpp | 2 +- test/test_to_wstring.cpp | 2 +- test/vs2017/etl.vcxproj | 4 + test/vs2017/etl.vcxproj.filters | 12 ++ 19 files changed, 411 insertions(+), 206 deletions(-) create mode 100644 include/etl/basic_format_spec.h create mode 100644 include/etl/u16format_spec.h create mode 100644 include/etl/u32format_spec.h create mode 100644 include/etl/wformat_spec.h diff --git a/include/etl/basic_format_spec.h b/include/etl/basic_format_spec.h new file mode 100644 index 00000000..38d48b48 --- /dev/null +++ b/include/etl/basic_format_spec.h @@ -0,0 +1,204 @@ +///\file + +/****************************************************************************** +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. +******************************************************************************/ + +#ifndef ETL_BASIC_FORMAT_SPEC_INCLUDED +#define ETL_BASIC_FORMAT_SPEC_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "type_traits.h" +#include "static_assert.h" + +namespace etl +{ + template + class basic_format_spec + { + public: + + //*************************************************************************** + /// Default constructor. + /// Sets:- + /// Base = 10 + /// Width = 0 + /// Upper case (for hex) = true + /// Left Justified = false + //*************************************************************************** + basic_format_spec() + : base_(10) + , width_(0) + , upper_case_(true) + , left_justified_(false) + , fill_(typename TString::value_type(' ')) + { + + } + + //*************************************************************************** + /// Sets the base. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& base(const uint32_t b) + { + base_ = static_cast(b); + return *this; + } + + //*************************************************************************** + /// Sets the base to binary. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& binary() + { + base(2); + return *this; + } + + //*************************************************************************** + /// Sets the base to octal. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& octal() + { + base(8); + return *this; + } + + //*************************************************************************** + /// Sets the base to decimal. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& decimal() + { + base(10); + return *this; + } + + //*************************************************************************** + /// Sets the base to hex. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& hex() + { + base(16); + return *this; + } + //*************************************************************************** + /// Gets the base. + //*************************************************************************** + uint32_t base() const + { + return base_; + } + + //*************************************************************************** + /// Sets the width. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& width(const uint32_t w) + { + width_ = static_cast(w); + return *this; + } + + //*************************************************************************** + /// Gets the width. + //*************************************************************************** + uint32_t width() const + { + return width_; + } + + //*************************************************************************** + /// Sets the upper case flag. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& upper_case(const bool u) + { + upper_case_ = u; + return *this; + } + + //*************************************************************************** + /// Gets the upper case flag. + //*************************************************************************** + bool upper_case() const + { + return upper_case_; + } + + //*************************************************************************** + /// Sets the fill character. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& fill(const typename TString::value_type c) + { + fill_ = c; + return *this; + } + + //*************************************************************************** + /// Gets the fill character. + //*************************************************************************** + typename TString::value_type fill() const + { + return fill_; + } + + //*************************************************************************** + /// Sets the left justify flag. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& left_justified(const bool l) + { + left_justified_ = l; + return *this; + } + + //*************************************************************************** + /// Gets the left justify flag. + //*************************************************************************** + bool left_justified() const + { + return left_justified_; + } + + private: + + uint_least8_t base_; + uint_least8_t width_; + bool upper_case_; + bool left_justified_; + typename TString::value_type fill_; + }; +} + +#endif diff --git a/include/etl/format_spec.h b/include/etl/format_spec.h index 238bc721..27cbcaa0 100644 --- a/include/etl/format_spec.h +++ b/include/etl/format_spec.h @@ -34,171 +34,12 @@ SOFTWARE. ///\ingroup string #include "platform.h" -#include "type_traits.h" -#include "static_assert.h" +#include "basic_format_spec.h" +#include "cstring.h" namespace etl { - template - class format_spec - { - public: - - //*************************************************************************** - /// Default constructor. - /// Sets:- - /// Base = 10 - /// Width = 0 - /// Upper case (for hex) = true - /// Left Justified = false - //*************************************************************************** - format_spec() - : base_(10) - , width_(0) - , upper_case_(true) - , left_justified_(false) - , fill_(typename TString::value_type(' ')) - { - - } - - //*************************************************************************** - /// Sets the base. - /// \return A reference to the format_spec. - //*************************************************************************** - format_spec& base(const uint32_t b) - { - base_ = static_cast(b); - return *this; - } - - //*************************************************************************** - /// Sets the base to binary. - /// \return A reference to the format_spec. - //*************************************************************************** - format_spec& binary() - { - base(2); - return *this; - } - - //*************************************************************************** - /// Sets the base to octal. - /// \return A reference to the format_spec. - //*************************************************************************** - format_spec& octal() - { - base(8); - return *this; - } - - //*************************************************************************** - /// Sets the base to decimal. - /// \return A reference to the format_spec. - //*************************************************************************** - format_spec& decimal() - { - base(10); - return *this; - } - - //*************************************************************************** - /// Sets the base to hex. - /// \return A reference to the format_spec. - //*************************************************************************** - format_spec& hex() - { - base(16); - return *this; - } - //*************************************************************************** - /// Gets the base. - //*************************************************************************** - uint32_t base() const - { - return base_; - } - - //*************************************************************************** - /// Sets the width. - /// \return A reference to the format_spec. - //*************************************************************************** - format_spec& width(const uint32_t w) - { - width_ = static_cast(w); - return *this; - } - - //*************************************************************************** - /// Gets the width. - //*************************************************************************** - uint32_t width() const - { - return width_; - } - - //*************************************************************************** - /// Sets the upper case flag. - /// \return A reference to the format_spec. - //*************************************************************************** - format_spec& upper_case(const bool u) - { - upper_case_ = u; - return *this; - } - - //*************************************************************************** - /// Gets the upper case flag. - //*************************************************************************** - bool upper_case() const - { - return upper_case_; - } - - //*************************************************************************** - /// Sets the fill character. - /// \return A reference to the format_spec. - //*************************************************************************** - format_spec& fill(const typename TString::value_type c) - { - fill_ = c; - return *this; - } - - //*************************************************************************** - /// Gets the fill character. - //*************************************************************************** - typename TString::value_type fill() const - { - return fill_; - } - - //*************************************************************************** - /// Sets the left justify flag. - /// \return A reference to the format_spec. - //*************************************************************************** - format_spec& left_justified(const bool l) - { - left_justified_ = l; - return *this; - } - - //*************************************************************************** - /// Gets the left justify flag. - //*************************************************************************** - bool left_justified() const - { - return left_justified_; - } - - private: - - uint_least8_t base_; - uint_least8_t width_; - bool upper_case_; - bool left_justified_; - typename TString::value_type fill_; - }; + typedef etl::basic_format_spec format_spec; } #endif diff --git a/include/etl/private/to_string_helper.h b/include/etl/private/to_string_helper.h index a384e1ec..ce4dfd50 100644 --- a/include/etl/private/to_string_helper.h +++ b/include/etl/private/to_string_helper.h @@ -36,7 +36,7 @@ SOFTWARE. #include "../platform.h" #include "../absolute.h" #include "../negative.h" -#include "../format_spec.h" +#include "../basic_format_spec.h" #include "../type_traits.h" #include "../stl/algorithm.h" @@ -51,7 +51,7 @@ namespace etl typename etl::enable_if::value, TIString&>::type to_string_helper(T value, TIString& str, - const etl::format_spec& format, + const etl::basic_format_spec& format, const bool append) { typedef typename TIString::value_type type; diff --git a/include/etl/to_string.h b/include/etl/to_string.h index 51f1991b..058ac099 100644 --- a/include/etl/to_string.h +++ b/include/etl/to_string.h @@ -28,8 +28,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ -#ifndef ETL_TO_U32STRING_INCLUDED -#define ETL_TO_U32STRING_INCLUDED +#ifndef ETL_TO_STRING_INCLUDED +#define ETL_TO_STRING_INCLUDED ///\ingroup string @@ -50,7 +50,7 @@ namespace etl !etl::is_same::value, const etl::istring&>::type to_string(const T value, etl::istring& str, const bool append = false) { - etl::format_spec format; + etl::format_spec format; return to_string_helper(int32_t(value), str, format, append); } @@ -62,7 +62,7 @@ namespace etl 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) + 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); } @@ -76,7 +76,7 @@ namespace etl !etl::is_same::value, const etl::istring&>::type to_string(const T value, etl::istring& str, const bool append = false) { - etl::format_spec format; + etl::format_spec format; return to_string_helper(uint32_t(value), str, format, append); } @@ -88,7 +88,7 @@ namespace etl 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) + 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); } @@ -102,7 +102,7 @@ namespace etl etl::is_same::value, const etl::istring&>::type to_string(const T value, etl::istring& str, const bool append = false) { - etl::format_spec format; + etl::format_spec format; return to_string_helper(int64_t(value), str, format, append); } @@ -114,7 +114,7 @@ namespace etl 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) + 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); } @@ -128,7 +128,7 @@ namespace etl etl::is_same::value, const etl::istring&>::type to_string(const T value, etl::istring& str, const bool append = false) { - etl::format_spec format; + etl::format_spec format; return to_string_helper(uint64_t(value), str, format, append); } @@ -140,7 +140,7 @@ namespace etl 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) + 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); } diff --git a/include/etl/to_u16string.h b/include/etl/to_u16string.h index 09a5ce0f..46bfbb59 100644 --- a/include/etl/to_u16string.h +++ b/include/etl/to_u16string.h @@ -36,7 +36,7 @@ SOFTWARE. #include "platform.h" #include "type_traits.h" #include "u16string.h" -#include "format_spec.h" +#include "u16format_spec.h" #include "private/to_string_helper.h" namespace etl @@ -50,7 +50,7 @@ namespace etl !etl::is_same::value, etl::iu16string&>::type to_u16string(const T value, etl::iu16string& str, const bool append = false) { - etl::format_spec format; + etl::u16format_spec format; return to_string_helper(int32_t(value), str, format, append); } @@ -62,7 +62,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } @@ -76,7 +76,7 @@ namespace etl !etl::is_same::value, etl::iu16string&>::type to_u16string(const T value, etl::iu16string& str, const bool append = false) { - etl::format_spec format; + etl::u16format_spec format; return to_string_helper(uint32_t(value), str, format, append); } @@ -88,7 +88,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } @@ -102,7 +102,7 @@ namespace etl etl::is_same::value, etl::iu16string&>::type to_u16string(const T value, etl::iu16string& str, const bool append = false) { - etl::format_spec format; + etl::u16format_spec format; return to_string_helper(int64_t(value), str, format, append); } @@ -114,7 +114,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } @@ -128,7 +128,7 @@ namespace etl etl::is_same::value, etl::iu16string&>::type to_u16string(const T value, etl::iu16string& str, const bool append = false) { - etl::format_spec format; + etl::u16format_spec format; return to_string_helper(uint64_t(value), str, format, append); } @@ -140,7 +140,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } diff --git a/include/etl/to_u32string.h b/include/etl/to_u32string.h index 9eaf6110..6caa403c 100644 --- a/include/etl/to_u32string.h +++ b/include/etl/to_u32string.h @@ -36,7 +36,7 @@ SOFTWARE. #include "platform.h" #include "type_traits.h" #include "u32string.h" -#include "format_spec.h" +#include "u32format_spec.h" #include "private/to_string_helper.h" namespace etl @@ -50,7 +50,7 @@ namespace etl !etl::is_same::value, etl::iu32string&>::type to_u32string(const T value, etl::iu32string& str, const bool append = false) { - etl::format_spec format; + etl::u32format_spec format; return to_string_helper(int32_t(value), str, format, append); } @@ -62,7 +62,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } @@ -76,7 +76,7 @@ namespace etl !etl::is_same::value, etl::iu32string&>::type to_u32string(const T value, etl::iu32string& str, const bool append = false) { - etl::format_spec format; + etl::u32format_spec format; return to_string_helper(uint32_t(value), str, format, append); } @@ -88,7 +88,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } @@ -102,7 +102,7 @@ namespace etl etl::is_same::value, etl::iu32string&>::type to_u32string(const T value, etl::iu32string& str, const bool append = false) { - etl::format_spec format; + etl::u32format_spec format; return to_string_helper(int64_t(value), str, format, append); } @@ -114,7 +114,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } @@ -128,7 +128,7 @@ namespace etl etl::is_same::value, etl::iu32string&>::type to_u32string(const T value, etl::iu32string& str, const bool append = false) { - etl::format_spec format; + etl::u32format_spec format; return to_string_helper(uint64_t(value), str, format, append); } @@ -140,7 +140,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } diff --git a/include/etl/to_wstring.h b/include/etl/to_wstring.h index ff55b483..9bd39944 100644 --- a/include/etl/to_wstring.h +++ b/include/etl/to_wstring.h @@ -36,7 +36,7 @@ SOFTWARE. #include "platform.h" #include "type_traits.h" #include "wstring.h" -#include "format_spec.h" +#include "wformat_spec.h" #include "private/to_string_helper.h" namespace etl @@ -50,7 +50,7 @@ namespace etl !etl::is_same::value, etl::iwstring&>::type to_wstring(const T value, etl::iwstring& str, const bool append = false) { - etl::format_spec format; + etl::wformat_spec format; return to_string_helper(int32_t(value), str, format, append); } @@ -62,7 +62,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } @@ -76,7 +76,7 @@ namespace etl !etl::is_same::value, etl::iwstring&>::type to_wstring(const T value, etl::iwstring& str, const bool append = false) { - etl::format_spec format; + etl::wformat_spec format; return to_string_helper(uint32_t(value), str, format, append); } @@ -88,7 +88,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } @@ -102,7 +102,7 @@ namespace etl etl::is_same::value, etl::iwstring&>::type to_wstring(const T value, etl::iwstring& str, const bool append = false) { - etl::format_spec format; + etl::wformat_spec format; return to_string_helper(int64_t(value), str, format, append); } @@ -114,7 +114,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } @@ -128,7 +128,7 @@ namespace etl etl::is_same::value, etl::iwstring&>::type to_wstring(const T value, etl::iwstring& str, const bool append = false) { - etl::format_spec format; + etl::wformat_spec format; return to_string_helper(uint64_t(value), str, format, append); } @@ -140,7 +140,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } diff --git a/include/etl/u16format_spec.h b/include/etl/u16format_spec.h new file mode 100644 index 00000000..39c523ca --- /dev/null +++ b/include/etl/u16format_spec.h @@ -0,0 +1,45 @@ +///\file + +/****************************************************************************** +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. +******************************************************************************/ + +#ifndef ETL_U16FORMAT_SPEC_INCLUDED +#define ETL_U16FORMAT_SPEC_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "basic_format_spec.h" +#include "u16string.h" + +namespace etl +{ + typedef etl::basic_format_spec u16format_spec; +} + +#endif diff --git a/include/etl/u32format_spec.h b/include/etl/u32format_spec.h new file mode 100644 index 00000000..2a9d34b8 --- /dev/null +++ b/include/etl/u32format_spec.h @@ -0,0 +1,45 @@ +///\file + +/****************************************************************************** +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. +******************************************************************************/ + +#ifndef ETL_U32FORMAT_SPEC_INCLUDED +#define ETL_U32FORMAT_SPEC_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "basic_format_spec.h" +#include "u32string.h" + +namespace etl +{ + typedef etl::basic_format_spec u32format_spec; +} + +#endif diff --git a/include/etl/version.h b/include/etl/version.h index 073313ad..c1d96ebd 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -39,7 +39,7 @@ SOFTWARE. #define ETL_VERSION_MAJOR 14 #define ETL_VERSION_MINOR 18 -#define ETL_VERSION_PATCH 0 +#define ETL_VERSION_PATCH 1 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) ETL_STRINGIFY(ETL_VERSION_MINOR) ETL_STRINGIFY(ETL_VERSION_PATCH) #define ETL_VERSION_W ETL_WIDE_STRING(ETL_CONCAT(ETL_CONCAT(ETL_VERSION_MAJOR, ETL_VERSION_MINOR), ETL_VERSION_PATCH)) diff --git a/include/etl/wformat_spec.h b/include/etl/wformat_spec.h new file mode 100644 index 00000000..0ff2d57e --- /dev/null +++ b/include/etl/wformat_spec.h @@ -0,0 +1,45 @@ +///\file + +/****************************************************************************** +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. +******************************************************************************/ + +#ifndef ETL_WFORMAT_SPEC_INCLUDED +#define ETL_WFORMAT_SPEC_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "basic_format_spec.h" +#include "wstring.h" + +namespace etl +{ + typedef etl::basic_format_spec wformat_spec; +} + +#endif diff --git a/support/Release notes.txt b/support/Release notes.txt index 60355b97..e43c898e 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,8 @@ +=============================================================================== +14.18.1 +Changed etl::format_sepc template to etl::basic_firmat_spec. +Created individual format_spec typedefs for each string type. + =============================================================================== 14.18.0 Added etl::to_string, etl::to_wstring, etl::to_u16string and etl::to_u32string diff --git a/test/codeblocks/ETL.cbp b/test/codeblocks/ETL.cbp index 8cd1b63a..a7041a42 100644 --- a/test/codeblocks/ETL.cbp +++ b/test/codeblocks/ETL.cbp @@ -154,6 +154,7 @@ + @@ -309,7 +310,9 @@ + + @@ -323,6 +326,7 @@ + From 494322e2a00b062423da736792ccc49aa8c1bf29 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Tue, 9 Apr 2019 06:36:36 +0100 Subject: [PATCH 10/41] Merge remote-tracking branch 'origin/feature/to_string' into development # Conflicts: # include/etl/format_spec.h # include/etl/private/to_string_helper.h # include/etl/to_string.h # include/etl/to_u16string.h # include/etl/to_u32string.h # include/etl/to_wstring.h # include/etl/version.h # support/Release notes.txt # test/test_to_string.cpp # test/test_to_u16string.cpp # test/test_to_u32string.cpp # test/test_to_wstring.cpp # test/vs2017/etl.vcxproj.filters --- include/etl/basic_format_spec.h | 204 +++++++++++++++++++++++++ include/etl/format_spec.h | 165 +------------------- include/etl/private/to_string_helper.h | 4 +- include/etl/to_string.h | 20 +-- include/etl/to_u16string.h | 18 +-- include/etl/to_u32string.h | 18 +-- include/etl/to_wstring.h | 18 +-- include/etl/u16format_spec.h | 45 ++++++ include/etl/u32format_spec.h | 45 ++++++ include/etl/version.h | 2 +- include/etl/wformat_spec.h | 45 ++++++ support/Release notes.txt | 5 + test/codeblocks/ETL.cbp | 4 + test/test_to_string.cpp | 2 +- test/test_to_u16string.cpp | 2 +- test/test_to_u32string.cpp | 2 +- test/test_to_wstring.cpp | 2 +- test/vs2017/etl.vcxproj | 4 + test/vs2017/etl.vcxproj.filters | 12 ++ 19 files changed, 411 insertions(+), 206 deletions(-) create mode 100644 include/etl/basic_format_spec.h create mode 100644 include/etl/u16format_spec.h create mode 100644 include/etl/u32format_spec.h create mode 100644 include/etl/wformat_spec.h diff --git a/include/etl/basic_format_spec.h b/include/etl/basic_format_spec.h new file mode 100644 index 00000000..38d48b48 --- /dev/null +++ b/include/etl/basic_format_spec.h @@ -0,0 +1,204 @@ +///\file + +/****************************************************************************** +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. +******************************************************************************/ + +#ifndef ETL_BASIC_FORMAT_SPEC_INCLUDED +#define ETL_BASIC_FORMAT_SPEC_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "type_traits.h" +#include "static_assert.h" + +namespace etl +{ + template + class basic_format_spec + { + public: + + //*************************************************************************** + /// Default constructor. + /// Sets:- + /// Base = 10 + /// Width = 0 + /// Upper case (for hex) = true + /// Left Justified = false + //*************************************************************************** + basic_format_spec() + : base_(10) + , width_(0) + , upper_case_(true) + , left_justified_(false) + , fill_(typename TString::value_type(' ')) + { + + } + + //*************************************************************************** + /// Sets the base. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& base(const uint32_t b) + { + base_ = static_cast(b); + return *this; + } + + //*************************************************************************** + /// Sets the base to binary. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& binary() + { + base(2); + return *this; + } + + //*************************************************************************** + /// Sets the base to octal. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& octal() + { + base(8); + return *this; + } + + //*************************************************************************** + /// Sets the base to decimal. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& decimal() + { + base(10); + return *this; + } + + //*************************************************************************** + /// Sets the base to hex. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& hex() + { + base(16); + return *this; + } + //*************************************************************************** + /// Gets the base. + //*************************************************************************** + uint32_t base() const + { + return base_; + } + + //*************************************************************************** + /// Sets the width. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& width(const uint32_t w) + { + width_ = static_cast(w); + return *this; + } + + //*************************************************************************** + /// Gets the width. + //*************************************************************************** + uint32_t width() const + { + return width_; + } + + //*************************************************************************** + /// Sets the upper case flag. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& upper_case(const bool u) + { + upper_case_ = u; + return *this; + } + + //*************************************************************************** + /// Gets the upper case flag. + //*************************************************************************** + bool upper_case() const + { + return upper_case_; + } + + //*************************************************************************** + /// Sets the fill character. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& fill(const typename TString::value_type c) + { + fill_ = c; + return *this; + } + + //*************************************************************************** + /// Gets the fill character. + //*************************************************************************** + typename TString::value_type fill() const + { + return fill_; + } + + //*************************************************************************** + /// Sets the left justify flag. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& left_justified(const bool l) + { + left_justified_ = l; + return *this; + } + + //*************************************************************************** + /// Gets the left justify flag. + //*************************************************************************** + bool left_justified() const + { + return left_justified_; + } + + private: + + uint_least8_t base_; + uint_least8_t width_; + bool upper_case_; + bool left_justified_; + typename TString::value_type fill_; + }; +} + +#endif diff --git a/include/etl/format_spec.h b/include/etl/format_spec.h index 238bc721..27cbcaa0 100644 --- a/include/etl/format_spec.h +++ b/include/etl/format_spec.h @@ -34,171 +34,12 @@ SOFTWARE. ///\ingroup string #include "platform.h" -#include "type_traits.h" -#include "static_assert.h" +#include "basic_format_spec.h" +#include "cstring.h" namespace etl { - template - class format_spec - { - public: - - //*************************************************************************** - /// Default constructor. - /// Sets:- - /// Base = 10 - /// Width = 0 - /// Upper case (for hex) = true - /// Left Justified = false - //*************************************************************************** - format_spec() - : base_(10) - , width_(0) - , upper_case_(true) - , left_justified_(false) - , fill_(typename TString::value_type(' ')) - { - - } - - //*************************************************************************** - /// Sets the base. - /// \return A reference to the format_spec. - //*************************************************************************** - format_spec& base(const uint32_t b) - { - base_ = static_cast(b); - return *this; - } - - //*************************************************************************** - /// Sets the base to binary. - /// \return A reference to the format_spec. - //*************************************************************************** - format_spec& binary() - { - base(2); - return *this; - } - - //*************************************************************************** - /// Sets the base to octal. - /// \return A reference to the format_spec. - //*************************************************************************** - format_spec& octal() - { - base(8); - return *this; - } - - //*************************************************************************** - /// Sets the base to decimal. - /// \return A reference to the format_spec. - //*************************************************************************** - format_spec& decimal() - { - base(10); - return *this; - } - - //*************************************************************************** - /// Sets the base to hex. - /// \return A reference to the format_spec. - //*************************************************************************** - format_spec& hex() - { - base(16); - return *this; - } - //*************************************************************************** - /// Gets the base. - //*************************************************************************** - uint32_t base() const - { - return base_; - } - - //*************************************************************************** - /// Sets the width. - /// \return A reference to the format_spec. - //*************************************************************************** - format_spec& width(const uint32_t w) - { - width_ = static_cast(w); - return *this; - } - - //*************************************************************************** - /// Gets the width. - //*************************************************************************** - uint32_t width() const - { - return width_; - } - - //*************************************************************************** - /// Sets the upper case flag. - /// \return A reference to the format_spec. - //*************************************************************************** - format_spec& upper_case(const bool u) - { - upper_case_ = u; - return *this; - } - - //*************************************************************************** - /// Gets the upper case flag. - //*************************************************************************** - bool upper_case() const - { - return upper_case_; - } - - //*************************************************************************** - /// Sets the fill character. - /// \return A reference to the format_spec. - //*************************************************************************** - format_spec& fill(const typename TString::value_type c) - { - fill_ = c; - return *this; - } - - //*************************************************************************** - /// Gets the fill character. - //*************************************************************************** - typename TString::value_type fill() const - { - return fill_; - } - - //*************************************************************************** - /// Sets the left justify flag. - /// \return A reference to the format_spec. - //*************************************************************************** - format_spec& left_justified(const bool l) - { - left_justified_ = l; - return *this; - } - - //*************************************************************************** - /// Gets the left justify flag. - //*************************************************************************** - bool left_justified() const - { - return left_justified_; - } - - private: - - uint_least8_t base_; - uint_least8_t width_; - bool upper_case_; - bool left_justified_; - typename TString::value_type fill_; - }; + typedef etl::basic_format_spec format_spec; } #endif diff --git a/include/etl/private/to_string_helper.h b/include/etl/private/to_string_helper.h index a384e1ec..ce4dfd50 100644 --- a/include/etl/private/to_string_helper.h +++ b/include/etl/private/to_string_helper.h @@ -36,7 +36,7 @@ SOFTWARE. #include "../platform.h" #include "../absolute.h" #include "../negative.h" -#include "../format_spec.h" +#include "../basic_format_spec.h" #include "../type_traits.h" #include "../stl/algorithm.h" @@ -51,7 +51,7 @@ namespace etl typename etl::enable_if::value, TIString&>::type to_string_helper(T value, TIString& str, - const etl::format_spec& format, + const etl::basic_format_spec& format, const bool append) { typedef typename TIString::value_type type; diff --git a/include/etl/to_string.h b/include/etl/to_string.h index 51f1991b..058ac099 100644 --- a/include/etl/to_string.h +++ b/include/etl/to_string.h @@ -28,8 +28,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ -#ifndef ETL_TO_U32STRING_INCLUDED -#define ETL_TO_U32STRING_INCLUDED +#ifndef ETL_TO_STRING_INCLUDED +#define ETL_TO_STRING_INCLUDED ///\ingroup string @@ -50,7 +50,7 @@ namespace etl !etl::is_same::value, const etl::istring&>::type to_string(const T value, etl::istring& str, const bool append = false) { - etl::format_spec format; + etl::format_spec format; return to_string_helper(int32_t(value), str, format, append); } @@ -62,7 +62,7 @@ namespace etl 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) + 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); } @@ -76,7 +76,7 @@ namespace etl !etl::is_same::value, const etl::istring&>::type to_string(const T value, etl::istring& str, const bool append = false) { - etl::format_spec format; + etl::format_spec format; return to_string_helper(uint32_t(value), str, format, append); } @@ -88,7 +88,7 @@ namespace etl 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) + 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); } @@ -102,7 +102,7 @@ namespace etl etl::is_same::value, const etl::istring&>::type to_string(const T value, etl::istring& str, const bool append = false) { - etl::format_spec format; + etl::format_spec format; return to_string_helper(int64_t(value), str, format, append); } @@ -114,7 +114,7 @@ namespace etl 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) + 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); } @@ -128,7 +128,7 @@ namespace etl etl::is_same::value, const etl::istring&>::type to_string(const T value, etl::istring& str, const bool append = false) { - etl::format_spec format; + etl::format_spec format; return to_string_helper(uint64_t(value), str, format, append); } @@ -140,7 +140,7 @@ namespace etl 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) + 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); } diff --git a/include/etl/to_u16string.h b/include/etl/to_u16string.h index 09a5ce0f..46bfbb59 100644 --- a/include/etl/to_u16string.h +++ b/include/etl/to_u16string.h @@ -36,7 +36,7 @@ SOFTWARE. #include "platform.h" #include "type_traits.h" #include "u16string.h" -#include "format_spec.h" +#include "u16format_spec.h" #include "private/to_string_helper.h" namespace etl @@ -50,7 +50,7 @@ namespace etl !etl::is_same::value, etl::iu16string&>::type to_u16string(const T value, etl::iu16string& str, const bool append = false) { - etl::format_spec format; + etl::u16format_spec format; return to_string_helper(int32_t(value), str, format, append); } @@ -62,7 +62,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } @@ -76,7 +76,7 @@ namespace etl !etl::is_same::value, etl::iu16string&>::type to_u16string(const T value, etl::iu16string& str, const bool append = false) { - etl::format_spec format; + etl::u16format_spec format; return to_string_helper(uint32_t(value), str, format, append); } @@ -88,7 +88,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } @@ -102,7 +102,7 @@ namespace etl etl::is_same::value, etl::iu16string&>::type to_u16string(const T value, etl::iu16string& str, const bool append = false) { - etl::format_spec format; + etl::u16format_spec format; return to_string_helper(int64_t(value), str, format, append); } @@ -114,7 +114,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } @@ -128,7 +128,7 @@ namespace etl etl::is_same::value, etl::iu16string&>::type to_u16string(const T value, etl::iu16string& str, const bool append = false) { - etl::format_spec format; + etl::u16format_spec format; return to_string_helper(uint64_t(value), str, format, append); } @@ -140,7 +140,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } diff --git a/include/etl/to_u32string.h b/include/etl/to_u32string.h index 9eaf6110..6caa403c 100644 --- a/include/etl/to_u32string.h +++ b/include/etl/to_u32string.h @@ -36,7 +36,7 @@ SOFTWARE. #include "platform.h" #include "type_traits.h" #include "u32string.h" -#include "format_spec.h" +#include "u32format_spec.h" #include "private/to_string_helper.h" namespace etl @@ -50,7 +50,7 @@ namespace etl !etl::is_same::value, etl::iu32string&>::type to_u32string(const T value, etl::iu32string& str, const bool append = false) { - etl::format_spec format; + etl::u32format_spec format; return to_string_helper(int32_t(value), str, format, append); } @@ -62,7 +62,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } @@ -76,7 +76,7 @@ namespace etl !etl::is_same::value, etl::iu32string&>::type to_u32string(const T value, etl::iu32string& str, const bool append = false) { - etl::format_spec format; + etl::u32format_spec format; return to_string_helper(uint32_t(value), str, format, append); } @@ -88,7 +88,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } @@ -102,7 +102,7 @@ namespace etl etl::is_same::value, etl::iu32string&>::type to_u32string(const T value, etl::iu32string& str, const bool append = false) { - etl::format_spec format; + etl::u32format_spec format; return to_string_helper(int64_t(value), str, format, append); } @@ -114,7 +114,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } @@ -128,7 +128,7 @@ namespace etl etl::is_same::value, etl::iu32string&>::type to_u32string(const T value, etl::iu32string& str, const bool append = false) { - etl::format_spec format; + etl::u32format_spec format; return to_string_helper(uint64_t(value), str, format, append); } @@ -140,7 +140,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } diff --git a/include/etl/to_wstring.h b/include/etl/to_wstring.h index ff55b483..9bd39944 100644 --- a/include/etl/to_wstring.h +++ b/include/etl/to_wstring.h @@ -36,7 +36,7 @@ SOFTWARE. #include "platform.h" #include "type_traits.h" #include "wstring.h" -#include "format_spec.h" +#include "wformat_spec.h" #include "private/to_string_helper.h" namespace etl @@ -50,7 +50,7 @@ namespace etl !etl::is_same::value, etl::iwstring&>::type to_wstring(const T value, etl::iwstring& str, const bool append = false) { - etl::format_spec format; + etl::wformat_spec format; return to_string_helper(int32_t(value), str, format, append); } @@ -62,7 +62,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } @@ -76,7 +76,7 @@ namespace etl !etl::is_same::value, etl::iwstring&>::type to_wstring(const T value, etl::iwstring& str, const bool append = false) { - etl::format_spec format; + etl::wformat_spec format; return to_string_helper(uint32_t(value), str, format, append); } @@ -88,7 +88,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } @@ -102,7 +102,7 @@ namespace etl etl::is_same::value, etl::iwstring&>::type to_wstring(const T value, etl::iwstring& str, const bool append = false) { - etl::format_spec format; + etl::wformat_spec format; return to_string_helper(int64_t(value), str, format, append); } @@ -114,7 +114,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } @@ -128,7 +128,7 @@ namespace etl etl::is_same::value, etl::iwstring&>::type to_wstring(const T value, etl::iwstring& str, const bool append = false) { - etl::format_spec format; + etl::wformat_spec format; return to_string_helper(uint64_t(value), str, format, append); } @@ -140,7 +140,7 @@ namespace etl 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::format_spec& format, const bool append = false) + 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); } diff --git a/include/etl/u16format_spec.h b/include/etl/u16format_spec.h new file mode 100644 index 00000000..39c523ca --- /dev/null +++ b/include/etl/u16format_spec.h @@ -0,0 +1,45 @@ +///\file + +/****************************************************************************** +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. +******************************************************************************/ + +#ifndef ETL_U16FORMAT_SPEC_INCLUDED +#define ETL_U16FORMAT_SPEC_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "basic_format_spec.h" +#include "u16string.h" + +namespace etl +{ + typedef etl::basic_format_spec u16format_spec; +} + +#endif diff --git a/include/etl/u32format_spec.h b/include/etl/u32format_spec.h new file mode 100644 index 00000000..2a9d34b8 --- /dev/null +++ b/include/etl/u32format_spec.h @@ -0,0 +1,45 @@ +///\file + +/****************************************************************************** +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. +******************************************************************************/ + +#ifndef ETL_U32FORMAT_SPEC_INCLUDED +#define ETL_U32FORMAT_SPEC_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "basic_format_spec.h" +#include "u32string.h" + +namespace etl +{ + typedef etl::basic_format_spec u32format_spec; +} + +#endif diff --git a/include/etl/version.h b/include/etl/version.h index 073313ad..c1d96ebd 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -39,7 +39,7 @@ SOFTWARE. #define ETL_VERSION_MAJOR 14 #define ETL_VERSION_MINOR 18 -#define ETL_VERSION_PATCH 0 +#define ETL_VERSION_PATCH 1 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) ETL_STRINGIFY(ETL_VERSION_MINOR) ETL_STRINGIFY(ETL_VERSION_PATCH) #define ETL_VERSION_W ETL_WIDE_STRING(ETL_CONCAT(ETL_CONCAT(ETL_VERSION_MAJOR, ETL_VERSION_MINOR), ETL_VERSION_PATCH)) diff --git a/include/etl/wformat_spec.h b/include/etl/wformat_spec.h new file mode 100644 index 00000000..0ff2d57e --- /dev/null +++ b/include/etl/wformat_spec.h @@ -0,0 +1,45 @@ +///\file + +/****************************************************************************** +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. +******************************************************************************/ + +#ifndef ETL_WFORMAT_SPEC_INCLUDED +#define ETL_WFORMAT_SPEC_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "basic_format_spec.h" +#include "wstring.h" + +namespace etl +{ + typedef etl::basic_format_spec wformat_spec; +} + +#endif diff --git a/support/Release notes.txt b/support/Release notes.txt index 60355b97..e43c898e 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,8 @@ +=============================================================================== +14.18.1 +Changed etl::format_sepc template to etl::basic_firmat_spec. +Created individual format_spec typedefs for each string type. + =============================================================================== 14.18.0 Added etl::to_string, etl::to_wstring, etl::to_u16string and etl::to_u32string diff --git a/test/codeblocks/ETL.cbp b/test/codeblocks/ETL.cbp index 8cd1b63a..a7041a42 100644 --- a/test/codeblocks/ETL.cbp +++ b/test/codeblocks/ETL.cbp @@ -154,6 +154,7 @@ + @@ -309,7 +310,9 @@ + + @@ -323,6 +326,7 @@ + From 9ef75bd89ec21150817b49bcfab33b5d077bf6cb Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 14 Apr 2019 19:26:55 +0100 Subject: [PATCH 11/41] 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); + } }; } From 480c56911e6b2e7bab7e58a7e6e83e0f06f323d7 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 14 Apr 2019 19:32:53 +0100 Subject: [PATCH 12/41] Merge remote-tracking branch 'origin/master' into feature/to_string # Conflicts: # include/etl/basic_format_spec.h # include/etl/private/to_string_helper.h # include/etl/to_string.h # include/etl/to_u16string.h # include/etl/to_u32string.h # include/etl/to_wstring.h # test/test_to_string.cpp # test/test_to_u16string.cpp # test/test_to_u32string.cpp # test/test_to_wstring.cpp --- test/test_to_u16string.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/test_to_u16string.cpp b/test/test_to_u16string.cpp index 8069a7d5..06227959 100644 --- a/test/test_to_u16string.cpp +++ b/test/test_to_u16string.cpp @@ -28,10 +28,6 @@ SOFTWARE. #include "UnitTest++.h" -#include -#include -#include - #include "etl/to_u16string.h" #include "etl/u16string.h" #include "etl/format_spec.h" @@ -43,8 +39,6 @@ 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) From 0a4110b4868a04ab793d8ee1b706c6fb9198ad9c Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 14 Apr 2019 19:34:45 +0100 Subject: [PATCH 13/41] Final unit tests for all string types. --- .vs/VSWorkspaceState.json | 10 ++++++++++ include/etl/version.h | 2 +- support/Release notes.txt | 4 ++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 .vs/VSWorkspaceState.json diff --git a/.vs/VSWorkspaceState.json b/.vs/VSWorkspaceState.json new file mode 100644 index 00000000..9e5a6885 --- /dev/null +++ b/.vs/VSWorkspaceState.json @@ -0,0 +1,10 @@ +{ + "ExpandedNodes": [ + "", + "\\include", + "\\include\\etl", + "\\test" + ], + "SelectedNode": "\\test\\test_to_string.cpp", + "PreviewInSolutionExplorer": false +} \ No newline at end of file diff --git a/include/etl/version.h b/include/etl/version.h index c1d96ebd..e59e8aed 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -39,7 +39,7 @@ SOFTWARE. #define ETL_VERSION_MAJOR 14 #define ETL_VERSION_MINOR 18 -#define ETL_VERSION_PATCH 1 +#define ETL_VERSION_PATCH 2 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) ETL_STRINGIFY(ETL_VERSION_MINOR) ETL_STRINGIFY(ETL_VERSION_PATCH) #define ETL_VERSION_W ETL_WIDE_STRING(ETL_CONCAT(ETL_CONCAT(ETL_VERSION_MAJOR, ETL_VERSION_MINOR), ETL_VERSION_PATCH)) diff --git a/support/Release notes.txt b/support/Release notes.txt index e43c898e..be6ff58f 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,7 @@ +=============================================================================== +14.18.2 +Finalised 'to string'. + =============================================================================== 14.18.1 Changed etl::format_sepc template to etl::basic_firmat_spec. From d6d078b49ea8a05eeeee50038aeac76aaca4462a Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Mon, 15 Apr 2019 14:58:12 +0100 Subject: [PATCH 14/41] Unified 'to_string' API --- include/etl/basic_string.h | 2 + include/etl/cstring.h | 1 + include/etl/to_string.h | 14 +- include/etl/to_u16string.h | 12 +- include/etl/to_u32string.h | 12 +- include/etl/to_wstring.h | 12 +- include/etl/u16string.h | 1 + include/etl/u32string.h | 1 + include/etl/wstring.h | 3 +- support/Release notes.txt | 4 + test/test_to_u16string.cpp | 272 +++++++++++++++++------------------ test/test_to_u32string.cpp | 274 ++++++++++++++++++------------------ test/test_to_wstring.cpp | 280 ++++++++++++++++++------------------- 13 files changed, 449 insertions(+), 439 deletions(-) diff --git a/include/etl/basic_string.h b/include/etl/basic_string.h index f5c13b5d..096c30b3 100644 --- a/include/etl/basic_string.h +++ b/include/etl/basic_string.h @@ -263,6 +263,8 @@ namespace etl { public: + typedef ibasic_string interface_type; + typedef T value_type; typedef T& reference; typedef const T& const_reference; diff --git a/include/etl/cstring.h b/include/etl/cstring.h index 147d3c4c..497aa40f 100644 --- a/include/etl/cstring.h +++ b/include/etl/cstring.h @@ -56,6 +56,7 @@ namespace etl public: typedef istring base_type; + typedef istring interface_type; typedef istring::value_type value_type; diff --git a/include/etl/to_string.h b/include/etl/to_string.h index 55076cfd..9bb0e6a1 100644 --- a/include/etl/to_string.h +++ b/include/etl/to_string.h @@ -46,21 +46,21 @@ namespace etl //*************************************************************************** /// Default format spec. //*************************************************************************** - template - const etl::istring& to_string(const T value, etl::istring& str, const bool append = false) + template + const etl::istring& to_string(const T value, etl::string& str, const bool append = false) { etl::format_spec format; - return private_to_string::to_string(value, str, format, append); + return private_to_string::to_string(value, static_cast(str), format, append); } //*************************************************************************** - /// Supplied format spec. + /// Supplied format spec.. //*************************************************************************** - template - const etl::istring& to_string(const T value, etl::istring& str, const etl::format_spec& format, const bool append = false) + template + const etl::istring& to_string(const T value, etl::string& str, const etl::format_spec& format, const bool append = false) { - return private_to_string::to_string(value, str, format, append); + return private_to_string::to_string(value, static_cast(str), format, append); } } diff --git a/include/etl/to_u16string.h b/include/etl/to_u16string.h index 58e47866..be9dcbd8 100644 --- a/include/etl/to_u16string.h +++ b/include/etl/to_u16string.h @@ -44,21 +44,21 @@ namespace etl //*************************************************************************** /// Default format spec. //*************************************************************************** - template - const etl::iu16string& to_u16string(const T value, etl::iu16string& str, const bool append = false) + template + const etl::iu16string& to_string(const T value, etl::u16string& str, const bool append = false) { etl::u16format_spec format; - return etl::private_to_string::to_string(value, str, format, append); + return private_to_string::to_string(value, static_cast(str), format, append); } //*************************************************************************** /// Supplied format spec. //*************************************************************************** - template - const etl::iu16string& to_u16string(const T value, etl::iu16string& str, const etl::u16format_spec& format, const bool append = false) + template + const etl::iu16string& to_string(const T value, etl::u16string& str, const etl::u16format_spec& format, const bool append = false) { - return etl::private_to_string::to_string(value, str, format, append); + return private_to_string::to_string(value, static_cast(str), format, append); } } diff --git a/include/etl/to_u32string.h b/include/etl/to_u32string.h index c45898d1..8b95a367 100644 --- a/include/etl/to_u32string.h +++ b/include/etl/to_u32string.h @@ -44,21 +44,21 @@ namespace etl //*************************************************************************** /// Default format spec. //*************************************************************************** - template - const etl::iu32string& to_u32string(const T value, etl::iu32string& str, const bool append = false) + template + const etl::iu32string& to_string(const T value, etl::u32string& str, const bool append = false) { etl::u32format_spec format; - return etl::private_to_string::to_string(value, str, format, append); + return private_to_string::to_string(value, static_cast(str), format, append); } //*************************************************************************** /// Supplied format spec. //*************************************************************************** - template - const etl::iu32string& to_u32string(const T value, etl::iu32string& str, const etl::u32format_spec& format, const bool append = false) + template + const etl::iu32string& to_string(const T value, etl::u32string& str, const etl::u32format_spec& format, const bool append = false) { - return etl::private_to_string::to_string(value, str, format, append); + return private_to_string::to_string(value, static_cast(str), format, append); } } diff --git a/include/etl/to_wstring.h b/include/etl/to_wstring.h index 97cc04b4..ad7283f2 100644 --- a/include/etl/to_wstring.h +++ b/include/etl/to_wstring.h @@ -44,21 +44,21 @@ namespace etl //*************************************************************************** /// Default format spec. //*************************************************************************** - template - const etl::iwstring& to_wstring(const T value, etl::iwstring& str, const bool append = false) + template + const etl::iwstring& to_string(const T value, etl::wstring& str, const bool append = false) { etl::wformat_spec format; - return etl::private_to_string::to_string(value, str, format, append); + return private_to_string::to_string(value, static_cast(str), format, append); } //*************************************************************************** /// Supplied format spec. //*************************************************************************** - template - const etl::iwstring& to_wstring(const T value, etl::iwstring& str, const etl::wformat_spec& format, const bool append = false) + template + const etl::iwstring& to_string(const T value, etl::wstring& str, const etl::wformat_spec& format, const bool append = false) { - return etl::private_to_string::to_string(value, str, format, append); + return private_to_string::to_string(value, static_cast(str), format, append); } } diff --git a/include/etl/u16string.h b/include/etl/u16string.h index 0a8d271c..659a4aac 100644 --- a/include/etl/u16string.h +++ b/include/etl/u16string.h @@ -56,6 +56,7 @@ namespace etl public: typedef iu16string base_type; + typedef iu16string interface_type; typedef iu16string::value_type value_type; diff --git a/include/etl/u32string.h b/include/etl/u32string.h index 3ad04ae6..70826841 100644 --- a/include/etl/u32string.h +++ b/include/etl/u32string.h @@ -56,6 +56,7 @@ namespace etl public: typedef iu32string base_type; + typedef iu32string interface_type; typedef iu32string::value_type value_type; diff --git a/include/etl/wstring.h b/include/etl/wstring.h index 44ba1798..28b55b54 100644 --- a/include/etl/wstring.h +++ b/include/etl/wstring.h @@ -56,7 +56,8 @@ namespace etl public: typedef iwstring base_type; - + typedef iwstring interface_type; + typedef iwstring::value_type value_type; static const size_t MAX_SIZE = MAX_SIZE_; diff --git a/support/Release notes.txt b/support/Release notes.txt index be6ff58f..76ca162b 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,7 @@ +=============================================================================== +14.18.3 +Unified 'to string' API. + =============================================================================== 14.18.2 Finalised 'to string'. diff --git a/test/test_to_u16string.cpp b/test/test_to_u16string.cpp index 06227959..2932ec5a 100644 --- a/test/test_to_u16string.cpp +++ b/test/test_to_u16string.cpp @@ -56,25 +56,25 @@ namespace { 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("0")), etl::to_string(uint8_t(0), str)); + CHECK_EQUAL(etl::u16string<20>(STR("0")), etl::to_string(uint16_t(0), str)); + CHECK_EQUAL(etl::u16string<20>(STR("0")), etl::to_string(uint32_t(0), str)); + CHECK_EQUAL(etl::u16string<20>(STR("0")), etl::to_string(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("128")), etl::to_string(uint8_t(128), str)); + CHECK_EQUAL(etl::u16string<20>(STR("32768")), etl::to_string(uint16_t(32768), str)); + CHECK_EQUAL(etl::u16string<20>(STR("2147483648")), etl::to_string(uint32_t(2147483648ul), str)); + CHECK_EQUAL(etl::u16string<20>(STR("9223372036854775808")), etl::to_string(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("127")), etl::to_string(int8_t(127), str)); + CHECK_EQUAL(etl::u16string<20>(STR("32767")), etl::to_string(int16_t(32767), str)); + CHECK_EQUAL(etl::u16string<20>(STR("2147483647")), etl::to_string(int32_t(2147483647ll), str)); + CHECK_EQUAL(etl::u16string<20>(STR("9223372036854775807")), etl::to_string(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(-9223372036854775807ll - 1), str)); + CHECK_EQUAL(etl::u16string<20>(STR("-128")), etl::to_string(int8_t(-128), str)); + CHECK_EQUAL(etl::u16string<20>(STR("-32768")), etl::to_string(int16_t(-32768), str)); + CHECK_EQUAL(etl::u16string<20>(STR("-2147483648")), etl::to_string(int32_t(-2147483648ll), str)); + CHECK_EQUAL(etl::u16string<20>(STR("-9223372036854775808")), etl::to_string(int64_t(-9223372036854775807ll - 1), str)); } //************************************************************************* @@ -82,25 +82,25 @@ namespace { 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("0")), etl::to_string(uint8_t(0), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("00")), etl::to_string(uint16_t(0), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("000")), etl::to_string(uint32_t(0), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("0000")), etl::to_string(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("0000128")), etl::to_string(uint8_t(128), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("000012832768")), etl::to_string(uint16_t(32768), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("0000128327682147483648")), etl::to_string(uint32_t(2147483648ul), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("00001283276821474836489223372036854775808")), etl::to_string(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("00001283276821474836489223372036854775808127")), etl::to_string(int8_t(127), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("0000128327682147483648922337203685477580812732767")), etl::to_string(int16_t(32767), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("00001283276821474836489223372036854775808127327672147483647")), etl::to_string(int32_t(2147483647ll), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807")), etl::to_string(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(-9223372036854775807ll - 1), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128")), etl::to_string(int8_t(-128), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768")), etl::to_string(int16_t(-32768), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648")), etl::to_string(int32_t(-2147483648ll), str, true)); + CHECK_EQUAL(etl::u16string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648-9223372036854775808")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, true)); } //************************************************************************* @@ -110,25 +110,25 @@ namespace 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("###################0")), etl::to_string(uint8_t(0), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("###################0")), etl::to_string(uint16_t(0), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("###################0")), etl::to_string(uint32_t(0), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("###################0")), etl::to_string(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("#################128")), etl::to_string(uint8_t(128), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("###############32768")), etl::to_string(uint16_t(32768), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("##########2147483648")), etl::to_string(uint32_t(2147483648ul), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("#9223372036854775808")), etl::to_string(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("#################127")), etl::to_string(int8_t(127), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("###############32767")), etl::to_string(int16_t(32767), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("##########2147483647")), etl::to_string(int32_t(2147483647ll), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("#9223372036854775807")), etl::to_string(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(-9223372036854775807ll - 1), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("################-128")), etl::to_string(int8_t(-128), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("##############-32768")), etl::to_string(int16_t(-32768), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("#########-2147483648")), etl::to_string(int32_t(-2147483648ll), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("-9223372036854775808")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, format)); } //************************************************************************* @@ -138,25 +138,25 @@ namespace 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)); - 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("0###################")), etl::to_string(uint8_t(0), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("0###################")), etl::to_string(uint16_t(0), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("0###################")), etl::to_string(uint32_t(0), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("0###################")), etl::to_string(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("128#################")), etl::to_string(uint8_t(128), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("32768###############")), etl::to_string(uint16_t(32768), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("2147483648##########")), etl::to_string(uint32_t(2147483648ul), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("9223372036854775808#")), etl::to_string(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("127#################")), etl::to_string(int8_t(127), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("32767###############")), etl::to_string(int16_t(32767), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("2147483647##########")), etl::to_string(int32_t(2147483647ll), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("9223372036854775807#")), etl::to_string(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(-9223372036854775807ll - 1), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("-128################")), etl::to_string(int8_t(-128), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("-32768##############")), etl::to_string(int16_t(-32768), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("-2147483648#########")), etl::to_string(int32_t(-2147483648ll), str, format)); + CHECK_EQUAL(etl::u16string<20>(STR("-9223372036854775808")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, format)); } //************************************************************************* @@ -164,25 +164,25 @@ namespace { 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("00000000")), etl::to_string(uint8_t(0), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("0000000000000000")), etl::to_string(uint16_t(0), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("00000000000000000000000000000000")), etl::to_string(uint32_t(0), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("0000000000000000000000000000000000000000000000000000000000000000")), etl::to_string(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("10000000")), etl::to_string(uint8_t(128), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("1000000000000000")), etl::to_string(uint16_t(32768), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("10000000000000000000000000000000")), etl::to_string(uint32_t(2147483648ul), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_string(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("01111111")), etl::to_string(int8_t(127), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("0111111111111111")), etl::to_string(int16_t(32767), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("01111111111111111111111111111111")), etl::to_string(int32_t(2147483647ll), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("0111111111111111111111111111111111111111111111111111111111111111")), etl::to_string(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(-9223372036854775807ll - 1), str, Format().base(2).width(64).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("10000000")), etl::to_string(int8_t(-128), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("1000000000000000")), etl::to_string(int16_t(-32768), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("10000000000000000000000000000000")), etl::to_string(int32_t(-2147483648ll), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, Format().base(2).width(64).fill(STR('0')))); } //************************************************************************* @@ -190,25 +190,25 @@ namespace { 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("000")), etl::to_string(uint8_t(0), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("000000")), etl::to_string(uint16_t(0), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("00000000000")), etl::to_string(uint32_t(0), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("0000000000000000000000")), etl::to_string(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("200")), etl::to_string(uint8_t(128), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("100000")), etl::to_string(uint16_t(32768), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("20000000000")), etl::to_string(uint32_t(2147483648ul), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("1000000000000000000000")), etl::to_string(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("177")), etl::to_string(int8_t(127), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("077777")), etl::to_string(int16_t(32767), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("17777777777")), etl::to_string(int32_t(2147483647ll), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("0777777777777777777777")), etl::to_string(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(-9223372036854775807ll - 1), str, Format().base(8).width(22).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("200")), etl::to_string(int8_t(-128), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("100000")), etl::to_string(int16_t(-32768), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("20000000000")), etl::to_string(int32_t(-2147483648ll), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<22>(STR("1000000000000000000000")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, Format().base(8).width(22).fill(STR('0')))); } //************************************************************************* @@ -216,25 +216,25 @@ namespace { 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("00")), etl::to_string(uint8_t(0), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("0000")), etl::to_string(uint16_t(0), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("00000000")), etl::to_string(uint32_t(0), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("0000000000000000")), etl::to_string(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("80")), etl::to_string(uint8_t(128), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("8000")), etl::to_string(uint16_t(32768), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("80000000")), etl::to_string(uint32_t(2147483648ul), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("8000000000000000")), etl::to_string(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("7F")), etl::to_string(int8_t(127), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("7FFF")), etl::to_string(int16_t(32767), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("7FFFFFFF")), etl::to_string(int32_t(2147483647ll), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("7FFFFFFFFFFFFFFF")), etl::to_string(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(-9223372036854775807ll - 1), str, Format().base(16).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("80")), etl::to_string(int8_t(-128), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("8000")), etl::to_string(int16_t(-32768), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("80000000")), etl::to_string(int32_t(-2147483648ll), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u16string<16>(STR("8000000000000000")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, Format().base(16).width(16).fill(STR('0')))); } //************************************************************************* @@ -242,10 +242,10 @@ namespace { etl::u16string<17> str; - CHECK_EQUAL(etl::u16string<17>(STR("11110001001000000")), etl::to_u16string(123456, str, Format().binary())); - CHECK_EQUAL(etl::u16string<17>(STR("361100")), etl::to_u16string(123456, str, Format().octal())); - 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())); + CHECK_EQUAL(etl::u16string<17>(STR("11110001001000000")), etl::to_string(123456, str, Format().binary())); + CHECK_EQUAL(etl::u16string<17>(STR("361100")), etl::to_string(123456, str, Format().octal())); + CHECK_EQUAL(etl::u16string<17>(STR("123456")), etl::to_string(123456, str, Format().decimal())); + CHECK_EQUAL(etl::u16string<17>(STR("1E240")), etl::to_string(123456, str, Format().hex())); } //************************************************************************* @@ -253,8 +253,8 @@ namespace { 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())); + CHECK_EQUAL(etl::u16string<20>(STR(" 12.345678")), etl::to_string(12.345678, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::u16string<20>(STR("12.345678 ")), etl::to_string(12.345678, str, Format().precision(6).width(10).left())); } //************************************************************************* @@ -263,10 +263,10 @@ namespace 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)); + CHECK_EQUAL(etl::u16string<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::u16string<20>(STR("Result 12.345678 ")), etl::to_u16string(12.345678, str, Format().precision(6).width(10).left(), true)); + CHECK_EQUAL(etl::u16string<20>(STR("Result 12.345678 ")), etl::to_string(12.345678, str, Format().precision(6).width(10).left(), true)); } //************************************************************************* @@ -274,15 +274,15 @@ namespace { 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(" 0")), to_string(false, str, Format().precision(6).width(10).right().boolalpha(false))); + CHECK_EQUAL(etl::u16string<20>(STR(" 1")), to_string(true, str, Format().precision(6).width(10).right().boolalpha(false))); + CHECK_EQUAL(etl::u16string<20>(STR("0 ")), to_string(false, str, Format().precision(6).width(10).left().boolalpha(false))); + CHECK_EQUAL(etl::u16string<20>(STR("1 ")), to_string(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))); + CHECK_EQUAL(etl::u16string<20>(STR(" false")), to_string(false, str, Format().precision(6).width(10).right().boolalpha(true))); + CHECK_EQUAL(etl::u16string<20>(STR(" true")), to_string(true, str, Format().precision(6).width(10).right().boolalpha(true))); + CHECK_EQUAL(etl::u16string<20>(STR("false ")), to_string(false, str, Format().precision(6).width(10).left().boolalpha(true))); + CHECK_EQUAL(etl::u16string<20>(STR("true ")), to_string(true, str, Format().precision(6).width(10).left().boolalpha(true))); } //************************************************************************* @@ -291,28 +291,28 @@ namespace 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)); + CHECK_EQUAL(etl::u16string<20>(STR("Result 0")), to_string(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)); + CHECK_EQUAL(etl::u16string<20>(STR("Result 1")), to_string(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)); + CHECK_EQUAL(etl::u16string<20>(STR("Result 0 ")), to_string(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)); + CHECK_EQUAL(etl::u16string<20>(STR("Result 1 ")), to_string(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)); + CHECK_EQUAL(etl::u16string<20>(STR("Result false")), to_string(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)); + CHECK_EQUAL(etl::u16string<20>(STR("Result true")), to_string(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)); + CHECK_EQUAL(etl::u16string<20>(STR("Result false ")), to_string(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)); + CHECK_EQUAL(etl::u16string<20>(STR("Result true ")), to_string(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 f900e88d..d13f6819 100644 --- a/test/test_to_u32string.cpp +++ b/test/test_to_u32string.cpp @@ -51,32 +51,32 @@ namespace return os; } - SUITE(test_to_u32string) + SUITE(test_to_string) { //************************************************************************* 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("0")), etl::to_string(uint8_t(0), str)); + CHECK_EQUAL(etl::u32string<20>(STR("0")), etl::to_string(uint16_t(0), str)); + CHECK_EQUAL(etl::u32string<20>(STR("0")), etl::to_string(uint32_t(0), str)); + CHECK_EQUAL(etl::u32string<20>(STR("0")), etl::to_string(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("128")), etl::to_string(uint8_t(128), str)); + CHECK_EQUAL(etl::u32string<20>(STR("32768")), etl::to_string(uint16_t(32768), str)); + CHECK_EQUAL(etl::u32string<20>(STR("2147483648")), etl::to_string(uint32_t(2147483648ul), str)); + CHECK_EQUAL(etl::u32string<20>(STR("9223372036854775808")), etl::to_string(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("127")), etl::to_string(int8_t(127), str)); + CHECK_EQUAL(etl::u32string<20>(STR("32767")), etl::to_string(int16_t(32767), str)); + CHECK_EQUAL(etl::u32string<20>(STR("2147483647")), etl::to_string(int32_t(2147483647ll), str)); + CHECK_EQUAL(etl::u32string<20>(STR("9223372036854775807")), etl::to_string(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(-9223372036854775807ll - 1), str)); + CHECK_EQUAL(etl::u32string<20>(STR("-128")), etl::to_string(int8_t(-128), str)); + CHECK_EQUAL(etl::u32string<20>(STR("-32768")), etl::to_string(int16_t(-32768), str)); + CHECK_EQUAL(etl::u32string<20>(STR("-2147483648")), etl::to_string(int32_t(-2147483648ll), str)); + CHECK_EQUAL(etl::u32string<20>(STR("-9223372036854775808")), etl::to_string(int64_t(-9223372036854775807ll - 1), str)); } //************************************************************************* @@ -84,25 +84,25 @@ namespace { 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("0")), etl::to_string(uint8_t(0), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("00")), etl::to_string(uint16_t(0), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("000")), etl::to_string(uint32_t(0), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("0000")), etl::to_string(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("0000128")), etl::to_string(uint8_t(128), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("000012832768")), etl::to_string(uint16_t(32768), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("0000128327682147483648")), etl::to_string(uint32_t(2147483648ul), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("00001283276821474836489223372036854775808")), etl::to_string(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("00001283276821474836489223372036854775808127")), etl::to_string(int8_t(127), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("0000128327682147483648922337203685477580812732767")), etl::to_string(int16_t(32767), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("00001283276821474836489223372036854775808127327672147483647")), etl::to_string(int32_t(2147483647ll), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807")), etl::to_string(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(-9223372036854775807ll - 1), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128")), etl::to_string(int8_t(-128), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768")), etl::to_string(int16_t(-32768), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648")), etl::to_string(int32_t(-2147483648ll), str, true)); + CHECK_EQUAL(etl::u32string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648-9223372036854775808")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, true)); } //************************************************************************* @@ -112,25 +112,25 @@ namespace 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("###################0")), etl::to_string(uint8_t(0), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("###################0")), etl::to_string(uint16_t(0), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("###################0")), etl::to_string(uint32_t(0), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("###################0")), etl::to_string(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("#################128")), etl::to_string(uint8_t(128), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("###############32768")), etl::to_string(uint16_t(32768), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("##########2147483648")), etl::to_string(uint32_t(2147483648ul), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("#9223372036854775808")), etl::to_string(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("#################127")), etl::to_string(int8_t(127), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("###############32767")), etl::to_string(int16_t(32767), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("##########2147483647")), etl::to_string(int32_t(2147483647ll), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("#9223372036854775807")), etl::to_string(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(-9223372036854775807ll - 1), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("################-128")), etl::to_string(int8_t(-128), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("##############-32768")), etl::to_string(int16_t(-32768), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("#########-2147483648")), etl::to_string(int32_t(-2147483648ll), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("-9223372036854775808")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, format)); } //************************************************************************* @@ -140,25 +140,25 @@ namespace 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)); - 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("0###################")), etl::to_string(uint8_t(0), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("0###################")), etl::to_string(uint16_t(0), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("0###################")), etl::to_string(uint32_t(0), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("0###################")), etl::to_string(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("128#################")), etl::to_string(uint8_t(128), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("32768###############")), etl::to_string(uint16_t(32768), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("2147483648##########")), etl::to_string(uint32_t(2147483648ul), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("9223372036854775808#")), etl::to_string(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("127#################")), etl::to_string(int8_t(127), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("32767###############")), etl::to_string(int16_t(32767), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("2147483647##########")), etl::to_string(int32_t(2147483647ll), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("9223372036854775807#")), etl::to_string(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(-9223372036854775807ll - 1), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("-128################")), etl::to_string(int8_t(-128), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("-32768##############")), etl::to_string(int16_t(-32768), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("-2147483648#########")), etl::to_string(int32_t(-2147483648ll), str, format)); + CHECK_EQUAL(etl::u32string<20>(STR("-9223372036854775808")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, format)); } //************************************************************************* @@ -166,25 +166,25 @@ namespace { 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("00000000")), etl::to_string(uint8_t(0), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("0000000000000000")), etl::to_string(uint16_t(0), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("00000000000000000000000000000000")), etl::to_string(uint32_t(0), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("0000000000000000000000000000000000000000000000000000000000000000")), etl::to_string(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("10000000")), etl::to_string(uint8_t(128), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("1000000000000000")), etl::to_string(uint16_t(32768), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("10000000000000000000000000000000")), etl::to_string(uint32_t(2147483648ul), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_string(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("01111111")), etl::to_string(int8_t(127), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("0111111111111111")), etl::to_string(int16_t(32767), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("01111111111111111111111111111111")), etl::to_string(int32_t(2147483647ll), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("0111111111111111111111111111111111111111111111111111111111111111")), etl::to_string(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(-9223372036854775807ll - 1), str, Format().base(2).width(64).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("10000000")), etl::to_string(int8_t(-128), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("1000000000000000")), etl::to_string(int16_t(-32768), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("10000000000000000000000000000000")), etl::to_string(int32_t(-2147483648ll), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, Format().base(2).width(64).fill(STR('0')))); } //************************************************************************* @@ -192,25 +192,25 @@ namespace { 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("000")), etl::to_string(uint8_t(0), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("000000")), etl::to_string(uint16_t(0), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("00000000000")), etl::to_string(uint32_t(0), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("0000000000000000000000")), etl::to_string(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("200")), etl::to_string(uint8_t(128), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("100000")), etl::to_string(uint16_t(32768), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("20000000000")), etl::to_string(uint32_t(2147483648ul), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("1000000000000000000000")), etl::to_string(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("177")), etl::to_string(int8_t(127), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("077777")), etl::to_string(int16_t(32767), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("17777777777")), etl::to_string(int32_t(2147483647ll), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("0777777777777777777777")), etl::to_string(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(-9223372036854775807ll - 1), str, Format().base(8).width(22).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("200")), etl::to_string(int8_t(-128), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("100000")), etl::to_string(int16_t(-32768), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("20000000000")), etl::to_string(int32_t(-2147483648ll), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<22>(STR("1000000000000000000000")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, Format().base(8).width(22).fill(STR('0')))); } //************************************************************************* @@ -218,25 +218,25 @@ namespace { 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("00")), etl::to_string(uint8_t(0), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("0000")), etl::to_string(uint16_t(0), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("00000000")), etl::to_string(uint32_t(0), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("0000000000000000")), etl::to_string(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("80")), etl::to_string(uint8_t(128), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("8000")), etl::to_string(uint16_t(32768), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("80000000")), etl::to_string(uint32_t(2147483648ul), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("8000000000000000")), etl::to_string(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("7F")), etl::to_string(int8_t(127), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("7FFF")), etl::to_string(int16_t(32767), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("7FFFFFFF")), etl::to_string(int32_t(2147483647ll), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("7FFFFFFFFFFFFFFF")), etl::to_string(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(-9223372036854775807ll - 1), str, Format().base(16).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("80")), etl::to_string(int8_t(-128), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("8000")), etl::to_string(int16_t(-32768), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("80000000")), etl::to_string(int32_t(-2147483648ll), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::u32string<16>(STR("8000000000000000")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, Format().base(16).width(16).fill(STR('0')))); } @@ -245,10 +245,10 @@ namespace { etl::u32string<17> str; - CHECK_EQUAL(etl::u32string<17>(STR("11110001001000000")), etl::to_u32string(123456, str, Format().binary())); - CHECK_EQUAL(etl::u32string<17>(STR("361100")), etl::to_u32string(123456, str, Format().octal())); - 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())); + CHECK_EQUAL(etl::u32string<17>(STR("11110001001000000")), etl::to_string(123456, str, Format().binary())); + CHECK_EQUAL(etl::u32string<17>(STR("361100")), etl::to_string(123456, str, Format().octal())); + CHECK_EQUAL(etl::u32string<17>(STR("123456")), etl::to_string(123456, str, Format().decimal())); + CHECK_EQUAL(etl::u32string<17>(STR("1E240")), etl::to_string(123456, str, Format().hex())); } //************************************************************************* @@ -256,8 +256,8 @@ namespace { 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())); + CHECK_EQUAL(etl::u32string<20>(STR(" 12.345678")), etl::to_string(12.345678, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::u32string<20>(STR("12.345678 ")), etl::to_string(12.345678, str, Format().precision(6).width(10).left())); } //************************************************************************* @@ -266,10 +266,10 @@ namespace 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)); + CHECK_EQUAL(etl::u32string<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::u32string<20>(STR("Result 12.345678 ")), etl::to_u32string(12.345678, str, Format().precision(6).width(10).left(), true)); + CHECK_EQUAL(etl::u32string<20>(STR("Result 12.345678 ")), etl::to_string(12.345678, str, Format().precision(6).width(10).left(), true)); } //************************************************************************* @@ -277,15 +277,15 @@ namespace { 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(" 0")), to_string(false, str, Format().precision(6).width(10).right().boolalpha(false))); + CHECK_EQUAL(etl::u32string<20>(STR(" 1")), to_string(true, str, Format().precision(6).width(10).right().boolalpha(false))); + CHECK_EQUAL(etl::u32string<20>(STR("0 ")), to_string(false, str, Format().precision(6).width(10).left().boolalpha(false))); + CHECK_EQUAL(etl::u32string<20>(STR("1 ")), to_string(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))); + CHECK_EQUAL(etl::u32string<20>(STR(" false")), to_string(false, str, Format().precision(6).width(10).right().boolalpha(true))); + CHECK_EQUAL(etl::u32string<20>(STR(" true")), to_string(true, str, Format().precision(6).width(10).right().boolalpha(true))); + CHECK_EQUAL(etl::u32string<20>(STR("false ")), to_string(false, str, Format().precision(6).width(10).left().boolalpha(true))); + CHECK_EQUAL(etl::u32string<20>(STR("true ")), to_string(true, str, Format().precision(6).width(10).left().boolalpha(true))); } //************************************************************************* @@ -294,28 +294,28 @@ namespace 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)); + CHECK_EQUAL(etl::u32string<20>(STR("Result 0")), to_string(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)); + CHECK_EQUAL(etl::u32string<20>(STR("Result 1")), to_string(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)); + CHECK_EQUAL(etl::u32string<20>(STR("Result 0 ")), to_string(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)); + CHECK_EQUAL(etl::u32string<20>(STR("Result 1 ")), to_string(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)); + CHECK_EQUAL(etl::u32string<20>(STR("Result false")), to_string(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)); + CHECK_EQUAL(etl::u32string<20>(STR("Result true")), to_string(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)); + CHECK_EQUAL(etl::u32string<20>(STR("Result false ")), to_string(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)); + CHECK_EQUAL(etl::u32string<20>(STR("Result true ")), to_string(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 dffbee4a..07819cba 100644 --- a/test/test_to_wstring.cpp +++ b/test/test_to_wstring.cpp @@ -60,25 +60,25 @@ namespace { 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("0")), etl::to_string(uint8_t(0), str)); + CHECK_EQUAL(etl::wstring<20>(STR("0")), etl::to_string(uint16_t(0), str)); + CHECK_EQUAL(etl::wstring<20>(STR("0")), etl::to_string(uint32_t(0), str)); + CHECK_EQUAL(etl::wstring<20>(STR("0")), etl::to_string(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("128")), etl::to_string(uint8_t(128), str)); + CHECK_EQUAL(etl::wstring<20>(STR("32768")), etl::to_string(uint16_t(32768), str)); + CHECK_EQUAL(etl::wstring<20>(STR("2147483648")), etl::to_string(uint32_t(2147483648ul), str)); + CHECK_EQUAL(etl::wstring<20>(STR("9223372036854775808")), etl::to_string(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("127")), etl::to_string(int8_t(127), str)); + CHECK_EQUAL(etl::wstring<20>(STR("32767")), etl::to_string(int16_t(32767), str)); + CHECK_EQUAL(etl::wstring<20>(STR("2147483647")), etl::to_string(int32_t(2147483647ll), str)); + CHECK_EQUAL(etl::wstring<20>(STR("9223372036854775807")), etl::to_string(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(-9223372036854775807ll - 1), str)); + CHECK_EQUAL(etl::wstring<20>(STR("-128")), etl::to_string(int8_t(-128), str)); + CHECK_EQUAL(etl::wstring<20>(STR("-32768")), etl::to_string(int16_t(-32768), str)); + CHECK_EQUAL(etl::wstring<20>(STR("-2147483648")), etl::to_string(int32_t(-2147483648ll), str)); + CHECK_EQUAL(etl::wstring<20>(STR("-9223372036854775808")), etl::to_string(int64_t(-9223372036854775807ll - 1), str)); } //************************************************************************* @@ -86,25 +86,25 @@ namespace { 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("0")), etl::to_string(uint8_t(0), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("00")), etl::to_string(uint16_t(0), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("000")), etl::to_string(uint32_t(0), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("0000")), etl::to_string(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("0000128")), etl::to_string(uint8_t(128), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("000012832768")), etl::to_string(uint16_t(32768), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("0000128327682147483648")), etl::to_string(uint32_t(2147483648ul), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("00001283276821474836489223372036854775808")), etl::to_string(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("00001283276821474836489223372036854775808127")), etl::to_string(int8_t(127), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("0000128327682147483648922337203685477580812732767")), etl::to_string(int16_t(32767), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("00001283276821474836489223372036854775808127327672147483647")), etl::to_string(int32_t(2147483647ll), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807")), etl::to_string(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(-9223372036854775807ll - 1), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128")), etl::to_string(int8_t(-128), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768")), etl::to_string(int16_t(-32768), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648")), etl::to_string(int32_t(-2147483648ll), str, true)); + CHECK_EQUAL(etl::wstring<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648-9223372036854775808")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, true)); } //************************************************************************* @@ -114,25 +114,25 @@ namespace 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("###################0")), etl::to_string(uint8_t(0), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("###################0")), etl::to_string(uint16_t(0), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("###################0")), etl::to_string(uint32_t(0), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("###################0")), etl::to_string(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("#################128")), etl::to_string(uint8_t(128), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("###############32768")), etl::to_string(uint16_t(32768), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("##########2147483648")), etl::to_string(uint32_t(2147483648ul), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("#9223372036854775808")), etl::to_string(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("#################127")), etl::to_string(int8_t(127), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("###############32767")), etl::to_string(int16_t(32767), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("##########2147483647")), etl::to_string(int32_t(2147483647ll), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("#9223372036854775807")), etl::to_string(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(-9223372036854775807ll - 1), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("################-128")), etl::to_string(int8_t(-128), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("##############-32768")), etl::to_string(int16_t(-32768), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("#########-2147483648")), etl::to_string(int32_t(-2147483648ll), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("-9223372036854775808")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, format)); } //************************************************************************* @@ -142,25 +142,25 @@ namespace 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)); - 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("0###################")), etl::to_string(uint8_t(0), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("0###################")), etl::to_string(uint16_t(0), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("0###################")), etl::to_string(uint32_t(0), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("0###################")), etl::to_string(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("128#################")), etl::to_string(uint8_t(128), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("32768###############")), etl::to_string(uint16_t(32768), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("2147483648##########")), etl::to_string(uint32_t(2147483648ul), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("9223372036854775808#")), etl::to_string(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("127#################")), etl::to_string(int8_t(127), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("32767###############")), etl::to_string(int16_t(32767), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("2147483647##########")), etl::to_string(int32_t(2147483647ll), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("9223372036854775807#")), etl::to_string(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(-9223372036854775807ll - 1), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("-128################")), etl::to_string(int8_t(-128), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("-32768##############")), etl::to_string(int16_t(-32768), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("-2147483648#########")), etl::to_string(int32_t(-2147483648ll), str, format)); + CHECK_EQUAL(etl::wstring<20>(STR("-9223372036854775808")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, format)); } //************************************************************************* @@ -168,25 +168,25 @@ namespace { 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("00000000")), etl::to_string(uint8_t(0), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("0000000000000000")), etl::to_string(uint16_t(0), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("00000000000000000000000000000000")), etl::to_string(uint32_t(0), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("0000000000000000000000000000000000000000000000000000000000000000")), etl::to_string(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("10000000")), etl::to_string(uint8_t(128), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("1000000000000000")), etl::to_string(uint16_t(32768), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("10000000000000000000000000000000")), etl::to_string(uint32_t(2147483648ul), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_string(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("01111111")), etl::to_string(int8_t(127), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("0111111111111111")), etl::to_string(int16_t(32767), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("01111111111111111111111111111111")), etl::to_string(int32_t(2147483647ll), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("0111111111111111111111111111111111111111111111111111111111111111")), etl::to_string(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(-9223372036854775807ll - 1), str, Format().base(2).width(64).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("10000000")), etl::to_string(int8_t(-128), str, Format().base(2).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("1000000000000000")), etl::to_string(int16_t(-32768), str, Format().base(2).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("10000000000000000000000000000000")), etl::to_string(int32_t(-2147483648ll), str, Format().base(2).width(32).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, Format().base(2).width(64).fill(STR('0')))); } //************************************************************************* @@ -194,25 +194,25 @@ namespace { 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("000")), etl::to_string(uint8_t(0), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("000000")), etl::to_string(uint16_t(0), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("00000000000")), etl::to_string(uint32_t(0), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("0000000000000000000000")), etl::to_string(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("200")), etl::to_string(uint8_t(128), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("100000")), etl::to_string(uint16_t(32768), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("20000000000")), etl::to_string(uint32_t(2147483648ul), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("1000000000000000000000")), etl::to_string(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("177")), etl::to_string(int8_t(127), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("077777")), etl::to_string(int16_t(32767), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("17777777777")), etl::to_string(int32_t(2147483647ll), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("0777777777777777777777")), etl::to_string(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(-9223372036854775807ll - 1), str, Format().base(8).width(22).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("200")), etl::to_string(int8_t(-128), str, Format().base(8).width(3).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("100000")), etl::to_string(int16_t(-32768), str, Format().base(8).width(6).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("20000000000")), etl::to_string(int32_t(-2147483648ll), str, Format().base(8).width(11).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<22>(STR("1000000000000000000000")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, Format().base(8).width(22).fill(STR('0')))); } //************************************************************************* @@ -220,25 +220,25 @@ namespace { 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("00")), etl::to_string(uint8_t(0), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("0000")), etl::to_string(uint16_t(0), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("00000000")), etl::to_string(uint32_t(0), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("0000000000000000")), etl::to_string(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("80")), etl::to_string(uint8_t(128), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("8000")), etl::to_string(uint16_t(32768), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("80000000")), etl::to_string(uint32_t(2147483648ul), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("8000000000000000")), etl::to_string(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("7F")), etl::to_string(int8_t(127), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("7FFF")), etl::to_string(int16_t(32767), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("7FFFFFFF")), etl::to_string(int32_t(2147483647ll), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("7FFFFFFFFFFFFFFF")), etl::to_string(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(-9223372036854775807ll - 1), str, Format().base(16).width(16).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("80")), etl::to_string(int8_t(-128), str, Format().base(16).width(2).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("8000")), etl::to_string(int16_t(-32768), str, Format().base(16).width(4).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("80000000")), etl::to_string(int32_t(-2147483648ll), str, Format().base(16).width(8).fill(STR('0')))); + CHECK_EQUAL(etl::wstring<16>(STR("8000000000000000")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, Format().base(16).width(16).fill(STR('0')))); } //************************************************************************* @@ -246,10 +246,10 @@ namespace { etl::wstring<17> str; - CHECK_EQUAL(etl::wstring<17>(STR("11110001001000000")), etl::to_wstring(123456, str, Format().binary())); - CHECK_EQUAL(etl::wstring<17>(STR("361100")), etl::to_wstring(123456, str, Format().octal())); - 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())); + CHECK_EQUAL(etl::wstring<17>(STR("11110001001000000")), etl::to_string(123456, str, Format().binary())); + CHECK_EQUAL(etl::wstring<17>(STR("361100")), etl::to_string(123456, str, Format().octal())); + CHECK_EQUAL(etl::wstring<17>(STR("123456")), etl::to_string(123456, str, Format().decimal())); + CHECK_EQUAL(etl::wstring<17>(STR("1E240")), etl::to_string(123456, str, Format().hex())); } //************************************************************************* @@ -257,8 +257,8 @@ namespace { 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())); + CHECK_EQUAL(etl::wstring<20>(STR(" 12.345678")), etl::to_string(12.345678, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::wstring<20>(STR("12.345678 ")), etl::to_string(12.345678, str, Format().precision(6).width(10).left())); } //************************************************************************* @@ -267,10 +267,10 @@ namespace 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)); + CHECK_EQUAL(etl::wstring<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::wstring<20>(STR("Result 12.345678 ")), etl::to_wstring(12.345678, str, Format().precision(6).width(10).left(), true)); + CHECK_EQUAL(etl::wstring<20>(STR("Result 12.345678 ")), etl::to_string(12.345678, str, Format().precision(6).width(10).left(), true)); } //************************************************************************* @@ -278,15 +278,15 @@ namespace { 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(" 0")), to_string(false, str, Format().precision(6).width(10).right().boolalpha(false))); + CHECK_EQUAL(etl::wstring<20>(STR(" 1")), to_string(true, str, Format().precision(6).width(10).right().boolalpha(false))); + CHECK_EQUAL(etl::wstring<20>(STR("0 ")), to_string(false, str, Format().precision(6).width(10).left().boolalpha(false))); + CHECK_EQUAL(etl::wstring<20>(STR("1 ")), to_string(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))); + CHECK_EQUAL(etl::wstring<20>(STR(" false")), to_string(false, str, Format().precision(6).width(10).right().boolalpha(true))); + CHECK_EQUAL(etl::wstring<20>(STR(" true")), to_string(true, str, Format().precision(6).width(10).right().boolalpha(true))); + CHECK_EQUAL(etl::wstring<20>(STR("false ")), to_string(false, str, Format().precision(6).width(10).left().boolalpha(true))); + CHECK_EQUAL(etl::wstring<20>(STR("true ")), to_string(true, str, Format().precision(6).width(10).left().boolalpha(true))); } //************************************************************************* @@ -295,28 +295,28 @@ namespace 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)); + CHECK_EQUAL(etl::wstring<20>(STR("Result 0")), to_string(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)); + CHECK_EQUAL(etl::wstring<20>(STR("Result 1")), to_string(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)); + CHECK_EQUAL(etl::wstring<20>(STR("Result 0 ")), to_string(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)); + CHECK_EQUAL(etl::wstring<20>(STR("Result 1 ")), to_string(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)); + CHECK_EQUAL(etl::wstring<20>(STR("Result false")), to_string(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)); + CHECK_EQUAL(etl::wstring<20>(STR("Result true")), to_string(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)); + CHECK_EQUAL(etl::wstring<20>(STR("Result false ")), to_string(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)); + CHECK_EQUAL(etl::wstring<20>(STR("Result true ")), to_string(true, str, Format().precision(6).width(10).left().boolalpha(true), true)); } //************************************************************************* @@ -333,7 +333,7 @@ namespace 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'))); + to_string(&cvi, str, Format().hex().width(10).right().fill(STR('0'))); CHECK_EQUAL(compare, str); oss.clear(); @@ -344,7 +344,7 @@ namespace temp = oss.str(); compare.assign(temp.begin(), temp.end()); - to_wstring(&cvi, str, Format().hex().width(10).left().fill(STR('0'))); + to_string(&cvi, str, Format().hex().width(10).left().fill(STR('0'))); CHECK_EQUAL(compare, str); } @@ -364,7 +364,7 @@ namespace 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); + to_string(&cvi, str, Format().hex().width(10).right().fill(STR('0')), true); CHECK_EQUAL(compare, str); oss.clear(); @@ -377,7 +377,7 @@ namespace compare.assign(temp.begin(), temp.end()); str.assign(STR("Result ")); - to_wstring(&cvi, str, Format().hex().width(10).left().fill(STR('0')), true); + to_string(&cvi, str, Format().hex().width(10).left().fill(STR('0')), true); CHECK_EQUAL(compare, str); } }; From 5ccd0ac39d3642f2c9eedbae03f98331ae7b7749 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Mon, 15 Apr 2019 19:18:52 +0100 Subject: [PATCH 15/41] Fixed test name --- test/test_to_u32string.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_to_u32string.cpp b/test/test_to_u32string.cpp index d13f6819..df822bb9 100644 --- a/test/test_to_u32string.cpp +++ b/test/test_to_u32string.cpp @@ -51,7 +51,7 @@ namespace return os; } - SUITE(test_to_string) + SUITE(test_to_u32string) { //************************************************************************* TEST(test_default_format_no_append) From e62ba1356d84e656b71dc54b786f5b9bbe602982 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Mon, 15 Apr 2019 19:23:54 +0100 Subject: [PATCH 16/41] Attempt to force changes to master --- test/test_to_string.cpp | 1 + test/test_to_u16string.cpp | 1 + test/test_to_u32string.cpp | 1 + test/test_to_wstring.cpp | 1 + 4 files changed, 4 insertions(+) diff --git a/test/test_to_string.cpp b/test/test_to_string.cpp index 01a9abe0..a32bb6da 100644 --- a/test/test_to_string.cpp +++ b/test/test_to_string.cpp @@ -384,3 +384,4 @@ namespace } }; } + diff --git a/test/test_to_u16string.cpp b/test/test_to_u16string.cpp index 2932ec5a..8b0b50a4 100644 --- a/test/test_to_u16string.cpp +++ b/test/test_to_u16string.cpp @@ -316,3 +316,4 @@ namespace } }; } + diff --git a/test/test_to_u32string.cpp b/test/test_to_u32string.cpp index df822bb9..23b80f9d 100644 --- a/test/test_to_u32string.cpp +++ b/test/test_to_u32string.cpp @@ -319,3 +319,4 @@ namespace } }; } + diff --git a/test/test_to_wstring.cpp b/test/test_to_wstring.cpp index 07819cba..6365236b 100644 --- a/test/test_to_wstring.cpp +++ b/test/test_to_wstring.cpp @@ -382,3 +382,4 @@ namespace } }; } + From 31f33e78ee898ea97e57b7fde876b8933b769989 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Tue, 16 Apr 2019 08:39:38 +0100 Subject: [PATCH 17/41] Fixed bug in fractional part for floating point with leading zeros after the decimal point. --- include/etl/private/to_string_helper.h | 21 +++++++++++++-------- include/etl/version.h | 2 +- support/Release notes.txt | 4 ++++ test/test_to_string.cpp | 12 ++++++++++++ test/test_to_u16string.cpp | 12 ++++++++++++ test/test_to_u32string.cpp | 12 ++++++++++++ test/test_to_wstring.cpp | 12 ++++++++++++ 7 files changed, 66 insertions(+), 9 deletions(-) diff --git a/include/etl/private/to_string_helper.h b/include/etl/private/to_string_helper.h index 76595dfc..0aa8013d 100644 --- a/include/etl/private/to_string_helper.h +++ b/include/etl/private/to_string_helper.h @@ -201,16 +201,17 @@ namespace etl void add_integral_fractional(const int64_t integral, const int64_t fractional, TIString& str, - const etl::basic_format_spec& format) + const etl::basic_format_spec& integral_format, + const etl::basic_format_spec& fractional_format) { typedef typename TIString::value_type type; - etl::private_to_string::add_integral(integral, str, format, true); + etl::private_to_string::add_integral(integral, str, integral_format, true); - if (format.get_precision() > 0) + if (fractional_format.get_precision() > 0) { str.push_back(type('.')); - etl::private_to_string::add_integral(fractional, str, format, true); + etl::private_to_string::add_integral(fractional, str, fractional_format, true); } } @@ -224,6 +225,7 @@ namespace etl const bool append) { typedef typename TIString::iterator iterator; + typedef typename TIString::value_type type; if (!append) { @@ -241,12 +243,15 @@ namespace etl // 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()); + etl::basic_format_spec integral_format = format; + integral_format.decimal().width(0).precision(format.get_precision() > max_precision ? max_precision : format.get_precision()); + + etl::basic_format_spec fractional_format = integral_format; + fractional_format.width(integral_format.get_precision()).fill(type('0')).right(); int64_t multiplier = 1; - for (uint32_t i = 0; i < local_format.get_precision(); ++i) + for (uint32_t i = 0; i < fractional_format.get_precision(); ++i) { multiplier *= 10U; } @@ -255,7 +260,7 @@ namespace etl 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_integral_fractional(integral, fractional, str, integral_format, fractional_format); } etl::private_to_string::add_alignment(str, start, format); diff --git a/include/etl/version.h b/include/etl/version.h index e59e8aed..b59b7ac7 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -39,7 +39,7 @@ SOFTWARE. #define ETL_VERSION_MAJOR 14 #define ETL_VERSION_MINOR 18 -#define ETL_VERSION_PATCH 2 +#define ETL_VERSION_PATCH 4 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) ETL_STRINGIFY(ETL_VERSION_MINOR) ETL_STRINGIFY(ETL_VERSION_PATCH) #define ETL_VERSION_W ETL_WIDE_STRING(ETL_CONCAT(ETL_CONCAT(ETL_VERSION_MAJOR, ETL_VERSION_MINOR), ETL_VERSION_PATCH)) diff --git a/support/Release notes.txt b/support/Release notes.txt index 76ca162b..0b2373fc 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,7 @@ +=============================================================================== +14.18.4 +Fixed bug in fractional part for floating point with leading zeros after the decimal point. + =============================================================================== 14.18.3 Unified 'to string' API. diff --git a/test/test_to_string.cpp b/test/test_to_string.cpp index 01a9abe0..0a98c783 100644 --- a/test/test_to_string.cpp +++ b/test/test_to_string.cpp @@ -259,6 +259,18 @@ namespace { etl::string<20> str; + CHECK_EQUAL(etl::string<20>(STR(" 0.000000")), etl::to_string(0.0, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::string<20>(STR("0.000000 ")), etl::to_string(0.0, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::string<20>(STR(" 0.000001")), etl::to_string(0.000001, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::string<20>(STR("0.000001 ")), etl::to_string(0.000001, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::string<20>(STR(" 1.000000")), etl::to_string(1.0, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::string<20>(STR("1.000000 ")), etl::to_string(1.0, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::string<20>(STR(" 1.000001")), etl::to_string(1.000001, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::string<20>(STR("1.000001 ")), etl::to_string(1.000001, str, Format().precision(6).width(10).left())); + 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())); } diff --git a/test/test_to_u16string.cpp b/test/test_to_u16string.cpp index 2932ec5a..48f8afa9 100644 --- a/test/test_to_u16string.cpp +++ b/test/test_to_u16string.cpp @@ -253,6 +253,18 @@ namespace { etl::u16string<20> str; + CHECK_EQUAL(etl::u16string<20>(STR(" 0.000000")), etl::to_string(0.0, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::u16string<20>(STR("0.000000 ")), etl::to_string(0.0, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::u16string<20>(STR(" 0.000001")), etl::to_string(0.000001, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::u16string<20>(STR("0.000001 ")), etl::to_string(0.000001, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::u16string<20>(STR(" 1.000000")), etl::to_string(1.0, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::u16string<20>(STR("1.000000 ")), etl::to_string(1.0, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::u16string<20>(STR(" 1.000001")), etl::to_string(1.000001, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::u16string<20>(STR("1.000001 ")), etl::to_string(1.000001, str, Format().precision(6).width(10).left())); + CHECK_EQUAL(etl::u16string<20>(STR(" 12.345678")), etl::to_string(12.345678, str, Format().precision(6).width(10).right())); CHECK_EQUAL(etl::u16string<20>(STR("12.345678 ")), etl::to_string(12.345678, str, Format().precision(6).width(10).left())); } diff --git a/test/test_to_u32string.cpp b/test/test_to_u32string.cpp index df822bb9..01ad8a8d 100644 --- a/test/test_to_u32string.cpp +++ b/test/test_to_u32string.cpp @@ -256,6 +256,18 @@ namespace { etl::u32string<20> str; + CHECK_EQUAL(etl::u32string<20>(STR(" 0.000000")), etl::to_string(0.0, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::u32string<20>(STR("0.000000 ")), etl::to_string(0.0, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::u32string<20>(STR(" 0.000001")), etl::to_string(0.000001, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::u32string<20>(STR("0.000001 ")), etl::to_string(0.000001, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::u32string<20>(STR(" 1.000000")), etl::to_string(1.0, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::u32string<20>(STR("1.000000 ")), etl::to_string(1.0, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::u32string<20>(STR(" 1.000001")), etl::to_string(1.000001, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::u32string<20>(STR("1.000001 ")), etl::to_string(1.000001, str, Format().precision(6).width(10).left())); + CHECK_EQUAL(etl::u32string<20>(STR(" 12.345678")), etl::to_string(12.345678, str, Format().precision(6).width(10).right())); CHECK_EQUAL(etl::u32string<20>(STR("12.345678 ")), etl::to_string(12.345678, str, Format().precision(6).width(10).left())); } diff --git a/test/test_to_wstring.cpp b/test/test_to_wstring.cpp index 07819cba..6a8b86e0 100644 --- a/test/test_to_wstring.cpp +++ b/test/test_to_wstring.cpp @@ -257,6 +257,18 @@ namespace { etl::wstring<20> str; + CHECK_EQUAL(etl::wstring<20>(STR(" 0.000000")), etl::to_string(0.0, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::wstring<20>(STR("0.000000 ")), etl::to_string(0.0, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::wstring<20>(STR(" 0.000001")), etl::to_string(0.000001, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::wstring<20>(STR("0.000001 ")), etl::to_string(0.000001, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::wstring<20>(STR(" 1.000000")), etl::to_string(1.0, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::wstring<20>(STR("1.000000 ")), etl::to_string(1.0, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::wstring<20>(STR(" 1.000001")), etl::to_string(1.000001, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::wstring<20>(STR("1.000001 ")), etl::to_string(1.000001, str, Format().precision(6).width(10).left())); + CHECK_EQUAL(etl::wstring<20>(STR(" 12.345678")), etl::to_string(12.345678, str, Format().precision(6).width(10).right())); CHECK_EQUAL(etl::wstring<20>(STR("12.345678 ")), etl::to_string(12.345678, str, Format().precision(6).width(10).left())); } From be65eb76fefee813fda483e436c5aeec27af2fa6 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Tue, 16 Apr 2019 18:25:55 +0100 Subject: [PATCH 18/41] Merge remote-tracking branch 'origin/hotfix/cyclic_value_for_unsigned' into development # Conflicts: # include/etl/version.h --- include/etl/cyclic_value.h | 8 ------ include/etl/version.h | 2 +- test/test_cyclic_value.cpp | 56 ++++++++++++++++++++++---------------- 3 files changed, 34 insertions(+), 32 deletions(-) diff --git a/include/etl/cyclic_value.h b/include/etl/cyclic_value.h index e1fdd12d..3bb3bbe7 100644 --- a/include/etl/cyclic_value.h +++ b/include/etl/cyclic_value.h @@ -117,10 +117,6 @@ namespace etl //************************************************************************* void advance(int n) { - T range = LAST - FIRST + T(1); - - n = n % range; - if (n > 0) { for (int i = 0; i < n; ++i) @@ -394,10 +390,6 @@ namespace etl //************************************************************************* void advance(int n) { - T range = last_value - first_value + T(1); - - n = n % range; - if (n > 0) { for (int i = 0; i < n; ++i) diff --git a/include/etl/version.h b/include/etl/version.h index b59b7ac7..5eae462c 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -39,7 +39,7 @@ SOFTWARE. #define ETL_VERSION_MAJOR 14 #define ETL_VERSION_MINOR 18 -#define ETL_VERSION_PATCH 4 +#define ETL_VERSION_PATCH 5 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) ETL_STRINGIFY(ETL_VERSION_MINOR) ETL_STRINGIFY(ETL_VERSION_PATCH) #define ETL_VERSION_W ETL_WIDE_STRING(ETL_CONCAT(ETL_CONCAT(ETL_VERSION_MAJOR, ETL_VERSION_MINOR), ETL_VERSION_PATCH)) diff --git a/test/test_cyclic_value.cpp b/test/test_cyclic_value.cpp index ce92742c..04220491 100644 --- a/test/test_cyclic_value.cpp +++ b/test/test_cyclic_value.cpp @@ -309,29 +309,6 @@ namespace CHECK_EQUAL(4, value); } - //************************************************************************* - TEST(test_advance_negative_compile_time) - { - etl::cyclic_value value; - - value.to_last(); - value.advance(-14); - - CHECK_EQUAL(5, value); - } - - //************************************************************************* - TEST(test_advance_negative_run_time) - { - etl::cyclic_value value; - - value.set(2, 7); - value.to_last(); - value.advance(-14); - - CHECK_EQUAL(5, value); - } - //************************************************************************* TEST(test_advance_negative_large_compile_time) { @@ -355,6 +332,39 @@ namespace CHECK_EQUAL(5, value); } + //************************************************************************* + TEST(test_advance_negative_unsigned_compile_time) + { + etl::cyclic_value value; + + value.advance(-2); + + CHECK_EQUAL(1U, value); + } + + //************************************************************************* + TEST(test_advance_negative_run_time) + { + etl::cyclic_value value; + + value.set(2, 7); + value.to_last(); + value.advance(-14); + + CHECK_EQUAL(5, value); + } + + //************************************************************************* + TEST(test_advance_negative_unsigned_run_time) + { + etl::cyclic_value value; + + value.set(0U, 2U); + value.advance(-2); + + CHECK_EQUAL(1U, value); + } + //************************************************************************* TEST(test_assignment_compile_time) { From 8f00a1d8229a83d531d30d6131f125105dcec0c3 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Wed, 17 Apr 2019 22:47:39 +0100 Subject: [PATCH 19/41] Added 'emplace' to etl::optional. C++03 & C++11 variants supplied depending on compiler support. --- include/etl/optional.h | 87 +++++++++++++++++++++++++++++++++++++++ include/etl/version.h | 4 +- support/Release notes.txt | 5 +++ test/etl_profile.h | 1 + test/test_optional.cpp | 18 ++++++++ 5 files changed, 113 insertions(+), 2 deletions(-) diff --git a/include/etl/optional.h b/include/etl/optional.h index 47f84004..36c03e0f 100644 --- a/include/etl/optional.h +++ b/include/etl/optional.h @@ -325,6 +325,93 @@ namespace etl } } +#if ETL_CPP11_SUPPORTED && !defined(ETL_STLPORT) && !defined(ETL_OPTIONAL_FORCE_CPP03) + //************************************************************************* + /// Emplaces a value. + ///\param args The arguments to construct with. + //************************************************************************* + template + void emplace(Args && ... args) + { + if (valid) + { + // Destroy the old one. + storage.template get_reference().~T(); + } + + ::new (storage.template get_address()) T(std::forward(args)...); + valid = true; + } +#else + //************************************************************************* + /// Emplaces a value. + /// 1 parameter. + //************************************************************************* + template + void emplace(const T1& value1) + { + if (valid) + { + // Destroy the old one. + storage.template get_reference().~T(); + } + + ::new (storage.template get_address()) T(value1); + valid = true; + } + + //************************************************************************* + /// Emplaces a value. + /// 2 parameters. + //************************************************************************* + template + void emplace(const T1& value1, const T2& value2) + { + if (valid) + { + // Destroy the old one. + storage.template get_reference().~T(); + } + + ::new (storage.template get_address()) T(value1, value2); + valid = true; + } + + //************************************************************************* + /// Emplaces a value. + /// 3 parameters. + //************************************************************************* + template + void emplace(const T1& value1, const T2& value2, const T3& value3) + { + if (valid) + { + // Destroy the old one. + storage.template get_reference().~T(); + } + + ::new (storage.template get_address()) T(value1, value2, value3); + valid = true; + } + + //************************************************************************* + /// Emplaces a value. + /// 4 parameters. + //************************************************************************* + template + void emplace(const T1& value1, const T2& value2, const T3& value3, const T4& value4) + { + if (valid) + { + // Destroy the old one. + storage.template get_reference().~T(); + } + + ::new (storage.template get_address()) T(value1, value2, value3, value4); + valid = true; + } +#endif + private: typename etl::aligned_storage_as::type storage; diff --git a/include/etl/version.h b/include/etl/version.h index 5eae462c..2ab16a18 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -38,8 +38,8 @@ SOFTWARE. ///\ingroup utilities #define ETL_VERSION_MAJOR 14 -#define ETL_VERSION_MINOR 18 -#define ETL_VERSION_PATCH 5 +#define ETL_VERSION_MINOR 19 +#define ETL_VERSION_PATCH 0 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) ETL_STRINGIFY(ETL_VERSION_MINOR) ETL_STRINGIFY(ETL_VERSION_PATCH) #define ETL_VERSION_W ETL_WIDE_STRING(ETL_CONCAT(ETL_CONCAT(ETL_VERSION_MAJOR, ETL_VERSION_MINOR), ETL_VERSION_PATCH)) diff --git a/support/Release notes.txt b/support/Release notes.txt index 0b2373fc..a8823399 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,8 @@ +=============================================================================== +14.19.0 +Added 'emplace' to etl::optional. +C++03 & C++11 variants supplied depending on compiler support. + =============================================================================== 14.18.4 Fixed bug in fractional part for floating point with leading zeros after the decimal point. diff --git a/test/etl_profile.h b/test/etl_profile.h index 95880884..ffcbee75 100644 --- a/test/etl_profile.h +++ b/test/etl_profile.h @@ -85,6 +85,7 @@ SOFTWARE. //#define ETL_QUEUE_MPMC_MUTEX_FORCE_CPP03 //#define ETL_QUEUE_ISR_FORCE_CPP03 //#define ETL_QUEUE_LOCKED_FORCE_CPP03 +//#define ETL_OPTIONAL_FORCE_CPP03 #ifdef _MSC_VER #include "etl/profiles/msvc_x86.h" diff --git a/test/test_optional.cpp b/test/test_optional.cpp index 35884641..c48039b7 100644 --- a/test/test_optional.cpp +++ b/test/test_optional.cpp @@ -36,6 +36,7 @@ SOFTWARE. #include "data.h" typedef TestDataNDC Data; +typedef TestDataM DataM; std::ostream& operator << (std::ostream& os, const Data& data) { @@ -85,6 +86,23 @@ namespace CHECK(!bool(data4)); } + //************************************************************************* + TEST(test_emplace) + { + etl::optional data; + + data.emplace(1U); + CHECK_EQUAL(1U, data.value().value); + + data.emplace(2U); + CHECK_EQUAL(2U, data.value().value); + + data.emplace(3U); + CHECK_EQUAL(3U, data.value().value); + + CHECK_EQUAL(1, DataM::get_instance_count()); + } + //************************************************************************* TEST(test_nullopt) { From a2f4ea123c334d6ed19d82a456242ac99c027b6e Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 14 Apr 2019 19:43:09 +0100 Subject: [PATCH 20/41] Merge remote-tracking branch 'origin/development' # Conflicts: # include/etl/basic_format_spec.h # include/etl/private/to_string_helper.h # include/etl/to_string.h # include/etl/to_u16string.h # include/etl/to_u32string.h # include/etl/to_wstring.h # include/etl/version.h # support/Release notes.txt --- test/test_to_u16string.cpp | 67 +++++++++++++++++++ test/test_to_u32string.cpp | 67 +++++++++++++++++++ test/test_to_wstring.cpp | 129 +++++++++++++++++++++++++++++++++++++ 3 files changed, 263 insertions(+) diff --git a/test/test_to_u16string.cpp b/test/test_to_u16string.cpp index f1aea7da..f36f0859 100644 --- a/test/test_to_u16string.cpp +++ b/test/test_to_u16string.cpp @@ -326,6 +326,73 @@ namespace str.assign(STR("Result ")); CHECK_EQUAL(etl::u16string<20>(STR("Result true ")), to_string(true, str, Format().precision(6).width(10).left().boolalpha(true), true)); } + + //************************************************************************* + 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 4e39fa41..e2ce928b 100644 --- a/test/test_to_u32string.cpp +++ b/test/test_to_u32string.cpp @@ -329,6 +329,73 @@ namespace str.assign(STR("Result ")); CHECK_EQUAL(etl::u32string<20>(STR("Result true ")), to_string(true, str, Format().precision(6).width(10).left().boolalpha(true), true)); } + + //************************************************************************* + 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 bc575ce1..48b8cb40 100644 --- a/test/test_to_wstring.cpp +++ b/test/test_to_wstring.cpp @@ -392,6 +392,135 @@ namespace to_string(&cvi, str, Format().hex().width(10).left().fill(STR('0')), true); CHECK_EQUAL(compare, str); } + + //************************************************************************* + 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); + } }; } From 9263ae05200e7d3e6a6ed193046f8acbd59a45bf Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Mon, 15 Apr 2019 19:39:27 +0100 Subject: [PATCH 21/41] Merge remote-tracking branch 'origin/development' # Conflicts: # include/etl/to_string.h # include/etl/to_u16string.h # include/etl/to_u32string.h # include/etl/to_wstring.h # support/Release notes.txt --- test/test_to_u16string.cpp | 67 ------------------- test/test_to_u32string.cpp | 67 ------------------- test/test_to_wstring.cpp | 129 ------------------------------------- 3 files changed, 263 deletions(-) diff --git a/test/test_to_u16string.cpp b/test/test_to_u16string.cpp index f36f0859..f1aea7da 100644 --- a/test/test_to_u16string.cpp +++ b/test/test_to_u16string.cpp @@ -326,73 +326,6 @@ namespace str.assign(STR("Result ")); CHECK_EQUAL(etl::u16string<20>(STR("Result true ")), to_string(true, str, Format().precision(6).width(10).left().boolalpha(true), true)); } - - //************************************************************************* - 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 e2ce928b..4e39fa41 100644 --- a/test/test_to_u32string.cpp +++ b/test/test_to_u32string.cpp @@ -329,73 +329,6 @@ namespace str.assign(STR("Result ")); CHECK_EQUAL(etl::u32string<20>(STR("Result true ")), to_string(true, str, Format().precision(6).width(10).left().boolalpha(true), true)); } - - //************************************************************************* - 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 48b8cb40..bc575ce1 100644 --- a/test/test_to_wstring.cpp +++ b/test/test_to_wstring.cpp @@ -392,135 +392,6 @@ namespace to_string(&cvi, str, Format().hex().width(10).left().fill(STR('0')), true); CHECK_EQUAL(compare, str); } - - //************************************************************************* - 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); - } }; } From ba3ced4420febcb5285e72552e2ca0149dd6989c Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 14 Apr 2019 19:43:09 +0100 Subject: [PATCH 22/41] Merge remote-tracking branch 'origin/development' # Conflicts: # include/etl/basic_format_spec.h # include/etl/private/to_string_helper.h # include/etl/to_string.h # include/etl/to_u16string.h # include/etl/to_u32string.h # include/etl/to_wstring.h # include/etl/version.h # support/Release notes.txt --- test/test_to_u16string.cpp | 67 +++++++++++++++++++ test/test_to_u32string.cpp | 67 +++++++++++++++++++ test/test_to_wstring.cpp | 129 +++++++++++++++++++++++++++++++++++++ 3 files changed, 263 insertions(+) diff --git a/test/test_to_u16string.cpp b/test/test_to_u16string.cpp index f1aea7da..f36f0859 100644 --- a/test/test_to_u16string.cpp +++ b/test/test_to_u16string.cpp @@ -326,6 +326,73 @@ namespace str.assign(STR("Result ")); CHECK_EQUAL(etl::u16string<20>(STR("Result true ")), to_string(true, str, Format().precision(6).width(10).left().boolalpha(true), true)); } + + //************************************************************************* + 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 4e39fa41..e2ce928b 100644 --- a/test/test_to_u32string.cpp +++ b/test/test_to_u32string.cpp @@ -329,6 +329,73 @@ namespace str.assign(STR("Result ")); CHECK_EQUAL(etl::u32string<20>(STR("Result true ")), to_string(true, str, Format().precision(6).width(10).left().boolalpha(true), true)); } + + //************************************************************************* + 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 bc575ce1..48b8cb40 100644 --- a/test/test_to_wstring.cpp +++ b/test/test_to_wstring.cpp @@ -392,6 +392,135 @@ namespace to_string(&cvi, str, Format().hex().width(10).left().fill(STR('0')), true); CHECK_EQUAL(compare, str); } + + //************************************************************************* + 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); + } }; } From cf02ec64c762af8b6bd02cc0d821a47f25d5cfb9 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Mon, 15 Apr 2019 19:39:27 +0100 Subject: [PATCH 23/41] Merge remote-tracking branch 'origin/development' # Conflicts: # include/etl/to_string.h # include/etl/to_u16string.h # include/etl/to_u32string.h # include/etl/to_wstring.h # support/Release notes.txt --- test/test_to_u16string.cpp | 67 ------------------- test/test_to_u32string.cpp | 69 +------------------- test/test_to_wstring.cpp | 129 ------------------------------------- 3 files changed, 1 insertion(+), 264 deletions(-) diff --git a/test/test_to_u16string.cpp b/test/test_to_u16string.cpp index f36f0859..f1aea7da 100644 --- a/test/test_to_u16string.cpp +++ b/test/test_to_u16string.cpp @@ -326,73 +326,6 @@ namespace str.assign(STR("Result ")); CHECK_EQUAL(etl::u16string<20>(STR("Result true ")), to_string(true, str, Format().precision(6).width(10).left().boolalpha(true), true)); } - - //************************************************************************* - 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 e2ce928b..4064dde8 100644 --- a/test/test_to_u32string.cpp +++ b/test/test_to_u32string.cpp @@ -267,7 +267,7 @@ namespace CHECK_EQUAL(etl::u32string<20>(STR(" 1.000001")), etl::to_string(1.000001, str, Format().precision(6).width(10).right())); CHECK_EQUAL(etl::u32string<20>(STR("1.000001 ")), etl::to_string(1.000001, str, Format().precision(6).width(10).left())); - + CHECK_EQUAL(etl::u32string<20>(STR(" 12.345678")), etl::to_string(12.345678, str, Format().precision(6).width(10).right())); CHECK_EQUAL(etl::u32string<20>(STR("12.345678 ")), etl::to_string(12.345678, str, Format().precision(6).width(10).left())); } @@ -329,73 +329,6 @@ namespace str.assign(STR("Result ")); CHECK_EQUAL(etl::u32string<20>(STR("Result true ")), to_string(true, str, Format().precision(6).width(10).left().boolalpha(true), true)); } - - //************************************************************************* - 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 48b8cb40..bc575ce1 100644 --- a/test/test_to_wstring.cpp +++ b/test/test_to_wstring.cpp @@ -392,135 +392,6 @@ namespace to_string(&cvi, str, Format().hex().width(10).left().fill(STR('0')), true); CHECK_EQUAL(compare, str); } - - //************************************************************************* - 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); - } }; } From 2b5fe55540203dbb0061c45d955a675a053ecdc4 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sat, 20 Apr 2019 11:14:24 +0100 Subject: [PATCH 24/41] First code, no unit tests --- include/etl/multi_array.h | 75 +++++++++++++++++++++++++++++++++ test/vs2017/etl.vcxproj | 1 + test/vs2017/etl.vcxproj.filters | 3 ++ 3 files changed, 79 insertions(+) create mode 100644 include/etl/multi_array.h diff --git a/include/etl/multi_array.h b/include/etl/multi_array.h new file mode 100644 index 00000000..86f28ce3 --- /dev/null +++ b/include/etl/multi_array.h @@ -0,0 +1,75 @@ +///\file + +/****************************************************************************** +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. +******************************************************************************/ + +#ifndef ETL_MULTI_ARRAY_INCLUDED +#define ETL_MULTI_ARRAY_INCLUDED + +#include "platform.h" +#include "array.h" + +///\defgroup multi_array multi_array +/// A multi dimentional array. +///\ingroup containers + +namespace etl +{ + +#if ETL_CPP11_SUPPORTED + + namespace private_multi_array + { + template + struct multi_array_t + { + using ArrayX = typename multi_array_t::type; + using type = etl::array; + }; + + template + struct multi_array_t + { + using type = etl::array; + }; + } + + template + using multi_array = private_multi_array::multi_array_t; + + template + using array2d = etl::multi_array; + + template + using array3d = etl::multi_array; + +#endif +} + +#endif + diff --git a/test/vs2017/etl.vcxproj b/test/vs2017/etl.vcxproj index 892c2035..ad876c13 100644 --- a/test/vs2017/etl.vcxproj +++ b/test/vs2017/etl.vcxproj @@ -379,6 +379,7 @@ + diff --git a/test/vs2017/etl.vcxproj.filters b/test/vs2017/etl.vcxproj.filters index b89c8cf5..69d55f04 100644 --- a/test/vs2017/etl.vcxproj.filters +++ b/test/vs2017/etl.vcxproj.filters @@ -765,6 +765,9 @@ ETL\Utilities + + ETL\Containers + From 8354de78e11c64aca8f8247f43e5ad27c262685d Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sat, 27 Apr 2019 15:51:13 +0100 Subject: [PATCH 25/41] Added etl::multi_array --- include/etl/multi_array.h | 16 +- include/etl/version.h | 2 +- support/Release notes.txt | 5 + test/test_multi_array.cpp | 495 ++++++++++++++++++++++++++++++++ test/vs2017/etl.vcxproj | 1 + test/vs2017/etl.vcxproj.filters | 3 + 6 files changed, 510 insertions(+), 12 deletions(-) create mode 100644 test/test_multi_array.cpp diff --git a/include/etl/multi_array.h b/include/etl/multi_array.h index 86f28ce3..a92c9f6a 100644 --- a/include/etl/multi_array.h +++ b/include/etl/multi_array.h @@ -40,7 +40,6 @@ SOFTWARE. namespace etl { - #if ETL_CPP11_SUPPORTED namespace private_multi_array @@ -48,25 +47,20 @@ namespace etl template struct multi_array_t { - using ArrayX = typename multi_array_t::type; - using type = etl::array; + using type = etl::array::type, D1>; + static constexpr size_t SIZE = D1; }; template struct multi_array_t { using type = etl::array; + static constexpr size_t SIZE = D1; }; } - template - using multi_array = private_multi_array::multi_array_t; - - template - using array2d = etl::multi_array; - - template - using array3d = etl::multi_array; + template + using multi_array = typename private_multi_array::multi_array_t::type; #endif } diff --git a/include/etl/version.h b/include/etl/version.h index 2ab16a18..ba1db1fc 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -38,7 +38,7 @@ SOFTWARE. ///\ingroup utilities #define ETL_VERSION_MAJOR 14 -#define ETL_VERSION_MINOR 19 +#define ETL_VERSION_MINOR 20 #define ETL_VERSION_PATCH 0 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) ETL_STRINGIFY(ETL_VERSION_MINOR) ETL_STRINGIFY(ETL_VERSION_PATCH) diff --git a/support/Release notes.txt b/support/Release notes.txt index a8823399..f1241241 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,8 @@ +=============================================================================== +14.20.0 +Added etl::multi_array for multi-dimensional arrays. +C++11 only. + =============================================================================== 14.19.0 Added 'emplace' to etl::optional. diff --git a/test/test_multi_array.cpp b/test/test_multi_array.cpp new file mode 100644 index 00000000..cfe82bd0 --- /dev/null +++ b/test/test_multi_array.cpp @@ -0,0 +1,495 @@ +/****************************************************************************** +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 "etl/array.h" +#include "etl/multi_array.h" + +#include +#include +#include + +#include "etl/integral_limits.h" + +namespace +{ + SUITE(test_array) + { + static const size_t SIZE1 = 4; + static const size_t SIZE2 = 3; + + int data1[4][3] = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } }; + + using Data = etl::multi_array; + using Compare_Data = std::array, SIZE1>; + + Compare_Data compare_data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + Compare_Data swap_data = { { { 12, 13, 14 }, { 15, 16, 17 }, { 18, 19, 20 }, { 21, 22, 23 } } }; + + //************************************************************************* + TEST(test_constructor) + { + Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + CHECK_EQUAL(data.size(), SIZE1); + CHECK_EQUAL(data.max_size(), SIZE1); + + CHECK_EQUAL(data[0].size(), SIZE2); + CHECK_EQUAL(data[0].max_size(), SIZE2); + } + + //************************************************************************* + TEST(test_assignment) + { + Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + Data other_data; + + other_data = data; + + bool isEqual = std::equal(data.begin(), data.end(), other_data.begin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST(test_at) + { + Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + for (size_t i = 0; i < data.size(); ++i) + { + for (size_t j = 0; j < data[0].size(); ++j) + { + CHECK_EQUAL(data[i].at(j), compare_data[i].at(j)); + } + } + + CHECK_THROW(data.at(data.size()), etl::array_out_of_range); + CHECK_THROW(data[0].at(data[0].size()), etl::array_out_of_range); + } + + //************************************************************************* + TEST(test_at_const) + { + const Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + for (size_t i = 0; i < data.size(); ++i) + { + for (size_t j = 0; j < data[0].size(); ++j) + { + CHECK_EQUAL(data[i].at(j), compare_data[i].at(j)); + } + + CHECK_THROW(data.at(data.size()), etl::array_out_of_range); + } + } + + //************************************************************************* + TEST(test_index_operator) + { + Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + for (size_t i = 0; i < data.size(); ++i) + { + for (size_t j = 0; j < data[0].size(); ++j) + { + CHECK_EQUAL(data[i][j], compare_data[i][j]); + } + } + } + + //************************************************************************* + TEST(test_index_operator_const) + { + const Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + for (size_t i = 0; i < data.size(); ++i) + { + for (size_t j = 0; j < data[0].size(); ++j) + { + CHECK_EQUAL(data[i][j], compare_data[i][j]); + } + } + } + + //************************************************************************* + TEST(test_front) + { + Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + int& ref = data.front().front(); + CHECK(ref == compare_data.front().front()); + } + + //************************************************************************* + TEST(test_front_const) + { + const Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + const int& ref = data.front().front(); + CHECK(ref == compare_data.front().front()); + } + + //************************************************************************* + TEST(test_back) + { + Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + int& ref = data.back().back(); + CHECK(ref == compare_data.back().back()); + } + + //************************************************************************* + TEST(test_back_const) + { + const Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + const int& ref = data.back().back(); + CHECK(ref == compare_data.back().back()); + } + + //************************************************************************* + TEST(test_data) + { + Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + bool isEqual = std::equal(data.begin(), data.end(), data.data()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST(test_data_const) + { + const Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + bool isEqual = std::equal(data.begin(), data.end(), data.data()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST(test_begin) + { + Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + CHECK(data.begin() == &data[0]); + CHECK(data[0].begin() == &data[0][0]); + CHECK(data[1].begin() == &data[1][0]); + CHECK(data[2].begin() == &data[2][0]); + CHECK(data[3].begin() == &data[3][0]); + } + + //************************************************************************* + TEST(test_end) + { + Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + CHECK(data.end() == &data[SIZE1]); + CHECK(data[0].end() == &data[0][SIZE2]); + CHECK(data[1].end() == &data[1][SIZE2]); + CHECK(data[2].end() == &data[2][SIZE2]); + CHECK(data[3].end() == &data[3][SIZE2]); + } + + //************************************************************************* + TEST(test_cbegin) + { + const Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + CHECK(data.cbegin() == &data[0]); + CHECK(data[0].cbegin() == &data[0][0]); + CHECK(data[1].cbegin() == &data[1][0]); + CHECK(data[2].cbegin() == &data[2][0]); + CHECK(data[3].cbegin() == &data[3][0]); + } + + //************************************************************************* + TEST(test_cend) + { + const Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + CHECK(data.cend() == &data[SIZE1]); + CHECK(data[0].cend() == &data[0][SIZE2]); + CHECK(data[1].cend() == &data[1][SIZE2]); + CHECK(data[2].cend() == &data[2][SIZE2]); + CHECK(data[3].cend() == &data[3][SIZE2]); + } + + //************************************************************************* + TEST(test_rbegin) + { + Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + typedef etl::multi_array Inner; + + CHECK(data.rbegin() == Data::reverse_iterator(&data[SIZE1])); + CHECK(data[0].rbegin() == Inner::reverse_iterator(&data[0][SIZE2])); + CHECK(data[1].rbegin() == Inner::reverse_iterator(&data[1][SIZE2])); + CHECK(data[2].rbegin() == Inner::reverse_iterator(&data[2][SIZE2])); + CHECK(data[3].rbegin() == Inner::reverse_iterator(&data[3][SIZE2])); + } + + //************************************************************************* + TEST(test_rend) + { + Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + typedef etl::multi_array Inner; + + CHECK(data.rend() == Data::reverse_iterator(&data[0])); + CHECK(data[0].rend() == Inner::reverse_iterator(&data[0][0])); + CHECK(data[1].rend() == Inner::reverse_iterator(&data[1][0])); + CHECK(data[2].rend() == Inner::reverse_iterator(&data[2][0])); + CHECK(data[3].rend() == Inner::reverse_iterator(&data[3][0])); + } + + //************************************************************************* + TEST(test_crbegin) + { + const Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + typedef etl::multi_array Inner; + + CHECK(data.rbegin() == Data::const_reverse_iterator(&data[SIZE1])); + CHECK(data[0].rbegin() == Inner::const_reverse_iterator(&data[0][SIZE2])); + CHECK(data[1].rbegin() == Inner::const_reverse_iterator(&data[1][SIZE2])); + CHECK(data[2].rbegin() == Inner::const_reverse_iterator(&data[2][SIZE2])); + CHECK(data[3].rbegin() == Inner::const_reverse_iterator(&data[3][SIZE2])); + } + + //************************************************************************* + TEST(test_crend) + { + const Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + typedef etl::multi_array Inner; + + CHECK(data.rend() == Data::const_reverse_iterator(&data[0])); + CHECK(data[0].rend() == Inner::const_reverse_iterator(&data[0][0])); + CHECK(data[1].rend() == Inner::const_reverse_iterator(&data[1][0])); + CHECK(data[2].rend() == Inner::const_reverse_iterator(&data[2][0])); + CHECK(data[3].rend() == Inner::const_reverse_iterator(&data[3][0])); + } + + //************************************************************************* + TEST(test_empty) + { + Data data; + + CHECK(!data.empty()); + CHECK(!data[0].empty()); + } + + //************************************************************************* + TEST(test_size) + { + Data data; + + CHECK_EQUAL(SIZE1, data.size()); + CHECK_EQUAL(SIZE2, data[0].size()); + + CHECK_EQUAL(SIZE1, data.SIZE); + CHECK_EQUAL(SIZE2, data[0].SIZE); + } + + //************************************************************************* + TEST(test_max_size) + { + Data data; + + CHECK_EQUAL(SIZE1, data.max_size()); + CHECK_EQUAL(SIZE2, data[0].max_size()); + } + + //************************************************************************* + TEST(test_fill) + { + Data data; + typedef etl::multi_array Inner; + const Inner fillData = { { 12, 13, 14 } }; + + data.fill(fillData); + + CHECK(fillData == data[0]); + CHECK(fillData == data[1]); + CHECK(fillData == data[2]); + CHECK(fillData == data[3]); + } + + //************************************************************************* + TEST(test_swap) + { + Data data1 = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + Data data2 = { { { 12, 13, 14 }, { 15, 16, 17 }, { 18, 19, 20 }, { 21, 22, 23 } } }; + + Data data1b = { { { 12, 13, 14 }, { 15, 16, 17 }, { 18, 19, 20 }, { 21, 22, 23 } } }; + Data data2b = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + swap(data1, data2); + + CHECK(std::equal(data1b.begin(), data1b.end(), data1.begin())); + CHECK(std::equal(data2b.begin(), data2b.end(), data2.begin())); + } + + //************************************************************************* + TEST(test_assign) + { + Data initial = { { { -1, -1, -1 }, { -1, -1, -1 }, { -1, -1, -1 }, { -1, -1, -1 } } }; + Data source = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + Data check1 = { { { 0, 1, 2 }, { 3, 4, 5 }, { -1, -1, -1 }, { -1, -1, -1 } } }; + Data check2 = { { { 0, 1, 2 }, { 3, 4, 5 }, { 99, 99, 99 }, { 99, 99, 99 } } }; + Data check3 = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + typedef etl::multi_array Inner; + const Inner ninetynine = { { 99, 99, 99 } }; + + Data data; + + // Initial data. + data.assign(std::begin(initial), std::end(initial)); + bool isEqual = std::equal(data.begin(), data.end(), std::begin(initial)); + CHECK(isEqual); + + // Assign smaller. + data.assign(std::begin(initial), std::end(initial)); + data.assign(&source[0], &source[2]); + isEqual = std::equal(data.begin(), data.end(), std::begin(check1)); + CHECK(isEqual); + + // Assign smaller + default. + data.assign(std::begin(initial), std::end(initial)); + data.assign(&source[0], &source[2], ninetynine); + isEqual = std::equal(data.begin(), data.end(), std::begin(check2)); + CHECK(isEqual); + } + + //************************************************************************* + TEST(test_insert_value) + { + Data initial = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + Data check1 = { { { 12, 13, 14 }, { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } } }; + Data check2 = { { { 0, 1, 2 }, { 12, 13, 14 }, { 3, 4, 5 }, { 6, 7, 8 } } }; + Data check3 = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 12, 13, 14 } } }; + + typedef etl::multi_array Inner; + const Inner inserted = { { 12, 13, 14 } }; + + Data data; + Data::iterator result; + + // Insert beginning. + data.assign(std::begin(initial), std::end(initial)); + result = data.insert_at(0, inserted); + CHECK(data[0] == *result); + bool isEqual = std::equal(data.begin(), data.end(), check1.begin()); + CHECK(isEqual); + + // Insert middle. + data.assign(std::begin(initial), std::end(initial)); + result = data.insert_at(1, inserted); + CHECK(data[1] == *result); + isEqual = std::equal(data.begin(), data.end(), check2.begin()); + CHECK(isEqual); + + // Insert end. + data.assign(std::begin(initial), std::end(initial)); + result = data.insert_at(3, inserted); + CHECK(data[3] == *result); + isEqual = std::equal(data.begin(), data.end(), check3.begin()); + CHECK(isEqual); + } + + //************************************************************************* + TEST(test_equal) + { + Data data1 = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + Data data2 = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + CHECK(data1 == data2); + } + + //************************************************************************* + TEST(test_not_equal) + { + Data data1 = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + Data data2 = { { { 0, 1, 2 }, { 3, 4, 6 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + CHECK(data1 != data2); + } + + //************************************************************************* + TEST(test_less_than) + { + Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + Data lesser = { { { 0, 1, 2 }, { 3, 4, 4 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + Data greater = { { { 0, 1, 2 }, { 3, 4, 6 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + CHECK(lesser < data); + CHECK(!(data < data)); + CHECK(!(greater < data)); + } + + //************************************************************************* + TEST(test_less_than_equal) + { + Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + Data lesser = { { { 0, 1, 2 }, { 3, 4, 4 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + Data greater = { { { 0, 1, 2 }, { 3, 4, 6 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + CHECK(lesser <= data); + CHECK(data <= data); + CHECK(!(greater <= data)); + } + + //************************************************************************* + TEST(test_greater_than) + { + Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + Data lesser = { { { 0, 1, 2 }, { 3, 4, 4 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + Data greater = { { { 0, 1, 2 }, { 3, 4, 6 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + CHECK(greater > data); + CHECK(!(data > data)); + CHECK(!(lesser > data)); + } + + //************************************************************************* + TEST(test_greater_than_equal) + { + Data data = { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + Data lesser = { { { 0, 1, 2 }, { 3, 4, 4 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + Data greater = { { { 0, 1, 2 }, { 3, 4, 6 }, { 6, 7, 8 }, { 9, 10, 11 } } }; + + CHECK(greater >= data); + CHECK(data >= data); + CHECK(!(lesser >= data)); + } + }; +} diff --git a/test/vs2017/etl.vcxproj b/test/vs2017/etl.vcxproj index ad876c13..09c910aa 100644 --- a/test/vs2017/etl.vcxproj +++ b/test/vs2017/etl.vcxproj @@ -585,6 +585,7 @@ + ../../../unittest-cpp diff --git a/test/vs2017/etl.vcxproj.filters b/test/vs2017/etl.vcxproj.filters index 69d55f04..9aa640d9 100644 --- a/test/vs2017/etl.vcxproj.filters +++ b/test/vs2017/etl.vcxproj.filters @@ -1217,6 +1217,9 @@ Source Files + + Source Files + From 895a132fcbb5bcd2b44670389809aab85c6b4a5d Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sat, 27 Apr 2019 16:29:02 +0100 Subject: [PATCH 26/41] Simplified definition --- include/etl/multi_array.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/etl/multi_array.h b/include/etl/multi_array.h index a92c9f6a..0c1f9f06 100644 --- a/include/etl/multi_array.h +++ b/include/etl/multi_array.h @@ -59,8 +59,8 @@ namespace etl }; } - template - using multi_array = typename private_multi_array::multi_array_t::type; + template + using multi_array = typename private_multi_array::multi_array_t::type; #endif } From 1326691a65725341dd3e2149860df6ca8c530539 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sat, 4 May 2019 22:14:06 +0100 Subject: [PATCH 27/41] Added variadic min and max functions. etl::multimin etl::multimin_compare etl::multimin_iter etl::multimin_iter_compare etl::multimax etl::multimax_compare etl::multimax_iter etl::multimax_iter_compare --- include/etl/algorithm.h | 126 ++++++++++++++++++++++++++++++++++++-- include/etl/version.h | 2 +- support/Release notes.txt | 13 ++++ test/test_algorithm.cpp | 36 +++++++++++ 4 files changed, 170 insertions(+), 7 deletions(-) diff --git a/include/etl/algorithm.h b/include/etl/algorithm.h index 65a23687..26036156 100644 --- a/include/etl/algorithm.h +++ b/include/etl/algorithm.h @@ -1131,21 +1131,21 @@ namespace etl void sort(TIterator first, TIterator last, TCompare compare) { typedef typename std::iterator_traits::difference_type difference_t; - + difference_t n = std::distance(first, last); - for (difference_t i = n / 2; i > 0; i /= 2) + for (difference_t i = n / 2; i > 0; i /= 2) { - for (difference_t j = i; j < n; ++j) + for (difference_t j = i; j < n; ++j) { for (difference_t k = j - i; k >= 0; k -= i) { - TIterator itr1 = first; + TIterator itr1 = first; TIterator itr2 = first; - + std::advance(itr1, k); std::advance(itr2, k + i); - + if (compare(*itr2, *itr1)) { std::iter_swap(itr1, itr2); @@ -1164,6 +1164,120 @@ namespace etl { etl::sort(first, last, std::less::value_type>()); } + +#if ETL_CPP11_SUPPORTED + //*************************************************************************** + + template + constexpr const T& multimax(const T& a, const T& b) + { + return a < b ? b : a; + } + + template + constexpr const T& multimax(const T& t, const Tx&... tx) + { + return multimax(t, multimax(tx...)); + } + + //*************************************************************************** + + template + constexpr const T& multimax_compare(TCompare compare, const T& a, const T& b) + { + return compare(a, b) ? b : a; + } + + template + constexpr const T& multimax_compare(TCompare compare, const T& t, const Tx&... tx) + { + return multimax_compare(compare, t, multimax_compare(compare, tx...)); + } + + //*************************************************************************** + + template + constexpr const T& multimin(const T& a, const T& b) + { + return a < b ? a : b; + } + + template + constexpr const T& multimin(const T& t, const Tx&... tx) + { + return multimin(t, multimin(tx...)); + } + + //*************************************************************************** + + template + constexpr const T& multimin_compare(TCompare compare, const T& a, const T& b) + { + return compare(a, b) ? a : b; + } + + template + constexpr const T& multimin_compare(TCompare compare, const T& t, const Tx&... tx) + { + return multimin_compare(compare, t, multimin_compare(compare, tx...)); + } + + //*************************************************************************** + + template + constexpr const TIterator& multimax_iter(const TIterator& a, const TIterator& b) + { + return *a < *b ? b : a; + } + + template + constexpr const TIterator& multimax_iter(const TIterator& t, const TIteratorx&... tx) + { + return multimax_iter(t, multimax_iter(tx...)); + } + + //*************************************************************************** + + template + constexpr const TIterator& multimax_iter_compare(TCompare compare, const TIterator& a, const TIterator& b) + { + return compare(*a, *b) ? b : a; + } + + template + constexpr const TIterator& multimax_iter_compare(TCompare compare, const TIterator& t, const TIteratorx&... tx) + { + return multimax_iter_compare(compare, t, multimax_iter_compare(compare, tx...)); + } + + //*************************************************************************** + + template + constexpr const TIterator& multimin_iter(const TIterator& a, const TIterator& b) + { + return *a < *b ? a : b; + } + + template + constexpr const TIterator& multimin_iter(const TIterator& t, const Tx&... tx) + { + return multimin_iter(t, multimin_iter(tx...)); + } + + //*************************************************************************** + + template + constexpr const TIterator& multimin_iter_compare(TCompare compare, const TIterator& a, const TIterator& b) + { + return compare(*a, *b) ? a : b; + } + + template + constexpr const TIterator& multimin_iter_compare(TCompare compare, const TIterator& t, const Tx&... tx) + { + return multimin_iter_compare(compare, t, multimin_iter_compare(compare, tx...)); + } +#endif } #endif diff --git a/include/etl/version.h b/include/etl/version.h index ba1db1fc..8ce39421 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -38,7 +38,7 @@ SOFTWARE. ///\ingroup utilities #define ETL_VERSION_MAJOR 14 -#define ETL_VERSION_MINOR 20 +#define ETL_VERSION_MINOR 21 #define ETL_VERSION_PATCH 0 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) ETL_STRINGIFY(ETL_VERSION_MINOR) ETL_STRINGIFY(ETL_VERSION_PATCH) diff --git a/support/Release notes.txt b/support/Release notes.txt index f1241241..cde56ef4 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,16 @@ +=============================================================================== +14.21.0 +Added variadic min and max functions. +etl::multimin +etl::multimin_compare +etl::multimin_iter +etl::multimin_iter_compare +etl::multimax +etl::multimax_compare +etl::multimax_iter +etl::multimax_iter_compare +C++11 only. + =============================================================================== 14.20.0 Added etl::multi_array for multi-dimensional arrays. diff --git a/test/test_algorithm.cpp b/test/test_algorithm.cpp index f7eea553..bbb7f6d7 100644 --- a/test/test_algorithm.cpp +++ b/test/test_algorithm.cpp @@ -909,5 +909,41 @@ namespace CHECK(is_same); } } + + //========================================================================= + TEST(multimax) + { + CHECK_EQUAL(8, etl::multimax(1, 2, 3, 4, 5, 6, 7, 8)); + CHECK_EQUAL(8, etl::multimax_compare(std::less(), 1, 2, 3, 4, 5, 6, 7, 8)); + CHECK_EQUAL(1, etl::multimax_compare(std::greater(), 1, 2, 3, 4, 5, 6, 7, 8)); + } + + //========================================================================= + TEST(multimax_iter) + { + int i[8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + + CHECK_EQUAL(8, *etl::multimax_iter(&i[0], &i[1], &i[2], &i[3], &i[4], &i[5], &i[6], &i[7])); + CHECK_EQUAL(8, *etl::multimax_iter_compare(std::less(), &i[0], &i[1], &i[2], &i[3], &i[4], &i[5], &i[6], &i[7])); + CHECK_EQUAL(1, *etl::multimax_iter_compare(std::greater(), &i[0], &i[1], &i[2], &i[3], &i[4], &i[5], &i[6], &i[7])); + } + + //========================================================================= + TEST(multimin) + { + CHECK_EQUAL(1, etl::multimin(1, 2, 3, 4, 5, 6, 7, 8)); + CHECK_EQUAL(1, etl::multimin_compare(std::less(), 1, 2, 3, 4, 5, 6, 7, 8)); + CHECK_EQUAL(8, etl::multimin_compare(std::greater(), 1, 2, 3, 4, 5, 6, 7, 8)); + } + + //========================================================================= + TEST(multimin_iter) + { + int i[8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + + CHECK_EQUAL(1, *etl::multimin_iter(&i[0], &i[1], &i[2], &i[3], &i[4], &i[5], &i[6], &i[7])); + CHECK_EQUAL(1, *etl::multimin_iter_compare(std::less(), &i[0], &i[1], &i[2], &i[3], &i[4], &i[5], &i[6], &i[7])); + CHECK_EQUAL(8, *etl::multimin_iter_compare(std::greater(), &i[0], &i[1], &i[2], &i[3], &i[4], &i[5], &i[6], &i[7])); + } }; } From 066981dbdf8bd3ec8d1fa947e987e246be99d8e4 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 5 May 2019 09:45:37 +0100 Subject: [PATCH 28/41] Updated doxygen comments --- include/etl/algorithm.h | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/include/etl/algorithm.h b/include/etl/algorithm.h index 26036156..626ca838 100644 --- a/include/etl/algorithm.h +++ b/include/etl/algorithm.h @@ -1167,7 +1167,8 @@ namespace etl #if ETL_CPP11_SUPPORTED //*************************************************************************** - + /// Returns the maximum value. + //*************************************************************************** template constexpr const T& multimax(const T& a, const T& b) { @@ -1181,7 +1182,9 @@ namespace etl } //*************************************************************************** - + /// Returns the maximum value. + /// User supplied compare function. + //*************************************************************************** template constexpr const T& multimax_compare(TCompare compare, const T& a, const T& b) { @@ -1195,7 +1198,8 @@ namespace etl } //*************************************************************************** - + /// Returns the maximum value. + //*************************************************************************** template constexpr const T& multimin(const T& a, const T& b) { @@ -1209,7 +1213,9 @@ namespace etl } //*************************************************************************** - + /// Returns the minimum value. + /// User supplied compare function. + //*************************************************************************** template constexpr const T& multimin_compare(TCompare compare, const T& a, const T& b) { @@ -1223,7 +1229,8 @@ namespace etl } //*************************************************************************** - + /// Returns the iterator to the maximum value. + //*************************************************************************** template constexpr const TIterator& multimax_iter(const TIterator& a, const TIterator& b) { @@ -1237,7 +1244,9 @@ namespace etl } //*************************************************************************** - + /// Returns the iterator to the maximum value. + /// User supplied compare function. + //*************************************************************************** template constexpr const TIterator& multimax_iter_compare(TCompare compare, const TIterator& a, const TIterator& b) { @@ -1251,7 +1260,8 @@ namespace etl } //*************************************************************************** - + /// Returns the iterator to the minimum value. + //*************************************************************************** template constexpr const TIterator& multimin_iter(const TIterator& a, const TIterator& b) { @@ -1265,7 +1275,9 @@ namespace etl } //*************************************************************************** - + /// Returns the iterator to the minimum value. + /// User supplied compare function. + //*************************************************************************** template constexpr const TIterator& multimin_iter_compare(TCompare compare, const TIterator& a, const TIterator& b) { From 281db12c17352428768b5f279b22b60f2a9353af Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 5 May 2019 11:39:47 +0100 Subject: [PATCH 29/41] Merge remote-tracking branch 'origin/master' into development --- include/etl/profiles/etl_profile.h | 26 +++++++++++++------------- library.json | 27 +++++++++++++++++++++++++++ test/codeblocks/ETL.cbp | 2 ++ 3 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 library.json diff --git a/include/etl/profiles/etl_profile.h b/include/etl/profiles/etl_profile.h index 187bcb5b..9242d71c 100644 --- a/include/etl/profiles/etl_profile.h +++ b/include/etl/profiles/etl_profile.h @@ -29,31 +29,31 @@ SOFTWARE. ******************************************************************************/ #ifndef __ETL_PROFILE_H__ #define __ETL_PROFILE_H__ -#ifdef PROFILE_MSVC +#if defined(PROFILE_MSVC) #include "msvc_x86.h" -#elif PROFILE_GCC_GENERIC +#elif defined(PROFILE_GCC_GENERIC) #include "gcc_generic.h" -#elif PROFILE_GCC_LINUX_X86 +#elif defined(PROFILE_GCC_LINUX_X86) #include "gcc_linux_x86.h" -#elif PROFILE_GCC_WINDOWS_X86 +#elif defined(PROFILE_GCC_WINDOWS_X86) #include "gcc_windows_x86.h" -#elif PROFILE_ARM_V5_GENERIC +#elif defined(PROFILE_ARM_V5_GENERIC) #include "armv5.h" -#elif PROFILE_ARM_V6_GENERIC +#elif defined(PROFILE_ARM_V6_GENERIC) #include "armv6.h" -#elif PROFILE_ARDUINO +#elif defined(PROFILE_ARDUINO) #include "arduino_arm.h" -#elif PROFILE_TICC +#elif defined(PROFILE_TICC) #include "ticc.h" -#elif PROFILE_CPP03_GENERIC +#elif defined(PROFILE_CPP03_GENERIC) #include "cpp03.h" -#elif PROFILE_CPP11_GENERIC +#elif defined(PROFILE_CPP11_GENERIC) #include "cpp11.h" -#elif PROFILE_CPP14_GENERIC +#elif defined(PROFILE_CPP14_GENERIC) #include "cpp14.h" -#elif PROFILE_CPP17_GENERIC +#elif defined(PROFILE_CPP17_GENERIC) #include "cpp17.h" -#elif PROFILE_CUSTOM +#elif defined(PROFILE_CUSTOM) #include "custom_profile.h" #else #error Must provide a profile header file when buiding ETL. See https://www.etlcpp.com/setup.html diff --git a/library.json b/library.json new file mode 100644 index 00000000..2a6fdd34 --- /dev/null +++ b/library.json @@ -0,0 +1,27 @@ +{ + "name": "Embedded Template Library", + "version": "10.21.2", + "authors": { + "name": "John Wellbelove", + "email": "" + }, + "homepage": "https://www.etlcpp.com/", + "license": "MIT", + "description": "A C++ template library tailored for embedded systems. Requires some support from STL. See http://andybrown.me.uk/2011/01/15/the-standard-template-library-stl-for-avr-with-c-streams/ or https://github.com/mike-matera/ArduinoSTL.git for Arduino.", + "repository": "https://github.com/ETLCPP/etl.git", + "platforms": [ + "espressif8266", + "espressif32" + ], + "frameworks": "arduino", + "build": { + "flags": [ + "-I include" + ], + "srcFilter": [ + "-", + "-", + "-" + ] +} +} diff --git a/test/codeblocks/ETL.cbp b/test/codeblocks/ETL.cbp index a7041a42..a4bfe86e 100644 --- a/test/codeblocks/ETL.cbp +++ b/test/codeblocks/ETL.cbp @@ -231,6 +231,7 @@ + @@ -397,6 +398,7 @@ + From 2a3aa9fceef55884d1a9fc9f1a2d99aae835f192 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Mon, 6 May 2019 20:53:15 +0100 Subject: [PATCH 30/41] Merge remote-tracking branch 'origin/feature/memory_set_clear' into development # Conflicts: # include/etl/version.h # support/Release notes.txt --- include/etl/memory.h | 34 ++++++++++++++++-------- include/etl/version.h | 2 +- support/Release notes.txt | 5 ++++ test/test_memory.cpp | 54 +++++++++++++++++---------------------- 4 files changed, 52 insertions(+), 43 deletions(-) diff --git a/include/etl/memory.h b/include/etl/memory.h index 35c8cfb9..4bdd43e4 100644 --- a/include/etl/memory.h +++ b/include/etl/memory.h @@ -1107,21 +1107,33 @@ namespace etl }; //***************************************************************************** - /// Base class for objects that require their memory to be wiped after use. - /// Erases the object's memory to zero. - /// Note: This must be the last destructor called for the derived object. - ///\tparam T The derived type. + /// A low level function that clears an object's memory to zero. + ///\tparam T The type. + ///\param object The object to clear. ///\ingroup memory //***************************************************************************** template - struct wipe_on_destruct + void memory_clear(T &object) { - ~wipe_on_destruct() - { - char* pobject = reinterpret_cast(static_cast(this)); - memset(pobject, 0, sizeof(T)); - } - }; + std::fill_n(reinterpret_cast(&object), + sizeof(T), + 0); + } + + //***************************************************************************** + /// A low level function that sets an object's memory to a value. + ///\tparam T The type. + ///\param object The object to set. + ///\param value The value to set the object with. + ///\ingroup memory + //***************************************************************************** + template + void memory_set(T &object, int value) + { + std::fill_n(reinterpret_cast(&object), + sizeof(T), + static_cast(value)); + } } #endif diff --git a/include/etl/version.h b/include/etl/version.h index 8ce39421..3bc191e9 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -38,7 +38,7 @@ SOFTWARE. ///\ingroup utilities #define ETL_VERSION_MAJOR 14 -#define ETL_VERSION_MINOR 21 +#define ETL_VERSION_MINOR 22 #define ETL_VERSION_PATCH 0 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) ETL_STRINGIFY(ETL_VERSION_MINOR) ETL_STRINGIFY(ETL_VERSION_PATCH) diff --git a/support/Release notes.txt b/support/Release notes.txt index cde56ef4..40daab67 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,8 @@ +=============================================================================== +14.22.0 +Added etl::memory_clear and etl::memory_set utility functions. +These functions will overwrite the memory allocated to the supplied object. + =============================================================================== 14.21.0 Added variadic min and max functions. diff --git a/test/test_memory.cpp b/test/test_memory.cpp index a0448891..49b0a3fb 100644 --- a/test/test_memory.cpp +++ b/test/test_memory.cpp @@ -506,7 +506,7 @@ namespace std::fill(std::begin(n), std::end(n), 0xFF); CHECK_EQUAL(0x00000000U, etl::make_default_at(pn)); - std::fill(std::begin(n), std::end(n), 0x00); + std::fill(std::begin(n), std::end(n), 0x00); CHECK_EQUAL(0xFFFFFFFFU, etl::make_value_at(pn, 0xFFFFFFFFU)); std::fill(std::begin(n), std::end(n), 0xFF); @@ -530,45 +530,37 @@ namespace } //************************************************************************* - TEST(test_wipe_on_destruct) + TEST(test_memory_clear) { - struct Data : public etl::wipe_on_destruct + struct Data { - Data(int a_, char b_, double c_) - : a(a_), - b(b_), - c(c_) - { - } - - bool operator ==(const Data& other) const - { - return (a == other.a) && (b == other.b) && (c == other.c); - } - - int a; - char b; - double c; + uint32_t d1; + char d2; }; - std::array buffer; - buffer.fill(0); + Data data = { 0xFFFFFFFF, char(0xFF) }; - // Construct in-place. - ::new (buffer.data()) Data(1, 'b', 3.4); + etl::memory_clear(data); - Data& other = *reinterpret_cast(buffer.data()); - CHECK(other == Data(1, 'b', 3.4)); + CHECK_EQUAL(0x00000000, data.d1); + CHECK_EQUAL(0x00, data.d2); + } - // Cleared compare buffer. - std::array clear; - clear.fill(0); + //************************************************************************* + TEST(test_memory_set) + { + struct Data + { + uint32_t d1; + char d2; + }; - // Destruct; - other.~Data(); + Data data = { 0xFFFFFFFF, char(0xFF) }; - // Storage should be wiped. - CHECK(buffer == clear); + etl::memory_set(data, 0x5A); + + CHECK_EQUAL(0x5A5A5A5A, data.d1); + CHECK_EQUAL(0x5A, data.d2); } }; } From a8253b2a3f221eb964a09c185d73f1f478fde7a4 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Tue, 7 May 2019 21:28:29 +0100 Subject: [PATCH 31/41] Merge remote-tracking branch 'origin/feature/memory_set_clear' into development # Conflicts: # include/etl/memory.h # include/etl/version.h # support/Release notes.txt --- include/etl/memory.h | 48 ++++++++++++++++++++++++++++++--------- include/etl/version.h | 2 +- support/Release notes.txt | 4 ++++ 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/include/etl/memory.h b/include/etl/memory.h index 4bdd43e4..69af6ed9 100644 --- a/include/etl/memory.h +++ b/include/etl/memory.h @@ -1108,31 +1108,57 @@ namespace etl //***************************************************************************** /// A low level function that clears an object's memory to zero. - ///\tparam T The type. - ///\param object The object to clear. + + ///\param p Pointer to the memory. + ///\param n Size of the memory. + ///\ingroup memory + //***************************************************************************** + inline void memory_clear(volatile char* p, size_t n) + { + while (n--) + { + *p++ = 0; + } + } + + //***************************************************************************** + /// A low level function that clears an object's memory to zero. + ///\tparam T The type. + ///\param object The object to clear. ///\ingroup memory //***************************************************************************** template void memory_clear(T &object) { - std::fill_n(reinterpret_cast(&object), - sizeof(T), - 0); + memory_clear(reinterpret_cast(&object), sizeof(T)); + } + + //***************************************************************************** + /// A low level function that clears an object's memory to zero. + ///\param p Pointer to the memory. + ///\param n Size of the memory. + ///\param value The value to set. + ///\ingroup memory + //***************************************************************************** + inline void memory_set(volatile char* p, size_t n, char value) + { + while (n--) + { + *p++ = value; + } } //***************************************************************************** /// A low level function that sets an object's memory to a value. ///\tparam T The type. - ///\param object The object to set. - ///\param value The value to set the object with. + ///\param object The object to set. + ///\param value The value to set the object's memory to. ///\ingroup memory //***************************************************************************** template - void memory_set(T &object, int value) + void memory_set(T &object, char value) { - std::fill_n(reinterpret_cast(&object), - sizeof(T), - static_cast(value)); + memory_set(reinterpret_cast(&object), sizeof(T), value); } } diff --git a/include/etl/version.h b/include/etl/version.h index 3bc191e9..ab365918 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -39,7 +39,7 @@ SOFTWARE. #define ETL_VERSION_MAJOR 14 #define ETL_VERSION_MINOR 22 -#define ETL_VERSION_PATCH 0 +#define ETL_VERSION_PATCH 1 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) ETL_STRINGIFY(ETL_VERSION_MINOR) ETL_STRINGIFY(ETL_VERSION_PATCH) #define ETL_VERSION_W ETL_WIDE_STRING(ETL_CONCAT(ETL_CONCAT(ETL_VERSION_MAJOR, ETL_VERSION_MINOR), ETL_VERSION_PATCH)) diff --git a/support/Release notes.txt b/support/Release notes.txt index 40daab67..e4201fa2 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,7 @@ +=============================================================================== +14.22.1 +Modified memory functions so that they will not be optimised away. + =============================================================================== 14.22.0 Added etl::memory_clear and etl::memory_set utility functions. From ae3c65c787dd9d5ccb3e403739d281f4c69990ec Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sat, 11 May 2019 13:54:04 +0100 Subject: [PATCH 32/41] Added secure clear to strings. --- include/etl/basic_string.h | 63 +++++++++++++++++++----- include/etl/memory.h | 79 +++++++++++++++++++++++++++++-- test/test_memory.cpp | 92 ++++++++++++++++++++++++++++++++++++ test/test_string_char.cpp | 85 +++++++++++++++++++++++++++++++++ test/test_string_u16.cpp | 85 +++++++++++++++++++++++++++++++++ test/test_string_u32.cpp | 85 +++++++++++++++++++++++++++++++++ test/test_string_wchar_t.cpp | 85 +++++++++++++++++++++++++++++++++ 7 files changed, 560 insertions(+), 14 deletions(-) diff --git a/include/etl/basic_string.h b/include/etl/basic_string.h index 096c30b3..cda271f1 100644 --- a/include/etl/basic_string.h +++ b/include/etl/basic_string.h @@ -50,6 +50,7 @@ SOFTWARE. #include "error_handler.h" #include "integral_limits.h" #include "exception.h" +#include "memory.h" #undef ETL_FILE #define ETL_FILE "27" @@ -229,15 +230,32 @@ namespace etl is_truncated = false; } + //************************************************************************* + /// Sets the 'secure' flag to the requested state. + //************************************************************************* + void set_secure() + { + clear_afer_use = true; + } + + //************************************************************************* + /// Gets the 'secure' state flag. +//************************************************************************* + bool is_secure() const + { + return clear_afer_use; + } + protected: //************************************************************************* /// Constructor. //************************************************************************* string_base(size_t max_size_) - : is_truncated(false), - current_size(0), - CAPACITY(max_size_) + : is_truncated(false) + , clear_afer_use(false) + , current_size(0) + , CAPACITY(max_size_) { } @@ -248,9 +266,10 @@ namespace etl { } - bool is_truncated; ///< Set to true if the operation truncated the string. - size_type current_size; ///< The current number of elements in the string. - const size_type CAPACITY; ///< The maximum number of elements in the string. + bool is_truncated; ///< Set to true if the operation truncated the string. + bool clear_afer_use; ///< Set to true if the string must be cleared after use. + size_type current_size; ///< The current number of elements in the string. + const size_type CAPACITY; ///< The maximum number of elements in the string. }; //*************************************************************************** @@ -531,6 +550,8 @@ namespace etl { is_truncated = true; } + + cleanup(); } //********************************************************************* @@ -1059,6 +1080,7 @@ namespace etl current_size -= n_delete; p_buffer[current_size] = 0; + cleanup(); return first; } @@ -1383,6 +1405,11 @@ namespace etl // Insert the new stuff. insert(position, str, subposition, sublength); + if (str.truncated()) + { + is_truncated = true; + } + return *this; } @@ -1983,7 +2010,8 @@ namespace etl void initialise() { current_size = 0; - p_buffer[0] = 0; + cleanup(); + p_buffer[0] = 0; is_truncated = false; } @@ -2037,6 +2065,17 @@ namespace etl } } + //************************************************************************* + /// Clear the unused trailing portion of the string. + //************************************************************************* + void cleanup() + { + if (is_secure()) + { + etl::memory_clear_range(&p_buffer[current_size], &p_buffer[CAPACITY]); + } + } + // Disable copy construction. ibasic_string(const ibasic_string&); @@ -2047,15 +2086,17 @@ namespace etl //************************************************************************* #if defined(ETL_POLYMORPHIC_STRINGS) || defined(ETL_POLYMORPHIC_CONTAINERS) public: - virtual ~ibasic_string() - { - } + virtual #else protected: +#endif ~ibasic_string() { + if (is_secure()) + { + initialise(); + } } -#endif }; //*************************************************************************** diff --git a/include/etl/memory.h b/include/etl/memory.h index 69af6ed9..606e6a7a 100644 --- a/include/etl/memory.h +++ b/include/etl/memory.h @@ -1108,7 +1108,6 @@ namespace etl //***************************************************************************** /// A low level function that clears an object's memory to zero. - ///\param p Pointer to the memory. ///\param n Size of the memory. ///\ingroup memory @@ -1128,11 +1127,39 @@ namespace etl ///\ingroup memory //***************************************************************************** template - void memory_clear(T &object) + void memory_clear(volatile T &object) { memory_clear(reinterpret_cast(&object), sizeof(T)); } + //***************************************************************************** + /// A low level function that clears a range to zero. + ///\tparam T The type. + ///\param begin The first object in the range. + ///\param n The number of objects. + ///\ingroup memory + //***************************************************************************** + template + void memory_clear_range(volatile T* begin, size_t n) + { + memory_clear(reinterpret_cast(begin), n * sizeof(T)); + } + + //***************************************************************************** + /// A low level function that clears a range to zero. + ///\tparam T The type. + ///\param begin The first object in the range. + ///\param end One past the last object in the range. + ///\ingroup memory + //***************************************************************************** + template + void memory_clear_range(volatile T* begin, volatile T* end) + { + const size_t n = static_cast(std::distance(begin, end)); + + memory_clear_range(begin, n); + } + //***************************************************************************** /// A low level function that clears an object's memory to zero. ///\param p Pointer to the memory. @@ -1156,10 +1183,56 @@ namespace etl ///\ingroup memory //***************************************************************************** template - void memory_set(T &object, char value) + void memory_set(volatile T &object, const char value) { memory_set(reinterpret_cast(&object), sizeof(T), value); } + + //***************************************************************************** + /// A low level function that clears a range to zero. + ///\tparam T The type. + ///\param begin The first object in the range. + ///\param n The number of objects. + ///\param value The value to set the object's memory to. + ///\ingroup memory + //***************************************************************************** + template + void memory_set_range(volatile T* begin, size_t n, const char value) + { + memory_set(reinterpret_cast(begin), n * sizeof(T), value); + } + + //***************************************************************************** + /// A low level function that clears a range to zero. + ///\tparam T The type. + ///\param begin The first object in the range. + ///\param end One past the last object in the range. + ///\param value The value to set the object's memory to. + ///\ingroup memory + //***************************************************************************** + template + void memory_set_range(volatile T* begin, volatile T* end, const char value) + { + const size_t n = static_cast(std::distance(begin, end)); + + memory_set_range(begin, n, value); + } + + //***************************************************************************** + /// Base class for objects that require their memory to be wiped after use. + /// Erases the object's memory to zero. + /// Note: This may not work for multiply inherited objects. + ///\tparam T The derived type. + ///\ingroup memory + //***************************************************************************** + template + struct wipe_on_destruct + { + ~wipe_on_destruct() + { + memory_clear(static_cast(*this)); + } + }; } #endif diff --git a/test/test_memory.cpp b/test/test_memory.cpp index 49b0a3fb..68a62caa 100644 --- a/test/test_memory.cpp +++ b/test/test_memory.cpp @@ -546,6 +546,52 @@ namespace CHECK_EQUAL(0x00, data.d2); } + //************************************************************************* + TEST(test_memory_clear_range_pointer_n) + { + struct Data + { + uint32_t d1; + char d2; + }; + + Data data[3] = { { 0xFFFFFFFF, char(0xFF) }, { 0xFFFFFFFF, char(0xFF) }, { 0xFFFFFFFF, char(0xFF) } }; + + etl::memory_clear_range(data, 3); + + CHECK_EQUAL(0x00000000, data[0].d1); + CHECK_EQUAL(0x00, data[0].d2); + + CHECK_EQUAL(0x00000000, data[1].d1); + CHECK_EQUAL(0x00, data[1].d2); + + CHECK_EQUAL(0x00000000, data[2].d1); + CHECK_EQUAL(0x00, data[2].d2); + } + + //************************************************************************* + TEST(test_memory_clear_range_pointer_pointer) + { + struct Data + { + uint32_t d1; + char d2; + }; + + Data data[3] = { { 0xFFFFFFFF, char(0xFF) }, { 0xFFFFFFFF, char(0xFF) }, { 0xFFFFFFFF, char(0xFF) } }; + + etl::memory_clear_range(std::begin(data), std::end(data)); + + CHECK_EQUAL(0x00000000, data[0].d1); + CHECK_EQUAL(0x00, data[0].d2); + + CHECK_EQUAL(0x00000000, data[1].d1); + CHECK_EQUAL(0x00, data[1].d2); + + CHECK_EQUAL(0x00000000, data[2].d1); + CHECK_EQUAL(0x00, data[2].d2); + } + //************************************************************************* TEST(test_memory_set) { @@ -562,5 +608,51 @@ namespace CHECK_EQUAL(0x5A5A5A5A, data.d1); CHECK_EQUAL(0x5A, data.d2); } + + //************************************************************************* + TEST(test_memory_set_range_pointer_n) + { + struct Data + { + uint32_t d1; + char d2; + }; + + Data data[3] = { { 0xFFFFFFFF, char(0xFF) }, { 0xFFFFFFFF, char(0xFF) }, { 0xFFFFFFFF, char(0xFF) } }; + + etl::memory_set_range(data, 3, 0x5A); + + CHECK_EQUAL(0x5A5A5A5A, data[0].d1); + CHECK_EQUAL(0x5A, data[0].d2); + + CHECK_EQUAL(0x5A5A5A5A, data[1].d1); + CHECK_EQUAL(0x5A, data[1].d2); + + CHECK_EQUAL(0x5A5A5A5A, data[2].d1); + CHECK_EQUAL(0x5A, data[2].d2); + } + + //************************************************************************* + TEST(test_memory_set_range_pointer_pointer) + { + struct Data + { + uint32_t d1; + char d2; + }; + + Data data[3] = { { 0xFFFFFFFF, char(0xFF) }, { 0xFFFFFFFF, char(0xFF) }, { 0xFFFFFFFF, char(0xFF) } }; + + etl::memory_set_range(std::begin(data), std::end(data), 0x5A); + + CHECK_EQUAL(0x5A5A5A5A, data[0].d1); + CHECK_EQUAL(0x5A, data[0].d2); + + CHECK_EQUAL(0x5A5A5A5A, data[1].d1); + CHECK_EQUAL(0x5A, data[1].d2); + + CHECK_EQUAL(0x5A5A5A5A, data[2].d1); + CHECK_EQUAL(0x5A, data[2].d2); + } }; } diff --git a/test/test_string_char.cpp b/test/test_string_char.cpp index b40a4ae4..10c108ba 100644 --- a/test/test_string_char.cpp +++ b/test/test_string_char.cpp @@ -3651,5 +3651,90 @@ namespace text.clear_truncated(); CHECK(!text.truncated()); } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_destructor) + { + char buffer[sizeof(Text)]; + std::fill_n(buffer, sizeof(Text), 0); + ::new (buffer) Text(STR("ABCDEF")); + + Text& text = *reinterpret_cast(buffer); + text.set_secure(); + + CHECK(Text(STR("ABCDEF")) == text); + + Text::pointer pb = text.begin(); + Text::pointer pe = text.end(); + + // Destroy the text object. + text.~Text(); + + // Check there no non-zero values in the string. + CHECK(std::find_if(pb, pe, [](auto x) { return x != 0; }) == pe); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_assign) + { + Text text; + text.set_secure(); + text.assign(STR("ABCDEF")); + + Text::pointer pb = text.begin(); + Text::pointer pe = text.end(); + + text.assign(STR("ABC")); + + CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_erase) + { + Text text; + text.set_secure(); + text.assign(STR("ABCDEF")); + + Text::pointer pb = text.begin(); + Text::pointer pe = text.end(); + + text.erase(pb + 2, pb + 5); + + // Check there no non-zero values in the remainder of the string. + CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_replace) + { + Text text; + text.set_secure(); + text.assign(STR("ABCDEF")); + + Text::pointer pb = text.begin(); + Text::pointer pe = text.end(); + + text.replace(pb + 1, pb + 4, STR("G")); + + // Check there no non-zero values in the remainder of the string. + CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_clear) + { + Text text; + text.set_secure(); + text.assign(STR("ABCDEF")); + + Text::pointer pb = text.begin(); + Text::pointer pe = text.end(); + + text.clear(); + + // Check there no non-zero values in the remainder of the string. + CHECK(std::find_if(pb, pe, [](auto x) { return x != 0; }) == pe); + } }; } diff --git a/test/test_string_u16.cpp b/test/test_string_u16.cpp index ba4c94af..c2b6067c 100644 --- a/test/test_string_u16.cpp +++ b/test/test_string_u16.cpp @@ -3650,5 +3650,90 @@ namespace text.clear_truncated(); CHECK(!text.truncated()); } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_destructor) + { + char buffer[sizeof(Text)]; + std::fill_n(buffer, sizeof(Text), 0); + ::new (buffer) Text(STR("ABCDEF")); + + Text& text = *reinterpret_cast(buffer); + text.set_secure(); + + CHECK(Text(STR("ABCDEF")) == text); + + Text::pointer pb = text.begin(); + Text::pointer pe = text.end(); + + // Destroy the text object. + text.~Text(); + + // Check there no non-zero values in the string. + CHECK(std::find_if(pb, pe, [](auto x) { return x != 0; }) == pe); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_assign) + { + Text text; + text.set_secure(); + text.assign(STR("ABCDEF")); + + Text::pointer pb = text.begin(); + Text::pointer pe = text.end(); + + text.assign(STR("ABC")); + + CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_erase) + { + Text text; + text.set_secure(); + text.assign(STR("ABCDEF")); + + Text::pointer pb = text.begin(); + Text::pointer pe = text.end(); + + text.erase(pb + 2, pb + 5); + + // Check there no non-zero values in the remainder of the string. + CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_replace) + { + Text text; + text.set_secure(); + text.assign(STR("ABCDEF")); + + Text::pointer pb = text.begin(); + Text::pointer pe = text.end(); + + text.replace(pb + 1, pb + 4, STR("G")); + + // Check there no non-zero values in the remainder of the string. + CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_clear) + { + Text text; + text.set_secure(); + text.assign(STR("ABCDEF")); + + Text::pointer pb = text.begin(); + Text::pointer pe = text.end(); + + text.clear(); + + // Check there no non-zero values in the remainder of the string. + CHECK(std::find_if(pb, pe, [](auto x) { return x != 0; }) == pe); + } }; } diff --git a/test/test_string_u32.cpp b/test/test_string_u32.cpp index 4acfeb8a..cbdd7f45 100644 --- a/test/test_string_u32.cpp +++ b/test/test_string_u32.cpp @@ -3650,5 +3650,90 @@ namespace text.clear_truncated(); CHECK(!text.truncated()); } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_destructor) + { + char buffer[sizeof(Text)]; + std::fill_n(buffer, sizeof(Text), 0); + ::new (buffer) Text(STR("ABCDEF")); + + Text& text = *reinterpret_cast(buffer); + text.set_secure(); + + CHECK(Text(STR("ABCDEF")) == text); + + Text::pointer pb = text.begin(); + Text::pointer pe = text.end(); + + // Destroy the text object. + text.~Text(); + + // Check there no non-zero values in the string. + CHECK(std::find_if(pb, pe, [](auto x) { return x != 0; }) == pe); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_assign) + { + Text text; + text.set_secure(); + text.assign(STR("ABCDEF")); + + Text::pointer pb = text.begin(); + Text::pointer pe = text.end(); + + text.assign(STR("ABC")); + + CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_erase) + { + Text text; + text.set_secure(); + text.assign(STR("ABCDEF")); + + Text::pointer pb = text.begin(); + Text::pointer pe = text.end(); + + text.erase(pb + 2, pb + 5); + + // Check there no non-zero values in the remainder of the string. + CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_replace) + { + Text text; + text.set_secure(); + text.assign(STR("ABCDEF")); + + Text::pointer pb = text.begin(); + Text::pointer pe = text.end(); + + text.replace(pb + 1, pb + 4, STR("G")); + + // Check there no non-zero values in the remainder of the string. + CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_clear) + { + Text text; + text.set_secure(); + text.assign(STR("ABCDEF")); + + Text::pointer pb = text.begin(); + Text::pointer pe = text.end(); + + text.clear(); + + // Check there no non-zero values in the remainder of the string. + CHECK(std::find_if(pb, pe, [](auto x) { return x != 0; }) == pe); + } }; } diff --git a/test/test_string_wchar_t.cpp b/test/test_string_wchar_t.cpp index 0aba10d1..463b029f 100644 --- a/test/test_string_wchar_t.cpp +++ b/test/test_string_wchar_t.cpp @@ -3650,5 +3650,90 @@ namespace text.clear_truncated(); CHECK(!text.truncated()); } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_destructor) + { + char buffer[sizeof(Text)]; + std::fill_n(buffer, sizeof(Text), 0); + ::new (buffer) Text(STR("ABCDEF")); + + Text& text = *reinterpret_cast(buffer); + text.set_secure(); + + CHECK(Text(STR("ABCDEF")) == text); + + Text::pointer pb = text.begin(); + Text::pointer pe = text.end(); + + // Destroy the text object. + text.~Text(); + + // Check there no non-zero values in the string. + CHECK(std::find_if(pb, pe, [](auto x) { return x != 0; }) == pe); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_assign) + { + Text text; + text.set_secure(); + text.assign(STR("ABCDEF")); + + Text::pointer pb = text.begin(); + Text::pointer pe = text.end(); + + text.assign(STR("ABC")); + + CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_erase) + { + Text text; + text.set_secure(); + text.assign(STR("ABCDEF")); + + Text::pointer pb = text.begin(); + Text::pointer pe = text.end(); + + text.erase(pb + 2, pb + 5); + + // Check there no non-zero values in the remainder of the string. + CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_replace) + { + Text text; + text.set_secure(); + text.assign(STR("ABCDEF")); + + Text::pointer pb = text.begin(); + Text::pointer pe = text.end(); + + text.replace(pb + 1, pb + 4, STR("G")); + + // Check there no non-zero values in the remainder of the string. + CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_clear) + { + Text text; + text.set_secure(); + text.assign(STR("ABCDEF")); + + Text::pointer pb = text.begin(); + Text::pointer pe = text.end(); + + text.clear(); + + // Check there no non-zero values in the remainder of the string. + CHECK(std::find_if(pb, pe, [](auto x) { return x != 0; }) == pe); + } }; } From 392ebaab91c4fc1e55545da47400dcfb3f43491a Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 12 May 2019 11:05:37 +0100 Subject: [PATCH 33/41] Added secure clear to strings. --- include/etl/basic_string.h | 7 ++++++- include/etl/version.h | 4 ++-- support/Release notes.txt | 5 +++++ test/test_string_char.cpp | 10 +++++----- test/test_string_u16.cpp | 10 +++++----- test/test_string_u32.cpp | 10 +++++----- test/test_string_wchar_t.cpp | 10 +++++----- 7 files changed, 33 insertions(+), 23 deletions(-) diff --git a/include/etl/basic_string.h b/include/etl/basic_string.h index cda271f1..e3effdf0 100644 --- a/include/etl/basic_string.h +++ b/include/etl/basic_string.h @@ -2076,9 +2076,14 @@ namespace etl } } - // Disable copy construction. + //************************************************************************* + /// Disable copy construction. + //************************************************************************* ibasic_string(const ibasic_string&); + //************************************************************************* + /// Pointer to the string's buffer. + //************************************************************************* T* p_buffer; //************************************************************************* diff --git a/include/etl/version.h b/include/etl/version.h index ab365918..da98a129 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -38,8 +38,8 @@ SOFTWARE. ///\ingroup utilities #define ETL_VERSION_MAJOR 14 -#define ETL_VERSION_MINOR 22 -#define ETL_VERSION_PATCH 1 +#define ETL_VERSION_MINOR 23 +#define ETL_VERSION_PATCH 0 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) ETL_STRINGIFY(ETL_VERSION_MINOR) ETL_STRINGIFY(ETL_VERSION_PATCH) #define ETL_VERSION_W ETL_WIDE_STRING(ETL_CONCAT(ETL_CONCAT(ETL_VERSION_MAJOR, ETL_VERSION_MINOR), ETL_VERSION_PATCH)) diff --git a/support/Release notes.txt b/support/Release notes.txt index e4201fa2..42f12388 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,8 @@ +=============================================================================== +14.23.0 +Added an optional secure mode to strings so that unused space will be cleared to zero +and also cleared on destruction. + =============================================================================== 14.22.1 Modified memory functions so that they will not be optimised away. diff --git a/test/test_string_char.cpp b/test/test_string_char.cpp index 10c108ba..9d5644fc 100644 --- a/test/test_string_char.cpp +++ b/test/test_string_char.cpp @@ -3671,7 +3671,7 @@ namespace text.~Text(); // Check there no non-zero values in the string. - CHECK(std::find_if(pb, pe, [](auto x) { return x != 0; }) == pe); + CHECK(std::find_if(pb, pe, [](Text::value_type x) { return x != 0; }) == pe); } //************************************************************************* @@ -3686,7 +3686,7 @@ namespace text.assign(STR("ABC")); - CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + CHECK(std::find_if(text.end(), pe, [](Text::value_type x) { return x != 0; }) == pe); } //************************************************************************* @@ -3702,7 +3702,7 @@ namespace text.erase(pb + 2, pb + 5); // Check there no non-zero values in the remainder of the string. - CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + CHECK(std::find_if(text.end(), pe, [](Text::value_type x) { return x != 0; }) == pe); } //************************************************************************* @@ -3718,7 +3718,7 @@ namespace text.replace(pb + 1, pb + 4, STR("G")); // Check there no non-zero values in the remainder of the string. - CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + CHECK(std::find_if(text.end(), pe, [](Text::value_type x) { return x != 0; }) == pe); } //************************************************************************* @@ -3734,7 +3734,7 @@ namespace text.clear(); // Check there no non-zero values in the remainder of the string. - CHECK(std::find_if(pb, pe, [](auto x) { return x != 0; }) == pe); + CHECK(std::find_if(pb, pe, [](Text::value_type x) { return x != 0; }) == pe); } }; } diff --git a/test/test_string_u16.cpp b/test/test_string_u16.cpp index c2b6067c..08382b8c 100644 --- a/test/test_string_u16.cpp +++ b/test/test_string_u16.cpp @@ -3670,7 +3670,7 @@ namespace text.~Text(); // Check there no non-zero values in the string. - CHECK(std::find_if(pb, pe, [](auto x) { return x != 0; }) == pe); + CHECK(std::find_if(pb, pe, [](Text::value_type x) { return x != 0; }) == pe); } //************************************************************************* @@ -3685,7 +3685,7 @@ namespace text.assign(STR("ABC")); - CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + CHECK(std::find_if(text.end(), pe, [](Text::value_type x) { return x != 0; }) == pe); } //************************************************************************* @@ -3701,7 +3701,7 @@ namespace text.erase(pb + 2, pb + 5); // Check there no non-zero values in the remainder of the string. - CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + CHECK(std::find_if(text.end(), pe, [](Text::value_type x) { return x != 0; }) == pe); } //************************************************************************* @@ -3717,7 +3717,7 @@ namespace text.replace(pb + 1, pb + 4, STR("G")); // Check there no non-zero values in the remainder of the string. - CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + CHECK(std::find_if(text.end(), pe, [](Text::value_type x) { return x != 0; }) == pe); } //************************************************************************* @@ -3733,7 +3733,7 @@ namespace text.clear(); // Check there no non-zero values in the remainder of the string. - CHECK(std::find_if(pb, pe, [](auto x) { return x != 0; }) == pe); + CHECK(std::find_if(pb, pe, [](Text::value_type x) { return x != 0; }) == pe); } }; } diff --git a/test/test_string_u32.cpp b/test/test_string_u32.cpp index cbdd7f45..55e9400a 100644 --- a/test/test_string_u32.cpp +++ b/test/test_string_u32.cpp @@ -3670,7 +3670,7 @@ namespace text.~Text(); // Check there no non-zero values in the string. - CHECK(std::find_if(pb, pe, [](auto x) { return x != 0; }) == pe); + CHECK(std::find_if(pb, pe, [](Text::value_type x) { return x != 0; }) == pe); } //************************************************************************* @@ -3685,7 +3685,7 @@ namespace text.assign(STR("ABC")); - CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + CHECK(std::find_if(text.end(), pe, [](Text::value_type x) { return x != 0; }) == pe); } //************************************************************************* @@ -3701,7 +3701,7 @@ namespace text.erase(pb + 2, pb + 5); // Check there no non-zero values in the remainder of the string. - CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + CHECK(std::find_if(text.end(), pe, [](Text::value_type x) { return x != 0; }) == pe); } //************************************************************************* @@ -3717,7 +3717,7 @@ namespace text.replace(pb + 1, pb + 4, STR("G")); // Check there no non-zero values in the remainder of the string. - CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + CHECK(std::find_if(text.end(), pe, [](Text::value_type x) { return x != 0; }) == pe); } //************************************************************************* @@ -3733,7 +3733,7 @@ namespace text.clear(); // Check there no non-zero values in the remainder of the string. - CHECK(std::find_if(pb, pe, [](auto x) { return x != 0; }) == pe); + CHECK(std::find_if(pb, pe, [](Text::value_type x) { return x != 0; }) == pe); } }; } diff --git a/test/test_string_wchar_t.cpp b/test/test_string_wchar_t.cpp index 463b029f..a00d8e02 100644 --- a/test/test_string_wchar_t.cpp +++ b/test/test_string_wchar_t.cpp @@ -3670,7 +3670,7 @@ namespace text.~Text(); // Check there no non-zero values in the string. - CHECK(std::find_if(pb, pe, [](auto x) { return x != 0; }) == pe); + CHECK(std::find_if(pb, pe, [](Text::value_type x) { return x != 0; }) == pe); } //************************************************************************* @@ -3685,7 +3685,7 @@ namespace text.assign(STR("ABC")); - CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + CHECK(std::find_if(text.end(), pe, [](Text::value_type x) { return x != 0; }) == pe); } //************************************************************************* @@ -3701,7 +3701,7 @@ namespace text.erase(pb + 2, pb + 5); // Check there no non-zero values in the remainder of the string. - CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + CHECK(std::find_if(text.end(), pe, [](Text::value_type x) { return x != 0; }) == pe); } //************************************************************************* @@ -3717,7 +3717,7 @@ namespace text.replace(pb + 1, pb + 4, STR("G")); // Check there no non-zero values in the remainder of the string. - CHECK(std::find_if(text.end(), pe, [](auto x) { return x != 0; }) == pe); + CHECK(std::find_if(text.end(), pe, [](Text::value_type x) { return x != 0; }) == pe); } //************************************************************************* @@ -3733,7 +3733,7 @@ namespace text.clear(); // Check there no non-zero values in the remainder of the string. - CHECK(std::find_if(pb, pe, [](auto x) { return x != 0; }) == pe); + CHECK(std::find_if(pb, pe, [](Text::value_type x) { return x != 0; }) == pe); } }; } From e591ab56e32a3b6b39670d064027a9b69afffaf0 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 12 May 2019 18:13:17 +0100 Subject: [PATCH 34/41] Added missing 'cleanup' to string's resize() method. --- include/etl/basic_string.h | 1 + include/etl/version.h | 2 +- support/Release notes.txt | 4 ++++ test/test_string_char.cpp | 14 ++++++++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/include/etl/basic_string.h b/include/etl/basic_string.h index e3effdf0..10c0afc8 100644 --- a/include/etl/basic_string.h +++ b/include/etl/basic_string.h @@ -437,6 +437,7 @@ namespace etl current_size = new_size; p_buffer[new_size] = 0; + cleanup(); } //********************************************************************* diff --git a/include/etl/version.h b/include/etl/version.h index da98a129..f338b0c8 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -39,7 +39,7 @@ SOFTWARE. #define ETL_VERSION_MAJOR 14 #define ETL_VERSION_MINOR 23 -#define ETL_VERSION_PATCH 0 +#define ETL_VERSION_PATCH 1 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) ETL_STRINGIFY(ETL_VERSION_MINOR) ETL_STRINGIFY(ETL_VERSION_PATCH) #define ETL_VERSION_W ETL_WIDE_STRING(ETL_CONCAT(ETL_CONCAT(ETL_VERSION_MAJOR, ETL_VERSION_MINOR), ETL_VERSION_PATCH)) diff --git a/support/Release notes.txt b/support/Release notes.txt index 42f12388..b01728aa 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,7 @@ +=============================================================================== +14.23.1 +Added missing 'cleanup' to string's resize() method. + =============================================================================== 14.23.0 Added an optional secure mode to strings so that unused space will be cleared to zero diff --git a/test/test_string_char.cpp b/test/test_string_char.cpp index 9d5644fc..b30d30c1 100644 --- a/test/test_string_char.cpp +++ b/test/test_string_char.cpp @@ -3689,6 +3689,20 @@ namespace CHECK(std::find_if(text.end(), pe, [](Text::value_type x) { return x != 0; }) == pe); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_resize_down) + { + Text text; + text.set_secure(); + text.assign(STR("ABCDEF")); + + Text::pointer pe = text.end(); + + text.resize(text.size() - 3U); + + CHECK(std::find_if(text.end(), pe, [](Text::value_type x) { return x != 0; }) == pe); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_secure_after_erase) { From 8fb468e708e67f457c079a88a1ab2a15b7d865ee Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Wed, 15 May 2019 22:17:53 +0100 Subject: [PATCH 35/41] Added size_of<> --- include/etl/type_traits.h | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/include/etl/type_traits.h b/include/etl/type_traits.h index ee01e614..f75fb0f1 100644 --- a/include/etl/type_traits.h +++ b/include/etl/type_traits.h @@ -335,7 +335,7 @@ namespace etl struct conditional_integral_constant { ETL_STATIC_ASSERT(etl::is_integral::value, "Not an integral type"); - static const T value = FALSE_VALUE; + static const T value = FALSE_VALUE; }; /// make_signed @@ -485,14 +485,14 @@ namespace etl ///\ingroup types //*************************************************************************** template struct is_one_of { - static const bool value = + static const bool value = etl::is_same::value || etl::is_same::value || etl::is_same::value || @@ -586,6 +586,21 @@ namespace etl typedef const type_t* const_pointer; typedef const type_t* const const_pointer_const; }; + + //*************************************************************************** + // size_of + //*************************************************************************** + template + struct size_of + { + static const size_t size = sizeof(T); + }; + + template <> + struct size_of + { + static const size_t size = 1; + }; } #endif From e3fa5b912c68ff5e90cbce214b8906cfcbbf77c5 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Wed, 15 May 2019 22:31:30 +0100 Subject: [PATCH 36/41] Added size_of<> --- include/etl/type_traits_generator.h | 15 +++++++++++++++ test/test_type_traits.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/include/etl/type_traits_generator.h b/include/etl/type_traits_generator.h index 83b982bd..a48a78c6 100644 --- a/include/etl/type_traits_generator.h +++ b/include/etl/type_traits_generator.h @@ -598,6 +598,21 @@ namespace etl typedef const type_t* const_pointer; typedef const type_t* const const_pointer_const; }; + + //*************************************************************************** + // size_of + //*************************************************************************** + template + struct size_of + { + static const size_t size = sizeof(T); + }; + + template <> + struct size_of + { + static const size_t size = 1; + }; } #endif diff --git a/test/test_type_traits.cpp b/test/test_type_traits.cpp index 4ef72f27..e1aba926 100644 --- a/test/test_type_traits.cpp +++ b/test/test_type_traits.cpp @@ -41,6 +41,20 @@ namespace std #include "etl/type_traits.h" #include +namespace +{ + struct TestData { }; +} + +namespace etl +{ + template <> + struct etl::size_of + { + static const size_t size = 20; + }; +} + namespace { // A class to test non-fundamental types. @@ -728,4 +742,14 @@ namespace CHECK_EQUAL(1, v1); CHECK_EQUAL(2, v2); } + + //************************************************************************* + TEST(size_of) + { + CHECK_EQUAL(1, etl::size_of::size); + CHECK_EQUAL(1, etl::size_of::size); + CHECK_EQUAL(2, etl::size_of::size); + CHECK_EQUAL(4, etl::size_of::size); + CHECK_EQUAL(20, etl::size_of::size); + } } From d2719cb63deb6aefcbe78f79f3c1e3e842d9786b Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Thu, 16 May 2019 23:39:23 +0100 Subject: [PATCH 37/41] Merge remote-tracking branch 'origin/feature/size_of' into development --- include/etl/type_traits.h | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/include/etl/type_traits.h b/include/etl/type_traits.h index f75fb0f1..e71bcdea 100644 --- a/include/etl/type_traits.h +++ b/include/etl/type_traits.h @@ -314,6 +314,13 @@ namespace etl template struct is_trivially_copy_assignable : etl::is_pod {}; #endif +#if ETL_CPP11_SUPPORTED + /// is_rvalue_reference + ///\ingroup type_traits + template struct is_rvalue_reference : etl::false_type {}; + template struct is_rvalue_reference : etl::true_type {}; +#endif + /// conditional ///\ingroup type_traits template struct conditional { typedef T type; }; @@ -485,14 +492,14 @@ namespace etl ///\ingroup types //*************************************************************************** template struct is_one_of { - static const bool value = + static const bool value = etl::is_same::value || etl::is_same::value || etl::is_same::value || From d2d3db1659e230ab16d784b4896a0d302626c2a7 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 19 May 2019 23:16:24 +0100 Subject: [PATCH 38/41] Merge remote-tracking branch 'origin/feature/delegates' into development # Conflicts: # include/etl/type_traits.h # include/etl/type_traits_generator.h # test/test_type_traits.cpp --- .../FunctionInterruptSimulation.cpp | 149 ++++++ .../etl_profile.h | 48 ++ .../v15/.gitignore | 1 + .../v15/.gitignore | 1 + .../FunctionInterruptSimulation-Delegates.sln | 31 ++ ...ctionInterruptSimulation-Delegates.vcxproj | 156 ++++++ include/etl/delegate.h | 334 ++++++++++++ include/etl/delegate_service.h | 157 ++++++ include/etl/multicast_delegate.h | 143 +++++ include/etl/type_traits.h | 20 +- include/etl/type_traits_generator.h | 10 +- test/codeblocks/ETL.cbp | 7 + test/test_callback_service.cpp | 28 +- test/test_delegate.cpp | 506 ++++++++++++++++++ test/test_delegate_service.cpp | 333 ++++++++++++ test/test_function.cpp | 193 +++---- test/test_string_u16.cpp | 14 + test/test_string_u32.cpp | 14 + test/test_string_wchar_t.cpp | 14 + test/test_type_traits.cpp | 7 +- test/vs2017/etl.vcxproj | 4 + test/vs2017/etl.vcxproj.filters | 12 + 22 files changed, 2062 insertions(+), 120 deletions(-) create mode 100644 examples/FunctionInterruptSimulation-Delegates/FunctionInterruptSimulation.cpp create mode 100644 examples/FunctionInterruptSimulation-Delegates/etl_profile.h create mode 100644 examples/FunctionInterruptSimulation-Delegates/vs2017/.vs/FunctionInterruptSimulation-Delegates/v15/.gitignore create mode 100644 examples/FunctionInterruptSimulation-Delegates/vs2017/.vs/FunctionInterruptSimulation/v15/.gitignore create mode 100644 examples/FunctionInterruptSimulation-Delegates/vs2017/FunctionInterruptSimulation-Delegates.sln create mode 100644 examples/FunctionInterruptSimulation-Delegates/vs2017/FunctionInterruptSimulation-Delegates.vcxproj create mode 100644 include/etl/delegate.h create mode 100644 include/etl/delegate_service.h create mode 100644 include/etl/multicast_delegate.h create mode 100644 test/test_delegate.cpp create mode 100644 test/test_delegate_service.cpp diff --git a/examples/FunctionInterruptSimulation-Delegates/FunctionInterruptSimulation.cpp b/examples/FunctionInterruptSimulation-Delegates/FunctionInterruptSimulation.cpp new file mode 100644 index 00000000..2fb61c92 --- /dev/null +++ b/examples/FunctionInterruptSimulation-Delegates/FunctionInterruptSimulation.cpp @@ -0,0 +1,149 @@ +#include + +#include "etl/delegate.h" +#include "etl/delegate_service.h" + +enum VectorId +{ + TIM1_CC_IRQ_HANDLER = 42, + TIM2_IRQ_HANDLER = 43, + TIM3_IRQ_HANDLER = 44, + USART1_IRQ_HANDLER = 52, + USART2_IRQ_HANDLER = 53, + VECTOR_ID_END, + VECTOR_ID_OFFSET = TIM1_CC_IRQ_HANDLER, + VECTOR_ID_RANGE = VECTOR_ID_END - VECTOR_ID_OFFSET +}; + +typedef etl::delegate_service InterruptVectors; + +// Ensure that the callback service is initialised before use. +InterruptVectors& GetInterruptVectorsInstance() +{ + static InterruptVectors interruptVectors; + + return interruptVectors; +} + +extern "C" +{ + InterruptVectors& interruptVectors = GetInterruptVectorsInstance(); + + // Function called from the timer1 interrupt vector. + void TIM1_CC_IRQHandler() + { + interruptVectors.call(); + } + + // Function called from the timer2 interrupt vector. + void TIM2_IRQHandler() + { + interruptVectors.call(); + } + + // Function called from the timer3 interrupt vector. + void TIM3_IRQHandler() + { + interruptVectors.call(); + } + + // Function called from the usart1 interrupt vector. + void USART1_IRQHandler() + { + interruptVectors.call(); + } + + // Function called from the usart2 interrupt vector. + void USART2_IRQHandler() + { + interruptVectors.call(); + } +} + +//******************************** +// Timer driver. +//******************************** +class Timer +{ +public: + + // Handler for interrupts from the timer. + void InterruptHandler(const size_t id) + { + std::cout << "Timer interrupt (member) : ID " << id << "\n"; + } +}; + +//******************************** +// Free function timer driver. +//******************************** +void FreeTimerInterruptHandler(const size_t id) +{ + std::cout << "Timer interrupt (free) : ID " << id << "\n"; +} + +//******************************** +// UART driver. +//******************************** +class Uart +{ +public: + + // Constructor. + Uart(int port_id, size_t interruptId) + : port_id(port_id), + callback(etl::delegate::create(*this)) + { + GetInterruptVectorsInstance().register_delegate(interruptId, callback); + } + + // Handler for interrupts from the UART. + void InterruptHandler(const size_t id) + { + std::cout << "UART" << port_id << " : ID " << id << "\n"; + } + + etl::delegate callback; + + int port_id; +}; + +void UnhandledInterrupt(const size_t id) +{ + std::cout << "Unhandled Interrupt : ID " << id << "\n"; +} + +// Declare the driver instances. +Timer timer; +Uart uart1(0, USART1_IRQ_HANDLER); +Uart uart2(1, USART2_IRQ_HANDLER); + +// Declare a global callback for the timer. +// Uses the most efficient callback type for a class, as everthing is known at compile time. +etl::delegate timer_member_callback = etl::delegate::create(); + +// Declare the callbacks for the free functions. +etl::delegate timer_free_callback = etl::delegate::create(); +etl::delegate unhandled_callback = etl::delegate::create(); + +//******************************** +// Test it out. +//******************************** +int main() +{ + // Setup the callbacks. + InterruptVectors& interruptVectors = GetInterruptVectorsInstance(); + + interruptVectors.register_delegate(timer_member_callback); + interruptVectors.register_delegate(timer_free_callback); + interruptVectors.register_unhandled_delegate(unhandled_callback); + + // Simulate the interrupts. + TIM1_CC_IRQHandler(); + TIM2_IRQHandler(); + USART1_IRQHandler(); + USART2_IRQHandler(); + TIM3_IRQHandler(); // Unhandled! + + return 0; +} diff --git a/examples/FunctionInterruptSimulation-Delegates/etl_profile.h b/examples/FunctionInterruptSimulation-Delegates/etl_profile.h new file mode 100644 index 00000000..a321fa85 --- /dev/null +++ b/examples/FunctionInterruptSimulation-Delegates/etl_profile.h @@ -0,0 +1,48 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2017 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. +******************************************************************************/ + +#ifndef __ETL_PROFILE_H__ +#define __ETL_PROFILE_H__ + +#define ETL_THROW_EXCEPTIONS +#define ETL_VERBOSE_ERRORS +#define ETL_CHECK_PUSH_POP +#define ETL_ISTRING_REPAIR_ENABLE +#define ETL_IVECTOR_REPAIR_ENABLE +#define ETL_IDEQUE_REPAIR_ENABLE +#define ETL_IN_UNIT_TEST + +#ifdef _MSC_VER + #include "profiles/msvc_x86.h" +#else + #include "profiles/gcc_windows_x86.h" +#endif + +#endif diff --git a/examples/FunctionInterruptSimulation-Delegates/vs2017/.vs/FunctionInterruptSimulation-Delegates/v15/.gitignore b/examples/FunctionInterruptSimulation-Delegates/vs2017/.vs/FunctionInterruptSimulation-Delegates/v15/.gitignore new file mode 100644 index 00000000..83235bb7 --- /dev/null +++ b/examples/FunctionInterruptSimulation-Delegates/vs2017/.vs/FunctionInterruptSimulation-Delegates/v15/.gitignore @@ -0,0 +1 @@ +*.VC.opendb diff --git a/examples/FunctionInterruptSimulation-Delegates/vs2017/.vs/FunctionInterruptSimulation/v15/.gitignore b/examples/FunctionInterruptSimulation-Delegates/vs2017/.vs/FunctionInterruptSimulation/v15/.gitignore new file mode 100644 index 00000000..83235bb7 --- /dev/null +++ b/examples/FunctionInterruptSimulation-Delegates/vs2017/.vs/FunctionInterruptSimulation/v15/.gitignore @@ -0,0 +1 @@ +*.VC.opendb diff --git a/examples/FunctionInterruptSimulation-Delegates/vs2017/FunctionInterruptSimulation-Delegates.sln b/examples/FunctionInterruptSimulation-Delegates/vs2017/FunctionInterruptSimulation-Delegates.sln new file mode 100644 index 00000000..26180392 --- /dev/null +++ b/examples/FunctionInterruptSimulation-Delegates/vs2017/FunctionInterruptSimulation-Delegates.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26730.16 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FunctionInterruptSimulation-Delegates", "FunctionInterruptSimulation-Delegates.vcxproj", "{5157DB15-C255-4E47-9FB1-AF388437F90F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5157DB15-C255-4E47-9FB1-AF388437F90F}.Debug|x64.ActiveCfg = Debug|x64 + {5157DB15-C255-4E47-9FB1-AF388437F90F}.Debug|x64.Build.0 = Debug|x64 + {5157DB15-C255-4E47-9FB1-AF388437F90F}.Debug|x86.ActiveCfg = Debug|Win32 + {5157DB15-C255-4E47-9FB1-AF388437F90F}.Debug|x86.Build.0 = Debug|Win32 + {5157DB15-C255-4E47-9FB1-AF388437F90F}.Release|x64.ActiveCfg = Release|x64 + {5157DB15-C255-4E47-9FB1-AF388437F90F}.Release|x64.Build.0 = Release|x64 + {5157DB15-C255-4E47-9FB1-AF388437F90F}.Release|x86.ActiveCfg = Release|Win32 + {5157DB15-C255-4E47-9FB1-AF388437F90F}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {260225EB-60CB-44CC-A60C-16A23BBC10EB} + EndGlobalSection +EndGlobal diff --git a/examples/FunctionInterruptSimulation-Delegates/vs2017/FunctionInterruptSimulation-Delegates.vcxproj b/examples/FunctionInterruptSimulation-Delegates/vs2017/FunctionInterruptSimulation-Delegates.vcxproj new file mode 100644 index 00000000..ca2c92e1 --- /dev/null +++ b/examples/FunctionInterruptSimulation-Delegates/vs2017/FunctionInterruptSimulation-Delegates.vcxproj @@ -0,0 +1,156 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + + + 15.0 + {5157DB15-C255-4E47-9FB1-AF388437F90F} + Win32Proj + FunctionInterruptSimulation + 10.0.17763.0 + + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + Use + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + ../../../src + + + Console + true + + + + + NotUsing + Level3 + Disabled + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + ../../../include;%(AdditionalIncludeDirectories) + + + Console + true + + + + + Use + Level3 + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Use + Level3 + MaxSpeed + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + \ No newline at end of file diff --git a/include/etl/delegate.h b/include/etl/delegate.h new file mode 100644 index 00000000..d3f9a5c1 --- /dev/null +++ b/include/etl/delegate.h @@ -0,0 +1,334 @@ +///\file + +/****************************************************************************** +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. +******************************************************************************/ + +/****************************************************************************** + +Copyright (C) 2017 by Sergey A Kryukov: derived work +http://www.SAKryukov.org +http://www.codeproject.com/Members/SAKryukov + +Based on original work by Sergey Ryazanov: +"The Impossibly Fast C++ Delegates", 18 Jul 2005 +https://www.codeproject.com/articles/11015/the-impossibly-fast-c-delegates + +MIT license: +http://en.wikipedia.org/wiki/MIT_License + +Original publication: https://www.codeproject.com/Articles/1170503/The-Impossibly-Fast-Cplusplus-Delegates-Fixed + +******************************************************************************/ + +#ifndef ETL_DELEGATE_INCLUDED +#define ETL_DELEGATE_INCLUDED + +#include "platform.h" + +#if ETL_CPP11_SUPPORTED == 0 +#error NOT SUPPORTED FOR C++03 OR BELOW +#endif + +namespace etl +{ + template class delegate; + + template + class delegate final + { + public: + + //************************************************************************* + /// Default constructor. + //************************************************************************* + delegate() = default; + + //************************************************************************* + // Copy constructor. + //************************************************************************* + delegate(const delegate& other) + { + other.invocation.clone(invocation); + } + + //************************************************************************* + // Constructor from lambda or functor. + //************************************************************************* + template + delegate(const TLambda& instance) + { + assign((void*)(&instance), lambda_stub); + } + + //************************************************************************* + /// Create from function (Compile time). + //************************************************************************* + template + static delegate create() + { + return delegate(nullptr, function_stub); + } + + //************************************************************************* + /// Create from Lambda or Functor. + //************************************************************************* + template + static delegate create(const TLambda& instance) + { + return delegate((void*)(&instance), lambda_stub); + } + + //************************************************************************* + /// Create from instance method (Run time). + //************************************************************************* + template + static delegate create(T& instance) + { + return delegate((void*)(&instance), method_stub); + } + + //************************************************************************* + /// Create from const instance method (Run time). + //************************************************************************* + template + static delegate create(const T& instance) + { + return delegate((void*)(&instance), const_method_stub); + } + + //************************************************************************* + /// Create from instance method (Compile time). + //************************************************************************* + template + static delegate create() + { + return delegate(method_instance_stub); + } + + //************************************************************************* + /// Create from const instance method (Compile time). + //************************************************************************* + template + static delegate create() + { + return delegate(const_method_instance_stub); + } + + //************************************************************************* + /// Execute the delegate. + //************************************************************************* + TReturn operator()(TParams... args) const + { + return (*invocation.stub)(invocation.object, args...); + } + + //************************************************************************* + /// Create from function (Compile time). + //************************************************************************* + delegate& operator =(const delegate& other) + { + other.invocation.clone(invocation); + return *this; + } + + //************************************************************************* + /// Create from Lambda or Functor. + //************************************************************************* + template + delegate& operator =(const TLambda& instance) + { + assign((void*)(&instance), lambda_stub); + return *this; + } + + //************************************************************************* + /// Checks equality. + //************************************************************************* + bool operator == (const delegate& other) const + { + return invocation == other.invocation; + } + + //************************************************************************* + /// Returns true if the delegate is valid. + //************************************************************************* + bool operator != (const delegate& other) const + { + return invocation != other.invocation; + } + + //************************************************************************* + /// Returns true if the delegate is valid. + //************************************************************************* + bool is_valid() const + { + return invocation.stub != nullptr; + } + + //************************************************************************* + /// Returns true if the delegate is valid. + //************************************************************************* + operator bool() const + { + return is_valid(); + } + + private: + + using stub_type = TReturn(*)(void* object, TParams...); + + //************************************************************************* + /// The internal invocation object. + //************************************************************************* + struct invocation_element + { + invocation_element() = default; + + //*********************************************************************** + invocation_element(void* object_, stub_type stub_) + : object(object_) + , stub(stub_) + { + } + + //*********************************************************************** + void clone(invocation_element& target) const + { + target.stub = stub; + target.object = object; + } + + //*********************************************************************** + bool operator ==(const invocation_element& another) const + { + return (another.stub == stub) && (another.object == object); + } + + //*********************************************************************** + bool operator !=(const invocation_element& another) const + { + return (another.stub != stub) || (another.object != object); + } + + //*********************************************************************** + void* object = nullptr; + stub_type stub = nullptr; + }; + + //************************************************************************* + /// Constructs a delegate from an object and stub. + //************************************************************************* + delegate(void* object, stub_type stub) + { + invocation.object = object; + invocation.stub = stub; + } + + //************************************************************************* + /// Constructs a delegate from a stub. + //************************************************************************* + delegate(stub_type stub) + { + invocation.object = nullptr; + invocation.stub = stub; + } + + //************************************************************************* + /// Assign from an object and stub. + //************************************************************************* + void assign(void* object, stub_type stub) + { + invocation.object = object; + invocation.stub = stub; + } + + //************************************************************************* + /// Stub call for a member function. Run time instance. + //************************************************************************* + template + static TReturn method_stub(void* object, TParams... params) + { + T* p = static_cast(object); + return (p->*Method)(params...); + } + + //************************************************************************* + /// Stub call for a const member function. Run time instance. + //************************************************************************* + template + static TReturn const_method_stub(void* object, TParams... params) + { + T* const p = static_cast(object); + return (p->*Method)(params...); + } + + //************************************************************************* + /// Stub call for a member function. Compile time instance. + //************************************************************************* + template + static TReturn method_instance_stub(void*, TParams... params) + { + return (Instance.*Method)(params...); + } + + //************************************************************************* + /// Stub call for a const member function. Compile time instance. + //************************************************************************* + template + static TReturn const_method_instance_stub(void*, TParams... params) + { + return (Instance.*Method)(params...); + } + + //************************************************************************* + /// Stub call for a free function. + //************************************************************************* + template + static TReturn function_stub(void*, TParams... params) + { + return (Method)(params...); + } + + //************************************************************************* + /// Stub call for a lambda or functor function. + //************************************************************************* + template + static TReturn lambda_stub(void* object, TParams... arg) + { + TLambda* p = static_cast(object); + return (p->operator())(arg...); + } + + //************************************************************************* + /// The invocation object. + //************************************************************************* + invocation_element invocation; + }; +} + +#endif diff --git a/include/etl/delegate_service.h b/include/etl/delegate_service.h new file mode 100644 index 00000000..fa65f2c2 --- /dev/null +++ b/include/etl/delegate_service.h @@ -0,0 +1,157 @@ +///\file + +/****************************************************************************** +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. +******************************************************************************/ + +#ifndef ETL_DELEGATE_SERVICE_INCLUDED +#define ETL_DELEGATE_SERVICE_INCLUDED + +#include "platform.h" +#include "nullptr.h" +#include "static_assert.h" +#include "delegate.h" +#include "array.h" + +namespace etl +{ + //*************************************************************************** + /// An indexed delegate service. + /// \tparam RANGE The number of delegates to handle. + /// \tparam OFFSET The lowest delegate id value. + /// The delegate ids must range between OFFSET and OFFSET + RANGE - 1. + //*************************************************************************** + template + class delegate_service + { + public: + + //************************************************************************* + /// Reset the delegate service. + /// Sets all delegates to the internal default. + //************************************************************************* + delegate_service() + { + etl::delegate default_delegate = etl::delegate::create, &delegate_service::unhandled>(*this); + + lookup.fill(default_delegate); + } + + //************************************************************************* + /// Registers a delegate for the specified id. + /// Compile time assert if the id is out of range. + /// \tparam ID The id of the delegate. + /// \param delegate Reference to the delegate. + //************************************************************************* + template + void register_delegate(etl::delegate callback) + { + ETL_STATIC_ASSERT(ID < (OFFSET + RANGE), "Callback Id out of range"); + ETL_STATIC_ASSERT(ID >= OFFSET, "Callback Id out of range"); + + lookup[ID - OFFSET] = callback; + } + + //************************************************************************* + /// Registers a delegate for the specified id. + /// No action if the id is out of range. + /// \param id Id of the delegate. + /// \param delegate Reference to the delegate. + //************************************************************************* + void register_delegate(const size_t id, etl::delegate callback) + { + if ((id >= OFFSET) && (id < (OFFSET + RANGE))) + { + lookup[id - OFFSET] = callback; + } + } + + //************************************************************************* + /// Registers an alternative delegate for unhandled ids. + /// \param delegate A reference to the user supplied 'unhandled' delegate. + //************************************************************************* + void register_unhandled_delegate(etl::delegate callback) + { + unhandled_delegate = callback; + } + + //************************************************************************* + /// Executes the delegate function for the index. + /// Compile time assert if the id is out of range. + /// \tparam ID The id of the delegate. + //************************************************************************* + template + void call() + { + ETL_STATIC_ASSERT(ID < (OFFSET + RANGE), "Callback Id out of range"); + ETL_STATIC_ASSERT(ID >= OFFSET, "Callback Id out of range"); + + lookup[ID - OFFSET](ID); + } + + //************************************************************************* + /// Executes the delegate function for the index. + /// \param id Id of the delegate. + //************************************************************************* + void call(const size_t id) + { + if ((id >= OFFSET) && (id < (OFFSET + RANGE))) + { + lookup[id - OFFSET](id); + } + else + { + if (unhandled_delegate.is_valid()) + { + unhandled_delegate(id); + } + } + } + + private: + + //************************************************************************* + /// The default callback function. + /// Calls the user defined 'unhandled' callback if it exists. + //************************************************************************* + void unhandled(size_t id) + { + if (unhandled_delegate.is_valid()) + { + unhandled_delegate(id); + } + } + + /// The default delegate for unhandled ids. + etl::delegate unhandled_delegate; + + /// Lookup table of delegates. + etl::array, RANGE> lookup; + }; +} + +#endif diff --git a/include/etl/multicast_delegate.h b/include/etl/multicast_delegate.h new file mode 100644 index 00000000..9fc3f31a --- /dev/null +++ b/include/etl/multicast_delegate.h @@ -0,0 +1,143 @@ +///\file + +/****************************************************************************** +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. +******************************************************************************/ + +/****************************************************************************** + + Copyright (C) 2017 by Sergey A Kryukov: derived work + http://www.SAKryukov.org + http://www.codeproject.com/Members/SAKryukov + + Based on original work by Sergey Ryazanov: + "The Impossibly Fast C++ Delegates", 18 Jul 2005 + https://www.codeproject.com/articles/11015/the-impossibly-fast-c-delegates + + MIT license: + http://en.wikipedia.org/wiki/MIT_License + + Original publication: https://www.codeproject.com/Articles/1170503/The-Impossibly-Fast-Cplusplus-Delegates-Fixed + +******************************************************************************/ + +//#pragma once +//#include "delegate.h" +//#include +//#include +// +//namespace SA { +// +// template +// class multicast_delegate final : private delegate_base { +// public: +// +// multicast_delegate() = default; +// ~multicast_delegate() { +// for (auto& element : invocationList) delete element; +// invocationList.clear(); +// } //~multicast_delegate +// +// bool isNull() const { return invocationList.size() < 1; } +// bool operator ==(void* ptr) const { +// return (ptr == nullptr) && this->isNull(); +// } //operator == +// bool operator !=(void* ptr) const { +// return (ptr != nullptr) || (!this->isNull()); +// } //operator != +// +// size_t size() const { return invocationList.size(); } +// +// multicast_delegate& operator =(const multicast_delegate&) = delete; +// multicast_delegate(const multicast_delegate&) = delete; +// +// bool operator ==(const multicast_delegate& another) const { +// if (invocationList.size() != another.invocationList.size()) return false; +// auto anotherIt = another.invocationList.begin(); +// for (auto it = invocationList.begin(); it != invocationList.end(); ++it) +// if (**it != **anotherIt) return false; +// return true; +// } //== +// bool operator !=(const multicast_delegate& another) const { return !(*this == another); } +// +// bool operator ==(const delegate& another) const { +// if (isNull() && another.isNull()) return true; +// if (another.isNull() || (size() != 1)) return false; +// return (another.invocation == **invocationList.begin()); +// } //== +// bool operator !=(const delegate& another) const { return !(*this == another); } +// +// multicast_delegate& operator +=(const multicast_delegate& another) { +// for (auto& item : another.invocationList) // clone, not copy; flattens hierarchy: +// this->invocationList.push_back(new typename delegate_base::InvocationElement(item->object, item->stub)); +// return *this; +// } //operator += +// +// template // template instantiation is not neededm, will be deduced/inferred: +// multicast_delegate& operator +=(const LAMBDA & lambda) { +// delegate d = delegate::template create(lambda); +// return *this += d; +// } //operator += +// +// multicast_delegate& operator +=(const delegate& another) { +// if (another.isNull()) return *this; +// this->invocationList.push_back(new typename delegate_base::InvocationElement(another.invocation.object, another.invocation.stub)); +// return *this; +// } //operator += +// +// // will work even if RET is void, return values are ignored: +// // (for handling return values, see operator(..., handler)) +// void operator()(PARAMS... arg) const { +// for (auto& item : invocationList) +// (*(item->stub))(item->object, arg...); +// } //operator() +// +// template +// void operator()(PARAMS... arg, HANDLER handler) const { +// size_t index = 0; +// for (auto& item : invocationList) { +// RET value = (*(item->stub))(item->object, arg...); +// handler(index, &value); +// ++index; +// } //loop +// } //operator() +// +// void operator()(PARAMS... arg, delegate handler) const { +// operator()(arg..., handler); +// } //operator() +// void operator()(PARAMS... arg, std::function handler) const { +// operator()(arg..., handler); +// } //operator() +// +// private: +// +// std::list::InvocationElement *> invocationList; +// +// }; //class multicast_delegate +// +//} /* namespace SA */ +// diff --git a/include/etl/type_traits.h b/include/etl/type_traits.h index e71bcdea..359ed911 100644 --- a/include/etl/type_traits.h +++ b/include/etl/type_traits.h @@ -492,14 +492,14 @@ namespace etl ///\ingroup types //*************************************************************************** template struct is_one_of { - static const bool value = + static const bool value = etl::is_same::value || etl::is_same::value || etl::is_same::value || @@ -600,13 +600,19 @@ namespace etl template struct size_of { - static const size_t size = sizeof(T); + enum + { + size = sizeof(T) + }; }; template <> struct size_of { - static const size_t size = 1; + enum + { + size = 1 + }; }; } diff --git a/include/etl/type_traits_generator.h b/include/etl/type_traits_generator.h index a48a78c6..c45d1ea3 100644 --- a/include/etl/type_traits_generator.h +++ b/include/etl/type_traits_generator.h @@ -605,13 +605,19 @@ namespace etl template struct size_of { - static const size_t size = sizeof(T); + enum + { + size = sizeof(T) + }; }; template <> struct size_of { - static const size_t size = 1; + enum + { + size = 1 + }; }; } diff --git a/test/codeblocks/ETL.cbp b/test/codeblocks/ETL.cbp index a4bfe86e..beb8a603 100644 --- a/test/codeblocks/ETL.cbp +++ b/test/codeblocks/ETL.cbp @@ -181,6 +181,8 @@ + + @@ -248,6 +250,9 @@ + + + @@ -362,6 +367,8 @@ + + diff --git a/test/test_callback_service.cpp b/test/test_callback_service.cpp index 67de9b16..1173c3f5 100644 --- a/test/test_callback_service.cpp +++ b/test/test_callback_service.cpp @@ -101,22 +101,22 @@ namespace // Callback for 'unhandled'. etl::function_fp unhandled_callback; -} -//***************************************************************************** -// Initialises the test results. -//***************************************************************************** -struct SetupFixture -{ - SetupFixture() + //***************************************************************************** + // Initialises the test results. + //***************************************************************************** + struct SetupFixture { - called_id = UINT_MAX; - global_called = false; - member1_called = false; - member2_called = false; - unhandled_called = false; - } -}; + SetupFixture() + { + called_id = UINT_MAX; + global_called = false; + member1_called = false; + member2_called = false; + unhandled_called = false; + } + }; +} namespace { diff --git a/test/test_delegate.cpp b/test/test_delegate.cpp new file mode 100644 index 00000000..e2a76bd7 --- /dev/null +++ b/test/test_delegate.cpp @@ -0,0 +1,506 @@ +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +http://www.etlcpp.com + +Copyright(c) 2014 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 "etl/delegate.h" + +namespace +{ + //***************************************************************************** + const int VALUE1 = 1; + const int VALUE2 = 2; + bool function_called = false; + bool parameter_correct = false; + + //***************************************************************************** + // Test data structure. + //***************************************************************************** + struct Data + { + int d; + }; + + Data data; + + //***************************************************************************** + // The free function taking no parameters. + //***************************************************************************** + void free_void() + { + function_called = true; + } + + //***************************************************************************** + // The free function taking an int parameter. + //***************************************************************************** + void free_int(int i, int j) + { + function_called = true; + parameter_correct = (i == VALUE1) && (j == VALUE2); + } + + //***************************************************************************** + // The free function taking a Data reference parameter. + //***************************************************************************** + void free_reference(const Data& data, int j) + { + function_called = true; + parameter_correct = (data.d == VALUE1) && (j = VALUE2); + } + + //***************************************************************************** + // The test class with member functions. + //***************************************************************************** + class Test + { + public: + + //******************************************* + // void + void member_void() + { + function_called = true; + } + + void member_void_const() const + { + function_called = true; + } + + //******************************************* + // int + void member_int(int i, int j) + { + function_called = true; + parameter_correct = (i == VALUE1) && (j == VALUE2); + } + + void member_int_const(int i, int j) const + { + function_called = true; + parameter_correct = (i == VALUE1) && (j == VALUE2); + } + + //******************************************* + // reference + void member_reference(const Data& data, int j) + { + function_called = true; + parameter_correct = (data.d == VALUE1) && (j = VALUE2); + } + + void member_reference_const(const Data& data, int j) const + { + function_called = true; + parameter_correct = (data.d == VALUE1) && (j = VALUE2); + } + + //******************************************* + // static + static void member_static(const Data& data, int j) + { + function_called = true; + parameter_correct = (data.d == VALUE1) && (j = VALUE2); + } + + //******************************************* + // operator() + void operator()() + { + function_called = true; + } + + void operator()() const + { + function_called = true; + } + }; + + Test test_static; + const Test const_test_static; +} + +//***************************************************************************** +// Initialises the test results. +//***************************************************************************** +struct SetupFixture +{ + SetupFixture() + { + function_called = false; + parameter_correct = false; + } +}; + +namespace +{ + SUITE(test_delegate) + { + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_is_valid_false) + { + etl::delegate d; + + CHECK(!d.is_valid()); + CHECK(!d); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_is_valid_true) + { + etl::delegate d([] {}); + + CHECK(d.is_valid()); + CHECK(d); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_free_void) + { + etl::delegate d = etl::delegate::create(); + + d(); + + CHECK(function_called); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_free_int) + { + etl::delegate d = etl::delegate::create(); + + d(VALUE1, VALUE2); + + CHECK(function_called); + CHECK(parameter_correct); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_free_reference) + { + etl::delegate d = etl::delegate::create(); + + Data data; + data.d = VALUE1; + + d(data, VALUE2); + + CHECK(function_called); + CHECK(parameter_correct); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_lambda_int) + { + etl::delegate d([](int i, int j) { function_called = true; parameter_correct = (i == VALUE1) && (j == VALUE2); }); + + d(VALUE1, VALUE2); + + CHECK(function_called); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_lambda_int_create) + { + auto lambda = [](int i, int j) { function_called = true; parameter_correct = (i == VALUE1) && (j == VALUE2); }; + + etl::delegate d(lambda); + + d(VALUE1, VALUE2); + + CHECK(function_called); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_operator_void) + { + Test test; + + etl::delegate d(test); + + d(); + + CHECK(function_called); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_operator_void_create) + { + Test test; + + etl::delegate d = etl::delegate::create(test); + + d(); + + CHECK(function_called); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_operator_void_const) + { + const Test test; + + etl::delegate d(test); + + d(); + + CHECK(function_called); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment_member_operator_void) + { + Test test; + + etl::delegate d; + + d = test; + + d(); + + CHECK(function_called); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_void) + { + Test test; + + etl::delegate d = etl::delegate::create(test); + + d(); + + CHECK(function_called); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_void_const) + { + const Test test; + + etl::delegate d = etl::delegate::create(test); + + d(); + + CHECK(function_called); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_int) + { + Test test; + + etl::delegate d = etl::delegate::create(test); + + d(VALUE1, VALUE2); + + CHECK(function_called); + CHECK(parameter_correct); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_int_const) + { + const Test test; + + etl::delegate d = etl::delegate::create(test); + + d(VALUE1, VALUE2); + + CHECK(function_called); + CHECK(parameter_correct); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_reference) + { + Test test; + etl::delegate d = etl::delegate::create(test); + + Data data; + data.d = VALUE1; + + d(data, VALUE2); + + CHECK(function_called); + CHECK(parameter_correct); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_reference_const) + { + const Test test; + etl::delegate d = etl::delegate::create(test); + + Data data; + data.d = VALUE1; + + d(data, VALUE2); + + CHECK(function_called); + CHECK(parameter_correct); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_static) + { + etl::delegate d = etl::delegate::create(); + + Data data; + data.d = VALUE1; + + d(data, VALUE2); + + CHECK(function_called); + CHECK(parameter_correct); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_void_compile_time) + { + etl::delegate d = etl::delegate::create(); + + d(); + + CHECK(function_called); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_void_const_compile_time) + { + etl::delegate d = etl::delegate::create(); + + d(); + + CHECK(function_called); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_int_compile_time) + { + etl::delegate d = etl::delegate::create(); + + d(VALUE1, VALUE2); + + CHECK(function_called); + CHECK(parameter_correct); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_int_const_compile_time) + { + etl::delegate d = etl::delegate::create(); + + d(VALUE1, VALUE2); + + CHECK(function_called); + CHECK(parameter_correct); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_reference_compile_time) + { + etl::delegate d = etl::delegate::create(); + + Data data; + data.d = VALUE1; + + d(data, VALUE2); + + CHECK(function_called); + CHECK(parameter_correct); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_reference_const_compile_time) + { + etl::delegate d = etl::delegate::create(); + + Data data; + data.d = VALUE1; + + d(data, VALUE2); + + CHECK(function_called); + CHECK(parameter_correct); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_copy_construct) + { + Test test; + + etl::delegate d1 = etl::delegate::create(test); + etl::delegate d2(d1); + + d2(VALUE1, VALUE2); + + CHECK(function_called); + CHECK(parameter_correct); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_assignment) + { + Test test; + + etl::delegate d1 = etl::delegate::create(test); + etl::delegate d2; + + d2 = d1; + + d2(VALUE1, VALUE2); + + CHECK(function_called); + CHECK(parameter_correct); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_delegate_equal) + { + Test test; + + etl::delegate d1 = etl::delegate::create(test); + etl::delegate d2 = d1; + + CHECK(d1 == d2); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_delegate_not_equal) + { + Test test; + + etl::delegate d1 = etl::delegate::create(test); + etl::delegate d2 = etl::delegate::create(test);; + + CHECK(d1 != d2); + } + }; +} diff --git a/test/test_delegate_service.cpp b/test/test_delegate_service.cpp new file mode 100644 index 00000000..96f8a4f2 --- /dev/null +++ b/test/test_delegate_service.cpp @@ -0,0 +1,333 @@ +/****************************************************************************** +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 "etl/delegate.h" +#include "etl/delegate_service.h" + +namespace +{ + const size_t SIZE = 3U; + const size_t OFFSET = 5U; + + using Service = etl::delegate_service; + + //***************************************************************************** + bool global_called = false; + bool member1_called = false; + bool member2_called = false; + bool unhandled_called = false; + size_t called_id = UINT_MAX; + + //***************************************************************************** + // The global function taking no parameters. + //***************************************************************************** + void global(size_t id) + { + global_called = true; + called_id = id; + } + + //***************************************************************************** + // The external unhandled callback. + //***************************************************************************** + void unhandled(size_t id) + { + unhandled_called = true; + called_id = id; + } + + //***************************************************************************** + // The test class with member functions. + //***************************************************************************** + class Test + { + public: + + Test() + : callback(etl::delegate::create(*this)) + { + } + + void member1(size_t id) + { + member1_called = true; + called_id = id; + } + + void member2(size_t id) + { + member2_called = true; + called_id = id; + } + + // Callback for 'member1'. + etl::delegate callback; + }; + + Test test; + + // Callback for 'member2'. + etl::delegate member_callback = etl::delegate::create(); + + // Callback for 'global'. + etl::delegate global_callback = etl::delegate::create(); + + // Callback for 'unhandled'. + etl::delegate unhandled_callback = etl::delegate::create(); + + //***************************************************************************** + // Initialises the test results. + //***************************************************************************** + struct SetupFixture + { + SetupFixture() + { + called_id = UINT_MAX; + global_called = false; + member1_called = false; + member2_called = false; + unhandled_called = false; + } + }; +} + +namespace +{ + enum + { + GLOBAL = OFFSET, + MEMBER1, + MEMBER2, + OUT_OF_RANGE + }; + + SUITE(test_delegate_service) + { + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_delegate_global_compile_time) + { + Service service; + + service.register_delegate(global_callback); + service.register_delegate(test.callback); + service.register_delegate(member_callback); + + service.call(); + + CHECK_EQUAL(GLOBAL, called_id); + CHECK(global_called); + CHECK(!member1_called); + CHECK(!member2_called); + CHECK(!unhandled_called); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_delegate_global_run_time) + { + Service service; + + service.register_delegate(GLOBAL, global_callback); + service.register_delegate(MEMBER1, test.callback); + service.register_delegate(MEMBER2, member_callback); + + service.call(GLOBAL); + + CHECK_EQUAL(GLOBAL, called_id); + CHECK(global_called); + CHECK(!member1_called); + CHECK(!member2_called); + CHECK(!unhandled_called); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_delegate_member1_compile_time) + { + Service service; + + service.register_delegate(global_callback); + service.register_delegate(test.callback); + service.register_delegate(member_callback); + + service.call(); + + CHECK_EQUAL(MEMBER1, called_id); + CHECK(!global_called); + CHECK(member1_called); + CHECK(!member2_called); + CHECK(!unhandled_called); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_delegate_member1_run_time) + { + Service service; + + service.register_delegate(GLOBAL, global_callback); + service.register_delegate(MEMBER1, test.callback); + service.register_delegate(MEMBER2, member_callback); + + service.call(MEMBER1); + + CHECK_EQUAL(MEMBER1, called_id); + CHECK(!global_called); + CHECK(member1_called); + CHECK(!member2_called); + CHECK(!unhandled_called); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_delegate_member2_compile_time) + { + Service service; + + service.register_delegate(global_callback); + service.register_delegate(test.callback); + service.register_delegate(member_callback); + + service.call(); + + CHECK_EQUAL(MEMBER2, called_id); + CHECK(!global_called); + CHECK(!member1_called); + CHECK(member2_called); + CHECK(!unhandled_called); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_delegate_unhandled_out_of_range_run_time_default) + { + Service service; + + service.register_delegate(global_callback); + service.register_delegate(test.callback); + service.register_delegate(member_callback); + + service.call(OUT_OF_RANGE); + + CHECK_EQUAL(UINT_MAX, called_id); + CHECK(!global_called); + CHECK(!member1_called); + CHECK(!member2_called); + CHECK(!unhandled_called); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_delegate_unhandled_out_of_range_run_time_user_supplied) + { + Service service; + + service.register_delegate(global_callback); + service.register_delegate(test.callback); + service.register_delegate(member_callback); + + service.register_unhandled_delegate(unhandled_callback); + + service.call(OUT_OF_RANGE); + + CHECK_EQUAL(OUT_OF_RANGE, called_id); + CHECK(!global_called); + CHECK(!member1_called); + CHECK(!member2_called); + CHECK(unhandled_called); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_delegate_unhandled_not_registered_compile_time_default) + { + Service service; + + service.register_delegate(global_callback); + service.register_delegate(member_callback); + + service.call(); + + CHECK_EQUAL(UINT_MAX, called_id); + CHECK(!global_called); + CHECK(!member1_called); + CHECK(!member2_called); + CHECK(!unhandled_called); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_delegate_unhandled_not_registered_run_time_default) + { + Service service; + + service.register_delegate(GLOBAL, global_callback); + service.register_delegate(MEMBER2, member_callback); + + service.call(MEMBER1); + + CHECK_EQUAL(UINT_MAX, called_id); + CHECK(!global_called); + CHECK(!member1_called); + CHECK(!member2_called); + CHECK(!unhandled_called); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_delegate_not_registered_compile_time_user_supplied) + { + Service service; + + service.register_delegate(global_callback); + service.register_delegate(member_callback); + + service.register_unhandled_delegate(unhandled_callback); + + service.call(); + + CHECK_EQUAL(MEMBER1, called_id); + CHECK(!global_called); + CHECK(!member1_called); + CHECK(!member2_called); + CHECK(unhandled_called); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_delegate_unhandled_run_time_user_supplied) + { + Service service; + + service.register_delegate(GLOBAL, global_callback); + service.register_delegate(MEMBER2, member_callback); + + service.register_unhandled_delegate(unhandled_callback); + + service.call(MEMBER1); + + CHECK_EQUAL(MEMBER1, called_id); + CHECK(!global_called); + CHECK(!member1_called); + CHECK(!member2_called); + CHECK(unhandled_called); + } + }; +} diff --git a/test/test_function.cpp b/test/test_function.cpp index 35b7bf21..3f34e51b 100644 --- a/test/test_function.cpp +++ b/test/test_function.cpp @@ -30,123 +30,126 @@ SOFTWARE. #include "etl/function.h" -//***************************************************************************** -const int VALUE = 1; -bool function_called = false; -bool parameter_correct = false; - -//***************************************************************************** -// Test data structure. -//***************************************************************************** -struct Data +namespace { - int d; -}; + //***************************************************************************** + const int VALUE = 1; + bool function_called = false; + bool parameter_correct = false; -//***************************************************************************** -// Call the function taking no parameters. -//***************************************************************************** -void call(etl::ifunction& function) -{ - function(); -} + //***************************************************************************** + // Test data structure. + //***************************************************************************** + struct Data + { + int d; + }; -//***************************************************************************** -// Call the const function taking no parameters. -//***************************************************************************** -void call(const etl::ifunction& function) -{ - function(); -} + //***************************************************************************** + // Call the function taking no parameters. + //***************************************************************************** + void call(etl::ifunction& function) + { + function(); + } -//***************************************************************************** -// Call the function taking an int parameter. -//***************************************************************************** -void call(etl::ifunction& function) -{ - function(VALUE); -} + //***************************************************************************** + // Call the const function taking no parameters. + //***************************************************************************** + void call(const etl::ifunction& function) + { + function(); + } -//***************************************************************************** -// Call the const function taking an int parameter. -//***************************************************************************** -void call(const etl::ifunction& function) -{ - function(VALUE); -} + //***************************************************************************** + // Call the function taking an int parameter. + //***************************************************************************** + void call(etl::ifunction& function) + { + function(VALUE); + } -//***************************************************************************** -// Call the function taking a Data parameter. -//***************************************************************************** -void call(etl::ifunction& function) -{ - Data data; - data.d = VALUE; - function(data); -} + //***************************************************************************** + // Call the const function taking an int parameter. + //***************************************************************************** + void call(const etl::ifunction& function) + { + function(VALUE); + } -//***************************************************************************** -// Call the const function taking a Data parameter. -//***************************************************************************** -void call(const etl::ifunction& function) -{ - Data data; - data.d = VALUE; - function(data); -} + //***************************************************************************** + // Call the function taking a Data parameter. + //***************************************************************************** + void call(etl::ifunction& function) + { + Data data; + data.d = VALUE; + function(data); + } -//***************************************************************************** -// The free function taking no parameters. -//***************************************************************************** -void free_void() -{ - function_called = true; -} + //***************************************************************************** + // Call the const function taking a Data parameter. + //***************************************************************************** + void call(const etl::ifunction& function) + { + Data data; + data.d = VALUE; + function(data); + } -//***************************************************************************** -// The free function taking an int parameter. -//***************************************************************************** -void free_int(int i) -{ - function_called = true; - parameter_correct = (i == VALUE); -} - -//***************************************************************************** -// The free function taking a Data reference parameter. -//***************************************************************************** -void free_reference(const Data& data) -{ - function_called = true; - parameter_correct = (data.d == VALUE); -} - -//***************************************************************************** -// The test class with member functions. -//***************************************************************************** -class Test -{ -public: - - void member_void() + //***************************************************************************** + // The free function taking no parameters. + //***************************************************************************** + void free_void() { function_called = true; } - void member_int(int i) + //***************************************************************************** + // The free function taking an int parameter. + //***************************************************************************** + void free_int(int i) { function_called = true; parameter_correct = (i == VALUE); } - void member_reference(const Data& data) + //***************************************************************************** + // The free function taking a Data reference parameter. + //***************************************************************************** + void free_reference(const Data& data) { function_called = true; parameter_correct = (data.d == VALUE); } -}; -Test test_static; + //***************************************************************************** + // The test class with member functions. + //***************************************************************************** + class Test + { + public: + + void member_void() + { + function_called = true; + } + + void member_int(int i) + { + function_called = true; + parameter_correct = (i == VALUE); + } + + void member_reference(const Data& data) + { + function_called = true; + parameter_correct = (data.d == VALUE); + } + }; + + Test test_static; +} //***************************************************************************** // Initialises the test results. @@ -504,4 +507,4 @@ namespace CHECK(function_called); } }; -} \ No newline at end of file +} diff --git a/test/test_string_u16.cpp b/test/test_string_u16.cpp index 08382b8c..13882be2 100644 --- a/test/test_string_u16.cpp +++ b/test/test_string_u16.cpp @@ -3688,6 +3688,20 @@ namespace CHECK(std::find_if(text.end(), pe, [](Text::value_type x) { return x != 0; }) == pe); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_resize_down) + { + Text text; + text.set_secure(); + text.assign(STR("ABCDEF")); + + Text::pointer pe = text.end(); + + text.resize(text.size() - 3U); + + CHECK(std::find_if(text.end(), pe, [](Text::value_type x) { return x != 0; }) == pe); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_secure_after_erase) { diff --git a/test/test_string_u32.cpp b/test/test_string_u32.cpp index 55e9400a..451530f3 100644 --- a/test/test_string_u32.cpp +++ b/test/test_string_u32.cpp @@ -3688,6 +3688,20 @@ namespace CHECK(std::find_if(text.end(), pe, [](Text::value_type x) { return x != 0; }) == pe); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_resize_down) + { + Text text; + text.set_secure(); + text.assign(STR("ABCDEF")); + + Text::pointer pe = text.end(); + + text.resize(text.size() - 3U); + + CHECK(std::find_if(text.end(), pe, [](Text::value_type x) { return x != 0; }) == pe); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_secure_after_erase) { diff --git a/test/test_string_wchar_t.cpp b/test/test_string_wchar_t.cpp index a00d8e02..5f9af498 100644 --- a/test/test_string_wchar_t.cpp +++ b/test/test_string_wchar_t.cpp @@ -3688,6 +3688,20 @@ namespace CHECK(std::find_if(text.end(), pe, [](Text::value_type x) { return x != 0; }) == pe); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_secure_after_resize_down) + { + Text text; + text.set_secure(); + text.assign(STR("ABCDEF")); + + Text::pointer pe = text.end(); + + text.resize(text.size() - 3U); + + CHECK(std::find_if(text.end(), pe, [](Text::value_type x) { return x != 0; }) == pe); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_secure_after_erase) { diff --git a/test/test_type_traits.cpp b/test/test_type_traits.cpp index e1aba926..630dd230 100644 --- a/test/test_type_traits.cpp +++ b/test/test_type_traits.cpp @@ -49,9 +49,12 @@ namespace namespace etl { template <> - struct etl::size_of + struct size_of { - static const size_t size = 20; + enum + { + size = 20 + }; }; } diff --git a/test/vs2017/etl.vcxproj b/test/vs2017/etl.vcxproj index 09c910aa..b6bad029 100644 --- a/test/vs2017/etl.vcxproj +++ b/test/vs2017/etl.vcxproj @@ -372,6 +372,8 @@ + + @@ -582,6 +584,8 @@ + + diff --git a/test/vs2017/etl.vcxproj.filters b/test/vs2017/etl.vcxproj.filters index 9aa640d9..856baf1d 100644 --- a/test/vs2017/etl.vcxproj.filters +++ b/test/vs2017/etl.vcxproj.filters @@ -768,6 +768,12 @@ ETL\Containers + + ETL\Utilities + + + ETL\Frameworks + @@ -1220,6 +1226,12 @@ Source Files + + Source Files + + + Source Files + From 91f8d463bae5296c14b26f8835d72e699eb62c9b Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 19 May 2019 23:25:14 +0100 Subject: [PATCH 39/41] Merge branch 'master' into development # Conflicts: # include/etl/version.h # support/Release notes.txt --- include/etl/version.h | 4 ++-- support/Release notes.txt | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/etl/version.h b/include/etl/version.h index f338b0c8..58d3e5ab 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -38,8 +38,8 @@ SOFTWARE. ///\ingroup utilities #define ETL_VERSION_MAJOR 14 -#define ETL_VERSION_MINOR 23 -#define ETL_VERSION_PATCH 1 +#define ETL_VERSION_MINOR 24 +#define ETL_VERSION_PATCH 0 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) ETL_STRINGIFY(ETL_VERSION_MINOR) ETL_STRINGIFY(ETL_VERSION_PATCH) #define ETL_VERSION_W ETL_WIDE_STRING(ETL_CONCAT(ETL_CONCAT(ETL_VERSION_MAJOR, ETL_VERSION_MINOR), ETL_VERSION_PATCH)) diff --git a/support/Release notes.txt b/support/Release notes.txt index b01728aa..70a5a029 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,7 @@ +=============================================================================== +14.24.0 +Added 'delegate' and 'delegate_service'. + =============================================================================== 14.23.1 Added missing 'cleanup' to string's resize() method. From d9f79715e4576ac19bc57ba45f0d0b733bd1a332 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Wed, 22 May 2019 20:18:11 +0100 Subject: [PATCH 40/41] Merge remote-tracking branch 'origin/feature/delegates' into development # Conflicts: # include/etl/delegate.h # include/etl/version.h # support/Release notes.txt # test/test_delegate.cpp --- include/etl/delegate.h | 22 +++++++ include/etl/version.h | 2 +- support/Release notes.txt | 4 ++ test/test_delegate.cpp | 24 +++++++- test/vs2017/Debug LLVM/.gitignore | 7 +++ test/vs2017/etl.sln | 6 ++ test/vs2017/etl.vcxproj | 98 +++++++++++++++++++++++++++++++ 7 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 test/vs2017/Debug LLVM/.gitignore diff --git a/include/etl/delegate.h b/include/etl/delegate.h index d3f9a5c1..3d264bcd 100644 --- a/include/etl/delegate.h +++ b/include/etl/delegate.h @@ -139,6 +139,17 @@ namespace etl return delegate(const_method_instance_stub); } +#if !defined(ETL_COMPILER_GCC) + //************************************************************************* + /// Create from instance function operator (Compile time). + //************************************************************************* + template + static delegate create() + { + return delegate(operator_instance_stub); + } +#endif + //************************************************************************* /// Execute the delegate. //************************************************************************* @@ -305,6 +316,17 @@ namespace etl return (Instance.*Method)(params...); } +#if !defined(ETL_COMPILER_GCC) + //************************************************************************* + /// Stub call for a function operator. Compile time instance. + //************************************************************************* + template + static TReturn operator_instance_stub(void*, TParams... params) + { + return Instance.operator()(params...); + } +#endif + //************************************************************************* /// Stub call for a free function. //************************************************************************* diff --git a/include/etl/version.h b/include/etl/version.h index 58d3e5ab..7ef7d2b3 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -39,7 +39,7 @@ SOFTWARE. #define ETL_VERSION_MAJOR 14 #define ETL_VERSION_MINOR 24 -#define ETL_VERSION_PATCH 0 +#define ETL_VERSION_PATCH 1 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) ETL_STRINGIFY(ETL_VERSION_MINOR) ETL_STRINGIFY(ETL_VERSION_PATCH) #define ETL_VERSION_W ETL_WIDE_STRING(ETL_CONCAT(ETL_CONCAT(ETL_VERSION_MAJOR, ETL_VERSION_MINOR), ETL_VERSION_PATCH)) diff --git a/support/Release notes.txt b/support/Release notes.txt index 70a5a029..5b4e626b 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,7 @@ +=============================================================================== +14.24.1 +Added compile time functor version of 'create()'. + =============================================================================== 14.24.0 Added 'delegate' and 'delegate_service'. diff --git a/test/test_delegate.cpp b/test/test_delegate.cpp index e2a76bd7..55c6c35b 100644 --- a/test/test_delegate.cpp +++ b/test/test_delegate.cpp @@ -46,8 +46,6 @@ namespace int d; }; - Data data; - //***************************************************************************** // The free function taking no parameters. //***************************************************************************** @@ -273,6 +271,28 @@ namespace CHECK(function_called); } +#if !defined(ETL_COMPILER_GCC) + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_operator_void_compile_time) + { + etl::delegate d = etl::delegate::create(); + + d(); + + CHECK(function_called); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_operator_void_compile_time_const) + { + etl::delegate d = etl::delegate::create(); + + d(); + + CHECK(function_called); + } +#endif + //************************************************************************* TEST_FIXTURE(SetupFixture, test_assignment_member_operator_void) { diff --git a/test/vs2017/Debug LLVM/.gitignore b/test/vs2017/Debug LLVM/.gitignore new file mode 100644 index 00000000..998d6c2d --- /dev/null +++ b/test/vs2017/Debug LLVM/.gitignore @@ -0,0 +1,7 @@ +/random_clcg.csv +/random_hash.csv +/random_lcg.csv +/random_lsfr.csv +/random_mwc.csv +/random_pcg.csv +/random_xorshift.csv diff --git a/test/vs2017/etl.sln b/test/vs2017/etl.sln index 553eb8ef..b7bfc67b 100644 --- a/test/vs2017/etl.sln +++ b/test/vs2017/etl.sln @@ -9,6 +9,8 @@ Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug 64|Win32 = Debug 64|Win32 Debug 64|x64 = Debug 64|x64 + Debug LLVM|Win32 = Debug LLVM|Win32 + Debug LLVM|x64 = Debug LLVM|x64 Debug No Unit Tests|Win32 = Debug No Unit Tests|Win32 Debug No Unit Tests|x64 = Debug No Unit Tests|x64 Debug|Win32 = Debug|Win32 @@ -21,6 +23,10 @@ Global {C21DF78C-D8E0-46AB-9D6F-D38A3C1CB0FB}.Debug 64|Win32.Build.0 = Debug No Unit Tests|Win32 {C21DF78C-D8E0-46AB-9D6F-D38A3C1CB0FB}.Debug 64|x64.ActiveCfg = Debug64|x64 {C21DF78C-D8E0-46AB-9D6F-D38A3C1CB0FB}.Debug 64|x64.Build.0 = Debug64|x64 + {C21DF78C-D8E0-46AB-9D6F-D38A3C1CB0FB}.Debug LLVM|Win32.ActiveCfg = Debug LLVM|Win32 + {C21DF78C-D8E0-46AB-9D6F-D38A3C1CB0FB}.Debug LLVM|Win32.Build.0 = Debug LLVM|Win32 + {C21DF78C-D8E0-46AB-9D6F-D38A3C1CB0FB}.Debug LLVM|x64.ActiveCfg = Debug LLVM|x64 + {C21DF78C-D8E0-46AB-9D6F-D38A3C1CB0FB}.Debug LLVM|x64.Build.0 = Debug LLVM|x64 {C21DF78C-D8E0-46AB-9D6F-D38A3C1CB0FB}.Debug No Unit Tests|Win32.ActiveCfg = Debug No Unit Tests|Win32 {C21DF78C-D8E0-46AB-9D6F-D38A3C1CB0FB}.Debug No Unit Tests|Win32.Build.0 = Debug No Unit Tests|Win32 {C21DF78C-D8E0-46AB-9D6F-D38A3C1CB0FB}.Debug No Unit Tests|x64.ActiveCfg = Debug No Unit Tests|x64 diff --git a/test/vs2017/etl.vcxproj b/test/vs2017/etl.vcxproj index b6bad029..331a6f54 100644 --- a/test/vs2017/etl.vcxproj +++ b/test/vs2017/etl.vcxproj @@ -1,6 +1,14 @@  + + Debug LLVM + Win32 + + + Debug LLVM + x64 + Debug64 Win32 @@ -48,12 +56,24 @@ v141 Unicode + + Application + true + llvm + Unicode + Application true v141 Unicode + + Application + true + v141 + Unicode + Application true @@ -98,9 +118,15 @@ + + + + + + @@ -125,10 +151,19 @@ true \$(IntDir) + + false + true + \$(IntDir) + true true + + true + true + true true @@ -175,6 +210,28 @@ $(OutDir)\etl.exe + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;_LIB;_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + ../../../unittest-cpp/UnitTest++/;../../include;../../include/etl/c;../../test + + + false + stdcpp14 + false + + + Console + false + + + "$(OutDir)etl.exe" + + @@ -196,6 +253,27 @@ $(OutDir)\etl.exe + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;_LIB;_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + ../../unittest-cpp/UnitTest++/;../../include/etl;../../include/etl/c;../../test + + + false + stdcpp14 + + + Console + true + + + $(OutDir)\etl.exe + + @@ -617,7 +695,9 @@ false false false + false false + false false false false @@ -638,7 +718,9 @@ false + false false + false false false false @@ -654,31 +736,41 @@ false + false false + false false false false + false false + false false false false + false false + false false false false + false false + false false false false + false false + false false false @@ -689,7 +781,9 @@ false + false false + false false false false @@ -699,7 +793,9 @@ true true false + false false + false false false @@ -718,7 +814,9 @@ false + false false + false false false false From 2e998a6832fca188b3db79f3d1ce7872766c3015 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Fri, 24 May 2019 23:09:03 +0100 Subject: [PATCH 41/41] Fixed bug in 'vector of pointers' move constructor --- include/etl/private/ivectorpointer.h | 36 ++++++++++++++++++++++++++++ include/etl/vector.h | 4 ++-- include/etl/version.h | 2 +- support/Release notes.txt | 4 ++++ 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/include/etl/private/ivectorpointer.h b/include/etl/private/ivectorpointer.h index a24f2bad..eef997f2 100644 --- a/include/etl/private/ivectorpointer.h +++ b/include/etl/private/ivectorpointer.h @@ -435,6 +435,24 @@ namespace etl : pvoidvector(reinterpret_cast(p_buffer_), MAX_SIZE_) { } + + //********************************************************************* + /// Initialise the source vector after a move. + //********************************************************************* + void initialise_source_external_buffer_after_move() + { + ETL_SUBTRACT_DEBUG_COUNT(int32_t(std::distance(p_buffer, p_end))) + + p_end = p_buffer; + } + + //********************************************************************* + /// Initialise the destination vector after a move. + //********************************************************************* + void initialise_destination_external_buffer_after_move() + { + ETL_ADD_DEBUG_COUNT(int32_t(std::distance(p_buffer, p_end))) + } }; template @@ -828,6 +846,24 @@ namespace etl : pvoidvector(reinterpret_cast(const_cast(p_buffer_)), MAX_SIZE_) { } + + //********************************************************************* + /// Initialise the source vector after a move. + //********************************************************************* + void initialise_source_external_buffer_after_move() + { + ETL_SUBTRACT_DEBUG_COUNT(int32_t(std::distance(p_buffer, p_end))) + + p_end = p_buffer; + } + + //********************************************************************* + /// Initialise the destination vector after a move. + //********************************************************************* + void initialise_destination_external_buffer_after_move() + { + ETL_ADD_DEBUG_COUNT(int32_t(std::distance(p_buffer, p_end))) + } }; //*************************************************************************** diff --git a/include/etl/vector.h b/include/etl/vector.h index 5c3ecc63..1545c321 100644 --- a/include/etl/vector.h +++ b/include/etl/vector.h @@ -1555,7 +1555,7 @@ namespace etl { this->initialise(); - typename etl::ivector::iterator itr = other.begin(); + typename etl::ivector::iterator itr = other.begin(); while (itr != other.end()) { this->push_back(std::move(*itr)); @@ -1574,7 +1574,7 @@ namespace etl if (&rhs != this) { this->clear(); - typename etl::ivector::iterator itr = rhs.begin(); + typename etl::ivector::iterator itr = rhs.begin(); while (itr != rhs.end()) { this->push_back(std::move(*itr)); diff --git a/include/etl/version.h b/include/etl/version.h index 7ef7d2b3..4d6f366a 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -39,7 +39,7 @@ SOFTWARE. #define ETL_VERSION_MAJOR 14 #define ETL_VERSION_MINOR 24 -#define ETL_VERSION_PATCH 1 +#define ETL_VERSION_PATCH 2 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) ETL_STRINGIFY(ETL_VERSION_MINOR) ETL_STRINGIFY(ETL_VERSION_PATCH) #define ETL_VERSION_W ETL_WIDE_STRING(ETL_CONCAT(ETL_CONCAT(ETL_VERSION_MAJOR, ETL_VERSION_MINOR), ETL_VERSION_PATCH)) diff --git a/support/Release notes.txt b/support/Release notes.txt index 5b4e626b..1578bab8 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,7 @@ +=============================================================================== +14.24.2 +Fixed bug in 'vector of pointers' move constructor. + =============================================================================== 14.24.1 Added compile time functor version of 'create()'.