summaryrefslogtreecommitdiffstats
path: root/tests/test_map.cpp
blob: 3136645a6da2b1e6fa73217272544f6e57f9f99e (plain)
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
#include <algorithm>
#include <frozen/map.h>
#include <functional>
#include <iostream>
#include <map>

#include "bench.hpp"
#include "catch.hpp"

TEST_CASE("empty frozen map", "[map]") {
  constexpr frozen::map<int, float, 0> ze_map{};

  constexpr auto empty = ze_map.empty();
  REQUIRE(empty);

  constexpr auto size = ze_map.size();
  REQUIRE(size == 0);

  constexpr auto max_size = ze_map.max_size();
  REQUIRE(max_size == 0);

  constexpr auto count = ze_map.count(3);
  REQUIRE(count == 0);

  constexpr auto find = ze_map.find(5);
  REQUIRE(find == ze_map.end());

  constexpr auto range = ze_map.equal_range(0);
  REQUIRE(std::get<0>(range) == ze_map.end());
  REQUIRE(std::get<1>(range) == ze_map.end());

  constexpr auto lower_bound = ze_map.lower_bound(1);
  REQUIRE(lower_bound == ze_map.end());

  constexpr auto upper_bound = ze_map.upper_bound(1);
  REQUIRE(upper_bound == ze_map.end());

  auto constexpr begin = ze_map.begin(), end = ze_map.end();
  REQUIRE(begin == end);

  auto constexpr key_comp = ze_map.key_comp();
  auto constexpr key_comparison = key_comp(1, 2);
  REQUIRE(key_comparison);

  auto constexpr value_comp = ze_map.value_comp();
  auto constexpr value_comparison = value_comp(11, 12);
  REQUIRE(value_comparison);

  auto constexpr cbegin = ze_map.cbegin(), cend = ze_map.cend();
  REQUIRE(cbegin == cend);

  std::for_each(ze_map.begin(), ze_map.end(), [](std::tuple<int, float>) {});
  REQUIRE(std::distance(ze_map.rbegin(), ze_map.rend()) == 0);
  REQUIRE(std::count(ze_map.crbegin(), ze_map.crend(),
                     decltype(ze_map)::value_type(3, 5.3f)) == 0);
}

TEST_CASE("singleton frozen map", "[map]") {
  constexpr frozen::map<int, double, 1> ze_map = {{1, 3.14}};

  constexpr auto empty = ze_map.empty();
  REQUIRE(!empty);

  constexpr auto size = ze_map.size();
  REQUIRE(size == 1);

  constexpr auto max_size = ze_map.max_size();
  REQUIRE(max_size == 1);

  constexpr auto count1 = ze_map.count(1);
  REQUIRE(count1 == 1);

  constexpr auto count2 = ze_map.count(2);
  REQUIRE(count2 == 0);

  const auto find1 = ze_map.find(1);
  REQUIRE(find1 == ze_map.begin());
  REQUIRE(std::get<0>(*find1) == 1);
  REQUIRE(std::get<1>(*find1) == 3.14);

  const auto find5 = ze_map.find(5);
  REQUIRE(find5 == ze_map.end());

  const auto range0 = ze_map.equal_range(0);
  REQUIRE(std::get<0>(range0) == ze_map.end());
  REQUIRE(std::get<1>(range0) == ze_map.end());

  const auto range1 = ze_map.equal_range(1);
  REQUIRE(std::get<0>(range1) == ze_map.begin());
  REQUIRE(std::get<1>(range1) == ze_map.end());

  const auto lower_bound0 = ze_map.lower_bound(0);
  REQUIRE(lower_bound0 == ze_map.end());

  const auto lower_bound1 = ze_map.lower_bound(1);
  REQUIRE(lower_bound1 == ze_map.find(1));

  const auto lower_bound2 = ze_map.lower_bound(2);
  REQUIRE(lower_bound2 == ze_map.end());

  const auto upper_bound0 = ze_map.upper_bound(0);
  REQUIRE(upper_bound0 == ze_map.end());

  const auto upper_bound1 = ze_map.upper_bound(1);
  REQUIRE(upper_bound1 == ze_map.end());

  const auto upper_bound2 = ze_map.upper_bound(2);
  REQUIRE(upper_bound2 == ze_map.end());

  auto const begin = ze_map.begin(), end = ze_map.end();
  REQUIRE((begin + 1) == end);

  auto const key_comp = ze_map.key_comp();
  auto const key_comparison = key_comp(1, 2);
  REQUIRE(key_comparison);

  auto const value_comp = ze_map.value_comp();
  auto const value_comparison = value_comp(11, 12);
  REQUIRE(value_comparison);

  auto const cbegin = ze_map.cbegin(), cend = ze_map.cend();
  REQUIRE(cbegin == (cend - 1));

  std::for_each(ze_map.begin(), ze_map.end(), [](std::tuple<short, double>) {});
  REQUIRE(std::distance(ze_map.rbegin(), ze_map.rend()) == 1);
  REQUIRE(std::count(ze_map.crbegin(), ze_map.crend(),
                     decltype(ze_map)::value_type(3, 14)) == 0);
  REQUIRE(std::count(ze_map.crbegin(), ze_map.crend(),
                     decltype(ze_map)::value_type(1, 3.14)) == 1);
}

