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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
|
//
// immer: immutable data structures for C++
// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente
//
// This software is distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt
//
#ifndef MAP_T
#error "define the map template to use in MAP_T"
#include <immer/map.hpp>
#define MAP_T ::immer::map
#endif
#include <immer/algorithm.hpp>
#include <immer/box.hpp>
#include "test/dada.hpp"
#include "test/util.hpp"
#include <catch2/catch_test_macros.hpp>
#include <random>
#include <unordered_map>
#include <unordered_set>
IMMER_RANGES_CHECK(std::ranges::forward_range<MAP_T<std::string, std::string>>);
using memory_policy_t = MAP_T<unsigned, unsigned>::memory_policy_type;
template <typename T = unsigned>
auto make_generator()
{
auto engine = std::default_random_engine{42};
auto dist = std::uniform_int_distribution<T>{};
return std::bind(dist, engine);
}
struct conflictor
{
unsigned v1;
unsigned v2;
bool operator==(const conflictor& x) const
{
return v1 == x.v1 && v2 == x.v2;
}
};
struct hash_conflictor
{
std::size_t operator()(const conflictor& x) const { return x.v1; }
};
auto make_values_with_collisions(unsigned n)
{
auto gen = make_generator();
auto vals = std::vector<std::pair<conflictor, unsigned>>{};
auto vals_ = std::unordered_set<conflictor, hash_conflictor>{};
auto i = 0u;
generate_n(back_inserter(vals), n, [&] {
auto newv = conflictor{};
do {
newv = {unsigned(gen() % (n / 2)), gen()};
} while (!vals_.insert(newv).second);
return std::pair<conflictor, unsigned>{newv, i++};
});
return vals;
}
auto make_test_map(unsigned n)
{
auto s = MAP_T<unsigned, unsigned>{};
for (auto i = 0u; i < n; ++i)
s = std::move(s).insert({i, i});
return s;
}
auto make_test_map(const std::vector<std::pair<conflictor, unsigned>>& vals)
{
auto s = MAP_T<conflictor, unsigned, hash_conflictor>{};
for (auto&& v : vals)
s = std::move(s).insert(v);
return s;
}
TEST_CASE("instantiation")
{
SECTION("default")
{
auto v = MAP_T<int, int>{};
CHECK(v.size() == 0u);
CHECK(v.identity() == MAP_T<int, int>{}.identity());
}
}
TEST_CASE("basic insertion")
{
auto v1 = MAP_T<int, int>{};
CHECK(v1.count(42) == 0);
auto v2 = v1.insert({42, {}});
CHECK(v1.count(42) == 0);
CHECK(v2.count(42) == 1);
auto v3 = v2.insert({42, {}});
CHECK(v1.count(42) == 0);
CHECK(v2.count(42) == 1);
CHECK(v3.count(42) == 1);
}
TEST_CASE("initializer list and range constructors")
{
auto v0 = std::unordered_map<std::string, int>{
{{"foo", 42}, {"bar", 13}, {"baz", 18}, {"zab", 64}}};
auto v1 = MAP_T<std::string, int>{
{{"foo", 42}, {"bar", 13}, {"baz", 18}, {"zab", 64}}};
auto v2 = MAP_T<std::string, int>{v0.begin(), v0.end()};
CHECK(v1.size() == 4);
CHECK(v1.count(std::string{"foo"}) == 1);
CHECK(v1.at(std::string{"bar"}) == 13);
CHECK(v1 == v2);
}
TEST_CASE("accessor")
{
const auto n = 666u;
auto v = make_test_map(n);
CHECK(v[0] == 0);
CHECK(v[42] == 42);
CHECK(v[665] == 665);
CHECK(v[666] == 0);
CHECK(v[1234] == 0);
}
TEST_CASE("at")
{
const auto n = 666u;
auto v = make_test_map(n);
CHECK(v.at(0) == 0);
CHECK(v.at(42) == 42);
CHECK(v.at(665) == 665);
#ifndef IMMER_NO_EXCEPTIONS
CHECK_THROWS_AS(v.at(666), std::out_of_range);
CHECK_THROWS_AS(v.at(1234), std::out_of_range);
#endif
}
TEST_CASE("find")
{
const auto n = 666u;
auto v = make_test_map(n);
CHECK(*v.find(0) == 0);
CHECK(*v.find(42) == 42);
CHECK(*v.find(665) == 665);
CHECK(v.find(666) == nullptr);
CHECK(v.find(1234) == nullptr);
}
TEST_CASE("equals and setting")
{
const auto n = 666u;
auto v = make_test_map(n);
CHECK(v == v);
CHECK(v != v.insert({1234, 42}));
CHECK(v != v.erase(32));
CHECK(v == v.insert({1234, 42}).erase(1234));
CHECK(v == v.erase(32).insert({32, 32}));
CHECK(v.set(1234, 42) == v.insert({1234, 42}));
CHECK(v.update(1234, [](auto&& x) { return x + 1; }) == v.set(1234, 1));
CHECK(v.update(42, [](auto&& x) { return x + 1; }) == v.set(42, 43));
CHECK(v.update_if_exists(1234, [](auto&& x) { return x + 1; }) == v);
CHECK(v.update_if_exists(42, [](auto&& x) { return x + 1; }) ==
v.set(42, 43));
CHECK(v.update_if_exists(1234, [](auto&& x) { return x + 1; }).identity() ==
v.identity());
CHECK(v.update_if_exists(42, [](auto&& x) { return x + 1; }).identity() !=
v.set(42, 43).identity());
#if IMMER_DEBUG_STATS
std::cout << (v.impl().get_debug_stats() + v.impl().get_debug_stats())
.get_summary();
#endif
}
#if IMMER_DEBUG_STATS
TEST_CASE("debug stats")
{
{
std::cout
<< immer::map<int, int>{}.impl().get_debug_stats().get_summary();
}
{
immer::map<int, int> map;
for (int i = 0; i <= 10; i++) {
map = std::move(map).set(i, i);
}
std::cout << map.impl().get_debug_stats().get_summary();
}
}
#endif
TEST_CASE("iterator")
{
const auto N = 666u;
auto v = make_test_map(N);
SECTION("empty set")
{
auto s = MAP_T<unsigned, unsigned>{};
CHECK(s.begin() == s.end());
}
SECTION("works with range loop")
{
auto seen = std::unordered_set<unsigned>{};
for (const auto& x : v)
CHECK(seen.insert(x.first).second);
CHECK(seen.size() == v.size());
}
SECTION("iterator and collisions")
{
auto vals = make_values_with_collisions(N);
auto s = make_test_map(vals);
auto seen = std::unordered_set<conflictor, hash_conflictor>{};
for (const auto& x : s)
CHECK(seen.insert(x.first).second);
CHECK(seen.size() == s.size());
}
}
TEST_CASE("accumulate")
{
const auto n = 666u;
auto v = make_test_map(n);
auto expected_n = [](auto n) { return n * (n - 1) / 2; };
SECTION("sum collection")
{
auto acc = [](unsigned acc, const std::pair<unsigned, unsigned>& x) {
return acc + x.first + x.second;
};
auto sum = immer::accumulate(v, 0u, acc);
CHECK(sum == 2 * expected_n(v.size()));
}
SECTION("sum collisions")
{
auto vals = make_values_with_collisions(n);
auto s = make_test_map(vals);
auto acc = [](unsigned r, std::pair<conflictor, unsigned> x) {
return r + x.first.v1 + x.first.v2 + x.second;
};
auto sum1 = std::accumulate(vals.begin(), vals.end(), 0u, acc);
auto sum2 = immer::accumulate(s, 0u, acc);
CHECK(sum1 == sum2);
}
}
TEST_CASE("update a lot")
{
auto v = make_test_map(666u);
SECTION("immutable")
{
for (decltype(v.size()) i = 0; i < v.size(); ++i) {
v = v.update(i, [](auto&& x) { return x + 1; });
CHECK(v[i] == i + 1);
}
}
SECTION("move")
{
for (decltype(v.size()) i = 0; i < v.size(); ++i) {
v = std::move(v).update(i, [](auto&& x) { return x + 1; });
CHECK(v[i] == i + 1);
}
}
SECTION("erase")
{
for (decltype(v.size()) i = 0; i < v.size(); ++i) {
v = std::move(v).erase(i);
CHECK(v.count(i) == 0);
}
}
}
TEST_CASE("update_if_exists a lot")
{
auto v = make_test_map(666u);
SECTION("immutable")
{
for (decltype(v.size()) i = 0; i < v.size(); ++i) {
v = v.update_if_exists(i, [](auto&& x) { return x + 1; });
CHECK(v[i] == i + 1);
}
}
SECTION("move")
{
for (decltype(v.size()) i = 0; i < v.size(); ++i) {
v = std::move(v).update_if_exists(i,
[](auto&& x) { return x + 1; });
CHECK(v[i] == i + 1);
}
}
}
#if !IMMER_IS_LIBGC_TEST
TEST_CASE("update boxed move string")
{
constexpr auto N = 666u;
constexpr auto S = 7;
auto s = MAP_T<std::string, immer::box<std::string, memory_policy_t>>{};
SECTION("preserve immutability")
{
auto s0 = s;
auto i0 = 0u;
// insert
for (auto i = 0u; i < N; ++i) {
if (i % S == 0) {
s0 = s;
i0 = i;
}
s = std::move(s).update(std::to_string(i),
[&](auto&&) { return std::to_string(i); });
{
CHECK(s.size() == i + 1);
for (auto j : test_irange(0u, i + 1)) {
CHECK(s.count(std::to_string(j)) == 1);
CHECK(*s.find(std::to_string(j)) == std::to_string(j));
}
for (auto j : test_irange(i + 1u, N))
CHECK(s.count(std::to_string(j)) == 0);
}
{
CHECK(s0.size() == i0);
for (auto j : test_irange(0u, i0)) {
CHECK(s0.count(std::to_string(j)) == 1);
CHECK(*s0.find(std::to_string(j)) == std::to_string(j));
}
for (auto j : test_irange(i0, N))
CHECK(s0.count(std::to_string(j)) == 0);
}
}
// update
for (auto i = 0u; i < N; ++i) {
if (i % S == 0) {
s0 = s;
i0 = i;
}
s = std::move(s).update(std::to_string(i), [&](auto&&) {
return std::to_string(i + 1);
});
{
CHECK(s.size() == N);
for (auto j : test_irange(0u, i + 1))
CHECK(*s.find(std::to_string(j)) == std::to_string(j + 1));
for (auto j : test_irange(i + 1u, N))
CHECK(*s.find(std::to_string(j)) == std::to_string(j));
}
{
CHECK(s0.size() == N);
for (auto j : test_irange(0u, i0))
CHECK(*s0.find(std::to_string(j)) == std::to_string(j + 1));
for (auto j : test_irange(i0, N))
CHECK(*s0.find(std::to_string(j)) == std::to_string(j));
}
}
}
}
#endif
#if !IMMER_IS_LIBGC_TEST
TEST_CASE("update_if_exists boxed move string")
{
constexpr auto N = 666u;
constexpr auto S = 7;
auto s = MAP_T<std::string, immer::box<std::string, memory_policy_t>>{};
SECTION("preserve immutability")
{
auto s0 = s;
auto i0 = 0u;
// insert
for (auto i = 0u; i < N; ++i) {
s = std::move(s).set(std::to_string(i), std::to_string(i));
}
// update
for (auto i = 0u; i < N; ++i) {
if (i % S == 0) {
s0 = s;
i0 = i;
}
s = std::move(s).update_if_exists(std::to_string(i), [&](auto&&) {
return std::to_string(i + 1);
});
{
CHECK(s.size() == N);
for (auto j : test_irange(0u, i + 1))
CHECK(*s.find(std::to_string(j)) == std::to_string(j + 1));
for (auto j : test_irange(i + 1u, N))
CHECK(*s.find(std::to_string(j)) == std::to_string(j));
}
{
CHECK(s0.size() == N);
for (auto j : test_irange(0u, i0))
CHECK(*s0.find(std::to_string(j)) == std::to_string(j + 1));
for (auto j : test_irange(i0, N))
CHECK(*s0.find(std::to_string(j)) == std::to_string(j));
}
}
}
}
#endif
TEST_CASE("exception safety")
{
constexpr auto n = 2666u;
using dadaist_map_t =
typename dadaist_wrapper<MAP_T<unsigned, unsigned>>::type;
using dadaist_conflictor_map_t = typename dadaist_wrapper<
MAP_T<conflictor, unsigned, hash_conflictor>>::type;
SECTION("update")
{
auto v = dadaist_map_t{};
auto d = dadaism{};
for (auto i = 0u; i < n; ++i)
v = std::move(v).set(i, i);
for (auto i = 0u; i < v.size();) {
try {
auto s = d.next();
v = v.update(i, [](auto x) { return x + 1; });
++i;
} catch (dada_error) {
}
for (auto i : test_irange(0u, i))
CHECK(v.at(i) == i + 1);
for (auto i : test_irange(i, n))
CHECK(v.at(i) == i);
}
CHECK(d.happenings > 0);
IMMER_TRACE_E(d.happenings);
}
SECTION("update_if_exists")
{
auto v = dadaist_map_t{};
auto d = dadaism{};
for (auto i = 0u; i < n; ++i)
v = std::move(v).set(i, i);
for (auto i = 0u; i < v.size();) {
try {
auto s = d.next();
v = v.update_if_exists(i, [](auto x) { return x + 1; });
++i;
} catch (dada_error) {
}
for (auto i : test_irange(0u, i))
CHECK(v.at(i) == i + 1);
for (auto i : test_irange(i, n))
CHECK(v.at(i) == i);
}
CHECK(d.happenings > 0);
IMMER_TRACE_E(d.happenings);
}
SECTION("update collisisions")
{
auto vals = make_values_with_collisions(n);
auto v = dadaist_conflictor_map_t{};
auto d = dadaism{};
for (auto i = 0u; i < n; ++i)
v = v.insert(vals[i]);
for (auto i = 0u; i < v.size();) {
try {
auto s = d.next();
v = v.update(vals[i].first, [](auto x) { return x + 1; });
++i;
} catch (dada_error) {
}
for (auto i : test_irange(0u, i))
CHECK(v.at(vals[i].first) == vals[i].second + 1);
for (auto i : test_irange(i, n))
CHECK(v.at(vals[i].first) == vals[i].second);
}
CHECK(d.happenings > 0);
IMMER_TRACE_E(d.happenings);
}
SECTION("update_if_exists collisisions")
{
auto vals = make_values_with_collisions(n);
auto v = dadaist_conflictor_map_t{};
auto d = dadaism{};
for (auto i = 0u; i < n; ++i)
v = v.insert(vals[i]);
for (auto i = 0u; i < v.size();) {
try {
auto s = d.next();
v = v.update_if_exists(vals[i].first,
[](auto x) { return x + 1; });
++i;
} catch (dada_error) {
}
for (auto i : test_irange(0u, i))
CHECK(v.at(vals[i].first) == vals[i].second + 1);
for (auto i : test_irange(i, n))
CHECK(v.at(vals[i].first) == vals[i].second);
}
CHECK(d.happenings > 0);
IMMER_TRACE_E(d.happenings);
}
SECTION("set collisisions")
{
auto vals = make_values_with_collisions(n);
auto v = dadaist_conflictor_map_t{};
auto d = dadaism{};
for (auto i = 0u; i < n; ++i)
v = v.insert(vals[i]);
for (auto i = 0u; i < v.size();) {
try {
auto s = d.next();
auto x = vals[i].second;
v = v.set(vals[i].first, x + 1);
++i;
} catch (dada_error) {
}
for (auto i : test_irange(0u, i))
CHECK(v.at(vals[i].first) == vals[i].second + 1);
for (auto i : test_irange(i, n))
CHECK(v.at(vals[i].first) == vals[i].second);
}
CHECK(d.happenings > 0);
IMMER_TRACE_E(d.happenings);
}
SECTION("set collisisions move")
{
auto vals = make_values_with_collisions(n);
auto v = dadaist_conflictor_map_t{};
auto d = dadaism{};
for (auto i = 0u; i < n; ++i)
v = v.insert(vals[i]);
for (auto i = 0u; i < v.size();) {
try {
auto s = d.next();
auto x = vals[i].second;
v = std::move(v).set(vals[i].first, x + 1);
++i;
} catch (dada_error) {
}
for (auto i : test_irange(0u, i))
CHECK(v.at(vals[i].first) == vals[i].second + 1);
for (auto i : test_irange(i, n))
CHECK(v.at(vals[i].first) == vals[i].second);
}
CHECK(d.happenings > 0);
IMMER_TRACE_E(d.happenings);
}
SECTION("update collisisions move")
{
auto vals = make_values_with_collisions(n);
auto v = dadaist_conflictor_map_t{};
auto d = dadaism{};
for (auto i = 0u; i < n; ++i)
v = std::move(v).insert(vals[i]);
for (auto i = 0u; i < v.size();) {
try {
auto s = d.next();
v = std::move(v).update(vals[i].first,
[](auto x) { return x + 1; });
++i;
} catch (dada_error) {
}
for (auto i : test_irange(0u, i))
CHECK(v.at(vals[i].first) == vals[i].second + 1);
for (auto i : test_irange(i, n))
CHECK(v.at(vals[i].first) == vals[i].second);
}
CHECK(d.happenings > 0);
IMMER_TRACE_E(d.happenings);
}
SECTION("update_if_exists collisisions move")
{
auto vals = make_values_with_collisions(n);
auto v = dadaist_conflictor_map_t{};
auto d = dadaism{};
for (auto i = 0u; i < n; ++i)
v = std::move(v).insert(vals[i]);
for (auto i = 0u; i < v.size();) {
try {
auto s = d.next();
v = std::move(v).update_if_exists(vals[i].first,
[](auto x) { return x + 1; });
++i;
} catch (dada_error) {
}
for (auto i : test_irange(0u, i))
CHECK(v.at(vals[i].first) == vals[i].second + 1);
for (auto i : test_irange(i, n))
CHECK(v.at(vals[i].first) == vals[i].second);
}
CHECK(d.happenings > 0);
IMMER_TRACE_E(d.happenings);
}
SECTION("erase collisisions move")
{
auto vals = make_values_with_collisions(n);
auto v = dadaist_conflictor_map_t{};
auto d = dadaism{};
for (auto i = 0u; i < n; ++i)
v = std::move(v).insert(vals[i]);
for (auto i = 0u; i < v.size();) {
try {
// auto s = d.next();
v = std::move(v).erase(vals[i].first);
++i;
} catch (dada_error) {
}
for (auto i : test_irange(0u, i))
CHECK(v.count(vals[i].first) == 0);
for (auto i : test_irange(i, n))
CHECK(v.at(vals[i].first) == vals[i].second);
}
CHECK(d.happenings == 0);
IMMER_TRACE_E(d.happenings);
}
}
namespace {
struct KeyType
{
explicit KeyType(unsigned v)
: value(v)
{
}
unsigned value;
};
struct LookupType
{
explicit LookupType(unsigned v)
: value(v)
{
}
unsigned value;
};
struct TransparentHash
{
using hash_type = std::hash<unsigned>;
using is_transparent = void;
size_t operator()(KeyType const& k) const { return hash_type{}(k.value); }
size_t operator()(LookupType const& k) const
{
return hash_type{}(k.value);
}
};
bool operator==(KeyType const& k, KeyType const& l)
{
return k.value == l.value;
}
bool operator==(KeyType const& k, LookupType const& l)
{
return k.value == l.value;
}
} // namespace
TEST_CASE("lookup with transparent hash")
{
SECTION("default")
{
auto m = MAP_T<KeyType, int, TransparentHash, std::equal_to<>>{};
m = m.insert({KeyType{1}, 12});
auto const& v = m.at(LookupType{1});
CHECK(v == 12);
}
}
namespace {
class KElem
{
public:
KElem(int* elem) { this->elem = elem; }
bool operator==(const KElem& other) const
{
return this->elem == other.elem;
}
bool operator!=(const KElem& other) const { return !(*this == other); }
int* elem;
};
struct HashBlock
{
size_t operator()(const KElem& block) const noexcept
{
return (uintptr_t) block.elem & 0xffffffff00000000;
}
};
using map = immer::map<KElem, KElem, HashBlock, std::equal_to<KElem>>;
TEST_CASE("issue 134")
{
int a[100];
map m;
for (int i = 0; i < 100; i++) {
m = m.set(KElem(a + i), KElem(a + i));
}
}
} // namespace
void test_diff(unsigned old_num,
unsigned add_num,
unsigned remove_num,
unsigned change_num)
{
auto values = make_values_with_collisions(old_num + add_num);
std::vector<std::pair<conflictor, unsigned>> initial_values(
values.begin(), values.begin() + old_num);
std::vector<std::pair<conflictor, unsigned>> new_values(
values.begin() + old_num, values.end());
auto map = make_test_map(initial_values);
std::vector<conflictor> old_keys;
for (auto const& val : map)
old_keys.push_back(val.first);
auto first_snapshot = map;
CHECK(old_num == first_snapshot.size());
// remove
auto shuffle = old_keys;
std::random_device rd{};
auto g = std::mt19937{rd()};
std::shuffle(shuffle.begin(), shuffle.end(), g);
std::vector<conflictor> remove_keys(shuffle.begin(),
shuffle.begin() + remove_num);
std::vector<conflictor> rest_keys(shuffle.begin() + remove_num,
shuffle.end());
using key_set = std::unordered_set<conflictor, hash_conflictor>;
key_set removed_keys(remove_keys.begin(), remove_keys.end());
for (auto const& key : remove_keys)
map = map.erase(key);
CHECK(old_num - remove_num == map.size());
// add
key_set added_keys;
for (auto const& data : new_values) {
map = map.set(data.first, data.second);
added_keys.insert(data.first);
}
// change
key_set changed_keys;
for (auto i = 0u; i < change_num; i++) {
auto key = rest_keys[i];
map = map.update(key, [](auto val) { return ++val; });
changed_keys.insert(key);
}
diff(
first_snapshot,
map,
[&](auto const& data) { REQUIRE(added_keys.erase(data.first) > 0); },
[&](auto const& data) { REQUIRE(removed_keys.erase(data.first) > 0); },
[&](auto const& old_data, auto const& new_data) {
(void) old_data;
REQUIRE(changed_keys.erase(new_data.first) > 0);
});
CHECK(added_keys.empty());
CHECK(changed_keys.empty());
CHECK(removed_keys.empty());
}
TEST_CASE("diff")
{
test_diff(16, 10, 10, 3);
test_diff(100, 10, 10, 10);
test_diff(1500, 10, 1000, 100);
test_diff(16, 1500, 10, 3);
test_diff(100, 0, 0, 50);
}
|