File: generic.ipp

package info (click to toggle)
immer 0.8.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,132 kB
  • sloc: cpp: 27,932; python: 534; makefile: 227; lisp: 175; sh: 114; javascript: 64
file content (104 lines) | stat: -rw-r--r-- 2,498 bytes parent folder | download
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
//
// 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
//

#include "test/util.hpp"

#include <catch2/catch.hpp>

#ifndef SET_T
#error "define the set template to use in SET_T"
#endif

#ifndef SET_TRANSIENT_T
#error "define the set template to use in SET_TRANSIENT_T"
#endif

IMMER_RANGES_CHECK(std::ranges::forward_range<SET_T<std::string>>);
IMMER_RANGES_CHECK(std::ranges::forward_range<SET_TRANSIENT_T<std::string>>);

TEST_CASE("instantiate")
{
    auto t = SET_TRANSIENT_T<int>{};
    auto m = SET_T<int>{};
    CHECK(t.persistent() == m);
    CHECK(t.persistent() == m.transient().persistent());
}

TEST_CASE("access")
{
    auto m = SET_T<int>{12, 42};
    auto t = m.transient();
    CHECK(t.size() == 2);
    CHECK(t.count(42) == 1);
    CHECK(*t.find(12) == 12);
    CHECK(std::accumulate(t.begin(), t.end(), 0) == 54);
}

TEST_CASE("insert")
{
    auto t = SET_TRANSIENT_T<int>{};

    t.insert(42);
    CHECK(*t.find(42) == 42);
    CHECK(t.size() == 1);

    t.insert(13);
    CHECK(*t.find(13) == 13);
    CHECK(t.size() == 2);

    t.insert(42);
    CHECK(*t.find(42) == 42);
    CHECK(t.size() == 2);
}

TEST_CASE("erase")
{
    auto t = SET_T<int>{12, 42}.transient();

    t.erase(42);
    CHECK(t.find(42) == nullptr);
    CHECK(t.count(42) == 0);
    CHECK(t.find(12) != nullptr);
    CHECK(t.count(12) == 1);
    CHECK(t.size() == 1);
}

TEST_CASE("insert erase many")
{
    auto t = SET_T<int>{}.transient();
    auto n = 1000;
    for (auto i = 0; i < n; ++i) {
        t.insert(i);
        CHECK(t.find(i) != nullptr);
        CHECK(t.size() == static_cast<std::size_t>(i + 1));
    }
    for (auto i = 0; i < n; ++i) {
        t.erase(i);
        CHECK(t.find(i) == nullptr);
        CHECK(t.size() == static_cast<std::size_t>(n - i - 1));
    }
}

TEST_CASE("erase many from non transient")
{
    const auto n = 10000;
    auto t       = [] {
        auto t = SET_T<int>{};
        for (auto i = 0; i < n; ++i) {
            t = t.insert(i);
            CHECK(t.find(i) != nullptr);
            CHECK(t.size() == static_cast<std::size_t>(i + 1));
        }
        return t.transient();
    }();
    for (auto i = 0; i < n; ++i) {
        t.erase(i);
        CHECK(t.find(i) == nullptr);
        CHECK(t.size() == static_cast<std::size_t>(n - i - 1));
    }
}