TEST_CASE("triple frozen map", "[map]") {
  constexpr frozen::map<int, bool, 3> ze_map{
      {10, true}, {20, false}, {30, true}};

  constexpr auto empty = ze_map.empty();
  REQUIRE(!empty);

  constexpr auto size = ze_map.size();
  REQUIRE(size == 3);

  constexpr auto max_size = ze_map.max_size();
  REQUIRE(max_size == 3);

  constexpr auto count1 = ze_map.count(1);
  REQUIRE(count1 == 0);

  constexpr auto count10 = ze_map.count(10);
  REQUIRE(count10 == 1);

  const auto find10 = ze_map.find(10);
  REQUIRE(find10 == ze_map.begin());
  REQUIRE(std::get<0>(*find10) == 10);
  REQUIRE(std::get<1>(*find10) == true);

  const auto find15 = ze_map.find(15);
  REQUIRE(find15 == ze_map.end());

  const auto range0 = ze_map.equal_range(0);
  REQUIRE(std::get<0>(range0) == ze_map.end());
  REQUIRE(std::get<1>(range0) == ze_map.end());

  const auto range1 = ze_map.equal_range(10);
  REQUIRE(std::get<0>(range1) == ze_map.begin());
  REQUIRE(std::get<1>(range1) == ze_map.begin() + 1);

  const auto lower_bound0 = ze_map.lower_bound(0);
  REQUIRE(lower_bound0 == ze_map.end());

  for (auto val : ze_map) {
    const auto lower_bound = ze_map.lower_bound(std::get<0>(val));
    REQUIRE(lower_bound == ze_map.find(std::get<0>(val)));
  }

  const auto lower_bound2 = ze_map.lower_bound(40);
  REQUIRE(lower_bound2 == ze_map.end());

  const auto upper_bound0 = ze_map.upper_bound(0);
  REQUIRE(upper_bound0 == ze_map.end());

  const auto upper_bound1 = ze_map.upper_bound(10);
  REQUIRE(upper_bound1 == (ze_map.begin() + 1));

  const auto upper_bound2 = ze_map.upper_bound(40);
  REQUIRE(upper_bound2 == ze_map.end());

  auto const begin = ze_map.begin(), end = ze_map.end();
  REQUIRE((begin + ze_map.size()) == end);

  auto const key_comp = ze_map.key_comp();
  auto const key_comparison = key_comp(1, 2);
  REQUIRE(key_comparison);

  auto const value_comp = ze_map.value_comp();
  auto const value_comparison = value_comp(11, 12);
  REQUIRE(value_comparison);

  auto const cbegin = ze_map.cbegin(), cend = ze_map.cend();
  REQUIRE(cbegin == (cend - ze_map.size()));

  std::for_each(ze_map.begin(), ze_map.end(), [](std::tuple<unsigned long, bool>) {});
  REQUIRE((std::size_t)std::distance(ze_map.rbegin(), ze_map.rend()) == ze_map.size());
  REQUIRE(std::count(ze_map.crbegin(), ze_map.crend(),
                     decltype(ze_map)::value_type(3, 14)) == 0);
  REQUIRE(std::count(ze_map.crbegin(), ze_map.crend(),
                     decltype(ze_map)::value_type(20, false)) == 1);
}

