aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/stllike/hashtable_test.cpp
blob: 972ae43b1fa16cc3a73508d10db0907ee1ca73d0 (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
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// Unit tests for hashtable.

#include <vespa/vespalib/stllike/hashtable.hpp>
#include <vespa/vespalib/stllike/hash_fun.h>
#include <vespa/vespalib/stllike/identity.h>
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/stllike/hash_map.hpp>
#include <memory>
#include <vector>

using vespalib::hashtable;
using std::vector;

using namespace vespalib;

namespace {

template<typename T>
struct Dereference {
    T &operator()(std::unique_ptr<T>& p) const { return *p; }
    const T& operator()(const std::unique_ptr<T>& p) const { return *p; }
};

template<typename K> using up_hashtable =
    hashtable<K, std::unique_ptr<K>,
              vespalib::hash<K>, std::equal_to<K>, Dereference<K>>;

TEST("require that hashtable can store unique_ptrs") {
    up_hashtable<int> table(100);
    using UP = std::unique_ptr<int>;
    table.insert(UP(new int(42)));
    auto it = table.find(42);
    EXPECT_EQUAL(42, **it);

    UP u = std::move(*it);  // This changes the key. Don't do this.
    EXPECT_EQUAL(42, *u);

    // it = table.find(42);  // This will crash, since the key is removed.
}


template <typename K, typename V> using Entry =
    std::pair<K, std::unique_ptr<V>>;
typedef hashtable<int, Entry<int, int>,
                  vespalib::hash<int>, std::equal_to<int>,
                  Select1st<Entry<int, int>>> PairHashtable;

TEST("require that hashtable can store pairs of <key, unique_ptr to value>") {
    PairHashtable table(100);
    table.insert(make_pair(42, std::unique_ptr<int>(new int(84))));
    PairHashtable::iterator it = table.find(42);
    EXPECT_EQUAL(84, *it->second);
    auto it2 = table.find(42);
    EXPECT_EQUAL(84, *it2->second);  // find is not destructive.

    std::unique_ptr<int> up = std::move(it->second);
    it2 = table.find(42);
    EXPECT_FALSE(it2->second.get());  // value has been moved out.
}

template<typename K> using set_hashtable =
    hashtable<K, K, vespalib::hash<K>, std::equal_to<K>, Identity>;

TEST("require that hashtable<int> can be copied") {
    set_hashtable<int> table(100);
    table.insert(42);
    set_hashtable<int> table2(table);
    EXPECT_EQUAL(42, *table2.find(42));
}

TEST("require that you can insert duplicates") {
    using Pair = std::pair<int, vespalib::string>;
    using Map = hashtable<int, Pair, vespalib::hash<int>, std::equal_to<int>, Select1st<Pair>>;

    Map m(1);
    EXPECT_EQUAL(0u, m.size());
    EXPECT_EQUAL(8u, m.capacity());
    auto res = m.insert(Pair(1, "1"));
    EXPECT_TRUE(res.second);
    EXPECT_EQUAL(1u, m.size());
    EXPECT_EQUAL(8u, m.capacity());
    res = m.insert(Pair(1, "1.2"));
    EXPECT_FALSE(res.second);
    auto found = m.find(1);
    ASSERT_TRUE(found != m.end());
    EXPECT_EQUAL(found->second, "1");

    m.force_insert(Pair(1, "1.2"));
    EXPECT_EQUAL(2u, m.size());
    EXPECT_EQUAL(8u, m.capacity());
    m.force_insert(Pair(1, "1.3"));
    EXPECT_EQUAL(3u, m.size());
    EXPECT_EQUAL(16u, m.capacity()); // Resize has been conducted
    Pair expected[3] = {{1,"1"},{1,"1.2"},{1,"1.3"}};
    size_t index(0);
    for (const auto & e : m) {
        EXPECT_EQUAL(expected[index].first, e.first);
        EXPECT_EQUAL(expected[index].second, e.second);
        index++;
    }
    found = m.find(1);
    ASSERT_TRUE(found != m.end());
    EXPECT_EQUAL(found->second, "1");

    m.erase(1);
    EXPECT_EQUAL(2u, m.size());
    EXPECT_EQUAL(16u, m.capacity());
    found = m.find(1);
    ASSERT_TRUE(found != m.end());
    EXPECT_EQUAL(found->second, "1.3");

    m.erase(1);
    EXPECT_EQUAL(1u, m.size());
    EXPECT_EQUAL(16u, m.capacity());
    found = m.find(1);
    ASSERT_TRUE(found != m.end());
    EXPECT_EQUAL(found->second, "1.2");
}

template<typename To, typename Vector>
struct FirstInVector {
    To &operator()(Vector& v) const { return v[0]; }
    const To& operator()(const Vector& v) const { return v[0]; }
};

TEST("require that hashtable<vector<int>> can be copied") {
    typedef hashtable<int, vector<int>, vespalib::hash<int>,
        std::equal_to<int>, FirstInVector<int, vector<int>>> VectorHashtable;
    VectorHashtable table(100);
    table.insert(std::vector<int>{2, 4, 6});
    VectorHashtable table2(table);
    EXPECT_EQUAL(6, (*table2.find(2))[2]);
    EXPECT_EQUAL(6, (*table.find(2))[2]);
}

/**
 * Test to profile destruction and recreation of hash map.
 * It revealed some unexpected behaviour. Results with 10k iterations on 2018 macbook pro 2.6 Ghz i7
 * 1 - previous - 14.7s hash_node() : _node(), _next(invalid) {}
 * 2 - test     -  6.6s hash_node() : _next(invalid) { memset(_node, 0, sizeof(node)); }
 * 3 - current  -  2.3s hash_node() : _next(invalid) {}
 */
TEST("benchmark hash table reconstruction with POD objects") {
    vespalib::hash_map<uint32_t, uint32_t> m(1000000);
    constexpr size_t NUM_ITER = 10; // Set to 1k-10k to get measurable numbers 10k ~= 2.3s
    for (size_t i(0); i < NUM_ITER; i++) {
        m[46] = 17;
        EXPECT_FALSE(m.empty());
        EXPECT_EQUAL(1u, m.size());
        EXPECT_EQUAL(1048576u, m.capacity());
        m.clear();
        EXPECT_TRUE(m.empty());
        EXPECT_EQUAL(1048576u, m.capacity());
    }
}

class NonPOD {
public:
    NonPOD() noexcept
        : _v(rand())
    {
        construction_count++;
    }
    NonPOD(NonPOD && rhs) noexcept { _v = rhs._v; rhs._v = -1; }
    NonPOD & operator =(NonPOD && rhs) noexcept { _v = rhs._v; rhs._v = -1; return *this; }
    NonPOD(const NonPOD &) = delete;
    NonPOD & operator =(const NonPOD &) = delete;
    ~NonPOD() {
        if (_v != -1) {
            destruction_count++;
        }
    }
    int32_t _v;
    static size_t construction_count;
    static size_t destruction_count;
};

size_t NonPOD::construction_count = 0;
size_t NonPOD::destruction_count = 0;

/**
 * Performance is identical for NonPOD objects as with POD object.
 * Object are are only constructed on insert, and destructed on erase/clear.
 */
TEST("benchmark hash table reconstruction with non POD objects") {
    vespalib::hash_map<uint32_t, NonPOD> m(1000000);
    constexpr size_t NUM_ITER = 10; // Set to 1k-10k to get measurable numbers 10k ~= 2.3s
    NonPOD::construction_count = 0;
    NonPOD::destruction_count = 0;
    for (size_t i(0); i < NUM_ITER; i++) {
        EXPECT_EQUAL(i, NonPOD::construction_count);
        EXPECT_EQUAL(i, NonPOD::destruction_count);
        m.insert(std::make_pair(46, NonPOD()));
        EXPECT_EQUAL(i+1, NonPOD::construction_count);
        EXPECT_EQUAL(i, NonPOD::destruction_count);
        EXPECT_FALSE(m.empty());
        EXPECT_EQUAL(1u, m.size());
        EXPECT_EQUAL(1048576u, m.capacity());
        m.clear();
        EXPECT_EQUAL(i+1, NonPOD::construction_count);
        EXPECT_EQUAL(i+1, NonPOD::destruction_count);
        EXPECT_TRUE(m.empty());
        EXPECT_EQUAL(1048576u, m.capacity());
    }
    EXPECT_EQUAL(NUM_ITER, NonPOD::construction_count);
    EXPECT_EQUAL(NUM_ITER, NonPOD::destruction_count);
}

}  // namespace

TEST_MAIN() { TEST_RUN_ALL(); }