1b8021494Sopenharmony_ci// Copyright 2014, VIXL authors
2b8021494Sopenharmony_ci// All rights reserved.
3b8021494Sopenharmony_ci//
4b8021494Sopenharmony_ci// Redistribution and use in source and binary forms, with or without
5b8021494Sopenharmony_ci// modification, are permitted provided that the following conditions are met:
6b8021494Sopenharmony_ci//
7b8021494Sopenharmony_ci//   * Redistributions of source code must retain the above copyright notice,
8b8021494Sopenharmony_ci//     this list of conditions and the following disclaimer.
9b8021494Sopenharmony_ci//   * Redistributions in binary form must reproduce the above copyright notice,
10b8021494Sopenharmony_ci//     this list of conditions and the following disclaimer in the documentation
11b8021494Sopenharmony_ci//     and/or other materials provided with the distribution.
12b8021494Sopenharmony_ci//   * Neither the name of ARM Limited nor the names of its contributors may be
13b8021494Sopenharmony_ci//     used to endorse or promote products derived from this software without
14b8021494Sopenharmony_ci//     specific prior written permission.
15b8021494Sopenharmony_ci//
16b8021494Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17b8021494Sopenharmony_ci// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18b8021494Sopenharmony_ci// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19b8021494Sopenharmony_ci// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20b8021494Sopenharmony_ci// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21b8021494Sopenharmony_ci// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22b8021494Sopenharmony_ci// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23b8021494Sopenharmony_ci// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24b8021494Sopenharmony_ci// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25b8021494Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26b8021494Sopenharmony_ci
27b8021494Sopenharmony_ci#include "invalset-vixl.h"
28b8021494Sopenharmony_ci#include "test-runner.h"
29b8021494Sopenharmony_ci
30b8021494Sopenharmony_cinamespace vixl {
31b8021494Sopenharmony_ci
32b8021494Sopenharmony_ci// This file contains tests for the `InvalSet` and `InvalSetIterator` classes.
33b8021494Sopenharmony_ci
34b8021494Sopenharmony_ci#define TEST(name) TEST_(INVALSET_##name)
35b8021494Sopenharmony_ci
36b8021494Sopenharmony_citypedef ptrdiff_t KeyType;
37b8021494Sopenharmony_citypedef ptrdiff_t ValType;
38b8021494Sopenharmony_ci
39b8021494Sopenharmony_ci// We test with an object for which the key and the value are distinct.
40b8021494Sopenharmony_ciclass Obj {
41b8021494Sopenharmony_ci public:
42b8021494Sopenharmony_ci  Obj() {}
43b8021494Sopenharmony_ci  Obj(KeyType key, ValType val) : key_(key), val_(val) {}
44b8021494Sopenharmony_ci  KeyType key_;
45b8021494Sopenharmony_ci  ValType val_;
46b8021494Sopenharmony_ci
47b8021494Sopenharmony_ci  bool operator==(const Obj& other) const {
48b8021494Sopenharmony_ci    return (key_ == other.key_) && (val_ == other.val_);
49b8021494Sopenharmony_ci  }
50b8021494Sopenharmony_ci  bool operator<(const Obj& other) const {
51b8021494Sopenharmony_ci    return (key_ < other.key_) || ((key_ == other.key_) && (val_ < other.val_));
52b8021494Sopenharmony_ci  }
53b8021494Sopenharmony_ci  bool operator<=(const Obj& other) const {
54b8021494Sopenharmony_ci    return (key_ <= other.key_) ||
55b8021494Sopenharmony_ci           ((key_ == other.key_) && (val_ <= other.val_));
56b8021494Sopenharmony_ci  }
57b8021494Sopenharmony_ci  bool operator>(const Obj& other) const {
58b8021494Sopenharmony_ci    return (key_ > other.key_) || ((key_ == other.key_) && (val_ > other.val_));
59b8021494Sopenharmony_ci  }
60b8021494Sopenharmony_ci};
61b8021494Sopenharmony_ci
62b8021494Sopenharmony_cistatic const unsigned kNPreallocatedElements = 8;
63b8021494Sopenharmony_cistatic const KeyType kInvalidKey = PTRDIFF_MAX;
64b8021494Sopenharmony_cistatic const size_t kReclaimFrom = 1000;
65b8021494Sopenharmony_cistatic const unsigned kReclaimFactor = 10;
66b8021494Sopenharmony_ci
67b8021494Sopenharmony_citypedef InvalSet<Obj,
68b8021494Sopenharmony_ci                 kNPreallocatedElements,
69b8021494Sopenharmony_ci                 KeyType,
70b8021494Sopenharmony_ci                 kInvalidKey,
71b8021494Sopenharmony_ci                 kReclaimFrom,
72b8021494Sopenharmony_ci                 kReclaimFactor>
73b8021494Sopenharmony_ci    TestSet;
74b8021494Sopenharmony_ci
75b8021494Sopenharmony_citemplate <>
76b8021494Sopenharmony_ciinline KeyType InvalSet<Obj,
77b8021494Sopenharmony_ci                        kNPreallocatedElements,
78b8021494Sopenharmony_ci                        KeyType,
79b8021494Sopenharmony_ci                        kInvalidKey,
80b8021494Sopenharmony_ci                        kReclaimFrom,
81b8021494Sopenharmony_ci                        kReclaimFactor>::GetKey(const Obj& obj) {
82b8021494Sopenharmony_ci  return obj.key_;
83b8021494Sopenharmony_ci}
84b8021494Sopenharmony_citemplate <>
85b8021494Sopenharmony_ciinline void InvalSet<Obj,
86b8021494Sopenharmony_ci                     kNPreallocatedElements,
87b8021494Sopenharmony_ci                     KeyType,
88b8021494Sopenharmony_ci                     kInvalidKey,
89b8021494Sopenharmony_ci                     kReclaimFrom,
90b8021494Sopenharmony_ci                     kReclaimFactor>::SetKey(Obj* obj, KeyType key) {
91b8021494Sopenharmony_ci  obj->key_ = key;
92b8021494Sopenharmony_ci}
93b8021494Sopenharmony_ci
94b8021494Sopenharmony_ci
95b8021494Sopenharmony_ciTEST(basic_test) {
96b8021494Sopenharmony_ci  TestSet set;
97b8021494Sopenharmony_ci  VIXL_CHECK(set.empty() && (set.size() == 0));
98b8021494Sopenharmony_ci
99b8021494Sopenharmony_ci  for (unsigned i = 0; i < kNPreallocatedElements; i++) {
100b8021494Sopenharmony_ci    set.insert(Obj(i, i));
101b8021494Sopenharmony_ci  }
102b8021494Sopenharmony_ci  VIXL_CHECK(set.size() == kNPreallocatedElements);
103b8021494Sopenharmony_ci
104b8021494Sopenharmony_ci  set.insert(Obj(-123, 456));
105b8021494Sopenharmony_ci  set.insert(Obj(2718, 2871828));
106b8021494Sopenharmony_ci  VIXL_CHECK(set.size() == kNPreallocatedElements + 2);
107b8021494Sopenharmony_ci  VIXL_CHECK(set.GetMinElement() == Obj(-123, 456));
108b8021494Sopenharmony_ci
109b8021494Sopenharmony_ci  set.erase(Obj(-123, 456));
110b8021494Sopenharmony_ci  VIXL_CHECK(set.GetMinElementKey() == 0);
111b8021494Sopenharmony_ci
112b8021494Sopenharmony_ci  set.clear();
113b8021494Sopenharmony_ci  VIXL_CHECK(set.empty() && (set.size() == 0));
114b8021494Sopenharmony_ci}
115b8021494Sopenharmony_ci
116b8021494Sopenharmony_ci
117b8021494Sopenharmony_ciTEST(valid_element) {
118b8021494Sopenharmony_ci  VIXL_CHECK(TestSet::IsValid(Obj(0, 0)));
119b8021494Sopenharmony_ci  VIXL_CHECK(TestSet::IsValid(Obj(-1, 0)));
120b8021494Sopenharmony_ci  VIXL_CHECK(TestSet::IsValid(Obj(kInvalidKey - 1, 0)));
121b8021494Sopenharmony_ci  VIXL_CHECK(!TestSet::IsValid(Obj(kInvalidKey, 0)));
122b8021494Sopenharmony_ci}
123b8021494Sopenharmony_ci
124b8021494Sopenharmony_ci
125b8021494Sopenharmony_ciTEST(insert) {
126b8021494Sopenharmony_ci  TestSet set;
127b8021494Sopenharmony_ci  VIXL_CHECK(set.empty() && (set.size() == 0));
128b8021494Sopenharmony_ci
129b8021494Sopenharmony_ci  for (unsigned i = 0; i < kNPreallocatedElements; i++) {
130b8021494Sopenharmony_ci    set.insert(Obj(i, i));
131b8021494Sopenharmony_ci  }
132b8021494Sopenharmony_ci  VIXL_CHECK(set.size() == kNPreallocatedElements);
133b8021494Sopenharmony_ci  set.insert(Obj(-123, 1));
134b8021494Sopenharmony_ci  VIXL_CHECK(set.size() == kNPreallocatedElements + 1);
135b8021494Sopenharmony_ci  set.insert(Obj(-123, 2));
136b8021494Sopenharmony_ci  set.insert(Obj(-123, 3));
137b8021494Sopenharmony_ci  VIXL_CHECK(set.size() == kNPreallocatedElements + 3);
138b8021494Sopenharmony_ci
139b8021494Sopenharmony_ci  set.clear();
140b8021494Sopenharmony_ci  VIXL_CHECK(set.empty() && (set.size() == 0));
141b8021494Sopenharmony_ci}
142b8021494Sopenharmony_ci
143b8021494Sopenharmony_ci
144b8021494Sopenharmony_ciTEST(erase) {
145b8021494Sopenharmony_ci  TestSet set;
146b8021494Sopenharmony_ci  VIXL_CHECK(set.empty() && (set.size() == 0));
147b8021494Sopenharmony_ci
148b8021494Sopenharmony_ci  // Test with only preallocated elements in the set.
149b8021494Sopenharmony_ci  VIXL_STATIC_ASSERT(kNPreallocatedElements >= 2);
150b8021494Sopenharmony_ci  set.insert(Obj(2718, 0));
151b8021494Sopenharmony_ci  set.erase(Obj(2718, 0));
152b8021494Sopenharmony_ci  VIXL_CHECK(set.empty() && (set.size() == 0));
153b8021494Sopenharmony_ci  set.insert(Obj(2718, 0));
154b8021494Sopenharmony_ci  VIXL_CHECK(set.size() == 1);
155b8021494Sopenharmony_ci  set.insert(Obj(2718, 1));
156b8021494Sopenharmony_ci  VIXL_CHECK(set.size() == 2);
157b8021494Sopenharmony_ci  set.erase(Obj(2718, 0));
158b8021494Sopenharmony_ci  VIXL_CHECK(set.size() == 1);
159b8021494Sopenharmony_ci
160b8021494Sopenharmony_ci  // Test with more elements.
161b8021494Sopenharmony_ci  for (unsigned i = 0; i < 100 * kNPreallocatedElements; i++) {
162b8021494Sopenharmony_ci    set.insert(Obj(i * i, i % 30));
163b8021494Sopenharmony_ci    set.insert(Obj(i, -1));
164b8021494Sopenharmony_ci  }
165b8021494Sopenharmony_ci  size_t max_size = set.size();
166b8021494Sopenharmony_ci  set.erase(Obj(100, -1));
167b8021494Sopenharmony_ci  VIXL_CHECK(set.size() == max_size - 1);
168b8021494Sopenharmony_ci  for (size_t i = 2; i <= max_size; i++) {
169b8021494Sopenharmony_ci    set.erase(set.GetMinElement());
170b8021494Sopenharmony_ci    VIXL_CHECK(set.size() == max_size - i);
171b8021494Sopenharmony_ci  }
172b8021494Sopenharmony_ci
173b8021494Sopenharmony_ci  VIXL_CHECK(set.empty() && (set.size() == 0));
174b8021494Sopenharmony_ci}
175b8021494Sopenharmony_ci
176b8021494Sopenharmony_ci
177b8021494Sopenharmony_ciTEST(min) {
178b8021494Sopenharmony_ci  TestSet set;
179b8021494Sopenharmony_ci  VIXL_CHECK(set.empty() && (set.size() == 0));
180b8021494Sopenharmony_ci
181b8021494Sopenharmony_ci  // Test with only preallocated elements in the set.
182b8021494Sopenharmony_ci  VIXL_STATIC_ASSERT(kNPreallocatedElements >= 4);
183b8021494Sopenharmony_ci  set.insert(Obj(-1, -1));
184b8021494Sopenharmony_ci  set.insert(Obj(-1, 0));
185b8021494Sopenharmony_ci  set.insert(Obj(0, 0));
186b8021494Sopenharmony_ci  set.insert(Obj(1, 0));
187b8021494Sopenharmony_ci  VIXL_CHECK(set.GetMinElement() == Obj(-1, -1));
188b8021494Sopenharmony_ci  VIXL_CHECK(set.GetMinElementKey() == -1);
189b8021494Sopenharmony_ci  VIXL_CHECK(set.GetMinElement().key_ == set.GetMinElementKey());
190b8021494Sopenharmony_ci
191b8021494Sopenharmony_ci  // Test with more elements.
192b8021494Sopenharmony_ci  set.clear();
193b8021494Sopenharmony_ci  int max_index = 100 * kNPreallocatedElements;
194b8021494Sopenharmony_ci  for (int i = 0; i <= max_index; i++) {
195b8021494Sopenharmony_ci    // Insert elements out of order.
196b8021494Sopenharmony_ci    int sign = ((i % 2) == 0) ? -1 : 1;
197b8021494Sopenharmony_ci    set.insert(Obj(sign * i, i));
198b8021494Sopenharmony_ci  }
199b8021494Sopenharmony_ci  VIXL_CHECK(set.GetMinElement() == Obj(-max_index, max_index));
200b8021494Sopenharmony_ci  VIXL_CHECK(set.GetMinElement().key_ == set.GetMinElementKey());
201b8021494Sopenharmony_ci
202b8021494Sopenharmony_ci  set.erase(Obj(0, 0));
203b8021494Sopenharmony_ci  VIXL_CHECK(set.GetMinElement() == Obj(-max_index, max_index));
204b8021494Sopenharmony_ci  set.erase(set.GetMinElement());
205b8021494Sopenharmony_ci  VIXL_CHECK(set.GetMinElement() == Obj(-(max_index - 2), max_index - 2));
206b8021494Sopenharmony_ci
207b8021494Sopenharmony_ci  set.clear();
208b8021494Sopenharmony_ci  VIXL_CHECK(set.empty() && (set.size() == 0));
209b8021494Sopenharmony_ci}
210b8021494Sopenharmony_ci
211b8021494Sopenharmony_ci
212b8021494Sopenharmony_ciTEST(iterator) {
213b8021494Sopenharmony_ci  TestSet set;
214b8021494Sopenharmony_ci  VIXL_CHECK(set.empty() && (set.size() == 0));
215b8021494Sopenharmony_ci
216b8021494Sopenharmony_ci  // Ensure that set.begin() == set.end() for the empty set.
217b8021494Sopenharmony_ci  VIXL_CHECK(set.begin() == set.end());
218b8021494Sopenharmony_ci
219b8021494Sopenharmony_ci  // Test with only preallocated elements in the set.
220b8021494Sopenharmony_ci  size_t expected_total = 0;
221b8021494Sopenharmony_ci  for (unsigned i = 0; i < kNPreallocatedElements; i++) {
222b8021494Sopenharmony_ci    set.insert(Obj(i, i));
223b8021494Sopenharmony_ci    expected_total += i;
224b8021494Sopenharmony_ci  }
225b8021494Sopenharmony_ci
226b8021494Sopenharmony_ci  size_t size = 0;
227b8021494Sopenharmony_ci  size_t total = 0;
228b8021494Sopenharmony_ci  for (InvalSetIterator<TestSet> it = set.begin(); it != set.end(); ++it) {
229b8021494Sopenharmony_ci    total += it->val_;
230b8021494Sopenharmony_ci    size++;
231b8021494Sopenharmony_ci  }
232b8021494Sopenharmony_ci  VIXL_CHECK(size == set.size());
233b8021494Sopenharmony_ci  VIXL_CHECK(total == expected_total);
234b8021494Sopenharmony_ci
235b8021494Sopenharmony_ci  // Test with more elements.
236b8021494Sopenharmony_ci  for (unsigned i = kNPreallocatedElements; i < 4 * kNPreallocatedElements;
237b8021494Sopenharmony_ci       i++) {
238b8021494Sopenharmony_ci    set.insert(Obj(i, i));
239b8021494Sopenharmony_ci    expected_total += i;
240b8021494Sopenharmony_ci  }
241b8021494Sopenharmony_ci
242b8021494Sopenharmony_ci  size = 0;
243b8021494Sopenharmony_ci  total = 0;
244b8021494Sopenharmony_ci  for (InvalSetIterator<TestSet> it = set.begin(); it != set.end(); ++it) {
245b8021494Sopenharmony_ci    total += it->val_;
246b8021494Sopenharmony_ci    size++;
247b8021494Sopenharmony_ci  }
248b8021494Sopenharmony_ci  VIXL_CHECK(size == set.size());
249b8021494Sopenharmony_ci  VIXL_CHECK(total == expected_total);
250b8021494Sopenharmony_ci
251b8021494Sopenharmony_ci  // Test after elements have been deleted.
252b8021494Sopenharmony_ci  // - Select an interesting list of elements to erase.
253b8021494Sopenharmony_ci  std::vector<Obj> to_erase;
254b8021494Sopenharmony_ci  unsigned step = 0;
255b8021494Sopenharmony_ci  for (unsigned i = 0; i < set.size(); i += step, step++) {
256b8021494Sopenharmony_ci    to_erase.push_back(Obj(i, i));
257b8021494Sopenharmony_ci  }
258b8021494Sopenharmony_ci  to_erase.push_back(Obj(set.size() - 1, set.size() - 1));  // The last element.
259b8021494Sopenharmony_ci  to_erase.push_back(Obj(set.size(), set.size()));          // Not in the set.
260b8021494Sopenharmony_ci
261b8021494Sopenharmony_ci  // - Erase one at a time, retesting after each one.
262b8021494Sopenharmony_ci  while (!to_erase.empty()) {
263b8021494Sopenharmony_ci    size_t erased = set.erase(to_erase.back());
264b8021494Sopenharmony_ci    if (erased > 0) {
265b8021494Sopenharmony_ci      VIXL_CHECK(erased == 1);
266b8021494Sopenharmony_ci      expected_total -= to_erase.back().val_;
267b8021494Sopenharmony_ci    } else {
268b8021494Sopenharmony_ci    }
269b8021494Sopenharmony_ci    to_erase.pop_back();
270b8021494Sopenharmony_ci
271b8021494Sopenharmony_ci    size = 0;
272b8021494Sopenharmony_ci    total = 0;
273b8021494Sopenharmony_ci    for (InvalSetIterator<TestSet> it = set.begin(); it != set.end(); ++it) {
274b8021494Sopenharmony_ci      total += it->val_;
275b8021494Sopenharmony_ci      size++;
276b8021494Sopenharmony_ci    }
277b8021494Sopenharmony_ci    VIXL_CHECK(size == set.size());
278b8021494Sopenharmony_ci    VIXL_CHECK(total == expected_total);
279b8021494Sopenharmony_ci  }
280b8021494Sopenharmony_ci}
281b8021494Sopenharmony_ci
282b8021494Sopenharmony_ci
283b8021494Sopenharmony_ci#if __cplusplus >= 201103L
284b8021494Sopenharmony_ciTEST(iterator_cxx11) {
285b8021494Sopenharmony_ci  TestSet set;
286b8021494Sopenharmony_ci  VIXL_CHECK(set.empty() && (set.size() == 0));
287b8021494Sopenharmony_ci
288b8021494Sopenharmony_ci  // Test with only preallocated elements in the set.
289b8021494Sopenharmony_ci  size_t expected_total = 0;
290b8021494Sopenharmony_ci  for (unsigned i = 0; i < kNPreallocatedElements; i++) {
291b8021494Sopenharmony_ci    set.insert(Obj(i, i));
292b8021494Sopenharmony_ci    expected_total += i;
293b8021494Sopenharmony_ci  }
294b8021494Sopenharmony_ci
295b8021494Sopenharmony_ci  size_t size = 0;
296b8021494Sopenharmony_ci  size_t total = 0;
297b8021494Sopenharmony_ci  for (auto object : set) {
298b8021494Sopenharmony_ci    total += object.val_;
299b8021494Sopenharmony_ci    size++;
300b8021494Sopenharmony_ci  }
301b8021494Sopenharmony_ci  VIXL_CHECK(size == set.size());
302b8021494Sopenharmony_ci  VIXL_CHECK(total == expected_total);
303b8021494Sopenharmony_ci
304b8021494Sopenharmony_ci  // Test with more elements.
305b8021494Sopenharmony_ci  for (unsigned i = kNPreallocatedElements; i < 4 * kNPreallocatedElements;
306b8021494Sopenharmony_ci       i++) {
307b8021494Sopenharmony_ci    set.insert(Obj(i, i));
308b8021494Sopenharmony_ci    expected_total += i;
309b8021494Sopenharmony_ci  }
310b8021494Sopenharmony_ci
311b8021494Sopenharmony_ci  size = 0;
312b8021494Sopenharmony_ci  total = 0;
313b8021494Sopenharmony_ci  for (auto object : set) {
314b8021494Sopenharmony_ci    total += object.val_;
315b8021494Sopenharmony_ci    size++;
316b8021494Sopenharmony_ci  }
317b8021494Sopenharmony_ci  VIXL_CHECK(size == set.size());
318b8021494Sopenharmony_ci  VIXL_CHECK(total == expected_total);
319b8021494Sopenharmony_ci
320b8021494Sopenharmony_ci  // Test after elements have been deleted.
321b8021494Sopenharmony_ci  // - Select an interesting list of elements to erase.
322b8021494Sopenharmony_ci  std::vector<Obj> to_erase;
323b8021494Sopenharmony_ci  unsigned step = 0;
324b8021494Sopenharmony_ci  for (unsigned i = 0; i < set.size(); i += step, step++) {
325b8021494Sopenharmony_ci    to_erase.push_back(Obj(i, i));
326b8021494Sopenharmony_ci  }
327b8021494Sopenharmony_ci  to_erase.push_back(Obj(set.size() - 1, set.size() - 1));  // The last element.
328b8021494Sopenharmony_ci  to_erase.push_back(Obj(set.size(), set.size()));          // Not in the set.
329b8021494Sopenharmony_ci
330b8021494Sopenharmony_ci  // - Erase one at a time, retesting after each one.
331b8021494Sopenharmony_ci  while (!to_erase.empty()) {
332b8021494Sopenharmony_ci    size_t erased = set.erase(to_erase.back());
333b8021494Sopenharmony_ci    if (erased > 0) {
334b8021494Sopenharmony_ci      VIXL_CHECK(erased == 1);
335b8021494Sopenharmony_ci      expected_total -= to_erase.back().val_;
336b8021494Sopenharmony_ci    } else {
337b8021494Sopenharmony_ci    }
338b8021494Sopenharmony_ci    to_erase.pop_back();
339b8021494Sopenharmony_ci
340b8021494Sopenharmony_ci    size = 0;
341b8021494Sopenharmony_ci    total = 0;
342b8021494Sopenharmony_ci    for (auto object : set) {
343b8021494Sopenharmony_ci      total += object.val_;
344b8021494Sopenharmony_ci      size++;
345b8021494Sopenharmony_ci    }
346b8021494Sopenharmony_ci    VIXL_CHECK(size == set.size());
347b8021494Sopenharmony_ci    VIXL_CHECK(total == expected_total);
348b8021494Sopenharmony_ci  }
349b8021494Sopenharmony_ci}
350b8021494Sopenharmony_ci#endif
351b8021494Sopenharmony_ci
352b8021494Sopenharmony_ci
353b8021494Sopenharmony_ciTEST(stl_forward_iterator) {
354b8021494Sopenharmony_ci  {
355b8021494Sopenharmony_ci    TestSet::iterator default_it;           // Default-constructible.
356b8021494Sopenharmony_ci    TestSet::iterator copy_it(default_it);  // Copy-constructible.
357b8021494Sopenharmony_ci    copy_it = default_it;                   // Copy-assignable.
358b8021494Sopenharmony_ci  }                                         // Destructible.
359b8021494Sopenharmony_ci
360b8021494Sopenharmony_ci  TestSet set1;
361b8021494Sopenharmony_ci  VIXL_CHECK(set1.empty() && (set1.size() == 0));
362b8021494Sopenharmony_ci
363b8021494Sopenharmony_ci  TestSet set2;
364b8021494Sopenharmony_ci  VIXL_CHECK(set2.empty() && (set2.size() == 0));
365b8021494Sopenharmony_ci
366b8021494Sopenharmony_ci  // Test with only preallocated elements in the set.
367b8021494Sopenharmony_ci  for (unsigned i = 0; i < kNPreallocatedElements; i++) {
368b8021494Sopenharmony_ci    set1.insert(Obj(i, 1));
369b8021494Sopenharmony_ci    set2.insert(Obj(i, 2));
370b8021494Sopenharmony_ci  }
371b8021494Sopenharmony_ci
372b8021494Sopenharmony_ci  TestSet::iterator it1_a = set1.begin();
373b8021494Sopenharmony_ci  TestSet::iterator it1_b = set1.begin();
374b8021494Sopenharmony_ci
375b8021494Sopenharmony_ci  // Incrementable (whilst valid).
376b8021494Sopenharmony_ci  it1_a++;
377b8021494Sopenharmony_ci  ++it1_b;
378b8021494Sopenharmony_ci
379b8021494Sopenharmony_ci  // Testable for equivalence.
380b8021494Sopenharmony_ci  VIXL_CHECK(it1_a == it1_b);
381b8021494Sopenharmony_ci  VIXL_CHECK(set1.begin() != set1.end());
382b8021494Sopenharmony_ci  VIXL_CHECK(set2.begin() != set2.end());
383b8021494Sopenharmony_ci  VIXL_CHECK(set1.begin() != set2.begin());
384b8021494Sopenharmony_ci  VIXL_CHECK(set1.end() != set2.end());
385b8021494Sopenharmony_ci
386b8021494Sopenharmony_ci  // Dereferencable.
387b8021494Sopenharmony_ci  VIXL_CHECK((*it1_a++).key_ == 1);
388b8021494Sopenharmony_ci  VIXL_CHECK(((*it1_a).key_ == 2) && ((*it1_a).val_ == 1));
389b8021494Sopenharmony_ci  *it1_b = Obj(42, 1);
390b8021494Sopenharmony_ci  VIXL_CHECK((it1_b->key_ == 42) && (it1_b->val_ == 1));
391b8021494Sopenharmony_ci
392b8021494Sopenharmony_ci#if __cplusplus >= 201103L
393b8021494Sopenharmony_ci  // Swappable.
394b8021494Sopenharmony_ci  std::swap(it1_a, it1_b);
395b8021494Sopenharmony_ci  VIXL_CHECK(it1_a->key_ == 42);
396b8021494Sopenharmony_ci  VIXL_CHECK(it1_b->key_ == 2);
397b8021494Sopenharmony_ci#endif
398b8021494Sopenharmony_ci}
399b8021494Sopenharmony_ci
400b8021494Sopenharmony_ci
401b8021494Sopenharmony_ci}  // namespace vixl
402