|
| 1 | +#include <array> |
| 2 | +#include <beman/inplace_vector/inplace_vector.hpp> |
| 3 | +#include <type_traits> |
| 4 | + |
| 5 | +/** |
| 6 | + * These tests are only meant to test that suitable constexpr functions compiles |
| 7 | + * in a constant evaluation environment. |
| 8 | + * |
| 9 | + * The validity of the underlying implementation is tested in the main test |
| 10 | + * case as there's no diverging logic between constexpr code and runtime code. |
| 11 | + */ |
| 12 | + |
| 13 | +struct Some { |
| 14 | + constexpr Some() = default; |
| 15 | + constexpr Some(int v) : val(v) {} |
| 16 | + |
| 17 | + int val; |
| 18 | + |
| 19 | + constexpr bool operator==(const Some &other) const { |
| 20 | + return val == other.val; |
| 21 | + } |
| 22 | + constexpr auto operator<=>(const Some &other) const { |
| 23 | + return val <=> other.val; |
| 24 | + } |
| 25 | +}; |
| 26 | +static_assert(std::is_trivial_v<Some>); |
| 27 | + |
| 28 | +#define TEST(NAME) \ |
| 29 | + static_assert(std::invoke([]() { \ |
| 30 | + NAME<beman::inplace_vector<int, 20>>(); \ |
| 31 | + NAME<beman::inplace_vector<Some, 20>>(); \ |
| 32 | + return true; \ |
| 33 | + }), \ |
| 34 | + "##NAME"); |
| 35 | + |
| 36 | +template <typename IV> constexpr void test_constructors() { |
| 37 | + using T = IV::value_type; |
| 38 | + |
| 39 | + std::array<T, 5> arr; |
| 40 | + arr.fill(T(20)); |
| 41 | + |
| 42 | + { |
| 43 | + IV v; |
| 44 | + } |
| 45 | + { |
| 46 | + IV v(10); |
| 47 | + } |
| 48 | + { |
| 49 | + IV v(5, T(10)); |
| 50 | + } |
| 51 | + { |
| 52 | + IV v(arr.begin(), arr.end()); |
| 53 | + } |
| 54 | + { |
| 55 | + IV v(beman::from_range_t{}, arr); |
| 56 | + } |
| 57 | + { |
| 58 | + IV other{0, 1}; |
| 59 | + IV copy(other); |
| 60 | + } |
| 61 | + { |
| 62 | + IV other{0, 1}; |
| 63 | + IV copy(std::move(other)); |
| 64 | + } |
| 65 | + { |
| 66 | + IV v({0, 1}); |
| 67 | + } |
| 68 | +} |
| 69 | +TEST(test_constructors); |
| 70 | + |
| 71 | +template <typename IV> constexpr void test_op_eq() { |
| 72 | + using T = IV::value_type; |
| 73 | + |
| 74 | + { |
| 75 | + IV v, other{0, 1}; |
| 76 | + v = other; |
| 77 | + } |
| 78 | + { |
| 79 | + IV v, other{0, 1}; |
| 80 | + v = std::move(other); |
| 81 | + } |
| 82 | + { |
| 83 | + IV v; |
| 84 | + v = {0, 1}; |
| 85 | + } |
| 86 | +} |
| 87 | +TEST(test_op_eq); |
| 88 | + |
| 89 | +template <typename IV> constexpr void test_assignment() { |
| 90 | + using T = IV::value_type; |
| 91 | + |
| 92 | + std::array<T, 5> arr; |
| 93 | + arr.fill(T(20)); |
| 94 | + |
| 95 | + { |
| 96 | + IV v; |
| 97 | + v.assign(arr.begin(), arr.end()); |
| 98 | + } |
| 99 | + { |
| 100 | + IV v; |
| 101 | + v.assign_range(arr); |
| 102 | + } |
| 103 | + { |
| 104 | + IV v; |
| 105 | + v.assign(5, T(20)); |
| 106 | + } |
| 107 | + { |
| 108 | + IV v; |
| 109 | + v.assign({0, 1, 2}); |
| 110 | + } |
| 111 | +} |
| 112 | +TEST(test_assignment); |
| 113 | + |
| 114 | +template <typename IV> constexpr void test_iterator_access() { |
| 115 | + using T = IV::value_type; |
| 116 | + |
| 117 | + { |
| 118 | + IV v{0, 1}; |
| 119 | + (void)v.begin(); |
| 120 | + (void)v.end(); |
| 121 | + (void)v.rbegin(); |
| 122 | + (void)v.rend(); |
| 123 | + (void)v.cbegin(); |
| 124 | + (void)v.cend(); |
| 125 | + } |
| 126 | +} |
| 127 | +TEST(test_iterator_access); |
| 128 | + |
| 129 | +template <typename IV> constexpr void test_size_capacity() { |
| 130 | + using T = IV::value_type; |
| 131 | + |
| 132 | + { |
| 133 | + IV v{0, 1}; |
| 134 | + (void)v.empty(); |
| 135 | + (void)v.size(); |
| 136 | + (void)v.max_size(); |
| 137 | + (void)v.capacity(); |
| 138 | + } |
| 139 | + |
| 140 | + { |
| 141 | + IV v{0, 1}; |
| 142 | + v.resize(3); |
| 143 | + v.resize(2, T(20)); |
| 144 | + } |
| 145 | + |
| 146 | + { |
| 147 | + IV v{0, 1}; |
| 148 | + v.reserve(5); |
| 149 | + v.shrink_to_fit(); |
| 150 | + } |
| 151 | +} |
| 152 | +TEST(test_size_capacity); |
| 153 | + |
| 154 | +template <typename IV> constexpr void test_element_access() { |
| 155 | + { |
| 156 | + IV v{0, 1}; |
| 157 | + (void)v[0]; |
| 158 | + (void)v.at(0); |
| 159 | + (void)v.front(); |
| 160 | + (void)v.back(); |
| 161 | + } |
| 162 | + |
| 163 | + { |
| 164 | + const IV v{0, 1}; |
| 165 | + (void)v[0]; |
| 166 | + (void)v.at(0); |
| 167 | + (void)v.front(); |
| 168 | + (void)v.back(); |
| 169 | + } |
| 170 | +} |
| 171 | +TEST(test_element_access); |
| 172 | + |
| 173 | +template <typename IV> constexpr void test_data_access() { |
| 174 | + { |
| 175 | + IV v{0, 1}; |
| 176 | + v.data(); |
| 177 | + } |
| 178 | + { |
| 179 | + const IV v{0, 1}; |
| 180 | + v.data(); |
| 181 | + } |
| 182 | +} |
| 183 | +TEST(test_data_access); |
| 184 | + |
| 185 | +template <typename IV> constexpr void test_modifiers() { |
| 186 | + using T = IV::value_type; |
| 187 | + |
| 188 | + std::array<T, 5> arr; |
| 189 | + arr.fill(T(20)); |
| 190 | + |
| 191 | + { |
| 192 | + IV v; |
| 193 | + v.emplace_back(20); |
| 194 | + v.push_back(arr[0]); |
| 195 | + v.push_back(T(20)); |
| 196 | + v.append_range(arr); |
| 197 | + v.pop_back(); |
| 198 | + } |
| 199 | + |
| 200 | + { |
| 201 | + IV v; |
| 202 | + v.try_emplace_back(20); |
| 203 | + v.try_push_back(arr[0]); |
| 204 | + v.try_push_back(T(20)); |
| 205 | + // v.try_append_range(arr); |
| 206 | + } |
| 207 | + |
| 208 | + { |
| 209 | + IV v; |
| 210 | + v.unchecked_emplace_back(20); |
| 211 | + v.unchecked_push_back(arr[0]); |
| 212 | + v.unchecked_push_back(T(20)); |
| 213 | + } |
| 214 | + |
| 215 | + { |
| 216 | + IV v{0, 1}; |
| 217 | + v.emplace(v.begin(), 20); |
| 218 | + v.insert(v.begin(), arr[0]); |
| 219 | + v.insert(v.begin(), T(20)); |
| 220 | + v.insert(v.begin(), 2, arr[0]); |
| 221 | + v.insert(v.begin(), arr.begin(), arr.end()); |
| 222 | + v.insert_range(v.begin(), arr); |
| 223 | + v.insert(v.begin(), {1, 2}); |
| 224 | + v.erase(v.begin()); |
| 225 | + v.erase(v.begin(), v.begin() + 2); |
| 226 | + } |
| 227 | + |
| 228 | + { |
| 229 | + IV v, other{0, 1}; |
| 230 | + v.swap(other); |
| 231 | + } |
| 232 | + |
| 233 | + { |
| 234 | + IV v{0, 1}; |
| 235 | + v.clear(); |
| 236 | + } |
| 237 | +} |
| 238 | +TEST(test_modifiers); |
| 239 | + |
| 240 | +template <typename IV> constexpr void test_op_comp() { |
| 241 | + IV v{0, 1, 2}, other{3, 4}; |
| 242 | + |
| 243 | + (void)(v == v); |
| 244 | + (void)(v == other); |
| 245 | + (void)(v <=> v); |
| 246 | + (void)(v <=> other); |
| 247 | +} |
| 248 | +TEST(test_op_comp); |
| 249 | + |
| 250 | +template <typename IV> constexpr void test_erase() { |
| 251 | + IV v{0, 1, 2, 3, 3, 5}; |
| 252 | + (void)beman::erase(v, 3); |
| 253 | + (void)beman::erase_if(v, [](auto v) { return v < 3; }); |
| 254 | +} |
| 255 | +TEST(test_erase) |
| 256 | + |
| 257 | +int main() { |
| 258 | + // compile means passing |
| 259 | +} |
0 commit comments