-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathattribute_store.test.cpp
More file actions
174 lines (142 loc) · 5.03 KB
/
Copy pathattribute_store.test.cpp
File metadata and controls
174 lines (142 loc) · 5.03 KB
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
#include <iostream>
#include <algorithm>
#include <atomic>
#include <thread>
#include <vector>
#include "external/minunit.h"
#include "attribute_store.h"
MU_TEST(test_attribute_store) {
AttributeStore store;
store.reset();
mu_check(store.size() == 0);
AttributeSet s1;
store.addAttribute(s1, "str1", std::string("someval"), 0);
store.addAttribute(s1, "str2", std::string("a very long string"), 14);
store.addAttribute(s1, "bool1", false, 0);
store.addAttribute(s1, "bool2", true, 0);
store.addAttribute(s1, "double1", (double)42.0, 4);
store.addAttribute(s1, "int1", 43, 8);
const auto s1Index = store.add(s1);
mu_check(store.size() == 1);
const auto s1Pairs = store.getUnsafe(s1Index);
mu_check(s1Pairs.size() == 6);
const auto str1 = std::find_if(s1Pairs.begin(), s1Pairs.end(), [&store](auto ap) {
return ap->keyIndex == store.keyStore.key2index("str1");
});
mu_check(str1 != s1Pairs.end());
mu_check((*str1)->hasStringValue());
mu_check((*str1)->stringValue() == "someval");
const auto str2 = std::find_if(s1Pairs.begin(), s1Pairs.end(), [&store](auto ap) {
return ap->keyIndex == store.keyStore.key2index("str2");
});
mu_check(str2 != s1Pairs.end());
mu_check((*str2)->hasStringValue());
mu_check((*str2)->stringValue() == "a very long string");
mu_check((*str2)->minzoom == 14);
const auto bool1 = std::find_if(s1Pairs.begin(), s1Pairs.end(), [&store](auto ap) {
return ap->keyIndex == store.keyStore.key2index("bool1");
});
mu_check(bool1 != s1Pairs.end());
mu_check((*bool1)->hasBoolValue());
mu_check((*bool1)->boolValue() == false);
const auto bool2 = std::find_if(s1Pairs.begin(), s1Pairs.end(), [&store](auto ap) {
return ap->keyIndex == store.keyStore.key2index("bool2");
});
mu_check(bool2 != s1Pairs.end());
mu_check((*bool2)->hasBoolValue());
mu_check((*bool2)->boolValue() == true);
const auto double1 = std::find_if(s1Pairs.begin(), s1Pairs.end(), [&store](auto ap) {
return ap->keyIndex == store.keyStore.key2index("double1");
});
mu_check(double1 != s1Pairs.end());
mu_check((*double1)->hasFloatValue());
mu_check((*double1)->floatValue() == 42);
mu_check((*double1)->minzoom == 4);
const auto int1 = std::find_if(s1Pairs.begin(), s1Pairs.end(), [&store](auto ap) {
return ap->keyIndex == store.keyStore.key2index("int1");
});
mu_check(int1 != s1Pairs.end());
mu_check((*int1)->hasIntValue());
mu_check((*int1)->intValue() == 43);
mu_check((*int1)->minzoom == 8);
}
MU_TEST(test_attribute_store_reuses) {
AttributeStore store;
store.reset();
mu_check(store.size() == 0);
{
AttributeSet s1a;
store.addAttribute(s1a, "str1", std::string("someval"), 0);
const auto s1aIndex = store.add(s1a);
AttributeSet s1b;
store.addAttribute(s1b, "str1", std::string("someval"), 0);
const auto s1bIndex = store.add(s1b);
mu_check(s1aIndex == s1bIndex);
}
{
AttributeSet s1a;
store.addAttribute(s1a, "str1", std::string("this is a very long string"), 0);
const auto s1aIndex = store.add(s1a);
AttributeSet s1b;
store.addAttribute(s1b, "str1", std::string("this is a very long string"), 0);
const auto s1bIndex = store.add(s1b);
mu_check(s1aIndex == s1bIndex);
}
}
MU_TEST(test_attribute_store_capacity) {
// We support a maximum of 511 attribute name, so confirm that we can roundtrip
// this value.
AttributePair pair(511, true, 0);
mu_check(pair.keyIndex == 511);
// Confirm that the attribute store will throw if we try to add more than 511 keys.
AttributeKeyStore keys;
for (int i = 1; i <= 511; i++) {
const uint16_t keyIndex = keys.key2index("key" + std::to_string(i));
mu_check(keyIndex == i);
}
// Trying to add a 512th key should throw
bool caughtException = false;
try {
keys.key2index("key512");
} catch (std::out_of_range) {
caughtException = true;
}
mu_check(caughtException == true);
}
MU_TEST(test_attribute_key_store_threaded) {
AttributeKeyStore keys;
const int keyCount = 128;
const int threadCount = 8;
const int iterations = 100;
std::atomic<bool> start(false);
std::atomic<bool> failed(false);
std::vector<std::thread> threads;
for (int thread = 0; thread < threadCount; thread++) {
threads.emplace_back([&, thread]() {
while (!start.load(std::memory_order_acquire)) {}
for (int i = 0; i < iterations * keyCount; i++) {
const int keyNum = (i + thread * 17) % keyCount;
const std::string key = "key" + std::to_string(keyNum);
const uint16_t firstIndex = keys.key2index(key);
const uint16_t secondIndex = keys.key2index(key);
if (firstIndex == 0 || firstIndex > keyCount || firstIndex != secondIndex)
failed.store(true, std::memory_order_release);
}
});
}
start.store(true, std::memory_order_release);
for (std::thread& thread : threads)
thread.join();
mu_check(failed.load(std::memory_order_acquire) == false);
}
MU_TEST_SUITE(test_suite_attribute_store) {
MU_RUN_TEST(test_attribute_store);
MU_RUN_TEST(test_attribute_store_reuses);
MU_RUN_TEST(test_attribute_store_capacity);
MU_RUN_TEST(test_attribute_key_store_threaded);
}
int main() {
MU_RUN_SUITE(test_suite_attribute_store);
MU_REPORT();
return MU_EXIT_CODE;
}