From b110a967abee0338a1668bd6921d9d803226c577 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Fri, 1 Mar 2019 21:08:40 +0100 Subject: [PATCH 01/12] 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/12] 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/12] 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/12] 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/12] 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/12] 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/12] 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/12] 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/12] 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 9ef75bd89ec21150817b49bcfab33b5d077bf6cb Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 14 Apr 2019 19:26:55 +0100 Subject: [PATCH 10/12] 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 11/12] 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 12/12] 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.