File: linear_map_tests.cc

package info (click to toggle)
scram 0.16.2-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,016 kB
  • sloc: xml: 120,766; cpp: 23,966; python: 1,256; ansic: 100; makefile: 9
file content (443 lines) | stat: -rw-r--r-- 11,326 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (C) 2016-2018 Olzhas Rakhimov
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "ext/linear_map.h"

#include <string>

#include <boost/container/vector.hpp>

#include <catch.hpp>

// Explicit instantiations with some common types.
template class ext::linear_map<int, int>;
template class ext::linear_map<int, double>;
template class ext::linear_map<int, std::string>;
template class ext::linear_map<std::string, std::string>;

namespace {
// The bare minimum class to be a key type for the linear map.
class KeyClass {
  friend bool operator==(const KeyClass& lhs, const KeyClass& rhs) {
    return lhs.a == rhs.a && lhs.b == rhs.b;
  }

  int a;
  std::string b;
};
}  // namespace
template class ext::linear_map<KeyClass, std::string>;

// Move erase policy instantiation.
template class ext::linear_map<KeyClass, std::string, ext::MoveEraser>;

#ifndef __INTEL_COMPILER
// Passing another underlying container types.
template class ext::linear_map<int, int, ext::DefaultEraser,
                               boost::container::vector>;
template class ext::linear_map<KeyClass, std::string, ext::MoveEraser,
                               boost::container::vector>;
#endif

namespace std {

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& out, const std::pair<T1, T2>& value) {
  out << "{" << Catch::StringMaker<T1>::convert(value.first) << ", "
      << Catch::StringMaker<T2>::convert(value.second) << "}";
  return out;
}

}  // namespace std

