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/*
* Vool - Unit tests for vec_map
*
* Copyright (c) 2016 Lukas Bergdoll - www.lukas-bergdoll.net
*
* This code is licensed under the Apache License 2.0 (https://opensource.org/licenses/Apache-2.0)
*/
#include "AllTests.h"
#include <Vecmap.h>
#include <vector>
#include <exception>
namespace vool
{
namespace tests
{
struct BigData
{
using array_t = int;
static const size_t size = (sizeof(size_t) * 6) / sizeof(array_t);
array_t sampleArray[size];
};
void test_Vecmap()
{
// configuration
using K = size_t;
using V = BigData;
size_t containerSize = static_cast<K>(1e4);
static_assert(std::is_same<
vool::vec_map<K, V>::bucket_t,
vool::vec_map_util::ref_bucket<K, V>
>::value, "vec_map bucket selection fail!");
V value; // custom value type
value.sampleArray[0] = 33;
// size construction
vool::vec_map<K, V> vecMap;
vecMap.reserve(containerSize);
for (size_t key = 0; key < containerSize; ++key)
vecMap.insert(key, value);
if (vecMap.size() != containerSize)
throw std::exception("key value insert error");
if (vecMap[containerSize - 1].sampleArray[0] != value.sampleArray[0])
throw std::exception("key value insert error");
// test if returned value is actually what we looked for
{
vool::vec_map<K, K> vMap({ { 0, 0 },{ 1, 1 },{ 3, 3 } });
bool access = false;
try
{
auto res = vMap.at(2);
static_cast<void>(res);
access = true;
}
catch (std::exception& e) { static_cast<void>(e); }; // this should fail
if (access)
throw std::exception("could access value using at() with wrong key");
}
// begin() and end() test and bucket range insert test
{
V::array_t rangeLastItem = 5;
{
vool::vec_map<K, V> noReserve; // default construction
for (size_t key = containerSize; key < containerSize * 1.5; ++key)
noReserve.insert(key, value); // key value insertion
noReserve[0].sampleArray[V::size - 1] = rangeLastItem;
vecMap.insert(noReserve.begin(), noReserve.end());
}
if (vecMap.size() != containerSize * 1.5)
throw std::exception("bucket range insert size error");
if (vecMap[containerSize].sampleArray[V::size - 1] != rangeLastItem)
throw std::exception("bucket range insert copy error");
}
{
std::vector<std::pair<K, V>> keyAndValueVec;
keyAndValueVec.reserve(containerSize);
for (size_t key = 0; key < containerSize; ++key)
keyAndValueVec.push_back(std::make_pair(key, value));
vool::vec_map<K, V> range;
std::for_each(keyAndValueVec.begin(), keyAndValueVec.end(),
[&range](const auto& kvPair)
{ range.insert(kvPair.first, kvPair.second); }
);
if (range.size() != keyAndValueVec.size())
throw std::exception("key value range insert error");
std::vector<K> keyVec;
keyVec.reserve(containerSize);
for (const auto& key : keyAndValueVec)
keyVec.push_back(key.first);
range.erase(keyVec.begin(), keyVec.end()); // key range erase
if (range.size() != 0)
throw std::exception("key value range erase error");
}
// forced relocation
{
auto tmp = vecMap; // copy assignment
vecMap = std::move(tmp); // move assignment
auto copyConstructed(vecMap); // copy construction
auto moveCopy = vecMap;
auto moveConstructed(std::move(moveCopy)); // move construction
auto vecMapCopy = vecMap; // copy construct assignment
auto itStart = vecMapCopy.begin();
std::advance(itStart, (vecMapCopy.size() / 4));
auto itEnd = vecMapCopy.begin();
std::advance(itEnd, (vecMapCopy.size() / 2));
vecMapCopy.erase(itStart, itEnd); // bucket range erase
if (vecMapCopy.at(0).sampleArray[0] != value.sampleArray[0])
throw std::exception("bucket erase and or at() error");
if (!vecMapCopy.is_sorted())
throw std::exception("after any kind of read, vec_map should be sorted");
}
// capacity tests
{
auto frontKey = vecMap.begin()->key();
vecMap.erase(frontKey); // key erase
vecMap.insert(frontKey, value);
if (vecMap.is_sorted())
throw std::exception("container should be unsorted after key insert");
auto vecMapSize = vecMap.size();
vecMap.reserve(vecMapSize * 2);
vecMap.shrink_to_fit(); // should reduce capacity down to size
if (vecMap.capacity() != vecMapSize)
throw std::exception("shrink_to_fit error");
auto vecMapCopy = vecMap;
vecMapCopy.clear(); // calls internal vector clear()
if (vecMapCopy.size() != 0)
throw std::exception("clear error");
}
}
}
}