summaryrefslogtreecommitdiffstats
path: root/mfbt/tests/TestDoublyLinkedList.cpp
blob: 3065b15ddb0027285ca9980151099c9220c81186 (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/Assertions.h"
#include "mozilla/DoublyLinkedList.h"

using mozilla::DoublyLinkedList;
using mozilla::DoublyLinkedListElement;

struct SomeClass : public DoublyLinkedListElement<SomeClass> {
  unsigned int mValue;
  explicit SomeClass(int aValue) : mValue(aValue) {}
  void incr() { ++mValue; }
  bool operator==(const SomeClass& other) const {
    return mValue == other.mValue;
  }
};

template <typename ListType, size_t N>
static void CheckListValues(ListType& list, unsigned int (&values)[N]) {
  size_t count = 0;
  for (auto& x : list) {
    MOZ_RELEASE_ASSERT(x.mValue == values[count]);
    ++count;
  }
  MOZ_RELEASE_ASSERT(count == N);
}

static void TestDoublyLinkedList() {
  DoublyLinkedList<SomeClass> list;

  SomeClass one(1), two(2), three(3);

  MOZ_RELEASE_ASSERT(list.isEmpty());
  MOZ_RELEASE_ASSERT(!list.begin());
  MOZ_RELEASE_ASSERT(!list.end());

  for (SomeClass& x : list) {
    MOZ_RELEASE_ASSERT(x.mValue);
    MOZ_RELEASE_ASSERT(false);
  }

  list.pushFront(&one);
  {
    unsigned int check[]{1};
    CheckListValues(list, check);
  }

  MOZ_RELEASE_ASSERT(list.contains(one));
  MOZ_RELEASE_ASSERT(!list.contains(two));
  MOZ_RELEASE_ASSERT(!list.contains(three));

  MOZ_RELEASE_ASSERT(!list.isEmpty());
  MOZ_RELEASE_ASSERT(list.begin()->mValue == 1);
  MOZ_RELEASE_ASSERT(!list.end());

  list.pushFront(&two);
  {
    unsigned int check[]{2, 1};
    CheckListValues(list, check);
  }

  MOZ_RELEASE_ASSERT(list.begin()->mValue == 2);
  MOZ_RELEASE_ASSERT(!list.end());
  MOZ_RELEASE_ASSERT(!list.contains(three));

  list.pushBack(&three);
  {
    unsigned int check[]{2, 1, 3};
    CheckListValues(list, check);
  }

  MOZ_RELEASE_ASSERT(list.begin()->mValue == 2);
  MOZ_RELEASE_ASSERT(!list.end());

  list.remove(&one);
  {
    unsigned int check[]{2, 3};
    CheckListValues(list, check);
  }

  list.insertBefore(list.find(three), &one);
  {
    unsigned int check[]{2, 1, 3};
    CheckListValues(list, check);
  }

  list.remove(&three);
  {
    unsigned int check[]{2, 1};
    CheckListValues(list, check);
  }

  list.insertBefore(list.find(two), &three);
  {
    unsigned int check[]{3, 2, 1};
    CheckListValues(list, check);
  }

  list.remove(&three);
  {
    unsigned int check[]{2, 1};
    CheckListValues(list, check);
  }

  list.insertBefore(++list.find(two), &three);
  {
    unsigned int check[]{2, 3, 1};
    CheckListValues(list, check);
  }

  list.remove(&one);
  {
    unsigned int check[]{2, 3};
    CheckListValues(list, check);
  }

  list.remove(&two);
  {
    unsigned int check[]{3};
    CheckListValues(list, check);
  }

  list.insertBefore(list.find(three), &two);
  {
    unsigned int check[]{2, 3};
    CheckListValues(list, check);
  }

  list.remove(&three);
  {
    unsigned int check[]{2};
    CheckListValues(list, check);
  }

  list.remove(&two);
  MOZ_RELEASE_ASSERT(list.isEmpty());

  list.pushBack(&three);
  {
    unsigned int check[]{3};
    CheckListValues(list, check);
  }

  list.pushFront(&two);
  {
    unsigned int check[]{2, 3};
    CheckListValues(list, check);
  }

  // This should modify the values of |two| and |three| as pointers to them are
  // stored in the list, not copies.
  for (SomeClass& x : list) {
    x.incr();
  }

  MOZ_RELEASE_ASSERT(*list.begin() == two);
  MOZ_RELEASE_ASSERT(*++list.begin() == three);

  SomeClass four(4);
  MOZ_RELEASE_ASSERT(++list.begin() == list.find(four));
}

struct InTwoLists {
  explicit InTwoLists(unsigned int aValue) : mValue(aValue) {}
  DoublyLinkedListElement<InTwoLists> mListOne;
  DoublyLinkedListElement<InTwoLists> mListTwo;
  unsigned int mValue;

  struct GetListOneTrait {
    static DoublyLinkedListElement<InTwoLists>& Get(InTwoLists* aThis) {
      return aThis->mListOne;
    }
  };
};

namespace mozilla {

template <>
struct GetDoublyLinkedListElement<InTwoLists> {
  static DoublyLinkedListElement<InTwoLists>& Get(InTwoLists* aThis) {
    return aThis->mListTwo;
  }
};

}  // namespace mozilla

static void TestCustomAccessor() {
  DoublyLinkedList<InTwoLists, InTwoLists::GetListOneTrait> listOne;
  DoublyLinkedList<InTwoLists> listTwo;

  InTwoLists one(1);
  InTwoLists two(2);

  listOne.pushBack(&one);
  listOne.pushBack(&two);
  {
    unsigned int check[]{1, 2};
    CheckListValues(listOne, check);
  }

  listTwo.pushBack(&one);
  listTwo.pushBack(&two);
  {
    unsigned int check[]{1, 2};
    CheckListValues(listOne, check);
  }
  {
    unsigned int check[]{1, 2};
    CheckListValues(listTwo, check);
  }

  (void)listTwo.popBack();
  {
    unsigned int check[]{1, 2};
    CheckListValues(listOne, check);
  }
  {
    unsigned int check[]{1};
    CheckListValues(listTwo, check);
  }

  (void)listOne.popBack();
  {
    unsigned int check[]{1};
    CheckListValues(listOne, check);
  }
  {
    unsigned int check[]{1};
    CheckListValues(listTwo, check);
  }
}

static void TestSafeDoubleLinkedList() {
  mozilla::SafeDoublyLinkedList<SomeClass> list;
  auto* elt1 = new SomeClass(0);
  auto* elt2 = new SomeClass(0);
  auto* elt3 = new SomeClass(0);
  auto* elt4 = new SomeClass(0);
  list.pushBack(elt1);
  list.pushBack(elt2);
  list.pushBack(elt3);
  auto iter = list.begin();

  // basic tests for iterator validity
  MOZ_RELEASE_ASSERT(
      &*iter == elt1,
      "iterator returned by begin() must point to the first element!");
  MOZ_RELEASE_ASSERT(
      &*(iter.next()) == elt2,
      "iterator returned by begin() must have the second element as 'next'!");
  list.remove(elt2);
  MOZ_RELEASE_ASSERT(
      &*(iter.next()) == elt3,
      "After removal of the 2nd element 'next' must point to the 3rd element!");
  ++iter;
  MOZ_RELEASE_ASSERT(
      &*iter == elt3,
      "After advancing one step the current element must be the 3rd one!");
  MOZ_RELEASE_ASSERT(!iter.next(), "This is the last element of the list!");
  list.pushBack(elt4);
  MOZ_RELEASE_ASSERT(&*(iter.next()) == elt4,
                     "After adding an element at the end of the list the "
                     "iterator must be updated!");

  // advance to last element, then remove last element
  ++iter;
  list.popBack();
  MOZ_RELEASE_ASSERT(bool(iter) == false,
                     "After removing the last element, the iterator pointing "
                     "to the last element must be empty!");

  // iterate the whole remaining list, increment values
  for (auto& el : list) {
    el.incr();
  }
  MOZ_RELEASE_ASSERT(elt1->mValue == 1);
  MOZ_RELEASE_ASSERT(elt2->mValue == 0);
  MOZ_RELEASE_ASSERT(elt3->mValue == 1);
  MOZ_RELEASE_ASSERT(elt4->mValue == 0);

  // Removing the first element of the list while iterating must empty the
  // iterator
  for (auto it = list.begin(); it != list.end(); ++it) {
    MOZ_RELEASE_ASSERT(bool(it) == true, "The iterator must contain a value!");
    list.popFront();
    MOZ_RELEASE_ASSERT(
        bool(it) == false,
        "After removing the first element, the iterator must be empty!");
  }

  delete elt1;
  delete elt2;
  delete elt3;
  delete elt4;
}

int main() {
  TestDoublyLinkedList();
  TestCustomAccessor();
  TestSafeDoubleLinkedList();
  return 0;
}