summaryrefslogtreecommitdiffstats
path: root/mfbt/tests/TestLinkedList.cpp
blob: bb1ffe08c0a40f38bdbe66e3bd2c30630d37c335 (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
/* -*- 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/LinkedList.h"

using mozilla::AutoCleanLinkedList;
using mozilla::LinkedList;
using mozilla::LinkedListElement;

struct SomeClass : public LinkedListElement<SomeClass> {
  unsigned int mValue;
  explicit SomeClass(int aValue = 0) : mValue(aValue) {}
  SomeClass(SomeClass&&) = default;
  SomeClass& operator=(SomeClass&&) = default;
  void incr() { ++mValue; }
};

template <size_t N>
static void CheckListValues(LinkedList<SomeClass>& list,
                            unsigned int (&values)[N]) {
  size_t count = 0;
  for (SomeClass* x : list) {
    MOZ_RELEASE_ASSERT(x->mValue == values[count]);
    ++count;
  }
  MOZ_RELEASE_ASSERT(count == N);
}

static void TestList() {
  LinkedList<SomeClass> list;

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

  MOZ_RELEASE_ASSERT(list.isEmpty());
  MOZ_RELEASE_ASSERT(list.length() == 0);
  MOZ_RELEASE_ASSERT(!list.getFirst());
  MOZ_RELEASE_ASSERT(!list.getLast());
  MOZ_RELEASE_ASSERT(!list.popFirst());
  MOZ_RELEASE_ASSERT(!list.popLast());

  for (SomeClass* x : list) {
    MOZ_RELEASE_ASSERT(x);
    MOZ_RELEASE_ASSERT(false);
  }

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

  MOZ_RELEASE_ASSERT(one.isInList());
  MOZ_RELEASE_ASSERT(!two.isInList());
  MOZ_RELEASE_ASSERT(!three.isInList());

  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.length() == 1);
  MOZ_RELEASE_ASSERT(list.getFirst()->mValue == 1);
  MOZ_RELEASE_ASSERT(list.getLast()->mValue == 1);

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

  MOZ_RELEASE_ASSERT(list.length() == 2);
  MOZ_RELEASE_ASSERT(list.getFirst()->mValue == 2);
  MOZ_RELEASE_ASSERT(list.getLast()->mValue == 1);

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

  MOZ_RELEASE_ASSERT(list.length() == 3);
  MOZ_RELEASE_ASSERT(list.getFirst()->mValue == 2);
  MOZ_RELEASE_ASSERT(list.getLast()->mValue == 3);

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

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

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

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

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

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

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

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

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

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

  two.remove();

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

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

  for (SomeClass* x : list) {
    x->incr();
  }

  MOZ_RELEASE_ASSERT(list.length() == 2);
  MOZ_RELEASE_ASSERT(list.getFirst() == &two);
  MOZ_RELEASE_ASSERT(list.getLast() == &three);
  MOZ_RELEASE_ASSERT(list.getFirst()->mValue == 3);
  MOZ_RELEASE_ASSERT(list.getLast()->mValue == 4);

  const LinkedList<SomeClass>& constList = list;
  for (const SomeClass* x : constList) {
    MOZ_RELEASE_ASSERT(x);
  }
}

static void TestExtendLists() {
  AutoCleanLinkedList<SomeClass> list1, list2;

  constexpr unsigned int N = 5;
  for (unsigned int i = 0; i < N; ++i) {
    list1.insertBack(new SomeClass(static_cast<int>(i)));

    AutoCleanLinkedList<SomeClass> singleItemList;
    singleItemList.insertFront(new SomeClass(static_cast<int>(i + N)));
    list2.extendBack(std::move(singleItemList));
  }
  // list1 = { 0, 1, 2, 3, 4 }
  // list2 = { 5, 6, 7, 8, 9 }

  list1.extendBack(AutoCleanLinkedList<SomeClass>());
  list1.extendBack(std::move(list2));

  // Make sure the line above has properly emptied |list2|.
  MOZ_RELEASE_ASSERT(list2.isEmpty());  // NOLINT(bugprone-use-after-move)

  size_t i = 0;
  for (SomeClass* x : list1) {
    MOZ_RELEASE_ASSERT(x->mValue == i++);
  }
  MOZ_RELEASE_ASSERT(i == N * 2);
}

void TestSplice() {
  AutoCleanLinkedList<SomeClass> list1, list2;
  for (unsigned int i = 1; i <= 5; ++i) {
    list1.insertBack(new SomeClass(static_cast<int>(i)));

    AutoCleanLinkedList<SomeClass> singleItemList;
    singleItemList.insertFront(new SomeClass(static_cast<int>(i * 10)));
    list2.extendBack(std::move(singleItemList));
  }
  // list1 = { 1, 2, 3, 4, 5 }
  // list2 = { 10, 20, 30, 40, 50 }

  list1.splice(2, list2, 0, 5);

  MOZ_RELEASE_ASSERT(list2.isEmpty());
  unsigned int kExpected1[]{1, 2, 10, 20, 30, 40, 50, 3, 4, 5};
  CheckListValues(list1, kExpected1);

  // Since aSourceLen=100 exceeds list1's end, the function transfers
  // three items [3, 4, 5].
  list2.splice(0, list1, 7, 100);

  unsigned int kExpected2[]{1, 2, 10, 20, 30, 40, 50};
  unsigned int kExpected3[]{3, 4, 5};
  CheckListValues(list1, kExpected2);
  CheckListValues(list2, kExpected3);

  // Since aDestinationPos=100 exceeds list2's end, the function transfers
  // items to list2's end.
  list2.splice(100, list1, 1, 1);

  unsigned int kExpected4[]{1, 10, 20, 30, 40, 50};
  unsigned int kExpected5[]{3, 4, 5, 2};
  CheckListValues(list1, kExpected4);
  CheckListValues(list2, kExpected5);
}

static void TestMove() {
  auto MakeSomeClass = [](unsigned int aValue) -> SomeClass {
    return SomeClass(aValue);
  };

  LinkedList<SomeClass> list1;

  // Test move constructor for LinkedListElement.
  SomeClass c1(MakeSomeClass(1));
  list1.insertBack(&c1);

  // Test move assignment for LinkedListElement from an element not in a
  // list.
  SomeClass c2;
  c2 = MakeSomeClass(2);
  list1.insertBack(&c2);

  // Test move assignment of LinkedListElement from an element already in a
  // list.
  SomeClass c3;
  c3 = std::move(c2);
  MOZ_RELEASE_ASSERT(!c2.isInList());
  MOZ_RELEASE_ASSERT(c3.isInList());

  // Test move constructor for LinkedList.
  LinkedList<SomeClass> list2(std::move(list1));
  {
    unsigned int check[]{1, 2};
    CheckListValues(list2, check);
  }
  MOZ_RELEASE_ASSERT(list1.isEmpty());

  // Test move assignment for LinkedList.
  LinkedList<SomeClass> list3;
  list3 = std::move(list2);
  {
    unsigned int check[]{1, 2};
    CheckListValues(list3, check);
  }
  MOZ_RELEASE_ASSERT(list2.isEmpty());

  list3.clear();
}

static void TestRemoveAndGet() {
  LinkedList<SomeClass> list;

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

  MOZ_RELEASE_ASSERT(two.removeAndGetNext() == &three);
  {
    unsigned int check[]{1, 3};
    CheckListValues(list, check);
  }

  MOZ_RELEASE_ASSERT(three.removeAndGetPrevious() == &one);
  {
    unsigned int check[]{1};
    CheckListValues(list, check);
  }
}

struct PrivateClass : private LinkedListElement<PrivateClass> {
  friend class mozilla::LinkedList<PrivateClass>;
  friend class mozilla::LinkedListElement<PrivateClass>;
};

static void TestPrivate() {
  LinkedList<PrivateClass> list;
  PrivateClass one, two;
  list.insertBack(&one);
  list.insertBack(&two);

  size_t count = 0;
  for (PrivateClass* p : list) {
    MOZ_RELEASE_ASSERT(p, "cannot have null elements in list");
    count++;
  }
  MOZ_RELEASE_ASSERT(count == 2);
}

struct CountedClass : public LinkedListElement<RefPtr<CountedClass>> {
  int mCount;
  void AddRef() { mCount++; }
  void Release() { mCount--; }

  CountedClass() : mCount(0) {}
  ~CountedClass() { MOZ_RELEASE_ASSERT(mCount == 0); }
};

static void TestRefPtrList() {
  LinkedList<RefPtr<CountedClass>> list;
  CountedClass* elt1 = new CountedClass;
  CountedClass* elt2 = new CountedClass;

  list.insertBack(elt1);
  list.insertBack(elt2);

  MOZ_RELEASE_ASSERT(elt1->mCount == 1);
  MOZ_RELEASE_ASSERT(elt2->mCount == 1);

  for (RefPtr<CountedClass> p : list) {
    MOZ_RELEASE_ASSERT(p->mCount == 2);
  }

  RefPtr<CountedClass> ptr = list.getFirst();
  while (ptr) {
    MOZ_RELEASE_ASSERT(ptr->mCount == 2);
    RefPtr<CountedClass> next = ptr->getNext();
    ptr->remove();
    ptr = std::move(next);
  }
  ptr = nullptr;

  MOZ_RELEASE_ASSERT(elt1->mCount == 0);
  MOZ_RELEASE_ASSERT(elt2->mCount == 0);

  list.insertBack(elt1);
  elt1->setNext(elt2);

  MOZ_RELEASE_ASSERT(elt1->mCount == 1);
  MOZ_RELEASE_ASSERT(elt2->mCount == 1);

  RefPtr<CountedClass> first = list.popFirst();

  MOZ_RELEASE_ASSERT(elt1->mCount == 1);
  MOZ_RELEASE_ASSERT(elt2->mCount == 1);

  RefPtr<CountedClass> second = list.popFirst();

  MOZ_RELEASE_ASSERT(elt1->mCount == 1);
  MOZ_RELEASE_ASSERT(elt2->mCount == 1);

  first = second = nullptr;

  delete elt1;
  delete elt2;
}

int main() {
  TestList();
  TestExtendLists();
  TestSplice();
  TestPrivate();
  TestMove();
  TestRemoveAndGet();
  TestRefPtrList();
  return 0;
}