1 | /* |
---|
2 | Copyright (c) 2006-2018 Elmar Pruesse <elmar.pruesse@ucdenver.edu> |
---|
3 | |
---|
4 | This file is part of SINA. |
---|
5 | SINA is free software: you can redistribute it and/or modify it under |
---|
6 | the terms of the GNU General Public License as published by the Free |
---|
7 | Software Foundation, either version 3 of the License, or (at your |
---|
8 | option) any later version. |
---|
9 | |
---|
10 | SINA is distributed in the hope that it will be useful, but WITHOUT ANY |
---|
11 | WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
12 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
13 | for more details. |
---|
14 | |
---|
15 | You should have received a copy of the GNU General Public License |
---|
16 | along with SINA. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | |
---|
18 | Additional permission under GNU GPL version 3 section 7 |
---|
19 | |
---|
20 | If you modify SINA, or any covered work, by linking or combining it |
---|
21 | with components of ARB (or a modified version of that software), |
---|
22 | containing parts covered by the terms of the |
---|
23 | ARB-public-library-license, the licensors of SINA grant you additional |
---|
24 | permission to convey the resulting work. Corresponding Source for a |
---|
25 | non-source form of such a combination shall include the source code |
---|
26 | for the parts of ARB used as well as that of the covered work. |
---|
27 | */ |
---|
28 | |
---|
29 | #include "../idset.h" |
---|
30 | |
---|
31 | #define BOOST_TEST_MODULE bitmap |
---|
32 | #include <boost/test/unit_test.hpp> |
---|
33 | #include <boost/test/data/test_case.hpp> |
---|
34 | namespace bdata = boost::unit_test::data; |
---|
35 | |
---|
36 | #include <random> |
---|
37 | #include <set> |
---|
38 | |
---|
39 | BOOST_TEST_SPECIALIZED_COLLECTION_COMPARE(std::vector<idset::inc_t>); |
---|
40 | |
---|
41 | BOOST_AUTO_TEST_SUITE(bitmap_test); |
---|
42 | |
---|
43 | |
---|
44 | struct test_set { |
---|
45 | test_set() = default; |
---|
46 | void init (unsigned int size, int fill, int seed) { |
---|
47 | // fill data with random sequence of monotonically rising numbers |
---|
48 | n = size * fill / 100; |
---|
49 | std::mt19937 engine(seed); |
---|
50 | std::uniform_real_distribution<> dist; |
---|
51 | std::set<unsigned int> d; |
---|
52 | for (unsigned int i=0; i<n ; i++) { |
---|
53 | unsigned int val = dist(engine) * (size-1); |
---|
54 | while (not d.insert(val).second) |
---|
55 | { |
---|
56 | val++; |
---|
57 | if (val >= size) { |
---|
58 | val=0; |
---|
59 | } |
---|
60 | } |
---|
61 | } |
---|
62 | data.resize(n); |
---|
63 | std::copy(d.begin(), d.end(), data.begin()); |
---|
64 | std::sort(data.begin(), data.end()); |
---|
65 | |
---|
66 | // fill expected_count |
---|
67 | expected_counts.resize(size, 0); |
---|
68 | for (auto i : data) { |
---|
69 | expected_counts[i]++; |
---|
70 | } |
---|
71 | } |
---|
72 | std::vector<unsigned int> data; |
---|
73 | idset::inc_t expected_counts; |
---|
74 | unsigned int n; |
---|
75 | }; |
---|
76 | |
---|
77 | #if 1 |
---|
78 | int map_sizes[] = {0, 255,256,257, 10000}; |
---|
79 | int map_fill[] = {0, 10, 50, 100}; |
---|
80 | int map_seed[] = {132456, 54321, 242424}; |
---|
81 | #else |
---|
82 | int map_sizes[] = {10}; |
---|
83 | int map_fill[] = {0, 50, 100}; |
---|
84 | int map_seed[] = {132456}; |
---|
85 | #endif |
---|
86 | idset* map_type[] = {new bitmap(0), new imap_abs(0), new vlimap_abs(0), new vlimap(0) }; |
---|
87 | |
---|
88 | |
---|
89 | BOOST_DATA_TEST_CASE_F(test_set, |
---|
90 | bitmap_test, |
---|
91 | bdata::make(map_sizes) * |
---|
92 | bdata::make(map_fill) * |
---|
93 | bdata::make(map_seed), |
---|
94 | map_size, map_fill, map_seed) { |
---|
95 | // init random set |
---|
96 | init(map_size, map_fill, map_seed); |
---|
97 | |
---|
98 | // init bitmap |
---|
99 | bitmap b(map_size); |
---|
100 | for (auto i : data) { |
---|
101 | b.set(i); |
---|
102 | } |
---|
103 | |
---|
104 | // check get() |
---|
105 | unsigned int matching_set = 0; |
---|
106 | for (auto i: data) { |
---|
107 | if (b.get(i)) { |
---|
108 | matching_set++; |
---|
109 | } |
---|
110 | } |
---|
111 | BOOST_CHECK_EQUAL(n, matching_set); |
---|
112 | |
---|
113 | // check count() |
---|
114 | BOOST_CHECK_EQUAL(n, b.count()); |
---|
115 | |
---|
116 | // check increment() |
---|
117 | idset::inc_t count(map_size, 0); |
---|
118 | b.increment(count); |
---|
119 | matching_set = 0; |
---|
120 | for (auto i: data) { |
---|
121 | matching_set += count[i]; |
---|
122 | } |
---|
123 | BOOST_CHECK_EQUAL(n, matching_set); |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | BOOST_DATA_TEST_CASE_F(test_set, |
---|
128 | idset_test, |
---|
129 | bdata::make(map_sizes) * |
---|
130 | bdata::make(map_fill) * |
---|
131 | bdata::make(map_seed) * |
---|
132 | bdata::make(map_type), |
---|
133 | map_size, map_fill, map_seed, type) { |
---|
134 | // init random set |
---|
135 | init(map_size, map_fill, map_seed); |
---|
136 | |
---|
137 | // init bitmap |
---|
138 | idset* b = type->make_new(map_size); |
---|
139 | BOOST_REQUIRE_EQUAL(b->size(), 0u); |
---|
140 | for (auto i : data) { |
---|
141 | b->push_back(i); |
---|
142 | } |
---|
143 | BOOST_CHECK_EQUAL(data.size(), b->size()); |
---|
144 | |
---|
145 | // check increment() |
---|
146 | idset::inc_t count(map_size, 0); |
---|
147 | b->increment(count); |
---|
148 | unsigned int matching_set = 0; |
---|
149 | for (auto i: data) { |
---|
150 | matching_set += count[i]; |
---|
151 | } |
---|
152 | BOOST_CHECK_EQUAL(n, matching_set); |
---|
153 | if (n != matching_set) { |
---|
154 | for (auto i: data) { |
---|
155 | BOOST_CHECK_EQUAL(count[i]*i, i); |
---|
156 | } |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | BOOST_DATA_TEST_CASE_F(test_set, |
---|
162 | vlipmap_abs_test, |
---|
163 | bdata::make(map_sizes) * |
---|
164 | bdata::make(map_fill) * |
---|
165 | bdata::make(map_seed), |
---|
166 | map_size, map_fill, map_seed) { |
---|
167 | // init random set |
---|
168 | init(map_size, map_fill, map_seed); |
---|
169 | |
---|
170 | // init vlimap |
---|
171 | vlimap_abs a, b; |
---|
172 | |
---|
173 | unsigned int mid = data.size() / 2; |
---|
174 | for (unsigned int i = 0; i < mid; i++) { |
---|
175 | a.push_back(data[i]); |
---|
176 | } |
---|
177 | for (unsigned int i = mid; i < data.size(); i++) { |
---|
178 | b.push_back(data[i]); |
---|
179 | } |
---|
180 | |
---|
181 | idset::inc_t count(map_size, 0); |
---|
182 | a.increment(count); |
---|
183 | b.increment(count); |
---|
184 | BOOST_TEST(count == expected_counts, boost::test_tools::per_element()) ; |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | BOOST_DATA_TEST_CASE_F(test_set, |
---|
189 | vlipmap_test, |
---|
190 | bdata::make(map_sizes) * |
---|
191 | bdata::make(map_fill) * |
---|
192 | bdata::make(map_seed), |
---|
193 | map_size, map_fill, map_seed) { |
---|
194 | // init random set |
---|
195 | init(map_size, map_fill, map_seed); |
---|
196 | |
---|
197 | // init vlimap |
---|
198 | vlimap a(map_size), b(map_size); |
---|
199 | |
---|
200 | unsigned int mid = data.size() / 2; |
---|
201 | for (unsigned int i = 0; i < mid; i++) { |
---|
202 | a.push_back(data[i]); |
---|
203 | } |
---|
204 | for (unsigned int i = mid; i < data.size(); i++) { |
---|
205 | b.push_back(data[i]); |
---|
206 | } |
---|
207 | |
---|
208 | { |
---|
209 | idset::inc_t count(map_size, 0); |
---|
210 | a.increment(count); |
---|
211 | b.increment(count); |
---|
212 | BOOST_TEST(count == expected_counts, boost::test_tools::per_element()) ; |
---|
213 | } |
---|
214 | |
---|
215 | BOOST_TEST_CONTEXT("split = " << mid << |
---|
216 | "(" << (data.size()?data[mid-1]:-1) << |
---|
217 | "/" << (data.size()?data[mid]:-1) << ")") { |
---|
218 | idset::inc_t count(map_size, 0); |
---|
219 | a.append(b); |
---|
220 | a.increment(count); |
---|
221 | BOOST_TEST(count == expected_counts, boost::test_tools::per_element()) ; |
---|
222 | } |
---|
223 | |
---|
224 | { |
---|
225 | idset::inc_t count(map_size, 1); |
---|
226 | a.invert(); |
---|
227 | int res = a.increment(count); |
---|
228 | BOOST_CHECK_EQUAL(res, 1); |
---|
229 | BOOST_TEST(count == expected_counts, boost::test_tools::per_element()) ; |
---|
230 | } |
---|
231 | { |
---|
232 | idset::inc_t count(map_size, 1); |
---|
233 | vlimap *tmp = new vlimap(a); |
---|
234 | int res = tmp->increment(count); |
---|
235 | BOOST_CHECK_EQUAL(res, 1); |
---|
236 | BOOST_TEST(count == expected_counts, boost::test_tools::per_element()) ; |
---|
237 | delete tmp; |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | |
---|
242 | BOOST_DATA_TEST_CASE_F(test_set, |
---|
243 | vlipmap_store_test, |
---|
244 | bdata::make(map_sizes) * |
---|
245 | bdata::make(map_fill) * |
---|
246 | bdata::make(map_seed), |
---|
247 | map_size, map_fill, map_seed) { |
---|
248 | // init random set |
---|
249 | init(map_size, map_fill, map_seed); |
---|
250 | |
---|
251 | // init vlimap |
---|
252 | vlimap v(map_size), w(map_size), wi(map_size); |
---|
253 | for (auto i : data) { |
---|
254 | v.push_back(i); |
---|
255 | } |
---|
256 | |
---|
257 | { |
---|
258 | std::stringstream tmp; |
---|
259 | v.write(tmp); |
---|
260 | w.read(tmp); |
---|
261 | idset::inc_t count(map_size, 0); |
---|
262 | w.increment(count); |
---|
263 | BOOST_TEST(count == expected_counts, boost::test_tools::per_element()) ; |
---|
264 | BOOST_CHECK_EQUAL(v.size(), w.size()); |
---|
265 | } |
---|
266 | |
---|
267 | v.invert(); |
---|
268 | |
---|
269 | { |
---|
270 | std::stringstream tmp; |
---|
271 | v.write(tmp); |
---|
272 | wi.read(tmp); |
---|
273 | idset::inc_t count(map_size, 1); |
---|
274 | wi.increment(count); |
---|
275 | BOOST_TEST(count == expected_counts, boost::test_tools::per_element()) ; |
---|
276 | BOOST_CHECK_EQUAL(v.size(), w.size()); |
---|
277 | } |
---|
278 | } |
---|
279 | |
---|
280 | |
---|
281 | BOOST_AUTO_TEST_SUITE_END(); // cseq_test |
---|
282 | |
---|
283 | /* |
---|
284 | Local Variables: |
---|
285 | mode:c++ |
---|
286 | c-file-style:"stroustrup" |
---|
287 | c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) |
---|
288 | indent-tabs-mode:nil |
---|
289 | fill-column:99 |
---|
290 | End: |
---|
291 | */ |
---|
292 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : |
---|