aboutsummaryrefslogtreecommitdiff
path: root/test/test-invalset.cc
blob: ac53a04d25db4c0baab7ab368bb57fd5710bb011 (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
// Copyright 2014, VIXL authors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   * Redistributions of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//   * Redistributions in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//   * Neither the name of ARM Limited nor the names of its contributors may be
//     used to endorse or promote products derived from this software without
//     specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "invalset-vixl.h"
#include "test-runner.h"

namespace vixl {

// This file contains tests for the `InvalSet` and `InvalSetIterator` classes.

#define TEST(name) TEST_(INVALSET_##name)

typedef ptrdiff_t KeyType;
typedef ptrdiff_t ValType;

// We test with an object for which the key and the value are distinct.
class Obj {
 public:
  Obj() {}
  Obj(KeyType key, ValType val) : key_(key), val_(val) {}
  KeyType key_;
  ValType val_;

  bool operator==(const Obj& other) const {
    return (key_ == other.key_) && (val_ == other.val_);
  }
  bool operator<(const Obj& other) const {
    return (key_ < other.key_) || ((key_ == other.key_) && (val_ < other.val_));
  }
  bool operator<=(const Obj& other) const {
    return (key_ <= other.key_) ||
           ((key_ == other.key_) && (val_ <= other.val_));
  }
  bool operator>(const Obj& other) const {
    return (key_ > other.key_) || ((key_ == other.key_) && (val_ > other.val_));
  }
};

static const unsigned kNPreallocatedElements = 8;
static const KeyType kInvalidKey = PTRDIFF_MAX;
static const size_t kReclaimFrom = 1000;
static const unsigned kReclaimFactor = 10;

typedef InvalSet<Obj,
                 kNPreallocatedElements,
                 KeyType,
                 kInvalidKey,
                 kReclaimFrom,
                 kReclaimFactor>
    TestSet;

template <>
inline KeyType InvalSet<Obj,
                        kNPreallocatedElements,
                        KeyType,
                        kInvalidKey,
                        kReclaimFrom,
                        kReclaimFactor>::GetKey(const Obj& obj) {
  return obj.key_;
}
template <>
inline void InvalSet<Obj,
                     kNPreallocatedElements,
                     KeyType,
                     kInvalidKey,
                     kReclaimFrom,
                     kReclaimFactor>::SetKey(Obj* obj, KeyType key) {
  obj->key_ = key;
}


TEST(basic_test) {
  TestSet set;
  VIXL_CHECK(set.empty() && (set.size() == 0));

  for (unsigned i = 0; i < kNPreallocatedElements; i++) {
    set.insert(Obj(i, i));
  }
  VIXL_CHECK(set.size() == kNPreallocatedElements);

  set.insert(Obj(-123, 456));
  set.insert(Obj(2718, 2871828));
  VIXL_CHECK(set.size() == kNPreallocatedElements + 2);
  VIXL_CHECK(set.GetMinElement() == Obj(-123, 456));

  set.erase(Obj(-123, 456));
  VIXL_CHECK(set.GetMinElementKey() == 0);

  set.clear();
  VIXL_CHECK(set.empty() && (set.size() == 0));
}


TEST(valid_element) {
  VIXL_CHECK(TestSet::IsValid(Obj(0, 0)));
  VIXL_CHECK(TestSet::IsValid(Obj(-1, 0)));
  VIXL_CHECK(TestSet::IsValid(Obj(kInvalidKey - 1, 0)));
  VIXL_CHECK(!TestSet::IsValid(Obj(kInvalidKey, 0)));
}


TEST(insert) {
  TestSet set;
  VIXL_CHECK(set.empty() && (set.size() == 0));

  for (unsigned i = 0; i < kNPreallocatedElements; i++) {
    set.insert(Obj(i, i));
  }
  VIXL_CHECK(set.size() == kNPreallocatedElements);
  set.insert(Obj(-123, 1));
  VIXL_CHECK(set.size() == kNPreallocatedElements + 1);
  set.insert(Obj(-123, 2));
  set.insert(Obj(-123, 3));
  VIXL_CHECK(set.size() == kNPreallocatedElements + 3);

  set.clear();
  VIXL_CHECK(set.empty() && (set.size() == 0));
}


TEST(erase) {
  TestSet set;
  VIXL_CHECK(set.empty() && (set.size() == 0));

  // Test with only preallocated elements in the set.
  VIXL_STATIC_ASSERT(kNPreallocatedElements >= 2);
  set.insert(Obj(2718, 0));
  set.erase(Obj(2718, 0));
  VIXL_CHECK(set.empty() && (set.size() == 0));
  set.insert(Obj(2718, 0));
  VIXL_CHECK(set.size() == 1);
  set.insert(Obj(2718, 1));
  VIXL_CHECK(set.size() == 2);
  set.erase(Obj(2718, 0));
  VIXL_CHECK(set.size() == 1);

  // Test with more elements.
  for (unsigned i = 0; i < 100 * kNPreallocatedElements; i++) {
    set.insert(Obj(i * i, i % 30));
    set.insert(Obj(i, -1));
  }
  size_t max_size = set.size();
  set.erase(Obj(100, -1));
  VIXL_CHECK(set.size() == max_size - 1);
  for (size_t i = 2; i <= max_size; i++) {
    set.erase(set.GetMinElement());
    VIXL_CHECK(set.size() == max_size - i);
  }

  VIXL_CHECK(set.empty() && (set.size() == 0));
}


TEST(min) {
  TestSet set;
  VIXL_CHECK(set.empty() && (set.size() == 0));

  // Test with only preallocated elements in the set.
  VIXL_STATIC_ASSERT(kNPreallocatedElements >= 4);
  set.insert(Obj(-1, -1));
  set.insert(Obj(-1, 0));
  set.insert(Obj(0, 0));
  set.insert(Obj(1, 0));
  VIXL_CHECK(set.GetMinElement() == Obj(-1, -1));
  VIXL_CHECK(set.GetMinElementKey() == -1);
  VIXL_CHECK(set.GetMinElement().key_ == set.GetMinElementKey());

  // Test with more elements.
  set.clear();
  int max_index = 100 * kNPreallocatedElements;
  for (int i = 0; i <= max_index; i++) {
    // Insert elements out of order.
    int sign = ((i % 2) == 0) ? -1 : 1;
    set.insert(Obj(sign * i, i));
  }
  VIXL_CHECK(set.GetMinElement() == Obj(-max_index, max_index));
  VIXL_CHECK(set.GetMinElement().key_ == set.GetMinElementKey());

  set.erase(Obj(0, 0));
  VIXL_CHECK(set.GetMinElement() == Obj(-max_index, max_index));
  set.erase(set.GetMinElement());
  VIXL_CHECK(set.GetMinElement() == Obj(-(max_index - 2), max_index - 2));

  set.clear();
  VIXL_CHECK(set.empty() && (set.size() == 0));
}


TEST(iterator) {
  TestSet set;
  VIXL_CHECK(set.empty() && (set.size() == 0));

  // Ensure that set.begin() == set.end() for the empty set.
  VIXL_CHECK(set.begin() == set.end());

  // Test with only preallocated elements in the set.
  size_t expected_total = 0;
  for (unsigned i = 0; i < kNPreallocatedElements; i++) {
    set.insert(Obj(i, i));
    expected_total += i;
  }

  size_t size = 0;
  size_t total = 0;
  for (InvalSetIterator<TestSet> it = set.begin(); it != set.end(); ++it) {
    total += it->val_;
    size++;
  }
  VIXL_CHECK(size == set.size());
  VIXL_CHECK(total == expected_total);

  // Test with more elements.
  for (unsigned i = kNPreallocatedElements; i < 4 * kNPreallocatedElements;
       i++) {
    set.insert(Obj(i, i));
    expected_total += i;
  }

  size = 0;
  total = 0;
  for (InvalSetIterator<TestSet> it = set.begin(); it != set.end(); ++it) {
    total += it->val_;
    size++;
  }
  VIXL_CHECK(size == set.size());
  VIXL_CHECK(total == expected_total);

  // Test after elements have been deleted.
  // - Select an interesting list of elements to erase.
  std::vector<Obj> to_erase;
  unsigned step = 0;
  for (unsigned i = 0; i < set.size(); i += step, step++) {
    to_erase.push_back(Obj(i, i));
  }
  to_erase.push_back(Obj(set.size() - 1, set.size() - 1));  // The last element.
  to_erase.push_back(Obj(set.size(), set.size()));          // Not in the set.

  // - Erase one at a time, retesting after each one.
  while (!to_erase.empty()) {
    size_t erased = set.erase(to_erase.back());
    if (erased > 0) {
      VIXL_CHECK(erased == 1);
      expected_total -= to_erase.back().val_;
    } else {
    }
    to_erase.pop_back();

    size = 0;
    total = 0;
    for (InvalSetIterator<TestSet> it = set.begin(); it != set.end(); ++it) {
      total += it->val_;
      size++;
    }
    VIXL_CHECK(size == set.size());
    VIXL_CHECK(total == expected_total);
  }
}


#if __cplusplus >= 201103L
TEST(iterator_cxx11) {
  TestSet set;
  VIXL_CHECK(set.empty() && (set.size() == 0));

  // Test with only preallocated elements in the set.
  size_t expected_total = 0;
  for (unsigned i = 0; i < kNPreallocatedElements; i++) {
    set.insert(Obj(i, i));
    expected_total += i;
  }

  size_t size = 0;
  size_t total = 0;
  for (auto object : set) {
    total += object.val_;
    size++;
  }
  VIXL_CHECK(size == set.size());
  VIXL_CHECK(total == expected_total);

  // Test with more elements.
  for (unsigned i = kNPreallocatedElements; i < 4 * kNPreallocatedElements;
       i++) {
    set.insert(Obj(i, i));
    expected_total += i;
  }

  size = 0;
  total = 0;
  for (auto object : set) {
    total += object.val_;
    size++;
  }
  VIXL_CHECK(size == set.size());
  VIXL_CHECK(total == expected_total);

  // Test after elements have been deleted.
  // - Select an interesting list of elements to erase.
  std::vector<Obj> to_erase;
  unsigned step = 0;
  for (unsigned i = 0; i < set.size(); i += step, step++) {
    to_erase.push_back(Obj(i, i));
  }
  to_erase.push_back(Obj(set.size() - 1, set.size() - 1));  // The last element.
  to_erase.push_back(Obj(set.size(), set.size()));          // Not in the set.

  // - Erase one at a time, retesting after each one.
  while (!to_erase.empty()) {
    size_t erased = set.erase(to_erase.back());
    if (erased > 0) {
      VIXL_CHECK(erased == 1);
      expected_total -= to_erase.back().val_;
    } else {
    }
    to_erase.pop_back();

    size = 0;
    total = 0;
    for (auto object : set) {
      total += object.val_;
      size++;
    }
    VIXL_CHECK(size == set.size());
    VIXL_CHECK(total == expected_total);
  }
}
#endif


TEST(stl_forward_iterator) {
  {
    TestSet::iterator default_it;           // Default-constructible.
    TestSet::iterator copy_it(default_it);  // Copy-constructible.
    copy_it = default_it;                   // Copy-assignable.
  }                                         // Destructible.

  TestSet set1;
  VIXL_CHECK(set1.empty() && (set1.size() == 0));

  TestSet set2;
  VIXL_CHECK(set2.empty() && (set2.size() == 0));

  // Test with only preallocated elements in the set.
  for (unsigned i = 0; i < kNPreallocatedElements; i++) {
    set1.insert(Obj(i, 1));
    set2.insert(Obj(i, 2));
  }

  TestSet::iterator it1_a = set1.begin();
  TestSet::iterator it1_b = set1.begin();

  // Incrementable (whilst valid).
  it1_a++;
  ++it1_b;

  // Testable for equivalence.
  VIXL_CHECK(it1_a == it1_b);
  VIXL_CHECK(set1.begin() != set1.end());
  VIXL_CHECK(set2.begin() != set2.end());
  VIXL_CHECK(set1.begin() != set2.begin());
  VIXL_CHECK(set1.end() != set2.end());

  // Dereferencable.
  VIXL_CHECK((*it1_a++).key_ == 1);
  VIXL_CHECK(((*it1_a).key_ == 2) && ((*it1_a).val_ == 1));
  *it1_b = Obj(42, 1);
  VIXL_CHECK((it1_b->key_ == 42) && (it1_b->val_ == 1));

#if __cplusplus >= 201103L
  // Swappable.
  std::swap(it1_a, it1_b);
  VIXL_CHECK(it1_a->key_ == 42);
  VIXL_CHECK(it1_b->key_ == 2);
#endif
}


}  // namespace vixl