|
| 1 | +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN |
| 2 | +#include <doctest/doctest.h> |
| 3 | + |
| 4 | +#include <dice/template-library/static_string.hpp> |
| 5 | + |
| 6 | +#include <ranges> |
| 7 | + |
| 8 | +TEST_SUITE("static_string") { |
| 9 | + using namespace dice::template_library; |
| 10 | + |
| 11 | + template<typename T> |
| 12 | + struct weird_allocator : std::allocator<T> { |
| 13 | + using is_always_equal = std::false_type; |
| 14 | + using propagate_on_container_move_assignment = std::false_type; |
| 15 | + |
| 16 | + bool operator==([[maybe_unused]] weird_allocator const &other) const noexcept { |
| 17 | + return false; |
| 18 | + } |
| 19 | + }; |
| 20 | + |
| 21 | + using weird_static_string = basic_static_string<char, std::char_traits<char>, weird_allocator<char>>; |
| 22 | + |
| 23 | + static_assert(sizeof(static_string) == 2 * sizeof(void *)); |
| 24 | + |
| 25 | + template<typename Allocator> |
| 26 | + void check_empty_string(basic_static_string<char, std::char_traits<char>, Allocator> const &s) { |
| 27 | + CHECK_EQ(s.data(), nullptr); |
| 28 | + CHECK_EQ(s.size(), 0); |
| 29 | + CHECK(s.empty()); |
| 30 | + CHECK_EQ(s.begin(), s.end()); |
| 31 | + CHECK_EQ(s.rbegin(), s.rend()); |
| 32 | + CHECK_EQ(s.cbegin(), s.cend()); |
| 33 | + CHECK_EQ(s.crbegin(), s.crend()); |
| 34 | + CHECK_EQ(s, ""); |
| 35 | + CHECK_EQ(static_cast<std::string_view>(s), ""); |
| 36 | + } |
| 37 | + |
| 38 | + template<typename Allocator> |
| 39 | + void check_non_empty_string(basic_static_string<char, std::char_traits<char>, Allocator> const &actual, std::string_view expected) { |
| 40 | + assert(!expected.empty()); |
| 41 | + |
| 42 | + CHECK_EQ(actual.size(), expected.size()); |
| 43 | + CHECK_EQ(actual, actual); |
| 44 | + CHECK_EQ(actual, expected); |
| 45 | + CHECK_EQ(actual <=> expected, std::strong_ordering::equal); |
| 46 | + CHECK_EQ(actual <=> actual, std::strong_ordering::equal); |
| 47 | + CHECK_FALSE(actual.empty()); |
| 48 | + CHECK(std::ranges::equal(actual, expected)); |
| 49 | + CHECK(std::ranges::equal(actual | std::views::reverse, expected | std::views::reverse)); |
| 50 | + CHECK_EQ(memcmp(actual.data(), expected.data(), actual.size()), 0); |
| 51 | + CHECK_EQ(actual.front(), expected.front()); |
| 52 | + CHECK_EQ(actual.back(), expected.back()); |
| 53 | + CHECK_EQ(actual[0], expected[0]); |
| 54 | + CHECK_EQ(static_cast<std::string_view>(actual), expected); |
| 55 | + } |
| 56 | + |
| 57 | + TEST_CASE("empty string") { |
| 58 | + SUBCASE("default ctor") { |
| 59 | + static_string s; |
| 60 | + check_empty_string(s); |
| 61 | + } |
| 62 | + |
| 63 | + SUBCASE("string view ctor") { |
| 64 | + static_string s{""}; |
| 65 | + check_empty_string(s); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + TEST_CASE("move ctor") { |
| 70 | + std::string_view const expected = "Hello World"; |
| 71 | + static_string s{expected}; |
| 72 | + check_non_empty_string(s, expected); |
| 73 | + |
| 74 | + static_string s2{std::move(s)}; |
| 75 | + |
| 76 | + check_empty_string(s); |
| 77 | + check_non_empty_string(s2, expected); |
| 78 | + } |
| 79 | + |
| 80 | + TEST_CASE("copy ctor") { |
| 81 | + std::string_view const expected = "Hello World"; |
| 82 | + |
| 83 | + static_string const s{expected}; |
| 84 | + check_non_empty_string(s, expected); |
| 85 | + |
| 86 | + static_string const s2{s}; |
| 87 | + check_non_empty_string(s, expected); |
| 88 | + check_non_empty_string(s2, expected); |
| 89 | + } |
| 90 | + |
| 91 | + TEST_CASE("move assignment") { |
| 92 | + SUBCASE("regular") { |
| 93 | + SUBCASE("std allocator") { |
| 94 | + static_string s1{"Hello World"}; |
| 95 | + static_string s2{"Spherical Cow"}; |
| 96 | + s1 = std::move(s2); |
| 97 | + check_non_empty_string(s1, "Spherical Cow"); |
| 98 | + } |
| 99 | + |
| 100 | + SUBCASE("weird allocator") { |
| 101 | + weird_static_string s1{"Hello World"}; |
| 102 | + weird_static_string s2{"Spherical Cow"}; |
| 103 | + |
| 104 | + s1 = std::move(s2); |
| 105 | + check_non_empty_string(s1, "Spherical Cow"); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + SUBCASE("assign empty") { |
| 110 | + SUBCASE("std alloctor") { |
| 111 | + static_string empty; |
| 112 | + check_empty_string(empty); |
| 113 | + |
| 114 | + static_string s{"Hello World"}; |
| 115 | + check_non_empty_string(s, "Hello World"); |
| 116 | + |
| 117 | + s = std::move(empty); |
| 118 | + check_empty_string(s); |
| 119 | + } |
| 120 | + |
| 121 | + SUBCASE("weird allocator") { |
| 122 | + weird_static_string empty; |
| 123 | + check_empty_string(empty); |
| 124 | + |
| 125 | + weird_static_string s{"Hello World"}; |
| 126 | + check_non_empty_string(s, "Hello World"); |
| 127 | + |
| 128 | + s = std::move(empty); |
| 129 | + check_empty_string(s); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + TEST_CASE("copy assignment") { |
| 135 | + SUBCASE("regular") { |
| 136 | + SUBCASE("std allocator") { |
| 137 | + static_string s1{"Hello World"}; |
| 138 | + static_string const s2{"Spherical Cow"}; |
| 139 | + s1 = s2; |
| 140 | + check_non_empty_string(s1, "Spherical Cow"); |
| 141 | + check_non_empty_string(s2, "Spherical Cow"); |
| 142 | + } |
| 143 | + |
| 144 | + SUBCASE("weird allocator") { |
| 145 | + weird_static_string s1{"Hello World"}; |
| 146 | + weird_static_string const s2{"Spherical Cow"}; |
| 147 | + |
| 148 | + s1 = s2; |
| 149 | + check_non_empty_string(s1, "Spherical Cow"); |
| 150 | + check_non_empty_string(s2, "Spherical Cow"); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + SUBCASE("assign empty") { |
| 155 | + SUBCASE("std alloctor") { |
| 156 | + static_string const empty; |
| 157 | + check_empty_string(empty); |
| 158 | + |
| 159 | + static_string s{"Hello World"}; |
| 160 | + check_non_empty_string(s, "Hello World"); |
| 161 | + |
| 162 | + s = empty; |
| 163 | + check_empty_string(s); |
| 164 | + check_empty_string(empty); |
| 165 | + } |
| 166 | + |
| 167 | + SUBCASE("weird allocator") { |
| 168 | + weird_static_string const empty; |
| 169 | + check_empty_string(empty); |
| 170 | + |
| 171 | + weird_static_string s{"Hello World"}; |
| 172 | + check_non_empty_string(s, "Hello World"); |
| 173 | + |
| 174 | + s = empty; |
| 175 | + check_empty_string(s); |
| 176 | + check_empty_string(empty); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + TEST_CASE("swap") { |
| 182 | + std::string_view const expected1 = "Hello World"; |
| 183 | + std::string_view const expected2 = "Spherical Cow"; |
| 184 | + |
| 185 | + static_string s1{expected1}; |
| 186 | + check_non_empty_string(s1, expected1); |
| 187 | + |
| 188 | + static_string s2{expected2}; |
| 189 | + check_non_empty_string(s2, expected2); |
| 190 | + |
| 191 | + swap(s1, s2); |
| 192 | + check_non_empty_string(s1, expected2); |
| 193 | + check_non_empty_string(s2, expected1); |
| 194 | + } |
| 195 | +} |
0 commit comments