From f03359790adf4977e67fe988c3bc328c3836759e Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Thu, 21 Mar 2019 20:45:28 +0000 Subject: [PATCH 1/3] Modified 'truncated' to only be cleared on 'clear()' or 'assign()'. Added assignment from zero terminated string pointer. --- include/etl/basic_string.h | 46 +++++++++++++-------- include/etl/cstring.h | 10 +++++ include/etl/u16string.h | 14 +++++-- include/etl/u32string.h | 14 +++++-- include/etl/version.h | 2 +- include/etl/wstring.h | 14 +++++-- support/Release notes.txt | 5 +++ test/test_string_char.cpp | 79 +++++++++++++++++++++++++++++++++++- test/test_string_u16.cpp | 77 ++++++++++++++++++++++++++++++++++- test/test_string_u32.cpp | 77 ++++++++++++++++++++++++++++++++++- test/test_string_wchar_t.cpp | 77 ++++++++++++++++++++++++++++++++++- 11 files changed, 381 insertions(+), 34 deletions(-) diff --git a/include/etl/basic_string.h b/include/etl/basic_string.h index 043e3553..2354fae8 100644 --- a/include/etl/basic_string.h +++ b/include/etl/basic_string.h @@ -393,7 +393,10 @@ namespace etl //********************************************************************* void resize(size_t new_size, T value) { - is_truncated = (new_size > CAPACITY); + if (new_size > CAPACITY) + { + is_truncated = true; + } new_size = std::min(new_size, CAPACITY); @@ -639,7 +642,6 @@ namespace etl { p_buffer[current_size++] = value; p_buffer[current_size] = 0; - is_truncated = false; } else { @@ -734,8 +736,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); @@ -782,8 +782,6 @@ namespace etl //********************************************************************* void insert(const_iterator position, size_t n, T value) { - is_truncated = false; - if (n == 0) { return; @@ -803,7 +801,11 @@ namespace etl // 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); } @@ -844,8 +846,6 @@ namespace etl template void insert(iterator position, TIterator first, TIterator last) { - is_truncated = false; - if (first == last) { return; @@ -864,7 +864,11 @@ namespace etl // 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()) @@ -1004,8 +1008,6 @@ namespace etl std::copy(i_element + 1, end(), i_element); p_buffer[--current_size] = 0; - is_truncated = false; - return i_element; } @@ -1025,8 +1027,6 @@ namespace etl current_size -= n_delete; p_buffer[current_size] = 0; - is_truncated = false; - return first; } @@ -1046,7 +1046,10 @@ namespace etl //********************************************************************* size_t copy(pointer s, size_t len, size_t pos = 0) { - is_truncated = (pos + len > size()); + if ((pos + len > size())) + { + is_truncated = true; + } size_t endpos = std::min(pos + len, size()); @@ -1873,11 +1876,22 @@ namespace etl if (&rhs != this) { assign(rhs.cbegin(), rhs.cend()); + is_truncated = rhs.is_truncated; } return *this; } + //************************************************************************* + /// Assignment operator. + //************************************************************************* + ibasic_string& operator = (const_pointer rhs) + { + assign(rhs); + + return *this; + } + //************************************************************************* /// += operator. //************************************************************************* @@ -1894,7 +1908,7 @@ namespace etl //************************************************************************* /// += operator. //************************************************************************* - ibasic_string& operator += (const T* rhs) + ibasic_string& operator += (const_pointer rhs) { append(rhs); diff --git a/include/etl/cstring.h b/include/etl/cstring.h index 7e9ae7e2..3f192aa4 100644 --- a/include/etl/cstring.h +++ b/include/etl/cstring.h @@ -193,6 +193,16 @@ namespace etl 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/u16string.h b/include/etl/u16string.h index 67027206..7e26d9a7 100644 --- a/include/etl/u16string.h +++ b/include/etl/u16string.h @@ -75,7 +75,6 @@ namespace etl u16string(const etl::u16string& other) : iu16string(reinterpret_cast(&buffer), MAX_SIZE) { - this->initialise(); this->assign(other.begin(), other.end()); } @@ -100,7 +99,6 @@ namespace etl { ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds)); - this->initialise(); this->assign(other.begin() + position, other.begin() + position + length_); } @@ -111,7 +109,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)); } @@ -123,7 +120,6 @@ namespace etl u16string(const value_type* text, size_t count) : iu16string(reinterpret_cast(&buffer), MAX_SIZE) { - this->initialise(); this->assign(text, text + count); } @@ -197,6 +193,16 @@ namespace etl 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 3e349e96..68dc4312 100644 --- a/include/etl/u32string.h +++ b/include/etl/u32string.h @@ -75,7 +75,6 @@ namespace etl u32string(const etl::u32string& other) : iu32string(reinterpret_cast(&buffer), MAX_SIZE) { - this->initialise(); this->assign(other.begin(), other.end()); } @@ -100,7 +99,6 @@ namespace etl { ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds)); - this->initialise(); this->assign(other.begin() + position, other.begin() + position + length_); } @@ -111,7 +109,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)); } @@ -123,7 +120,6 @@ namespace etl u32string(const value_type* text, size_t count) : iu32string(reinterpret_cast(&buffer), MAX_SIZE) { - this->initialise(); this->assign(text, text + count); } @@ -197,6 +193,16 @@ namespace etl 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/version.h b/include/etl/version.h index 1198ebb6..1885892b 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 14 -#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/include/etl/wstring.h b/include/etl/wstring.h index 68e372f0..05879ea2 100644 --- a/include/etl/wstring.h +++ b/include/etl/wstring.h @@ -75,7 +75,6 @@ namespace etl wstring(const etl::wstring& other) : iwstring(reinterpret_cast(&buffer), MAX_SIZE) { - wstring::initialise(); wstring::assign(other.begin(), other.end()); } @@ -101,7 +100,6 @@ namespace etl { ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds)); - this->initialise(); this->assign(other.begin() + position, other.begin() + position + length_); } @@ -112,7 +110,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)); } @@ -124,7 +121,6 @@ namespace etl wstring(const value_type* text, size_t count) : iwstring(reinterpret_cast(&buffer), MAX_SIZE) { - this->initialise(); this->assign(text, text + count); } @@ -198,6 +194,16 @@ namespace etl 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 e3eb2130..3c93a938 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,8 @@ +=============================================================================== +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. diff --git a/test/test_string_char.cpp b/test/test_string_char.cpp index ceffa8bb..7cea71a0 100644 --- a/test/test_string_char.cpp +++ b/test/test_string_char.cpp @@ -361,7 +361,7 @@ namespace bool is_equal = Equal(text, other_text); CHECK(is_equal); CHECK(text.truncated()); - CHECK(!other_text.truncated()); + CHECK(other_text.truncated()); } //************************************************************************* @@ -397,7 +397,7 @@ namespace CHECK(is_equal); CHECK(text1.truncated()); - CHECK(!text2.truncated()); + CHECK(text2.truncated()); } //************************************************************************* @@ -430,6 +430,56 @@ namespace 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()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_begin) { @@ -3422,5 +3472,30 @@ 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()); + } }; } diff --git a/test/test_string_u16.cpp b/test/test_string_u16.cpp index 8fa7769b..b277e59c 100644 --- a/test/test_string_u16.cpp +++ b/test/test_string_u16.cpp @@ -396,7 +396,7 @@ namespace CHECK(is_equal); CHECK(text1.truncated()); - CHECK(!text2.truncated()); + CHECK(text2.truncated()); } //************************************************************************* @@ -429,6 +429,56 @@ namespace 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()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_begin) { @@ -3421,5 +3471,30 @@ 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()); + } }; } diff --git a/test/test_string_u32.cpp b/test/test_string_u32.cpp index 7207c070..5106b6bb 100644 --- a/test/test_string_u32.cpp +++ b/test/test_string_u32.cpp @@ -396,7 +396,7 @@ namespace CHECK(is_equal); CHECK(text1.truncated()); - CHECK(!text2.truncated()); + CHECK(text2.truncated()); } //************************************************************************* @@ -429,6 +429,56 @@ namespace 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()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_begin) { @@ -3421,5 +3471,30 @@ 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()); + } }; } diff --git a/test/test_string_wchar_t.cpp b/test/test_string_wchar_t.cpp index a8e3e060..1ef01b10 100644 --- a/test/test_string_wchar_t.cpp +++ b/test/test_string_wchar_t.cpp @@ -396,7 +396,7 @@ namespace CHECK(is_equal); CHECK(text1.truncated()); - CHECK(!text2.truncated()); + CHECK(text2.truncated()); } //************************************************************************* @@ -429,6 +429,56 @@ namespace 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()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_begin) { @@ -3421,5 +3471,30 @@ 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()); + } }; } From 6e72c5beca3a6efa533d20439cc72892774caa07 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Thu, 21 Mar 2019 21:01:27 +0000 Subject: [PATCH 2/3] Removed test for self in += operator. --- include/etl/basic_string.h | 5 +---- test/test_string_char.cpp | 27 +++++++++++++++++++++++++++ test/test_string_u16.cpp | 27 +++++++++++++++++++++++++++ test/test_string_u32.cpp | 27 +++++++++++++++++++++++++++ test/test_string_wchar_t.cpp | 27 +++++++++++++++++++++++++++ 5 files changed, 109 insertions(+), 4 deletions(-) diff --git a/include/etl/basic_string.h b/include/etl/basic_string.h index 2354fae8..b25fe44b 100644 --- a/include/etl/basic_string.h +++ b/include/etl/basic_string.h @@ -1897,10 +1897,7 @@ namespace etl //************************************************************************* ibasic_string& operator += (const ibasic_string& rhs) { - if (&rhs != this) - { - append(rhs); - } + append(rhs); return *this; } diff --git a/test/test_string_char.cpp b/test/test_string_char.cpp index 7cea71a0..8ab4e9f1 100644 --- a/test/test_string_char.cpp +++ b/test/test_string_char.cpp @@ -1275,6 +1275,33 @@ namespace 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()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_append_string_subpos_sublen) { diff --git a/test/test_string_u16.cpp b/test/test_string_u16.cpp index b277e59c..2f69a0c4 100644 --- a/test/test_string_u16.cpp +++ b/test/test_string_u16.cpp @@ -1274,6 +1274,33 @@ namespace 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()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_append_string_subpos_sublen) { diff --git a/test/test_string_u32.cpp b/test/test_string_u32.cpp index 5106b6bb..b67a590f 100644 --- a/test/test_string_u32.cpp +++ b/test/test_string_u32.cpp @@ -1274,6 +1274,33 @@ namespace 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()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_append_string_subpos_sublen) { diff --git a/test/test_string_wchar_t.cpp b/test/test_string_wchar_t.cpp index 1ef01b10..90472137 100644 --- a/test/test_string_wchar_t.cpp +++ b/test/test_string_wchar_t.cpp @@ -1274,6 +1274,33 @@ namespace 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()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_append_string_subpos_sublen) { From d2200d80d81d3baf3a8ef30a6fbda42e242475c4 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sat, 23 Mar 2019 14:24:39 +0000 Subject: [PATCH 3/3] Extended 'truncate' flag rules. --- include/etl/basic_string.h | 43 ++++++++++- include/etl/cstring.h | 11 ++- include/etl/u16string.h | 11 ++- include/etl/u32string.h | 11 ++- include/etl/wstring.h | 11 ++- test/test_string_char.cpp | 135 +++++++++++++++++++++++++++++++++- test/test_string_u16.cpp | 137 ++++++++++++++++++++++++++++++++-- test/test_string_u32.cpp | 137 ++++++++++++++++++++++++++++++++-- test/test_string_wchar_t.cpp | 139 +++++++++++++++++++++++++++++++++-- 9 files changed, 599 insertions(+), 36 deletions(-) diff --git a/include/etl/basic_string.h b/include/etl/basic_string.h index b25fe44b..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: //************************************************************************* @@ -515,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; + } } //********************************************************************* @@ -668,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; } @@ -682,6 +700,7 @@ namespace etl ETL_ASSERT(subposition <= str.size(), ETL_ERROR(string_out_of_bounds)); insert(size(), str, subposition, sublength); + return *this; } @@ -917,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; } @@ -938,6 +963,12 @@ namespace etl } insert(begin() + position, str.cbegin() + subposition, str.cbegin() + subposition + sublength); + + if (str.truncated()) + { + is_truncated = true; + } + return *this; } @@ -1324,6 +1355,11 @@ namespace etl // Insert the new stuff. insert(first_, str.begin(), str.end()); + if (str.truncated()) + { + is_truncated = true; + } + return *this; } @@ -1875,8 +1911,7 @@ namespace etl { if (&rhs != this) { - assign(rhs.cbegin(), rhs.cend()); - is_truncated = rhs.is_truncated; + assign(rhs); } return *this; diff --git a/include/etl/cstring.h b/include/etl/cstring.h index 3f192aa4..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); } //************************************************************************* @@ -100,6 +100,11 @@ namespace etl ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds)); this->assign(other.begin() + position, other.begin() + position + length_); + + if (other.truncated()) + { + this->is_truncated = true; + } } //************************************************************************* @@ -187,7 +192,7 @@ namespace etl { if (&rhs != this) { - this->assign(rhs.cbegin(), rhs.cend()); + this->assign(rhs); } return *this; diff --git a/include/etl/u16string.h b/include/etl/u16string.h index 7e26d9a7..f0d7a08f 100644 --- a/include/etl/u16string.h +++ b/include/etl/u16string.h @@ -75,7 +75,7 @@ namespace etl u16string(const etl::u16string& other) : iu16string(reinterpret_cast(&buffer), MAX_SIZE) { - this->assign(other.begin(), other.end()); + this->assign(other); } //************************************************************************* @@ -85,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); } //************************************************************************* @@ -100,6 +100,11 @@ namespace etl ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds)); this->assign(other.begin() + position, other.begin() + position + length_); + + if (other.truncated()) + { + this->is_truncated = true; + } } //************************************************************************* @@ -187,7 +192,7 @@ namespace etl { if (&rhs != this) { - this->assign(rhs.cbegin(), rhs.cend()); + this->assign(rhs); } return *this; diff --git a/include/etl/u32string.h b/include/etl/u32string.h index 68dc4312..27792fcb 100644 --- a/include/etl/u32string.h +++ b/include/etl/u32string.h @@ -75,7 +75,7 @@ namespace etl u32string(const etl::u32string& other) : iu32string(reinterpret_cast(&buffer), MAX_SIZE) { - this->assign(other.begin(), other.end()); + this->assign(other); } //************************************************************************* @@ -85,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); } //************************************************************************* @@ -100,6 +100,11 @@ namespace etl ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds)); this->assign(other.begin() + position, other.begin() + position + length_); + + if (other.truncated()) + { + this->is_truncated = true; + } } //************************************************************************* @@ -187,7 +192,7 @@ namespace etl { if (&rhs != this) { - this->assign(rhs.cbegin(), rhs.cend()); + this->assign(rhs); } return *this; diff --git a/include/etl/wstring.h b/include/etl/wstring.h index 05879ea2..388525c7 100644 --- a/include/etl/wstring.h +++ b/include/etl/wstring.h @@ -75,7 +75,7 @@ namespace etl wstring(const etl::wstring& other) : iwstring(reinterpret_cast(&buffer), MAX_SIZE) { - wstring::assign(other.begin(), other.end()); + this->assign(other); } //************************************************************************* @@ -85,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); } @@ -101,6 +101,11 @@ namespace etl ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds)); this->assign(other.begin() + position, other.begin() + position + length_); + + if (other.truncated()) + { + this->is_truncated = true; + } } //************************************************************************* @@ -188,7 +193,7 @@ namespace etl { if (&rhs != this) { - this->assign(rhs.cbegin(), rhs.cend()); + this->assign(rhs); } return *this; diff --git a/test/test_string_char.cpp b/test/test_string_char.cpp index 8ab4e9f1..b40a4ae4 100644 --- a/test/test_string_char.cpp +++ b/test/test_string_char.cpp @@ -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; @@ -243,9 +244,7 @@ namespace //************************************************************************* 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); @@ -283,6 +282,14 @@ namespace CHECK(text2.truncated()); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_copy_constructor_from_truncated) + { + Text text(longer_text.c_str()); + Text text2(text); + CHECK(text2.truncated()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_construct_position_length) { @@ -427,7 +434,7 @@ namespace CHECK(is_equal); CHECK(text.truncated()); - CHECK(!other_text.truncated()); + CHECK(other_text.truncated()); } //************************************************************************* @@ -767,6 +774,40 @@ namespace 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()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_assign_pointer) { @@ -1206,6 +1247,27 @@ namespace } } + //************************************************************************* + 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()); + } + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_insert_size_t_position_string_subpos_sunlen) { @@ -1275,6 +1337,17 @@ namespace 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) { @@ -1344,6 +1417,17 @@ namespace 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()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_append_c_string) { @@ -3524,5 +3608,48 @@ namespace 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 2f69a0c4..ba4c94af 100644 --- a/test/test_string_u16.cpp +++ b/test/test_string_u16.cpp @@ -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; @@ -242,9 +243,7 @@ namespace //************************************************************************* 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); @@ -282,6 +281,14 @@ namespace CHECK(text2.truncated()); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_copy_constructor_from_truncated) + { + Text text(longer_text.c_str()); + Text text2(text); + CHECK(text2.truncated()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_construct_position_length) { @@ -360,7 +367,7 @@ namespace bool is_equal = Equal(text, other_text); CHECK(is_equal); CHECK(text.truncated()); - CHECK(!other_text.truncated()); + CHECK(other_text.truncated()); } //************************************************************************* @@ -426,7 +433,7 @@ namespace CHECK(is_equal); CHECK(text.truncated()); - CHECK(!other_text.truncated()); + CHECK(other_text.truncated()); } //************************************************************************* @@ -766,6 +773,40 @@ namespace 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()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_assign_pointer) { @@ -1205,6 +1246,27 @@ namespace } } + //************************************************************************* + 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()); + } + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_insert_size_t_position_string_subpos_sunlen) { @@ -1274,6 +1336,17 @@ namespace 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) { @@ -1343,6 +1416,17 @@ namespace 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()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_append_c_string) { @@ -3523,5 +3607,48 @@ namespace 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 b67a590f..4acfeb8a 100644 --- a/test/test_string_u32.cpp +++ b/test/test_string_u32.cpp @@ -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; @@ -242,9 +243,7 @@ namespace //************************************************************************* 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); @@ -282,6 +281,14 @@ namespace CHECK(text2.truncated()); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_copy_constructor_from_truncated) + { + Text text(longer_text.c_str()); + Text text2(text); + CHECK(text2.truncated()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_construct_position_length) { @@ -360,7 +367,7 @@ namespace bool is_equal = Equal(text, other_text); CHECK(is_equal); CHECK(text.truncated()); - CHECK(!other_text.truncated()); + CHECK(other_text.truncated()); } //************************************************************************* @@ -426,7 +433,7 @@ namespace CHECK(is_equal); CHECK(text.truncated()); - CHECK(!other_text.truncated()); + CHECK(other_text.truncated()); } //************************************************************************* @@ -766,6 +773,40 @@ namespace 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()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_assign_pointer) { @@ -1205,6 +1246,27 @@ namespace } } + //************************************************************************* + 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()); + } + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_insert_size_t_position_string_subpos_sunlen) { @@ -1274,6 +1336,17 @@ namespace 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) { @@ -1343,6 +1416,17 @@ namespace 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()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_append_c_string) { @@ -3523,5 +3607,48 @@ namespace 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_wchar_t.cpp b/test/test_string_wchar_t.cpp index 90472137..0aba10d1 100644 --- a/test/test_string_wchar_t.cpp +++ b/test/test_string_wchar_t.cpp @@ -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; @@ -242,9 +243,7 @@ namespace //************************************************************************* 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); @@ -282,6 +281,14 @@ namespace CHECK(text2.truncated()); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_copy_constructor_from_truncated) + { + Text text(longer_text.c_str()); + Text text2(text); + CHECK(text2.truncated()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_construct_position_length) { @@ -360,7 +367,7 @@ namespace bool is_equal = Equal(text, other_text); CHECK(is_equal); CHECK(text.truncated()); - CHECK(!other_text.truncated()); + CHECK(other_text.truncated()); } //************************************************************************* @@ -426,7 +433,7 @@ namespace CHECK(is_equal); CHECK(text.truncated()); - CHECK(!other_text.truncated()); + CHECK(other_text.truncated()); } //************************************************************************* @@ -766,6 +773,40 @@ namespace 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()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_assign_pointer) { @@ -1205,6 +1246,27 @@ namespace } } + //************************************************************************* + 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()); + } + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_insert_size_t_position_string_subpos_sunlen) { @@ -1274,6 +1336,17 @@ namespace 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) { @@ -1343,6 +1416,17 @@ namespace 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()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_append_c_string) { @@ -3523,5 +3607,48 @@ namespace 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()); + } +}; }