TEST_CASE("frozen::map <> std::map", "[map]") {
#define INIT_SEQ                                                               \
    {19, 12}, {1, 12}, {2, 12}, {4, 12}, {5, 12}, {6, 12}, {7, 12}, {8, 12},   \
        {9, 12}, {10, 12}, {11, 12}, {111, 12}, {1112, 12}, {1115, 12},        \
        {1118, 12}, {1110, 12}, {1977, 12}, {177, 12}, {277, 12}, {477, 12},   \
        {577, 12}, {677, 12}, {777, 12}, {877, 12}, {977, 12}, {1077, 12},     \
        {1177, 12}, {11177, 12}, {111277, 12}, {111577, 12}, {111877, 12},     \
        {111077, 12}, {1999, 12}, {199, 12}, {299, 12}, {499, 12}, {599, 12},  \
        {699, 12}, {799, 12}, {899, 12}, {999, 12}, {1099, 12}, {1199, 12},    \
        {11199, 12}, {111299, 12}, {111599, 12}, {111899, 12}, {111099, 12},   \
        {197799, 12}, {17799, 12}, {27799, 12}, {47799, 12}, {57799, 12},      \
        {67799, 12}, {77799, 12}, {87799, 12}, {97799, 12}, {107799, 12},      \
        {117799, 12}, {1117799, 12}, {11127799, 12}, {11157799, 12},           \
        {11187799, 12}, {11107799, 12}, {1988, 12}, {188, 12}, {288, 12},      \
        {488, 12}, {588, 12}, {688, 12}, {788, 12}, {888, 12}, {988, 12},      \
        {1088, 12}, {1188, 12}, {11188, 12}, {111288, 12}, {111588, 12},       \
        {111888, 12}, {111088, 12}, {197788, 12}, {17788, 12}, {27788, 12},    \
        {47788, 12}, {57788, 12}, {67788, 12}, {77788, 12}, {87788, 12},       \
        {97788, 12}, {107788, 12}, {117788, 12}, {1117788, 12},                \
        {11127788, 12}, {11157788, 12}, {11187788, 12}, {11107788, 12},        \
        {199988, 12}, {19988, 12}, {29988, 12}, {49988, 12}, {59988, 12},      \
        {69988, 12}, {79988, 12}, {89988, 12}, {99988, 12}, {109988, 12},      \
        {119988, 12}, {1119988, 12}, {11129988, 12}, {11159988, 12},           \
        {11189988, 12}, {11109988, 12}, {19779988, 12}, {1779988, 12},         \
        {2779988, 12}, {4779988, 12}, {5779988, 12}, {6779988, 12},            \
        {7779988, 12}, {8779988, 12}, {9779988, 12}, {10779988, 12},           \
        {11779988, 12}, {111779988, 12}, {1112779988, 12}, {1115779988, 12},   \
        {1118779988, 12}, {                                                    \
      1110779988, 13                                                           \
    }

  const std::map<int, int> std_map = { INIT_SEQ };
  constexpr frozen::map<int, int, 128> frozen_map = { INIT_SEQ };
  SECTION("checking size and content") {
    REQUIRE(std_map.size() == frozen_map.size());
    for (auto v : std_map) {
      REQUIRE(frozen_map.count(std::get<0>(v)));
      REQUIRE(frozen_map.find(std::get<0>(v))->second == v.second);
    }
    for (auto v : frozen_map)
      REQUIRE(std_map.count(std::get<0>(v)));
  }

  static_assert(std::is_same<typename decltype(std_map)::key_type,
                             typename decltype(frozen_map)::key_type>::value, "");
  static_assert(std::is_same<typename decltype(std_map)::mapped_type,
                             typename decltype(frozen_map)::mapped_type>::value, "");
}

TEST_CASE("frozen::map <> frozen::make_map", "[map]") {
  constexpr frozen::map<int, int, 128> frozen_map = { INIT_SEQ };
  constexpr auto frozen_map2 = frozen::make_map<int, int>({INIT_SEQ});
  constexpr auto frozen_map3 = frozen::make_map(std::array<std::pair<int, int>, 128>{{INIT_SEQ}});
  REQUIRE(std::equal(frozen_map2.begin(), frozen_map2.end(), frozen_map3.begin()));

  SECTION("checking size and content") {
    REQUIRE(frozen_map.size() == frozen_map2.size());
    for (auto v : frozen_map2)
      REQUIRE(frozen_map.count(std::get<0>(v)));
    for (auto v : frozen_map)
      REQUIRE(frozen_map2.count(std::get<0>(v)));
  }

  constexpr frozen::map<int, short, 0> frozen_empty_map = {};
  constexpr auto frozen_empty_map2 = frozen::make_map<int, short>();
  constexpr auto frozen_empty_map3 = frozen::make_map<int, short>({});

  SECTION("checking empty map") {
    REQUIRE(frozen_empty_map.empty());
    REQUIRE(frozen_empty_map.size() == 0);
    REQUIRE(frozen_empty_map.begin() == frozen_empty_map.end());

    REQUIRE(frozen_empty_map2.empty());
    REQUIRE(frozen_empty_map2.size() == 0);
    REQUIRE(frozen_empty_map2.begin() == frozen_empty_map2.end());

    REQUIRE(frozen_empty_map3.empty());
    REQUIRE(frozen_empty_map3.size() == 0);
    REQUIRE(frozen_empty_map3.begin() == frozen_empty_map3.end());
  }
}

