Skip to content

Commit

Permalink
[string] Improve basic_inplace_string tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiashienzsch committed Apr 17, 2024
1 parent ed48889 commit 64954e5
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tests/string/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
project("string")

tetl_add_test(${PROJECT_NAME} basic_inplace_string)
tetl_add_test(${PROJECT_NAME} char_traits)
tetl_add_test(${PROJECT_NAME} inplace_string.access)
tetl_add_test(${PROJECT_NAME} inplace_string.algorithm)
tetl_add_test(${PROJECT_NAME} inplace_string.compare)
tetl_add_test(${PROJECT_NAME} inplace_string.construct)
tetl_add_test(${PROJECT_NAME} inplace_string.modify)
tetl_add_test(${PROJECT_NAME} inplace_u8string)
tetl_add_test(${PROJECT_NAME} inplace_u16string)
tetl_add_test(${PROJECT_NAME} inplace_u32string)
tetl_add_test(${PROJECT_NAME} inplace_wstring)
tetl_add_test(${PROJECT_NAME} stod)
tetl_add_test(${PROJECT_NAME} stoi)
Expand Down
93 changes: 93 additions & 0 deletions tests/string/basic_inplace_string.t.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// SPDX-License-Identifier: BSL-1.0

#include <etl/string.hpp>

#include "testing/testing.hpp"

namespace {

template <typename Char, typename String>
constexpr auto test() -> bool
{
CHECK_SAME_TYPE(typename String::value_type, Char);
CHECK_SAME_TYPE(typename String::size_type, etl::size_t);
CHECK_SAME_TYPE(typename String::difference_type, etl::ptrdiff_t);
CHECK_SAME_TYPE(typename String::traits_type, etl::char_traits<Char>);
CHECK_SAME_TYPE(typename String::pointer, Char*);
CHECK_SAME_TYPE(typename String::const_pointer, Char const*);
CHECK_SAME_TYPE(typename String::reference, Char&);
CHECK_SAME_TYPE(typename String::const_reference, Char const&);
CHECK_SAME_TYPE(typename String::iterator, Char*);
CHECK_SAME_TYPE(typename String::const_iterator, Char const*);
CHECK_SAME_TYPE(typename String::reverse_iterator, etl::reverse_iterator<Char*>);
CHECK_SAME_TYPE(typename String::const_reverse_iterator, etl::reverse_iterator<Char const*>);

// construct(default)
{
auto const str = String();
CHECK(str.empty());
CHECK(str.size() == 0); // NOLINT
CHECK(str.begin() == str.end());
CHECK(str == str);
CHECK_FALSE(str != str);
}

// construct(size, char)
{
auto const str = String(2, Char('A'));
CHECK_FALSE(str.empty());
CHECK(str.size() == 2);
CHECK(str.begin() != str.end());

CHECK_FALSE(str == String());
CHECK_FALSE(String() == str);

CHECK(str != String());
CHECK(String() != str);

CHECK_FALSE(str < String());
CHECK(String() < str);

CHECK_FALSE(str <= String());
CHECK(String() <= str);

CHECK(str > String());
CHECK_FALSE(String() > str);

CHECK(str >= String());
CHECK_FALSE(String() >= str);
}

return true;
}

template <typename Char>
constexpr auto test_char_type() -> bool
{
CHECK(test<Char, etl::basic_inplace_string<Char, 3>>());
CHECK(test<Char, etl::basic_inplace_string<Char, 5>>());
CHECK(test<Char, etl::basic_inplace_string<Char, 7>>());
CHECK(test<Char, etl::basic_inplace_string<Char, 8>>());
CHECK(test<Char, etl::basic_inplace_string<Char, 15>>());
CHECK(test<Char, etl::basic_inplace_string<Char, 16>>());
CHECK(test<Char, etl::basic_inplace_string<Char, 31>>());
return true;
}

constexpr auto test_all() -> bool
{
CHECK(test_char_type<char>());
CHECK(test_char_type<wchar_t>());
CHECK(test_char_type<char8_t>());
CHECK(test_char_type<char16_t>());
CHECK(test_char_type<char32_t>());
return true;
}

} // namespace

auto main() -> int
{
STATIC_CHECK(test_all());
return 0;
}
47 changes: 47 additions & 0 deletions tests/string/inplace_u16string.t.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: BSL-1.0

#include <etl/string.hpp>

#include "testing/testing.hpp"

template <typename String>
constexpr auto test() -> bool
{
auto str = String();
CHECK(str == u"");
CHECK(str.empty());
CHECK(str.size() == 0); // NOLINT
CHECK(size(str) == 0); // NOLINT

str = str + u"tes";
CHECK(str == u"tes");

str = str + char16_t('t');
CHECK(str == u"test");

str = str + String{u"_foo"};
CHECK(str == u"test_foo");

str = u"__" + str;
CHECK(str == u"__test_foo");

str = char16_t('a') + str;
CHECK(str == u"a__test_foo");
return true;
}

constexpr auto test_all() -> bool
{
CHECK(test<etl::inplace_u16string<16>>());
CHECK(test<etl::inplace_u16string<17>>());
CHECK(test<etl::inplace_u16string<18>>());
CHECK(test<etl::inplace_u16string<24>>());
CHECK(test<etl::inplace_u16string<32>>());
return true;
}

auto main() -> int
{
STATIC_CHECK(test_all());
return 0;
}
47 changes: 47 additions & 0 deletions tests/string/inplace_u32string.t.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: BSL-1.0

#include <etl/string.hpp>

#include "testing/testing.hpp"

template <typename String>
constexpr auto test() -> bool
{
auto str = String();
CHECK(str == U"");
CHECK(str.empty());
CHECK(str.size() == 0); // NOLINT
CHECK(size(str) == 0); // NOLINT

str = str + U"tes";
CHECK(str == U"tes");

str = str + char32_t('t');
CHECK(str == U"test");

str = str + String{U"_foo"};
CHECK(str == U"test_foo");

str = U"__" + str;
CHECK(str == U"__test_foo");

str = char32_t('a') + str;
CHECK(str == U"a__test_foo");
return true;
}

constexpr auto test_all() -> bool
{
CHECK(test<etl::inplace_u32string<16>>());
CHECK(test<etl::inplace_u32string<17>>());
CHECK(test<etl::inplace_u32string<18>>());
CHECK(test<etl::inplace_u32string<24>>());
CHECK(test<etl::inplace_u32string<32>>());
return true;
}

auto main() -> int
{
STATIC_CHECK(test_all());
return 0;
}

0 comments on commit 64954e5

Please sign in to comment.