aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/containers/flat_hash/lib/ut/iterator_ut.cpp
blob: 0b77bf043f3807c89c3edf60c907c2d9d3f35e23 (plain) (blame)
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
#include <library/cpp/containers/flat_hash/lib/iterator.h>
#include <library/cpp/containers/flat_hash/lib/containers.h>

#include <library/cpp/testing/unittest/registar.h>

#include <util/random/random.h>
#include <util/generic/algorithm.h>

using namespace NFlatHash;

namespace {
    constexpr size_t INIT_SIZE = 128;

    template <class Container>
    void SmokingTest(Container& cont) {
        using value_type = typename Container::value_type;
        using iterator = TIterator<Container, value_type>;
        using size_type = typename Container::size_type;

        iterator f(&cont), l(&cont, cont.Size());
        UNIT_ASSERT_EQUAL(f, l);
        UNIT_ASSERT_EQUAL((size_type)std::distance(f, l), cont.Taken());

        TVector<std::pair<size_type, value_type>> toAdd{
            { 0, (int)RandomNumber<size_type>(INIT_SIZE) },
            { 1 + RandomNumber<size_type>(INIT_SIZE - 2), (int)RandomNumber<size_type>(INIT_SIZE) },
            { INIT_SIZE - 1, (int)RandomNumber<size_type>(INIT_SIZE) }
        };

        for (const auto& p : toAdd) {
            UNIT_ASSERT(cont.IsEmpty(p.first));
            cont.InitNode(p.first, p.second);
        }
        UNIT_ASSERT_EQUAL(cont.Size(), INIT_SIZE);
        f = iterator{ &cont };
        l = iterator{ &cont, INIT_SIZE };
        UNIT_ASSERT_UNEQUAL(f, l);
        UNIT_ASSERT_EQUAL((size_type)std::distance(f, l), cont.Taken());

        TVector<value_type> added(f, l);
        UNIT_ASSERT(::Equal(toAdd.begin(), toAdd.end(), added.begin(), [](const auto& p, auto v) {
            return p.second == v;
        }));
    }

    template <class Container>
    void ConstTest(Container& cont) {
        using value_type = typename Container::value_type;
        using iterator = TIterator<Container, value_type>;
        using const_iterator = TIterator<const Container, const value_type>;

        iterator it{ &cont, INIT_SIZE / 2 };
        const_iterator cit1{ it };
        const_iterator cit2{ &cont, INIT_SIZE / 2 };

        UNIT_ASSERT_EQUAL(cit1, cit2);

        static_assert(std::is_same<decltype(*it), value_type&>::value);
        static_assert(std::is_same<decltype(*cit1), const value_type&>::value);
    }
}

Y_UNIT_TEST_SUITE(TIteratorTest) {
    Y_UNIT_TEST(SmokingTest) {
        {
            TFlatContainer<int> cont(INIT_SIZE);
            SmokingTest(cont);
        }
        {
            TDenseContainer<int, NSet::TStaticValueMarker<-1>> cont(INIT_SIZE);
            SmokingTest(cont);
        }
    }

    Y_UNIT_TEST(ConstTest) {
        {
            TFlatContainer<int> cont(INIT_SIZE);
            ConstTest(cont);
        }
        {
            TDenseContainer<int, NSet::TStaticValueMarker<-1>> cont(INIT_SIZE);
            ConstTest(cont);
        }
    }
}