TEST_CASE("frozen::map constexpr", "[map]") {
  constexpr frozen::map<int, unsigned, 2> ce = {{3,4}, {11,12}};
  static_assert(*ce.begin() == std::pair<int, unsigned>{3,4}, "");
  static_assert(*(ce.begin() + 1) == std::pair<int, unsigned>{11,12}, "");
  static_assert(ce.size() == 2, "");
  static_assert(ce.count(3), "");
  static_assert(!ce.count(0), "");
  static_assert(ce.find(0) == ce.end(), "");
  static_assert(ce.at(3) == 4, "");
}

TEST_CASE("Modifiable frozen::map", "[map]") {
  frozen::map<int, int, 3> frozen_map = {{0, 1}, {2, 3}, {4, 5}};

  SECTION("Lookup existing values") {
    REQUIRE(frozen_map.at(0) == 1);
    REQUIRE(frozen_map.find(0)->second == 1);
    REQUIRE(frozen_map.equal_range(0).first->second == 1);

    REQUIRE(frozen_map.at(2) == 3);
    REQUIRE(frozen_map.find(2)->second == 3);
    REQUIRE(frozen_map.equal_range(2).first->second == 3);

    REQUIRE(frozen_map.at(4) == 5);
    REQUIRE(frozen_map.find(4)->second == 5);
    REQUIRE(frozen_map.equal_range(4).first->second == 5);
  }

  SECTION("Lookup failure") {
    REQUIRE(frozen_map.find(5) == frozen_map.end());
    REQUIRE_THROWS_AS(frozen_map.at(5), std::out_of_range);
  }

  SECTION("Modify value") {
    frozen_map.at(0) = -1;
    REQUIRE(frozen_map.at(0) == -1);

    frozen_map.begin()->second = -2;
    REQUIRE(frozen_map.begin()->second == -2);

    (frozen_map.end() - 1)->second = -3;
    REQUIRE((frozen_map.end() - 1)->second == -3);

    frozen_map.equal_range(4).first->second = -5;
    REQUIRE(frozen_map.at(4) == -5);

    frozen_map.lower_bound(2)->second = -22;
    REQUIRE(frozen_map.at(2) == -22);

    frozen_map.upper_bound(2)->second = -33;
    REQUIRE(frozen_map.at(4) == -33);
  }
}

struct S {
  int x;
};
constexpr inline bool operator==(S const &a, S const &b) { return a.x == b.x; }
constexpr inline bool operator<(S const &a, S const &b) { return a.x < b.x; }
constexpr inline bool operator<(S const &a, int b) { return a.x < b; }
constexpr inline bool operator<(int a, S const &b) { return a < b.x; }

TEST_CASE("empty frozen transparent map", "[map]") {
  constexpr frozen::map<S, bool, 0, std::less<void>> ze_map{};

  constexpr auto count = ze_map.count(3);
  REQUIRE(count == 0);

  constexpr auto find = ze_map.find(5);
  REQUIRE(find == ze_map.end());

  constexpr auto range = ze_map.equal_range(0);
  REQUIRE(std::get<0>(range) == ze_map.end());
  REQUIRE(std::get<1>(range) == ze_map.end());

  constexpr auto lower_bound = ze_map.lower_bound(1);
  REQUIRE(lower_bound == ze_map.end());

  constexpr auto upper_bound = ze_map.upper_bound(1);
  REQUIRE(upper_bound == ze_map.end());
}