namespace scram::test {

using IntMap = ext::linear_map<int, int>;

TEST_CASE("linear map ctors", "[linear_map]") {
  SECTION("default ctor") {
    IntMap m_default;
    CHECK(m_default.size() == 0);
    CHECK(m_default.empty());
  }

  IntMap m_init_list = {{1, -1}, {2, -2}, {3, -3}};
  SECTION("initializer list") {
    CHECK(m_init_list.size() == 3);
    CHECK_FALSE(m_init_list.empty());
  }

  SECTION("copy ctor") {
    auto m_copy(m_init_list);
    CHECK(m_copy.size() == 3);
    CHECK_FALSE(m_copy.empty());
    CHECK(m_copy == m_init_list);

    SECTION("copy itself") {
      m_copy = m_copy;
      CHECK(m_copy.size() == 3);
      CHECK(m_copy == m_init_list);
    }
  }

  SECTION("move ctor") {
    auto construct_extra_copy(m_init_list);
    auto m_move(std::move(construct_extra_copy));
    CHECK(construct_extra_copy.empty());
    CHECK(m_move.size() == 3);
    CHECK_FALSE(m_move.empty());
    CHECK(m_move == m_init_list);
  }

  SECTION("copy assignment") {
    IntMap m_assign_copy;
    m_assign_copy = m_init_list;
    CHECK(m_assign_copy == m_init_list);
  }

  SECTION("move assignment") {
    IntMap m_assign_move;
    auto assign_extra_copy(m_init_list);
    m_assign_move = std::move(assign_extra_copy);
    CHECK(assign_extra_copy.empty());
    CHECK(m_assign_move == m_init_list);
  }

  SECTION("range ctor") {
    IntMap::container_type m_data = {{1, -1}, {2, -2}, {3, -3}, {3, -4}};
    IntMap m_range(m_data.begin(), m_data.end());
    CHECK(m_range == m_init_list);
  }

  SECTION("repeat") {
    IntMap m_repeat_init{{1, -1}, {2, -2}, {3, -3}, {3, -4}};
    CHECK(m_repeat_init == m_init_list);
  }
}

TEST_CASE("linear map equality", "[linear_map]") {
  IntMap m1;
  SECTION("compare to itself") { CHECK(m1 == m1); }

  IntMap m2;
  SECTION("compare to new") { CHECK(m2 == m1); }

  m1 = {{1, -1}, {2, -2}, {3, -3}};
  SECTION("with equal values") {
    CHECK(m1 == m1);
    CHECK_FALSE(m2 == m1);
    SECTION("copy") {
      m2 = m1;
      CHECK(m2 == m1);
    }
    SECTION("new with the same values") {
      m2 = {{1, -1}, {2, -2}, {3, -3}};
      CHECK(m2 == m1);
    }
    SECTION("new with different order") {
      m2 = {{2, -2}, {1, -1}, {3, -3}};
      CHECK(m2 == m1);
    }
  }
  SECTION("with unequal values") {
    SECTION("one less") {
      m2 = {{1, -1}, {2, -2}};
      CHECK_FALSE(m2 == m1);
    }
    SECTION("same keys but different values") {
      m2 = {{1, 1}, {2, 2}, {3, 3}};
      CHECK_FALSE(m2 == m1);
    }
  }
}

TEST_CASE("linear map iterators", "[linear_map]") {
  IntMap m = {{1, -1}, {2, -2}, {3, -3}};
  IntMap::container_type c = {{1, -1}, {2, -2}, {3, -3}};
  CHECK(std::next(m.begin(), m.size()) == m.end());
  CHECK(std::next(m.rbegin(), m.size()) == m.rend());
  CHECK(std::next(m.cbegin(), m.size()) == m.cend());
  CHECK(std::next(m.crbegin(), m.size()) == m.crend());

  const auto& m_ref = m;
  CHECK(std::next(m_ref.begin(), m_ref.size()) == m_ref.end());
  CHECK(std::next(m_ref.rbegin(), m_ref.size()) == m_ref.rend());
  CHECK(std::next(m_ref.cbegin(), m_ref.size()) == m_ref.cend());
  CHECK(std::next(m_ref.crbegin(), m_ref.size()) == m_ref.crend());

  auto it_c = c.begin();
  int num_elements = 0;
  int key_sum = 0;
  int value_sum = 0;
  for (const auto& entry : m) {
    INFO(std::distance(c.begin(), it_c));
    CHECK(*it_c++ == entry);
    ++num_elements;
    key_sum += entry.first;
    value_sum += entry.second;
  }
  CHECK(num_elements == 3);
  CHECK(key_sum == 6);
  CHECK(value_sum == -6);
  CHECK(m.data() == c);
}

SCENARIO("linear map clear", "[linear_map]") {
  GIVEN("empty linear map") {
    IntMap m;
    REQUIRE(m.empty());
    REQUIRE(m.capacity() >= m.size());
    auto init_capacity = m.capacity();

    WHEN("clear") {
      m.clear();
      THEN("no change") {
        CHECK(m.empty());
        CHECK(m.capacity() == init_capacity);
      }
    }
  }

  GIVEN("non empty linear map") {
    IntMap m = {{1, -1}, {2, -2}, {3, -3}};
    CHECK_FALSE(m.empty());
    REQUIRE(m.capacity() >= m.size());
    auto init_capacity = m.capacity();

    WHEN("clear") {
      m.clear();
      THEN("the map is empty but capacity is not") {
        CHECK(m.empty());
        CHECK(m.capacity() == init_capacity);
      }
    }
  }
}

SCENARIO("linear map capacity reserve", "[linear_map]") {
  GIVEN("A linear map with some items") {
    IntMap m = {{1, -1}, {2, -2}, {3, -3}};

    REQUIRE(m.size() == 3);
    REQUIRE(m.capacity() >= 3);

    WHEN("the capacity is increased") {
      m.reserve(10);

      THEN("the capacity change but not size") {
        REQUIRE(m.size() == 3);
        REQUIRE(m.capacity() >= 10);
      }
    }
    WHEN("the capacity is reduced") {
      m.reserve(0);

      THEN("the size and capacity do not change") {
        REQUIRE(m.size() == 3);
        REQUIRE(m.capacity() >= 3);
      }
    }
  }
}

TEST_CASE("linear map swap", "[linear_map]") {
  const IntMap m1 = {{1, -1}, {2, -2}, {3, -3}};
  const IntMap m2 = {{4, -4}, {5, -5}};
  IntMap ms1 = m1;
  IntMap ms2 = m2;

  SECTION("member swap") {
    ms1.swap(ms2);
    CHECK(ms2 == m1);
    CHECK(ms1 == m2);
  }

  SECTION("ADL swap") {
    swap(ms1, ms2);
    CHECK(ms2 == m1);
    CHECK(ms1 == m2);
  }

  SECTION("STD swap") {
    std::swap(ms1, ms2);
    CHECK(ms2 == m1);
    CHECK(ms1 == m2);
  }
}

TEST_CASE("linear map default erase", "[linear_map]") {
  IntMap m = {{1, -1}, {2, -2}, {3, -3}};
  IntMap m_expected = {{2, -2}, {3, -3}};

  SECTION("erase w/ key") {
    m.erase(1);
    CHECK(m == m_expected);
    CHECK(m.data() == m_expected.data());
  }

  SECTION("erase w/ iterator") {
    m.erase(m.begin());
    CHECK(m == m_expected);
    CHECK(m.data() == m_expected.data());
  }

  SECTION("erase w/ const iterator") {
    m.erase(m.cbegin());
    CHECK(m == m_expected);
    CHECK(m.data() == m_expected.data());
  }
}

TEST_CASE("linear map move erase", "[linear_map]") {
  using MoveMap = ext::linear_map<int, int, ext::MoveEraser>;
  MoveMap m = {{1, -1}, {2, -2}, {3, -3}};
  MoveMap m_expected = {{3, -3}, {2, -2}};

  SECTION("erase w/ key") {
    m.erase(1);
    CHECK(m == m_expected);
    CHECK(m.data() == m_expected.data());
  }

  SECTION("erase w/ iterator") {
    m.erase(m.begin());
    CHECK(m == m_expected);
    CHECK(m.data() == m_expected.data());
  }

  SECTION("erase w/ const iterator") {
    m.erase(m.cbegin());
    CHECK(m == m_expected);
    CHECK(m.data() == m_expected.data());
  }
}

TEST_CASE("linear map find", "[linear_map]") {
  IntMap m = {{1, -1}, {2, -2}, {3, -3}};

  CHECK(m.count(1) == 1);
  CHECK(m.count(5) == 0);

  CHECK(m.find(1) == m.begin());
  CHECK(m.find(1)->first == 1);
  int key = 2;
  CHECK(m.find(key) == std::next(m.begin(), 1));
  CHECK(m.find(key)->first == key);
  CHECK(m.find(3) == std::next(m.begin(), 2));
  CHECK(m.find(5) == m.end());
}

TEST_CASE("linear map operator index", "[linear_map]") {
  IntMap m;
  m[1] = -1;
  int k = 2;
  m[k] = -2;
  m[3] = -3;
  IntMap expected = {{1, -1}, {2, -2}, {3, -3}};
  REQUIRE(m == expected);

  m[3] = -4;
  IntMap changed = {{1, -1}, {2, -2}, {3, -4}};
  REQUIRE(m == changed);
}

TEST_CASE("linear map at", "[linear_map]") {
  IntMap m = {{1, -1}, {2, -2}, {3, -3}};
  CHECK(m.at(1) == -1);
  CHECK_THROWS_AS(m.at(5), std::out_of_range);

  const auto& m_ref = m;
  CHECK(m_ref.at(2) == -2);

  m.at(2) = -4;
  IntMap m_expected = {{1, -1}, {2, -4}, {3, -3}};
  CHECK(m == m_expected);
}

TEST_CASE("linear map insert single", "[linear_map]") {
  IntMap m;
  auto ret = m.insert({1, -1});
  CHECK(ret.second);
  REQUIRE_FALSE(ret.first == m.end());
  CHECK(ret.first == m.begin());
  CHECK(ret.first->first == 1);
  CHECK(ret.first->second == -1);

  IntMap::value_type v = {2, -2};
  ret = m.insert(v);
  CHECK(ret.second);
  CHECK(ret.first == std::next(m.begin(), 1));
  CHECK(ret.first->first == 2);
  CHECK(ret.first->second == -2);

  auto repeat = m.insert({2, -3});
  CHECK_FALSE(repeat.second);
  CHECK(ret.first == repeat.first);

  m.insert({3, -3});
  IntMap expected = {{1, -1}, {2, -2}, {3, -3}};
  CHECK(m == expected);
}

TEST_CASE("linear map insert range", "[linear_map]") {
  IntMap m;
  std::vector<std::pair<int, int>> data = {{1, -1}, {2, -2}, {3, -3}, {3, -4}};
  IntMap expected = {{1, -1}, {2, -2}, {3, -3}};

  SECTION("equal iterators") {
    m.insert(data.begin(), data.begin());
    CHECK(m.empty());
  }

  SECTION("shift two") {
    m.insert(data.begin(), data.begin() + 2);
    CHECK(m.size() == 2);
  }

  SECTION("begin to end") {
    m.insert(data.begin(), data.end());
    CHECK(m.size() == expected.size());
    CHECK(m == expected);
  }
}

TEST_CASE("linear map emplace", "[linear_map]") {
  IntMap m;
  auto ret = m.emplace(1, -1);
  CHECK(ret.second);
  CHECK(ret.first == m.begin());
  CHECK(ret.first->first == 1);
  CHECK(ret.first->second == -1);

  int k = 2;
  int v = -2;
  ret = m.emplace(k, v);
  CHECK(ret.second);
  CHECK(ret.first == std::next(m.begin()));
  CHECK(ret.first->first == 2);
  CHECK(ret.first->second == -2);

  auto repeat = m.emplace(2, -3);
  CHECK_FALSE(repeat.second);
  CHECK(repeat.first == ret.first);

  m.emplace(3, -3);
  IntMap expected = {{1, -1}, {2, -2}, {3, -3}};
  CHECK(m == expected);
}

}  // namespace scram::test