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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
#include <algorithm>
#include <frozen/set.h>
#include <iostream>
#include <set>
#include <type_traits>
#include "bench.hpp"
#include "catch.hpp"
TEST_CASE("empty frozen set", "[set]") {
constexpr frozen::set<int, 0> ze_set{};
constexpr auto empty = ze_set.empty();
REQUIRE(empty);
constexpr auto size = ze_set.size();
REQUIRE(size == 0);
constexpr auto max_size = ze_set.max_size();
REQUIRE(max_size == 0);
constexpr auto count = ze_set.count(3);
REQUIRE(count == 0);
constexpr auto find = ze_set.find(5);
REQUIRE(find == ze_set.end());
constexpr auto range = ze_set.equal_range(0);
REQUIRE(std::get<0>(range) == ze_set.end());
REQUIRE(std::get<1>(range) == ze_set.end());
constexpr auto lower_bound = ze_set.lower_bound(1);
REQUIRE(lower_bound == ze_set.end());
constexpr auto upper_bound = ze_set.upper_bound(1);
REQUIRE(upper_bound == ze_set.end());
auto constexpr begin = ze_set.begin(), end = ze_set.end();
REQUIRE(begin == end);
auto constexpr key_comp = ze_set.key_comp();
auto constexpr key_comparison = key_comp(1, 2);
REQUIRE(key_comparison);
auto constexpr value_comp = ze_set.value_comp();
auto constexpr value_comparison = value_comp(11, 12);
REQUIRE(value_comparison);
auto constexpr cbegin = ze_set.cbegin(), cend = ze_set.cend();
REQUIRE(cbegin == cend);
std::for_each(ze_set.begin(), ze_set.end(), [](int) {});
REQUIRE(std::distance(ze_set.rbegin(), ze_set.rend()) == 0);
REQUIRE(std::count(ze_set.crbegin(), ze_set.crend(), 3) == 0);
}
TEST_CASE("singleton frozen set", "[set]") {
constexpr frozen::set<short, 1> ze_set{1};
constexpr auto empty = ze_set.empty();
REQUIRE(!empty);
constexpr auto size = ze_set.size();
REQUIRE(size == 1);
constexpr auto max_size = ze_set.max_size();
REQUIRE(max_size == 1);
constexpr auto count1 = ze_set.count(1);
REQUIRE(count1 == 1);
constexpr auto count2 = ze_set.count(2);
REQUIRE(count2 == 0);
const auto find1 = ze_set.find(1);
REQUIRE(find1 == ze_set.begin());
const auto find5 = ze_set.find(5);
REQUIRE(find5 == ze_set.end());
const auto range0 = ze_set.equal_range(0);
REQUIRE(std::get<0>(range0) == ze_set.end());
REQUIRE(std::get<1>(range0) == ze_set.end());
const auto range1 = ze_set.equal_range(1);
REQUIRE(std::get<0>(range1) == ze_set.begin());
REQUIRE(std::get<1>(range1) == ze_set.end());
const auto lower_bound0 = ze_set.lower_bound(0);
REQUIRE(lower_bound0 == ze_set.end());
const auto lower_bound1 = ze_set.lower_bound(1);
REQUIRE(lower_bound1 == ze_set.find(1));
const auto lower_bound2 = ze_set.lower_bound(2);
REQUIRE(lower_bound2 == ze_set.end());
const auto upper_bound0 = ze_set.upper_bound(0);
REQUIRE(upper_bound0 == ze_set.end());
const auto upper_bound1 = ze_set.upper_bound(1);
REQUIRE(upper_bound1 == ze_set.end());
const auto upper_bound2 = ze_set.upper_bound(2);
REQUIRE(upper_bound2 == ze_set.end());
auto const begin = ze_set.begin(), end = ze_set.end();
REQUIRE((begin + 1) == end);
auto const key_comp = ze_set.key_comp();
auto const key_comparison = key_comp(1, 2);
REQUIRE(key_comparison);
auto const value_comp = ze_set.value_comp();
auto const value_comparison = value_comp(11, 12);
REQUIRE(value_comparison);
auto const cbegin = ze_set.cbegin(), cend = ze_set.cend();
REQUIRE(cbegin == (cend - 1));
std::for_each(ze_set.begin(), ze_set.end(), [](int) {});
REQUIRE(std::distance(ze_set.rbegin(), ze_set.rend()) == 1);
REQUIRE(std::count(ze_set.crbegin(), ze_set.crend(), 3) == 0);
REQUIRE(std::count(ze_set.crbegin(), ze_set.crend(), 1) == 1);
}
TEST_CASE("triple frozen set", "[set]") {
constexpr frozen::set<unsigned long, 3> ze_set{10, 20, 30};
constexpr auto empty = ze_set.empty();
REQUIRE(!empty);
constexpr auto size = ze_set.size();
REQUIRE(size == 3);
constexpr auto max_size = ze_set.max_size();
REQUIRE(max_size == 3);
constexpr auto count1 = ze_set.count(1);
REQUIRE(count1 == 0);
constexpr auto count10 = ze_set.count(10);
REQUIRE(count10 == 1);
const auto find10 = ze_set.find(10);
REQUIRE(find10 == ze_set.begin());
const auto find15 = ze_set.find(15);
REQUIRE(find15 == ze_set.end());
const auto range0 = ze_set.equal_range(0);
REQUIRE(std::get<0>(range0) == ze_set.end());
REQUIRE(std::get<1>(range0) == ze_set.end());
const auto range1 = ze_set.equal_range(10);
REQUIRE(std::get<0>(range1) == ze_set.begin());
REQUIRE(std::get<1>(range1) == ze_set.begin() + 1);
const auto lower_bound0 = ze_set.lower_bound(0);
REQUIRE(lower_bound0 == ze_set.end());
for (auto val : ze_set) {
const auto lower_bound = ze_set.lower_bound(val);
REQUIRE(lower_bound == ze_set.find(val));
}
const auto lower_bound2 = ze_set.lower_bound(40);
REQUIRE(lower_bound2 == ze_set.end());
const auto upper_bound0 = ze_set.upper_bound(0);
REQUIRE(upper_bound0 == ze_set.end());
const auto upper_bound1 = ze_set.upper_bound(10);
REQUIRE(upper_bound1 == (ze_set.begin() + 1));
const auto upper_bound2 = ze_set.upper_bound(40);
REQUIRE(upper_bound2 == ze_set.end());
auto const begin = ze_set.begin(), end = ze_set.end();
REQUIRE((begin + ze_set.size()) == end);
auto const key_comp = ze_set.key_comp();
auto const key_comparison = key_comp(1, 2);
REQUIRE(key_comparison);
auto const value_comp = ze_set.value_comp();
auto const value_comparison = value_comp(11, 12);
REQUIRE(value_comparison);
auto const cbegin = ze_set.cbegin(), cend = ze_set.cend();
REQUIRE(cbegin == (cend - ze_set.size()));
std::for_each(ze_set.begin(), ze_set.end(), [](int) {});
REQUIRE((std::size_t)std::distance(ze_set.rbegin(), ze_set.rend()) == ze_set.size());
REQUIRE(std::count(ze_set.crbegin(), ze_set.crend(), 3) == 0);
REQUIRE(std::count(ze_set.crbegin(), ze_set.crend(), 20) == 1);
}
TEST_CASE("frozen::set <> std::set", "[set]") {
#define INIT_SEQ \
19, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 111, 1112, 1115, 1118, 1110, 1977, 177, \
277, 477, 577, 677, 777, 877, 977, 1077, 1177, 11177, 111277, 111577, \
111877, 111077, 1999, 199, 299, 499, 599, 699, 799, 899, 999, 1099, \
1199, 11199, 111299, 111599, 111899, 111099, 197799, 17799, 27799, \
47799, 57799, 67799, 77799, 87799, 97799, 107799, 117799, 1117799, \
11127799, 11157799, 11187799, 11107799, 1988, 188, 288, 488, 588, 688, \
788, 888, 988, 1088, 1188, 11188, 111288, 111588, 111888, 111088, \
197788, 17788, 27788, 47788, 57788, 67788, 77788, 87788, 97788, 107788, \
117788, 1117788, 11127788, 11157788, 11187788, 11107788, 199988, 19988, \
29988, 49988, 59988, 69988, 79988, 89988, 99988, 109988, 119988, \
1119988, 11129988, 11159988, 11189988, 11109988, 19779988, 1779988, \
2779988, 4779988, 5779988, 6779988, 7779988, 8779988, 9779988, 10779988, \
11779988, 111779988, 1112779988, 1115779988, 1118779988, 1110779988
const std::set<int> std_set = {INIT_SEQ};
constexpr frozen::set<int, 128> frozen_set = {INIT_SEQ};
SECTION("checking size and content") {
REQUIRE(std_set.size() == frozen_set.size());
for (auto v : std_set)
REQUIRE(frozen_set.count(v));
for (auto v : frozen_set)
REQUIRE(std_set.count(v));
}
}
TEST_CASE("frozen::set <> frozen::make_set", "[set]") {
constexpr frozen::set<int, 128> from_ctor = { INIT_SEQ };
constexpr int init_array[]{INIT_SEQ};
constexpr auto from_c_array = frozen::make_set(init_array);
constexpr auto from_std_array = frozen::make_set(std::array<int, 128>{{INIT_SEQ}});
REQUIRE(std::equal(from_c_array.begin(), from_c_array.end(), from_std_array.begin()));
SECTION("checking size and content") {
REQUIRE(from_ctor.size() == from_c_array.size());
for (auto v : from_c_array)
REQUIRE(from_ctor.count(v));
for (auto v : from_ctor)
REQUIRE(from_c_array.count(v));
}
constexpr frozen::set<short, 0> frozen_empty_set = {};
constexpr auto frozen_empty_set2 = frozen::make_set<short>();
constexpr auto frozen_empty_set3 = frozen::make_set<short>({});
SECTION("checking empty set") {
REQUIRE(frozen_empty_set.empty());
REQUIRE(frozen_empty_set.size() == 0);
REQUIRE(frozen_empty_set.begin() == frozen_empty_set.end());
REQUIRE(frozen_empty_set2.empty());
REQUIRE(frozen_empty_set2.size() == 0);
REQUIRE(frozen_empty_set2.begin() == frozen_empty_set2.end());
REQUIRE(frozen_empty_set3.empty());
REQUIRE(frozen_empty_set3.size() == 0);
REQUIRE(frozen_empty_set3.begin() == frozen_empty_set3.end());
}
}
TEST_CASE("frozen::set constexpr", "[set]") {
constexpr frozen::set<unsigned, 2> ce = {3, 11};
static_assert(*ce.begin() == 3, "");
static_assert(*(ce.begin() + 1) == 11, "");
static_assert(ce.size() == 2, "");
static_assert(ce.count(3), "");
static_assert(!ce.count(0), "");
static_assert(ce.find(0) == ce.end(), "");
static_assert(ce.contains(3), "");
static_assert(!ce.contains(0), "");
}
TEST_CASE("frozen::set of frozen::set", "[set]") {
using s1 = frozen::set<unsigned, 1>;
constexpr frozen::set<s1, 2> ce = {{3}, {11}};
static_assert(*ce.begin() == s1({3}), "");
static_assert(*(ce.begin() + 1) == s1({11}), "");
static_assert(ce.size() == 2, "");
static_assert(ce.count(s1({3})), "");
static_assert(!ce.count(s1({0})), "");
static_assert(ce.find(s1({0})) == ce.end(), "");
static_assert(ce.contains(s1({3})), "");
static_assert(!ce.contains(s1({0})), "");
}
struct Foo {
int x;
};
constexpr inline bool operator==(Foo const &a, Foo const &b) { return a.x == b.x; }
constexpr inline bool operator<(Foo const &a, Foo const &b) { return a.x < b.x; }
constexpr inline bool operator<(Foo const &a, int b) { return a.x < b; }
constexpr inline bool operator<(int a, Foo const &b) { return a < b.x; }
TEST_CASE("frozen::set heterogeneous container", "[set]") {
constexpr std::array<Foo, 3> std_array{{{1}, {2}, {3}}};
constexpr Foo c_array[]{{1}, {2}, {3}};
constexpr auto from_std_array = frozen::make_set<Foo, std::less<>>(std_array);
constexpr auto from_c_array = frozen::make_set<Foo, std::less<>>(c_array);
REQUIRE(from_std_array == from_c_array);
for (const auto& set : {from_std_array, from_c_array}) {
REQUIRE(set.find(1) != set.end());
REQUIRE(set.count(2) == 1);
REQUIRE(set.count(42) == 0);
}
}
#ifdef FROZEN_LETITGO_HAS_DEDUCTION_GUIDES
TEST_CASE("frozen::set deduction guide", "[set]") {
constexpr frozen::set integersSet{1,2,3,4,5};
static_assert(std::is_same<
std::remove_cv_t<decltype(integersSet)>,
frozen::set<int, 5>>::value, "wrong type deduced");
}
#endif // FROZEN_LETITGO_HAS_DEDUCTION_GUIDES
|