forked from youtube/cobalt_sandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flat_map_test.cc
702 lines (575 loc) · 21.8 KB
/
flat_map_test.cc
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
// Copyright 2017 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "starboard/common/flat_map.h"
#include <map>
#include <sstream>
#include <string>
#include "starboard/system.h"
#include "starboard/thread.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace starboard {
namespace nplb {
namespace {
bool StringPairEquals(const std::pair<std::string, std::string>& a,
const std::pair<std::string, std::string>& b) {
return (a.first == b.first) && (a.second == b.second);
}
bool FlipCoin() {
return (std::rand() % 2) == 1;
}
int Random(int first_inclusive, int end_exclusive) {
size_t range = static_cast<size_t>(end_exclusive - first_inclusive);
size_t rand = 0;
SbSystemGetRandomData(&rand, sizeof(rand));
return static_cast<int>(rand % range) + first_inclusive;
}
template <typename MapA_Type, typename MapB_Type>
bool CheckMapEquality(const MapA_Type& map_a, const MapB_Type& map_b) {
typedef typename MapA_Type::const_iterator map_a_iterator;
typedef typename MapB_Type::const_iterator map_b_iterator;
if (map_a.size() != map_b.size()) {
typedef typename MapA_Type::key_type key_type;
std::vector<key_type> vector_a;
std::vector<key_type> vector_b;
for (map_a_iterator it = map_a.begin(); it != map_a.end(); ++it) {
vector_a.push_back(it->first);
}
for (map_b_iterator it = map_b.begin(); it != map_b.end(); ++it) {
vector_b.push_back(it->first);
}
std::vector<key_type> diff_vector;
std::set_symmetric_difference(vector_a.begin(), vector_a.end(),
vector_b.begin(), vector_b.end(),
std::back_inserter(diff_vector));
for (int i = 0; i < diff_vector.size(); ++i) {
EXPECT_TRUE(false) << "Mismatched key: " << diff_vector[i] << "\n";
}
return false;
}
map_a_iterator map_a_it = map_a.begin();
map_b_iterator map_b_it = map_b.begin();
bool ok = true;
while (map_a_it != map_a.end()) {
ok &= (map_a_it->first == map_b_it->first);
ok &= (map_a_it->second == map_b_it->second);
EXPECT_EQ(map_a_it->first, map_b_it->first);
EXPECT_EQ(map_a_it->second, map_b_it->second);
++map_a_it;
++map_b_it;
}
return ok;
}
SbTimeMonotonic GetThreadTimeMonotonicNow() {
if (SbTimeIsTimeThreadNowSupported())
return SbTimeGetMonotonicThreadNow();
return SbTimeGetMonotonicNow();
}
// Generic stringification of the input map type. This allows good error
// messages to be printed out.
template <typename MapType>
std::string MapToString(const MapType& map) {
typedef typename MapType::const_iterator const_iterator;
std::stringstream ss;
for (const_iterator it = map.begin(); it != map.end(); ++it) {
ss << "{" << it->first << "," << it->second << "},\n";
}
return ss.str();
}
// Tests FlatMap<int, int> by shadowing operations to an std::map<int, int>
// and checking for equality at every step of the way. This allows "fuzzing"
// the container and checking that it's operation match those from a known
// container.
struct MapTester {
typedef std::map<int, int>::const_iterator std_map_iterator;
typedef FlatMap<int, int>::const_iterator flat_map_iterator;
bool CheckEquality() { return CheckMapEquality(std_map, flat_map); }
void Insert(int key, int value) {
typedef std::pair<std_map_iterator, bool> StdMapPair;
typedef std::pair<flat_map_iterator, bool> FlatMapPair;
StdMapPair pair_a = std_map.insert(std::make_pair(key, value));
FlatMapPair pair_b = flat_map.insert(std::make_pair(key, value));
ASSERT_EQ(pair_a.second, pair_b.second)
<< "Insertion states are mismatched.";
ASSERT_EQ(pair_a.first->first, pair_b.first->first)
<< "Inserted keys have a mismatch.";
ASSERT_EQ(pair_a.first->second, pair_b.first->second)
<< "Inserted values have a mismatch.";
CheckEquality();
}
void BulkInsert(const std::vector<std::pair<int, int> >& values) {
std::map<int, int> old_std_map = std_map;
FlatMap<int, int> old_flat_map = flat_map;
std_map.insert(values.begin(), values.end());
flat_map.insert(values.begin(), values.end());
if (!CheckEquality()) {
// Failed so print out something interesting.
std::string str_old_std_map = MapToString(old_std_map);
std::string str_std_map = MapToString(std_map);
std::string str_values = MapToString(values);
std::string str_flat_map = MapToString(flat_map);
std::stringstream ss;
ss << "Original Map:\n" << str_old_std_map << "\n\n";
ss << "Bulk insert values:\n" << str_values << "\n\n";
ss << "Resulting map:\n" << str_flat_map << "\n\n";
ss << "But should have been:\n" << str_std_map << "\n\n";
SbLogRaw(ss.str().c_str());
}
}
void Erase(int key) {
std_map.erase(key);
flat_map.erase(key);
CheckEquality();
}
void BulkErase(const std::vector<int>& values) {
for (size_t i = 0; i < values.size(); ++i) {
std_map.erase(values[i]);
flat_map.erase(values[i]);
}
CheckEquality();
}
void Clear() {
std_map.clear();
flat_map.clear();
CheckEquality();
}
static int RandomKey() { return Random(0, 100); }
static int RandomValue() { return Random(0, 10000); }
std::map<int, int> std_map;
FlatMap<int, int> flat_map;
};
} // namespace.
////////////////////////////// UNIT TESTS /////////////////////////////////////
TEST(FlatMap, BasicUse) {
FlatMap<int, int> int_map;
int_map[4] = 3;
int_map[3] = 4;
EXPECT_EQ(2, int_map.size());
EXPECT_EQ(3, int_map[4]);
EXPECT_EQ(4, int_map[3]);
int_map.erase(3);
EXPECT_EQ(int_map[4], 3);
}
// Tests that a string map correctly can be used with this flat map.
TEST(FlatMap, StringMap) {
FlatMap<std::string, std::string> string_map;
string_map["one"] = "value-one";
string_map["two"] = "value-two";
string_map["three"] = "value-three";
string_map["four"] = "value-four";
string_map["five"] = "value-five";
EXPECT_EQ(std::string("value-one"), string_map["one"]);
EXPECT_EQ(std::string("value-two"), string_map["two"]);
EXPECT_EQ(std::string("value-three"), string_map["three"]);
EXPECT_EQ(std::string("value-four"), string_map["four"]);
EXPECT_EQ(std::string("value-five"), string_map["five"]);
}
struct CustomKey {
CustomKey() : value(0) {}
explicit CustomKey(int v) : value(v) {}
int value;
// Auto-binds to std::less, which is the default comparator of FlatMap
// as well as
bool operator<(const CustomKey& other) const { return value < other.value; }
};
TEST(FlatMap, CustomKeyType) {
FlatMap<CustomKey, int> custom_map;
custom_map[CustomKey(3)] = 1234;
EXPECT_EQ(1234, custom_map[CustomKey(3)]);
}
TEST(FlatMap, size) {
FlatMap<std::string, std::string> flat_map;
EXPECT_EQ(0, flat_map.size());
flat_map["one"] = "one-value";
EXPECT_EQ(1, flat_map.size());
}
TEST(FlatMap, empty) {
FlatMap<std::string, std::string> flat_map;
EXPECT_TRUE(flat_map.empty());
flat_map["one"] = "one-value";
EXPECT_FALSE(flat_map.empty());
}
TEST(FlatMap, clear) {
FlatMap<std::string, std::string> flat_map;
flat_map["one"] = "one-value";
flat_map.clear();
EXPECT_TRUE(flat_map.empty());
}
TEST(FlatMap, find) {
FlatMap<std::string, std::string> flat_map;
flat_map["one"] = "value-one";
flat_map["two"] = "value-two";
flat_map["three"] = "value-three";
flat_map["four"] = "value-four";
flat_map["five"] = "value-five";
EXPECT_EQ(std::string("value-one"), flat_map["one"]);
EXPECT_EQ(std::string("value-two"), flat_map["two"]);
EXPECT_EQ(std::string("value-three"), flat_map["three"]);
EXPECT_EQ(std::string("value-four"), flat_map["four"]);
EXPECT_EQ(std::string("value-five"), flat_map["five"]);
FlatMap<std::string, std::string>::const_iterator found_it =
flat_map.find("three");
ASSERT_NE(found_it, flat_map.end());
ASSERT_EQ(std::string("three"), found_it->first);
ASSERT_EQ(std::string("value-three"), found_it->second);
found_it = flat_map.find("twenty");
ASSERT_EQ(found_it, flat_map.end());
}
TEST(FlatMap, swap) {
FlatMap<int, int> map;
map[1] = -1;
FlatMap<int, int> other_map;
map.swap(other_map);
EXPECT_TRUE(map.empty());
EXPECT_EQ(1, other_map.size());
EXPECT_EQ(-1, other_map[1]);
}
TEST(FlatMap, DefaultAssignmentArrayOperator) {
FlatMap<int, int> map;
EXPECT_EQ(0, map[1]); // key [1] doesn't exist, so should default to 0.
}
TEST(FlatMap, lower_bound) {
FlatMap<int, int> map;
map[1] = 1;
map[3] = 3;
map[4] = 4;
FlatMap<int, int>::const_iterator lower_it = map.lower_bound(2);
ASSERT_TRUE(lower_it != map.end());
EXPECT_EQ(lower_it->first, 3);
lower_it = map.lower_bound(3);
ASSERT_TRUE(lower_it != map.end());
EXPECT_EQ(lower_it->first, 3);
}
TEST(FlatMap, upper_bound) {
FlatMap<int, int> map;
map[1] = 1;
map[3] = 3;
map[4] = 4;
FlatMap<int, int>::const_iterator upper_it = map.upper_bound(2);
ASSERT_TRUE(upper_it != map.end());
EXPECT_EQ(upper_it->first, 3);
upper_it = map.upper_bound(3);
ASSERT_TRUE(upper_it != map.end());
EXPECT_EQ(upper_it->first, 4); // 4 is the next one greater than 3.
}
TEST(FlatMap, equal_range) {
FlatMap<int, int> map;
typedef FlatMap<int, int>::iterator iterator;
map[1] = 1;
map[3] = 3;
map[4] = 4;
// Should not find this.
std::pair<iterator, iterator> range = map.equal_range(2);
ASSERT_EQ(range.first, map.end());
ASSERT_EQ(range.second, map.end());
// Should find the value.
range = map.equal_range(3);
ASSERT_EQ(range.first, map.begin() + 1);
ASSERT_EQ(range.second, map.begin() + 2); // exclusive
}
TEST(FlatMap, count) {
FlatMap<int, int> map;
typedef FlatMap<int, int>::iterator iterator;
map[1] = 1;
EXPECT_EQ(1, map.count(1));
EXPECT_EQ(0, map.count(4)); // We don't expect this to be found.
}
TEST(FlatMap, OperatorEquals) {
FlatMap<int, int> map_a;
FlatMap<int, int> map_b;
map_a[1] = 1;
map_b[1] = 1;
EXPECT_EQ(map_a, map_b);
map_b[1] = -1;
EXPECT_NE(map_a, map_b);
map_b[1] = 1;
EXPECT_EQ(map_a, map_b); // Expect equal again.
map_b[2] = 1;
EXPECT_NE(map_a, map_b);
}
TEST(FlatMap, Insert) {
FlatMap<int, int> int_map;
EXPECT_TRUE(int_map.empty());
EXPECT_EQ(0, int_map.size());
int_map.insert(std::make_pair(1, 10));
EXPECT_FALSE(int_map.empty());
EXPECT_EQ(1, int_map.size());
FlatMap<int, int>::iterator found = int_map.find(1);
EXPECT_EQ(10, found->second);
std::pair<FlatMap<int, int>::iterator, bool> insert_result =
int_map.insert(std::make_pair(1, 10));
EXPECT_FALSE(insert_result.second) << "Insert should have been rejected.";
EXPECT_EQ(insert_result.first, int_map.begin());
insert_result = int_map.insert(std::make_pair(0, -10));
EXPECT_TRUE(insert_result.second) << "Insert should have been succeed.";
// The new beginning contains the new value.
EXPECT_EQ(insert_result.first, int_map.begin());
}
TEST(FlatMap, BulkInsertZero) {
FlatMap<std::string, std::string> flat_string_map;
flat_string_map["one"] = "value-one";
flat_string_map["two"] = "value-two";
flat_string_map["three"] = "value-three";
flat_string_map["four"] = "value-four";
flat_string_map["five"] = "value-five";
std::map<std::string, std::string> string_map; // empty.
const size_t num_inserted =
flat_string_map.insert(string_map.begin(), string_map.end());
ASSERT_EQ(0, num_inserted);
EXPECT_EQ(5, flat_string_map.size());
// According to the sort invariant, "five" is the lowest value key and
// therefore should be the first element found in the map.
EXPECT_EQ(std::string("value-five"), flat_string_map.begin()->second);
}
TEST(FlatMap, BulkInsertOne) {
FlatMap<std::string, std::string> flat_string_map;
// Reference map verifies for correct behavior.
std::map<std::string, std::string> reference_map;
flat_string_map["one"] = "value-one";
flat_string_map["two"] = "value-two";
flat_string_map["three"] = "value-three";
flat_string_map["four"] = "value-four";
flat_string_map["five"] = "value-five";
reference_map["one"] = "value-one";
reference_map["two"] = "value-two";
reference_map["three"] = "value-three";
reference_map["four"] = "value-four";
reference_map["five"] = "value-five";
std::map<std::string, std::string> string_map; // empty.
string_map["six"] = "value-six";
const size_t num_inserted =
flat_string_map.insert(string_map.begin(), string_map.end());
reference_map.insert(string_map.begin(), string_map.end());
ASSERT_EQ(1, num_inserted); // "six" = "value-six" was inserted.
EXPECT_EQ(std::string("value-six"), flat_string_map["six"]);
CheckMapEquality(flat_string_map, reference_map);
}
TEST(FlatMap, BulkInsertDuplicate) {
FlatMap<int, int> flat_int_map;
flat_int_map[1] = 1;
std::vector<std::pair<int, int> > bulk_entries;
bulk_entries.push_back(std::pair<int, int>(1, -1));
flat_int_map.insert(bulk_entries.begin(), bulk_entries.end());
// Expect that resetting the key [1] => -1 failed because the key already
// existed.
EXPECT_EQ(1, flat_int_map[1]);
}
TEST(FlatMap, BulkInsert) {
std::map<std::string, std::string> string_map;
string_map["one"] = "value-one";
string_map["two"] = "value-two";
string_map["three"] = "value-three";
string_map["four"] = "value-four";
string_map["five"] = "value-five";
FlatMap<std::string, std::string> flat_string_map;
// Reference map verifies for correct behavior.
std::map<std::string, std::string> reference_map;
flat_string_map.insert(string_map.begin(), string_map.end());
reference_map.insert(string_map.begin(), string_map.end());
ASSERT_EQ(flat_string_map.size(), string_map.size());
ASSERT_EQ(reference_map.size(), string_map.size());
bool is_equal_range = std::equal(string_map.begin(), string_map.end(),
flat_string_map.begin(), StringPairEquals);
// Now insert again.
size_t num_inserted =
flat_string_map.insert(string_map.begin(), string_map.end());
EXPECT_EQ(0, num_inserted)
<< "No elements should be inserted because they all preexist.";
reference_map.insert(string_map.begin(), string_map.end());
// No change in map size.
ASSERT_EQ(flat_string_map.size(), string_map.size());
is_equal_range = std::equal(string_map.begin(), string_map.end(),
flat_string_map.begin(), StringPairEquals);
EXPECT_TRUE(is_equal_range);
CheckMapEquality(flat_string_map, reference_map);
}
TEST(FlatMap, UnsortedInsertWithDuplicates) {
typedef std::pair<std::string, std::string> StringPair;
std::vector<StringPair> vector;
vector.push_back(StringPair("one", "value-one"));
vector.push_back(StringPair("one", "value-one")); // Duplicate
vector.push_back(StringPair("three", "value-three"));
vector.push_back(StringPair("four", "value-four"));
vector.push_back(StringPair("five", "value-five"));
FlatMap<std::string, std::string> flat_string_map;
flat_string_map.insert(vector.begin(), vector.end());
// Asserts that the duplicate with key "one" was removed.
ASSERT_EQ(4, flat_string_map.size());
std::map<std::string, std::string> string_map;
string_map["one"] = "value-one";
string_map["two"] = "value-two";
string_map["three"] = "value-three";
string_map["four"] = "value-four";
string_map["five"] = "value-five";
const size_t num_inserted =
flat_string_map.insert(string_map.begin(), string_map.end());
ASSERT_EQ(1, num_inserted) << "Only one element should have been inserted.";
bool is_equal_range = std::equal(string_map.begin(), string_map.end(),
flat_string_map.begin(), StringPairEquals);
ASSERT_TRUE(is_equal_range);
}
TEST(FlatMap, FlatMapDetail_IsPod) {
EXPECT_TRUE(flat_map_detail::IsPod<bool>::value);
EXPECT_TRUE(flat_map_detail::IsPod<float>::value);
EXPECT_TRUE(flat_map_detail::IsPod<int8_t>::value);
EXPECT_TRUE(flat_map_detail::IsPod<uint8_t>::value);
EXPECT_TRUE(flat_map_detail::IsPod<int16_t>::value);
EXPECT_TRUE(flat_map_detail::IsPod<uint16_t>::value);
EXPECT_TRUE(flat_map_detail::IsPod<int32_t>::value);
EXPECT_TRUE(flat_map_detail::IsPod<uint32_t>::value);
EXPECT_TRUE(flat_map_detail::IsPod<int64_t>::value);
EXPECT_TRUE(flat_map_detail::IsPod<uint64_t>::value);
EXPECT_TRUE(flat_map_detail::IsPod<CustomKey*>::value);
EXPECT_FALSE(flat_map_detail::IsPod<std::string>::value);
EXPECT_FALSE(flat_map_detail::IsPod<std::vector<int> >::value);
}
////////////////////////////// PERFORMANCE TEST ///////////////////////////////
std::vector<int> GenerateRandomIntVector(size_t size_vector,
int min_random,
int max_random) {
std::vector<int> output;
for (size_t i = 0; i < size_vector; ++i) {
output.push_back(Random(min_random, max_random));
}
return output;
}
std::vector<std::pair<int, int> > GenerateRandomIntPairVector(
size_t size_vector,
int min_random,
int max_random) {
std::vector<std::pair<int, int> > output;
for (size_t i = 0; i < size_vector; ++i) {
std::pair<int, int> entry(Random(min_random, max_random),
Random(min_random, max_random));
output.push_back(entry);
}
return output;
}
template <typename MapIntType> // FlatMap<int, int> or std::map<int, int>
SbTime PerfTestFind(const MapIntType& map,
const std::vector<int>& search_queries_data,
size_t query_count) {
SbThreadYield(); // Stabilizes time
SbTime start_time = GetThreadTimeMonotonicNow();
size_t index = 0;
const size_t n = search_queries_data.size();
for (size_t i = 0; i < query_count; ++i) {
if (index == n) {
index = 0;
}
map.find(search_queries_data[index]);
++index;
}
SbTime delta_time = GetThreadTimeMonotonicNow() - start_time;
return delta_time;
}
TEST(FlatMap, DISABLED_PerformanceTestFind) {
std::vector<size_t> test_sizes;
test_sizes.push_back(5);
test_sizes.push_back(10);
test_sizes.push_back(25);
test_sizes.push_back(50);
test_sizes.push_back(100);
test_sizes.push_back(1000);
test_sizes.push_back(10000);
test_sizes.push_back(100000);
std::vector<std::pair<int, int> > insert_data;
const std::vector<int> query_data =
GenerateRandomIntVector(1000, // Number of elements.
0, // Min random value.
100000); // Max random value.
static const size_t kNumberOfQueries = 10000;
std::vector<double> speedup_results;
for (size_t i = 0; i < test_sizes.size(); ++i) {
const size_t test_size = test_sizes[i];
insert_data = GenerateRandomIntPairVector(test_size, 0, 100000);
FlatMap<int, int> flat_int_map(insert_data.begin(), insert_data.end());
std::map<int, int> std_int_map(insert_data.begin(), insert_data.end());
SbTime time_flat_int_map =
PerfTestFind(flat_int_map, query_data, kNumberOfQueries);
SbTime time_std_int_map =
PerfTestFind(std_int_map, query_data, kNumberOfQueries);
double flat_map_speedup = static_cast<double>(time_std_int_map) /
static_cast<double>(time_flat_int_map);
speedup_results.push_back(flat_map_speedup);
}
std::stringstream ss;
ss << "\n";
ss << "FlatMap<int,int>::find() Performance\n"
<< "NUMBER OF ELEMENTS | SPEED COMPARISON vs std::map\n"
<< "-------------------------------------\n";
for (size_t i = 0; i < test_sizes.size(); ++i) {
size_t test_size = test_sizes[i];
double speedup = speedup_results[i];
ss.width(18);
ss << std::right << test_size << " | ";
ss << std::left << (speedup * 100.0) << "%\n";
}
ss << "\n";
SbLogRaw(ss.str().c_str());
}
///////////////////////////////// FUZZER TEST /////////////////////////////////
// A stochastic test which randomly does insertions and erases and makes sure
// that the two data structures are equal at every step of the way.
TEST(FlatMap, FuzzerTest) {
static const size_t kNumTestIterations = 1000;
MapTester map_tester;
// Seed the random number generator so that any failures are reproducible
// between runs.
std::srand(0);
for (size_t test_loop = 0; test_loop < kNumTestIterations; ++test_loop) {
const size_t random_1_to_100 = 1 + (std::rand() % 100);
if (random_1_to_100 > 98) { // 2% - chance
// do clear.
map_tester.Clear();
} else if (random_1_to_100 > 48) { // 50% chance
// Do insert.
if (FlipCoin()) {
// Insert one element.
int key = MapTester::RandomKey();
int value = MapTester::RandomValue();
map_tester.Insert(key, value);
} else {
// Bulk insert
const size_t num_values = Random(0, 20);
std::vector<std::pair<int, int> > values;
for (size_t i = 0; i < num_values; ++i) {
int key = MapTester::RandomKey();
int value = MapTester::RandomValue();
values.push_back(std::make_pair(key, value));
}
map_tester.BulkInsert(values);
}
} else {
// Do erase.
if (FlipCoin()) {
// Erase one element.
int key = Random(0, 100);
map_tester.Erase(key);
} else {
// Erase bulk elements.
const size_t num_values = Random(0, 20);
std::vector<int> values;
for (size_t i = 0; i < num_values; ++i) {
int key = Random(0, 100);
values.push_back(key);
}
map_tester.BulkErase(values);
}
}
// Now check to make sure maps are still equal.
map_tester.CheckEquality();
}
}
} // namespace nplb
} // namespace starboard