TEST_CASE("frozen transparent map", "[map]") {
  constexpr frozen::map<S, bool, 4, std::less<void>> ze_map{
      {S{10}, true}, {S{20}, false}, {S{30}, true}, {S{40}, false}};

  constexpr auto empty = ze_map.empty();
  REQUIRE(!empty);

  constexpr auto size = ze_map.size();
  REQUIRE(size == 4);

  constexpr auto max_size = ze_map.max_size();
  REQUIRE(max_size == 4);

  constexpr auto count1 = ze_map.count(1);
  REQUIRE(count1 == 0);

  constexpr auto count10 = ze_map.count(10);
  REQUIRE(count10 == 1);

  const auto find10 = ze_map.find(10);
  REQUIRE(find10 == ze_map.begin());
  REQUIRE(std::get<0>(*find10) == S{10});
  REQUIRE(std::get<1>(*find10) == true);

  const auto find15 = ze_map.find(15);
  REQUIRE(find15 == ze_map.end());

  const auto range0 = ze_map.equal_range(0);
  REQUIRE(std::get<0>(range0) == ze_map.end());
  REQUIRE(std::get<1>(range0) == ze_map.end());

  const auto range1 = ze_map.equal_range(10);
  REQUIRE(std::get<0>(range1) == ze_map.begin());
  REQUIRE(std::get<1>(range1) == ze_map.begin() + 1);

  const auto lower_bound0 = ze_map.lower_bound(0);
  REQUIRE(lower_bound0 == ze_map.end());

  for (auto val : ze_map) {
    const auto lower_bound = ze_map.lower_bound(std::get<0>(val));
    REQUIRE(lower_bound == ze_map.find(std::get<0>(val)));
  }

  const auto lower_bound2 = ze_map.lower_bound(50);
  REQUIRE(lower_bound2 == ze_map.end());

  const auto upper_bound0 = ze_map.upper_bound(0);
  REQUIRE(upper_bound0 == ze_map.end());

  const auto upper_bound1 = ze_map.upper_bound(10);
  REQUIRE(upper_bound1 == (ze_map.begin() + 1));

  const auto upper_bound2 = ze_map.upper_bound(50);
  REQUIRE(upper_bound2 == ze_map.end());

  auto const begin = ze_map.begin(), end = ze_map.end();
  REQUIRE((begin + ze_map.size()) == end);

  auto const key_comp = ze_map.key_comp();
  auto const key_comparison = key_comp(1, 2);
  REQUIRE(key_comparison);

  auto const value_comp = ze_map.value_comp();
  auto const value_comparison = value_comp(11, 12);
  REQUIRE(value_comparison);

  auto const cbegin = ze_map.cbegin(), cend = ze_map.cend();
  REQUIRE(cbegin == (cend - ze_map.size()));

  std::for_each(ze_map.begin(), ze_map.end(), [](std::tuple<S, bool>) {});
  REQUIRE((std::size_t)std::distance(ze_map.rbegin(), ze_map.rend()) == ze_map.size());
  REQUIRE(std::count(ze_map.crbegin(), ze_map.crend(),
                     decltype(ze_map)::value_type{S{20}, true}) == 0);
  REQUIRE(std::count(ze_map.crbegin(), ze_map.crend(),
                     decltype(ze_map)::value_type{S{20}, false}) == 1);
}

TEST_CASE("frozen::map <> frozen::make_map transparent", "[map]") {
  constexpr frozen::map<int, int, 128, std::less<void>> frozen_map = { INIT_SEQ };
  constexpr auto frozen_map2 = frozen::make_map<int, int, std::less<void>>({INIT_SEQ});
  constexpr auto frozen_map3 = frozen::make_map<int, int, std::less<void>>(std::array<std::pair<int, int>, 128>{{INIT_SEQ}});
  REQUIRE(std::equal(frozen_map2.begin(), frozen_map2.end(), frozen_map3.begin()));

  SECTION("checking size and content") {
    REQUIRE(frozen_map.size() == frozen_map2.size());
    for (auto v : frozen_map2)
      REQUIRE(frozen_map.count(std::get<0>(v)));
    for (auto v : frozen_map)
      REQUIRE(frozen_map2.count(std::get<0>(v)));
  }

  constexpr frozen::map<int, short, 0, std::less<void>> frozen_empty_map = {};
  constexpr auto frozen_empty_map2 = frozen::make_map<int, short, std::less<void>>();
  constexpr auto frozen_empty_map3 = frozen::make_map<int, short, std::less<void>>({});

  SECTION("checking empty map") {
    REQUIRE(frozen_empty_map.empty());
    REQUIRE(frozen_empty_map.size() == 0);
    REQUIRE(frozen_empty_map.begin() == frozen_empty_map.end());

    REQUIRE(frozen_empty_map2.empty());
    REQUIRE(frozen_empty_map2.size() == 0);
    REQUIRE(frozen_empty_map2.begin() == frozen_empty_map2.end());

    REQUIRE(frozen_empty_map3.empty());
    REQUIRE(frozen_empty_map3.size() == 0);
    REQUIRE(frozen_empty_map3.begin() == frozen_empty_map3.end());
  }
}