1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2019 John Wellbelove
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 "unit_test_framework.h"
#include <string>
#include "etl/string.h"
#include "etl/wstring.h"
#include "etl/u16string.h"
#include "etl/u32string.h"
namespace
{
SUITE(test_make_string)
{
//*************************************************************************
template <typename T1, typename T2>
bool Equal(const T1& compare_text, const T2& text)
{
return (compare_text.size() == text.size()) &&
std::equal(text.begin(), text.end(), compare_text.begin());
}
//*************************************************************************
struct SetupFixture
{
};
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_make_string_empty)
{
size_t length = strlen("");
auto ctext = etl::make_string("");
auto wtext = etl::make_string(L"");
auto u16text = etl::make_string(u"");
auto u32text = etl::make_string(U"");
CHECK_EQUAL(length, ctext.size());
CHECK_EQUAL(length, wtext.size());
CHECK_EQUAL(length, u16text.size());
CHECK_EQUAL(length, u32text.size());
CHECK(Equal(std::string(""), ctext));
CHECK(Equal(std::wstring(L""), wtext));
CHECK(Equal(std::u16string(u""), u16text));
CHECK(Equal(std::u32string(U""), u32text));
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_make_string)
{
size_t length = strlen("Hello World");
auto ctext = etl::make_string("Hello World");
auto wtext = etl::make_string(L"Hello World");
auto u16text = etl::make_string(u"Hello World");
auto u32text = etl::make_string(U"Hello World");
CHECK_EQUAL(length, ctext.max_size());
CHECK_EQUAL(length, wtext.max_size());
CHECK_EQUAL(length, u16text.max_size());
CHECK_EQUAL(length, u32text.max_size());
CHECK(Equal(std::string("Hello World"), ctext));
CHECK(Equal(std::wstring(L"Hello World"), wtext));
CHECK(Equal(std::u16string(u"Hello World"), u16text));
CHECK(Equal(std::u32string(U"Hello World"), u32text));
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_make_string_with_capacity)
{
constexpr size_t CAPACITY = 20UL;
size_t length = strlen("Hello World");
auto ctext = etl::make_string_with_capacity<CAPACITY>("Hello World");
auto wtext = etl::make_string_with_capacity<CAPACITY>(L"Hello World");
auto u16text = etl::make_string_with_capacity<CAPACITY>(u"Hello World");
auto u32text = etl::make_string_with_capacity<CAPACITY>(U"Hello World");
CHECK_EQUAL(CAPACITY, ctext.max_size());
CHECK_EQUAL(length, ctext.size());
if (etl::traits::has_string_truncation_checks)
{
CHECK(!ctext.is_truncated());
}
CHECK_EQUAL(CAPACITY, wtext.max_size());
CHECK_EQUAL(length, wtext.size());
#if ETL_HAS_STRING_TRUNCATION_CHECKS
CHECK(!wtext.is_truncated());
#endif
CHECK_EQUAL(CAPACITY, u16text.max_size());
CHECK_EQUAL(length, u16text.size());
#if ETL_HAS_STRING_TRUNCATION_CHECKS
CHECK(!u16text.is_truncated());
#endif
CHECK_EQUAL(CAPACITY, u32text.max_size());
CHECK_EQUAL(length, u32text.size());
#if ETL_HAS_STRING_TRUNCATION_CHECKS
CHECK(!u32text.is_truncated());
#endif
CHECK(Equal(std::string("Hello World"), ctext));
CHECK(Equal(std::wstring(L"Hello World"), wtext));
CHECK(Equal(std::u16string(u"Hello World"), u16text));
CHECK(Equal(std::u32string(U"Hello World"), u32text));
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_make_string_with_capacity_truncated)
{
constexpr size_t CAPACITY = 10UL;
size_t length = strlen("Hello World");
#if ETL_HAS_ERROR_ON_STRING_TRUNCATION
CHECK_THROW(auto ctext = etl::make_string_with_capacity<CAPACITY>("Hello World"), etl::string_truncation);
CHECK_THROW(auto wtext = etl::make_string_with_capacity<CAPACITY>(L"Hello World"), etl::string_truncation);;
CHECK_THROW(auto u16text = etl::make_string_with_capacity<CAPACITY>(u"Hello World"), etl::string_truncation);;
CHECK_THROW(auto u32text = etl::make_string_with_capacity<CAPACITY>(U"Hello World"), etl::string_truncation);;
#else
auto ctext = etl::make_string_with_capacity<CAPACITY>("Hello World");
auto wtext = etl::make_string_with_capacity<CAPACITY>(L"Hello World");
auto u16text = etl::make_string_with_capacity<CAPACITY>(u"Hello World");
auto u32text = etl::make_string_with_capacity<CAPACITY>(U"Hello World");
CHECK_EQUAL(CAPACITY, ctext.max_size());
CHECK_EQUAL(length - 1, ctext.size());
#if ETL_HAS_STRING_TRUNCATION_CHECKS
CHECK(ctext.is_truncated());
#endif
CHECK_EQUAL(CAPACITY, wtext.max_size());
CHECK_EQUAL(length - 1, wtext.size());
#if ETL_HAS_STRING_TRUNCATION_CHECKS
CHECK(wtext.is_truncated());
#endif
CHECK_EQUAL(CAPACITY, u16text.max_size());
CHECK_EQUAL(length - 1, u16text.size());
#if ETL_HAS_STRING_TRUNCATION_CHECKS
CHECK(u16text.is_truncated());
#endif
CHECK_EQUAL(CAPACITY, u32text.max_size());
CHECK_EQUAL(length - 1, u32text.size());
#if ETL_HAS_STRING_TRUNCATION_CHECKS
CHECK(u32text.is_truncated());
#endif
CHECK(Equal(std::string("Hello Worl"), ctext));
CHECK(Equal(std::wstring(L"Hello Worl"), ctext));
CHECK(Equal(std::u16string(u"Hello Worl"), ctext));
CHECK(Equal(std::u32string(U"Hello Worl"), ctext));
#endif
}
};
}
|