-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_per_style_storage.cpp
331 lines (288 loc) · 8.67 KB
/
test_per_style_storage.cpp
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*------------------------------------------------------------------------------
*
* This file is part of rendermq
*
* Author: [email protected]
*
* Copyright 2010-1 Mapquest, Inc. All Rights reserved.
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*-----------------------------------------------------------------------------*/
#include "storage/null_storage.hpp"
#include "storage/per_style_storage.hpp"
#include "test/common.hpp"
#include <stdexcept>
#include <iostream>
#include <boost/noncopyable.hpp>
#include <boost/function.hpp>
#include <boost/format.hpp>
#include <boost/foreach.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/random/variate_generator.hpp>
#include <limits>
using boost::function;
using boost::shared_ptr;
using std::runtime_error;
using std::exception;
using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::numeric_limits;
using std::time_t;
using std::list;
using std::vector;
using std::make_pair;
using rendermq::per_style_storage;
using rendermq::null_storage;
using rendermq::tile_storage;
using rendermq::tile_protocol;
namespace
{
void assert_no_tile(const tile_protocol &t, const tile_storage &store)
{
shared_ptr<tile_storage::handle> handle = store.get(t);
if (handle->exists())
{
throw runtime_error((boost::format("Storage should not have tile %1%, but does.") % t).str());
}
}
void assert_exists_tile(const tile_protocol &t, const tile_storage &store)
{
shared_ptr<tile_storage::handle> handle = store.get(t);
if (!handle->exists())
{
throw runtime_error((boost::format("Storage should have tile %1%, but does not.") % t).str());
}
}
void assert_not_expired_tile(const tile_protocol &t, const tile_storage &store)
{
shared_ptr<tile_storage::handle> handle = store.get(t);
if (!handle->exists() || handle->expired())
{
throw runtime_error((boost::format("Storage should have tile %1% and it not be expired.") % t).str());
}
}
class exists_handle
: public tile_storage::handle
{
public:
bool exists() const { return true; }
time_t last_modified() const { std::time_t t = std::time(NULL); return t; }
bool data(string &str) const { str = ""; return true; }
bool expired() const { return false; }
};
class null_handle
: public tile_storage::handle
{
public:
bool exists() const { return false; }
time_t last_modified() const { return time_t(0); }
bool data(string &) const { return false; }
bool expired() const { return false; }
};
// even numbered tiles (x + y) & 1 == 0 exist, others are null
class predicate_tiles_exist
: public tile_storage
{
public:
predicate_tiles_exist(boost::function<bool (const tile_protocol &)> pred)
: m_pred(pred)
{
}
shared_ptr<tile_storage::handle> get(const tile_protocol &tile) const
{
if (m_pred(tile))
{
return shared_ptr<tile_storage::handle>(new exists_handle());
}
else
{
return shared_ptr<tile_storage::handle>(new null_handle());
}
}
bool get_meta(const tile_protocol &tile, string &str) const
{
if (m_pred(tile))
{
str = "";
return true;
}
else
{
return false;
}
}
bool put_meta(const tile_protocol &tile, const string &str) const
{
return m_pred(tile);
}
bool expire(const tile_protocol &tile) const
{
return m_pred(tile);
}
private:
boost::function<bool (const tile_protocol &)> m_pred;
};
bool is_even_tile(const tile_protocol &tile)
{
return ((tile.x + tile.y) & 1) == 0;
}
bool is_odd_tile(const tile_protocol &tile)
{
return ((tile.x + tile.y) & 1) == 1;
}
bool always_true(const tile_protocol &tile)
{
return true;
}
/* a storage which has no tiles, but records all attempts to get or put
* data to or from the storage. this is useful to check that things were
* routed to the right place.
*/
class recording_storage
: public tile_storage
{
public:
shared_ptr<tile_storage::handle> get(const tile_protocol &tile) const
{
gets.push_back(tile);
return shared_ptr<tile_storage::handle>(new null_handle());
}
bool get_meta(const tile_protocol &tile, string &str) const
{
gets.push_back(tile);
return false;
}
bool put_meta(const tile_protocol &tile, const string &str) const
{
puts.push_back(tile);
return false;
}
bool expire(const tile_protocol &tile) const
{
puts.push_back(tile);
return false;
}
mutable list<tile_protocol> gets, puts;
};
class randomized_tester
{
public:
randomized_tester();
virtual ~randomized_tester() {}
virtual void test(const tile_protocol &, tile_storage &) = 0;
void operator()();
virtual void finally() {}
protected:
per_style_storage::map_of_storage_t storages;
boost::shared_ptr<tile_storage> default_storage;
vector<string> style_names;
private:
boost::mt19937 prng;
boost::uniform_int<int> dist;
boost::variate_generator<boost::mt19937 &, boost::uniform_int<int> > rand;
};
randomized_tester::randomized_tester()
: storages(), default_storage(), style_names(), prng(),
dist(numeric_limits<int>::min(), numeric_limits<int>::max()),
rand(prng, dist)
{
}
void randomized_tester::operator()()
{
per_style_storage storage(storages, default_storage);
for (int i = 0; i < 100000; ++i)
{
// random zoom level from 0 to 19
int z = rand() % 19;
// random tiles within the allowed zoom range (not that it
// really matters for this test...)
int x = rand() & ((1 << z) - 1);
int y = rand() & ((1 << z) - 1);
// get a random style
if (style_names.empty()) { throw std::runtime_error("randomized_tester::style_names should not be empty in test."); }
const string &style = style_names[rand() % style_names.size()];
tile_protocol tile(rendermq::cmdRender, x, y, z, 0, style, rendermq::fmtPNG, 0, 0);
test(tile, storage);
}
finally();
}
class test_style_routing
: public randomized_tester
{
public:
test_style_routing()
: randomized_tester(),
foo_record(new recording_storage()),
bar_record(new recording_storage()),
def_record(new recording_storage())
{
storages.insert(make_pair("foo", shared_ptr<tile_storage>(foo_record)));
storages.insert(make_pair("bar", shared_ptr<tile_storage>(bar_record)));
default_storage = shared_ptr<tile_storage>(def_record);
style_names.push_back("foo");
style_names.push_back("bar");
style_names.push_back("other");
}
void test(const tile_protocol &t, tile_storage &storage)
{
// none of the recording storages claim to have tiles.
assert_no_tile(t, storage);
}
void finally()
{
BOOST_FOREACH(const tile_protocol &t, foo_record->gets)
{
if (t.style != "foo")
{
throw runtime_error((boost::format("Tile %1% should have style 'foo', but has '%2%' instead.") % t % t.style).str());
}
}
BOOST_FOREACH(const tile_protocol &t, bar_record->gets)
{
if (t.style != "bar")
{
throw runtime_error((boost::format("Tile %1% should have style 'bar', but has '%2%' instead.") % t % t.style).str());
}
}
BOOST_FOREACH(const tile_protocol &t, def_record->gets)
{
if (t.style != "other")
{
throw runtime_error((boost::format("Tile %1% should have style 'other', but has '%2%' instead.") % t % t.style).str());
}
}
}
private:
// see comment in test_odd_and_even_pass_thru
recording_storage *foo_record, *bar_record, *def_record;
};
} // anonymous namespace
int main()
{
int tests_failed = 0;
cout << "== Testing Per-Style Storage Backend ==" << endl << endl;
{
test_style_routing test;
tests_failed += test::run("test_style_routing", boost::ref(test));
}
//tests_failed += test::run("test_", &test_);
cout << " >> Tests failed: " << tests_failed << endl << endl;
return 0;
}