diff --git a/cetlvast/suites/unittest/test_pf17_string_view.cpp b/cetlvast/suites/unittest/test_pf17_string_view.cpp index 862f8f0..afd4cab 100644 --- a/cetlvast/suites/unittest/test_pf17_string_view.cpp +++ b/cetlvast/suites/unittest/test_pf17_string_view.cpp @@ -7,6 +7,8 @@ /// SPDX-License-Identifier: MIT /// +#include +#include #include #include @@ -18,43 +20,63 @@ #include #include #include -#include + +#if (__cplusplus >= CETL_CPP_STANDARD_17) +# include +#endif using testing::Gt; using testing::Lt; using testing::IsNull; -using CharTs = testing::Types; +template +struct TestSpec +{ + using sv_type = StringViewT; + using char_type = CharT; +}; +// +#if (__cplusplus >= CETL_CPP_STANDARD_17) +using TestTypes = testing::Types< // + TestSpec, + TestSpec, wchar_t>, + TestSpec, + TestSpec, wchar_t>>; +#else +using TestTypes = testing::Types< // + TestSpec, + TestSpec, wchar_t>>; +#endif -template +template class TestStringView; // -template <> -class TestStringView : public testing::Test +template +class TestStringView> : public testing::Test { public: static char toChar(const char ch) { return ch; } - static std::basic_string toStr(const char* cstr) + static std::basic_string toStr(const char* const cstr) { return {cstr}; } }; // -template <> -class TestStringView : public testing::Test +template +class TestStringView> : public testing::Test { public: static wchar_t toChar(const char ch) { return ch; } - static std::basic_string toStr(const char* cstr) + static std::basic_string toStr(const char* const cstr) { std::array wstr{}; - const auto result = std::mbstowcs(wstr.data(), cstr, wstr.size()); + const auto result = std::mbstowcs(wstr.data(), cstr, wstr.size()); assert(result == std::strlen(cstr)); (void) result; return {wstr.data()}; @@ -62,15 +84,15 @@ class TestStringView : public testing::Test }; // TestStringView -TYPED_TEST_SUITE(TestStringView, CharTs, ); +TYPED_TEST_SUITE(TestStringView, TestTypes, ); /// TESTS ----------------------------------------------------------------------------------------------------------- -using cetl::pf17::basic_string_view; - TYPED_TEST(TestStringView, DefaultConstructor) { - const basic_string_view sv; + using basic_string_view = typename TypeParam::sv_type; + + const basic_string_view sv; EXPECT_THAT(sv.data(), IsNull()); EXPECT_THAT(sv.size(), 0u); EXPECT_TRUE(sv.empty()); @@ -78,10 +100,13 @@ TYPED_TEST(TestStringView, DefaultConstructor) TYPED_TEST(TestStringView, ConstructFromCString) { + using char_type = typename TypeParam::char_type; + using basic_string_view = typename TypeParam::sv_type; + const auto test_str = TestFixture::toStr("Hello, World!"); - const TypeParam* const cstr = test_str.c_str(); - const basic_string_view sv(cstr); + const char_type* const cstr = test_str.c_str(); + const basic_string_view sv{cstr}; EXPECT_THAT(sv.data(), cstr); EXPECT_THAT(sv.size(), test_str.size()); EXPECT_THAT(sv, TestFixture::toStr("Hello, World!")); @@ -89,19 +114,24 @@ TYPED_TEST(TestStringView, ConstructFromCString) TYPED_TEST(TestStringView, ConstructFromCStringWithLength) { + using char_type = typename TypeParam::char_type; + using basic_string_view = typename TypeParam::sv_type; + const auto test_str = TestFixture::toStr("Hello, World!"); - const TypeParam* cstr = test_str.c_str(); - const basic_string_view sv(cstr, 5); + const char_type* cstr = test_str.c_str(); + const basic_string_view sv{cstr, 5}; EXPECT_THAT(sv.size(), 5u); EXPECT_THAT(sv, TestFixture::toStr("Hello")); } TYPED_TEST(TestStringView, ConstructFromStdString) { + using basic_string_view = typename TypeParam::sv_type; + const auto test_str = TestFixture::toStr("Hello, World!"); - const basic_string_view sv(test_str); + const basic_string_view sv{test_str}; EXPECT_THAT(sv.data(), test_str.data()); EXPECT_THAT(sv.size(), test_str.size()); EXPECT_THAT(sv, test_str); @@ -109,37 +139,42 @@ TYPED_TEST(TestStringView, ConstructFromStdString) TYPED_TEST(TestStringView, SizeAndLength) { - using string_view = basic_string_view; + using char_type = typename TypeParam::char_type; + using basic_string_view = typename TypeParam::sv_type; const auto test_str = TestFixture::toStr("Test string"); - const string_view sv(test_str); + const basic_string_view sv{test_str}; EXPECT_THAT(sv.size(), 11u); EXPECT_THAT(sv.length(), 11u); EXPECT_THAT(sv.max_size(), - (string_view::npos - sizeof(typename string_view::size_type) - sizeof(void*)) / - sizeof(typename string_view::value_type) / 4); + (basic_string_view::npos - sizeof(typename basic_string_view::size_type) - sizeof(void*)) / + sizeof(char_type) / 4); } TYPED_TEST(TestStringView, Empty) { - const basic_string_view sv1; + using basic_string_view = typename TypeParam::sv_type; + + const basic_string_view sv1; EXPECT_TRUE(sv1.empty()); - const auto test_str2 = TestFixture::toStr(""); - const basic_string_view sv2(test_str2); + const auto test_str2 = TestFixture::toStr(""); + const basic_string_view sv2{test_str2}; EXPECT_TRUE(sv2.empty()); - const auto test_str3 = TestFixture::toStr("Non-empty"); - const basic_string_view sv3(test_str3); + const auto test_str3 = TestFixture::toStr("Non-empty"); + const basic_string_view sv3{test_str3}; EXPECT_FALSE(sv3.empty()); } TYPED_TEST(TestStringView, ElementAccessOperator) { + using basic_string_view = typename TypeParam::sv_type; + const auto test_str = TestFixture::toStr("abcdef"); - const basic_string_view sv(test_str); + const basic_string_view sv{test_str}; EXPECT_THAT(sv[0], TestFixture::toChar('a')); EXPECT_THAT(sv[1], TestFixture::toChar('b')); EXPECT_THAT(sv[5], TestFixture::toChar('f')); @@ -147,40 +182,50 @@ TYPED_TEST(TestStringView, ElementAccessOperator) TYPED_TEST(TestStringView, ElementAccessAt) { + using basic_string_view = typename TypeParam::sv_type; + const auto test_str = TestFixture::toStr("abcdef"); - const basic_string_view sv(test_str); + const basic_string_view sv{test_str}; EXPECT_THAT(sv.at(0), TestFixture::toChar('a')); EXPECT_THAT(sv.at(5), TestFixture::toChar('f')); #if defined(__cpp_exceptions) - EXPECT_THROW(sv.at(6), std::out_of_range); + EXPECT_THROW((void) sv.at(6), std::out_of_range); #endif } TYPED_TEST(TestStringView, FrontBack) { + using basic_string_view = typename TypeParam::sv_type; + const auto test_str = TestFixture::toStr("abcdef"); - const basic_string_view sv(test_str); + const basic_string_view sv{test_str}; EXPECT_THAT(sv.front(), TestFixture::toChar('a')); EXPECT_THAT(sv.back(), TestFixture::toChar('f')); } TYPED_TEST(TestStringView, Data) { + using char_type = typename TypeParam::char_type; + using basic_string_view = typename TypeParam::sv_type; + const auto test_str = TestFixture::toStr("Hello, World!"); - const TypeParam* const cstr = test_str.c_str(); - const basic_string_view sv(cstr); + const char_type* const cstr = test_str.c_str(); + const basic_string_view sv{cstr}; EXPECT_THAT(sv.data(), cstr); } TYPED_TEST(TestStringView, Iterators) { + using char_type = typename TypeParam::char_type; + using basic_string_view = typename TypeParam::sv_type; + const auto test_str = TestFixture::toStr("abcdef"); - const basic_string_view sv(test_str); - std::basic_string str; + const basic_string_view sv{test_str}; + std::basic_string str; for (auto it = sv.begin(); it != sv.end(); ++it) // NOLINT { str += *it; @@ -190,10 +235,13 @@ TYPED_TEST(TestStringView, Iterators) TYPED_TEST(TestStringView, ConstIterators) { + using char_type = typename TypeParam::char_type; + using basic_string_view = typename TypeParam::sv_type; + const auto test_str = TestFixture::toStr("abcdef"); - const basic_string_view sv(test_str); - std::basic_string str; + const basic_string_view sv{test_str}; + std::basic_string str; for (auto it = sv.cbegin(); it != sv.cend(); ++it) // NOLINT { str += *it; @@ -203,29 +251,35 @@ TYPED_TEST(TestStringView, ConstIterators) TYPED_TEST(TestStringView, RemovePrefix) { + using basic_string_view = typename TypeParam::sv_type; + const auto test_str = TestFixture::toStr("abcdef"); - basic_string_view sv(test_str); + basic_string_view sv{test_str}; sv.remove_prefix(2); EXPECT_THAT(sv, TestFixture::toStr("cdef")); } TYPED_TEST(TestStringView, RemoveSuffix) { + using basic_string_view = typename TypeParam::sv_type; + const auto test_str = TestFixture::toStr("abcdef"); - basic_string_view sv(test_str); + basic_string_view sv{test_str}; sv.remove_suffix(2); EXPECT_THAT(sv, TestFixture::toStr("abcd")); } TYPED_TEST(TestStringView, Swap) { + using basic_string_view = typename TypeParam::sv_type; + const auto test_str1 = TestFixture::toStr("Hello"); const auto test_str2 = TestFixture::toStr("World"); - basic_string_view sv1(test_str1); - basic_string_view sv2(test_str2); + basic_string_view sv1{test_str1}; + basic_string_view sv2{test_str2}; sv1.swap(sv2); EXPECT_THAT(sv1, TestFixture::toStr("World")); EXPECT_THAT(sv2, TestFixture::toStr("Hello")); @@ -233,11 +287,13 @@ TYPED_TEST(TestStringView, Swap) TYPED_TEST(TestStringView, SwapNonMember) { + using basic_string_view = typename TypeParam::sv_type; + const auto test_str1 = TestFixture::toStr("Hello"); const auto test_str2 = TestFixture::toStr("World"); - basic_string_view sv1(test_str1); - basic_string_view sv2(test_str2); + basic_string_view sv1{test_str1}; + basic_string_view sv2{test_str2}; swap(sv1, sv2); EXPECT_THAT(sv1, TestFixture::toStr("World")); EXPECT_THAT(sv2, TestFixture::toStr("Hello")); @@ -245,38 +301,45 @@ TYPED_TEST(TestStringView, SwapNonMember) TYPED_TEST(TestStringView, Copy) { + using char_type = typename TypeParam::char_type; + using basic_string_view = typename TypeParam::sv_type; + const auto test_str = TestFixture::toStr("Hello, World!"); - const basic_string_view sv(test_str); - TypeParam buffer[20] = {}; - const size_t copied = sv.copy(buffer, 5); + const basic_string_view sv{test_str}; + char_type buffer[20] = {}; + const size_t copied = sv.copy(buffer, 5); EXPECT_THAT(copied, 5u); - EXPECT_THAT(std::basic_string(buffer, copied), TestFixture::toStr("Hello")); + EXPECT_THAT(std::basic_string(buffer, copied), TestFixture::toStr("Hello")); } TYPED_TEST(TestStringView, Substr) { + using basic_string_view = typename TypeParam::sv_type; + const auto test_str = TestFixture::toStr("Hello, World!"); - const basic_string_view sv(test_str); - const basic_string_view sub = sv.substr(7, 5); + const basic_string_view sv{test_str}; + const basic_string_view sub = sv.substr(7, 5); EXPECT_THAT(sub, TestFixture::toStr("World")); #if defined(__cpp_exceptions) - EXPECT_THROW(sv.substr(20), std::out_of_range); + EXPECT_THROW((void) sv.substr(20), std::out_of_range); #endif } TYPED_TEST(TestStringView, Compare) { + using basic_string_view = typename TypeParam::sv_type; + const auto test_str1 = TestFixture::toStr("abc"); const auto test_str2 = TestFixture::toStr("abc"); const auto test_str3 = TestFixture::toStr("abd"); const auto test_str4 = TestFixture::toStr("abcd"); - const basic_string_view sv1(test_str1); - const basic_string_view sv2(test_str2); - const basic_string_view sv3(test_str3); - const basic_string_view sv4(test_str4); + const basic_string_view sv1{test_str1}; + const basic_string_view sv2{test_str2}; + const basic_string_view sv3{test_str3}; + const basic_string_view sv4{test_str4}; EXPECT_THAT(sv1.compare(sv2), 0); EXPECT_THAT(sv1.compare(sv3), Lt(0)); @@ -287,35 +350,41 @@ TYPED_TEST(TestStringView, Compare) TYPED_TEST(TestStringView, FindChar) { + using basic_string_view = typename TypeParam::sv_type; + const auto test_str = TestFixture::toStr("Hello, World!"); - const basic_string_view sv(test_str); + const basic_string_view sv{test_str}; EXPECT_THAT(sv.find(TestFixture::toChar('W')), 7u); - EXPECT_THAT(sv.find(TestFixture::toChar('z')), basic_string_view::npos); + EXPECT_THAT(sv.find(TestFixture::toChar('z')), basic_string_view::npos); } TYPED_TEST(TestStringView, FindStringView) { + using basic_string_view = typename TypeParam::sv_type; + const auto test_str1 = TestFixture::toStr("Hello, World!"); const auto test_str2 = TestFixture::toStr("World"); - const basic_string_view sv(test_str1); - const basic_string_view to_find(test_str2); + const basic_string_view sv{test_str1}; + const basic_string_view to_find{test_str2}; EXPECT_THAT(sv.find(to_find), 7u); EXPECT_THAT(sv.find(TestFixture::toStr("")), 0u); - EXPECT_THAT(sv.find(TestFixture::toStr("Earth")), basic_string_view::npos); - EXPECT_THAT(sv.find(TestFixture::toStr("too long too long too long")), basic_string_view::npos); + EXPECT_THAT(sv.find(TestFixture::toStr("Earth")), basic_string_view::npos); + EXPECT_THAT(sv.find(TestFixture::toStr("too long too long too long")), basic_string_view::npos); } TYPED_TEST(TestStringView, RelationalOperators) { + using basic_string_view = typename TypeParam::sv_type; + const auto test_str1 = TestFixture::toStr("abc"); const auto test_str2 = TestFixture::toStr("abc"); const auto test_str3 = TestFixture::toStr("abd"); - const basic_string_view sv1(test_str1); - const basic_string_view sv2(test_str2); - const basic_string_view sv3(test_str3); + const basic_string_view sv1{test_str1}; + const basic_string_view sv2{test_str2}; + const basic_string_view sv3{test_str3}; EXPECT_TRUE(sv1 == sv2); EXPECT_FALSE(sv1 != sv2); @@ -327,43 +396,31 @@ TYPED_TEST(TestStringView, RelationalOperators) TYPED_TEST(TestStringView, FindOutOfBounds) { - const auto test_str = TestFixture::toStr("Hello"); - - const basic_string_view sv(test_str); - EXPECT_THAT(sv.find(TestFixture::toChar('H'), 10), basic_string_view::npos); - EXPECT_THAT(sv.find(TestFixture::toStr("He"), 10), basic_string_view::npos); -} - -TYPED_TEST(TestStringView, RemovePrefixOutOfBounds) -{ - const auto test_str = TestFixture::toStr("Hello"); - - basic_string_view sv(test_str); - sv.remove_prefix(10); - EXPECT_THAT(sv.size(), 0u); -} + using basic_string_view = typename TypeParam::sv_type; -TYPED_TEST(TestStringView, RemoveSuffixOutOfBounds) -{ const auto test_str = TestFixture::toStr("Hello"); - basic_string_view sv(test_str); - sv.remove_suffix(10); - EXPECT_THAT(sv.size(), 0u); + const basic_string_view sv{test_str}; + EXPECT_THAT(sv.find(TestFixture::toChar('H'), 10), basic_string_view::npos); + EXPECT_THAT(sv.find(TestFixture::toStr("He"), 10), basic_string_view::npos); } TYPED_TEST(TestStringView, SubstrWithNpos) { + using basic_string_view = typename TypeParam::sv_type; + const auto test_str = TestFixture::toStr("Hello, World!"); - const basic_string_view sv(test_str); - const basic_string_view sub = sv.substr(7); + const basic_string_view sv{test_str}; + const basic_string_view sub = sv.substr(7); EXPECT_THAT(sub, TestFixture::toStr("World!")); } TYPED_TEST(TestStringView, EmptyStringViewOperations) { - const basic_string_view sv; + using basic_string_view = typename TypeParam::sv_type; + + const basic_string_view sv; EXPECT_THAT(sv.size(), 0u); EXPECT_TRUE(sv.empty()); EXPECT_THAT(sv.data(), IsNull()); @@ -372,27 +429,33 @@ TYPED_TEST(TestStringView, EmptyStringViewOperations) TYPED_TEST(TestStringView, ComparisonWithString) { + using basic_string_view = typename TypeParam::sv_type; + const auto test_str = TestFixture::toStr("Hello"); - const basic_string_view sv(test_str); + const basic_string_view sv{test_str}; EXPECT_THAT(sv, test_str); EXPECT_THAT(test_str, sv); } TYPED_TEST(TestStringView, ComparisonWithCString) { + using basic_string_view = typename TypeParam::sv_type; + const auto test_str = TestFixture::toStr("Hello"); - const basic_string_view sv(test_str); + const basic_string_view sv{test_str}; EXPECT_THAT(sv, test_str.c_str()); EXPECT_THAT(test_str.c_str(), sv); } TYPED_TEST(TestStringView, FindPartial) { + using basic_string_view = typename TypeParam::sv_type; + const auto test_str = TestFixture::toStr("ababab"); - const basic_string_view sv(test_str); + const basic_string_view sv{test_str}; EXPECT_THAT(sv.find(TestFixture::toStr("aba")), 0u); EXPECT_THAT(sv.find(TestFixture::toStr("aba"), 1), 2u); } @@ -401,10 +464,13 @@ TYPED_TEST(TestStringView, CopyOutOfBounds) { #if defined(__cpp_exceptions) + using char_type = typename TypeParam::char_type; + using basic_string_view = typename TypeParam::sv_type; + const auto test_str = TestFixture::toStr("Hello"); - const basic_string_view sv(test_str); - TypeParam buffer[10]; + const basic_string_view sv{test_str}; + char_type buffer[10]; EXPECT_THROW(sv.copy(buffer, 5, 6), std::out_of_range); #else @@ -414,27 +480,28 @@ TYPED_TEST(TestStringView, CopyOutOfBounds) TYPED_TEST(TestStringView, stream_operator) { - using SV = basic_string_view; + using char_type = typename TypeParam::char_type; + using basic_string_view = typename TypeParam::sv_type; const auto test_str = TestFixture::toStr("Test"); { - std::basic_stringstream ss; - ss << SV{test_str} << SV{test_str}; + std::basic_stringstream ss; + ss << basic_string_view{test_str} << basic_string_view{test_str}; EXPECT_THAT(ss.str(), TestFixture::toStr("TestTest")); } { - std::basic_stringstream ss; - ss << std::setw(9) << std::setfill(TestFixture::toChar('-')) << std::left << SV{test_str}; + std::basic_stringstream ss; + ss << std::setw(9) << std::setfill(TestFixture::toChar('-')) << std::left << basic_string_view{test_str}; EXPECT_THAT(ss.str(), TestFixture::toStr("Test-----")); } { - std::basic_stringstream ss; - ss << std::setw(9) << std::setfill(TestFixture::toChar('-')) << std::right << SV{test_str}; + std::basic_stringstream ss; + ss << std::setw(9) << std::setfill(TestFixture::toChar('-')) << std::right << basic_string_view{test_str}; EXPECT_THAT(ss.str(), TestFixture::toStr("-----Test")); } { - std::basic_stringstream ss; - ss << std::setw(2) << SV{test_str}; + std::basic_stringstream ss; + ss << std::setw(2) << basic_string_view{test_str}; EXPECT_THAT(ss.str(), test_str); } } diff --git a/include/cetl/pf17/cetlpf.hpp b/include/cetl/pf17/cetlpf.hpp index c43ddcc..39aea01 100644 --- a/include/cetl/pf17/cetlpf.hpp +++ b/include/cetl/pf17/cetlpf.hpp @@ -202,6 +202,7 @@ using cetl::pf17::bad_variant_access; # endif // string_view +using cetl::pf17::basic_string_view; using cetl::pf17::string_view; } // namespace cetl