Debian Codegraph @

js/src/jsapi-tests/testSinglyLinkedList.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7
8 #include <array>
9
10 #include "ds/SinglyLinkedList.h"
11 #include "jsapi-tests/tests.h"
12
13 using namespace js;
14
15 struct IntSinglyLinkedElement {
16 int value;
17 IntSinglyLinkedElement* next = nullptr;
18
19 explicit IntSinglyLinkedElement(int v) : value(v) {}
20 };
21 using TestList = SinglyLinkedList<IntSinglyLinkedElement>;
22
23 BEGIN_TEST(testSinglyLinkedList) {
24 // Test empty lists.
25
26 TestList list;
27 CHECK(list.isEmpty());
28 CHECK(!list.first());
29 CHECK(!list.last());
30 CHECK(CountList(list) == 0);
31
32 // Test list pushBack and first/last accessors.
33
34 list.pushBack(MakeElement(1));
35 CHECK(!list.isEmpty());
36 CHECK(list.first()->value == 1);
37 CHECK(list.last()->value == 1);
38 CHECK(CheckList<1>(list));
39
40 list.pushBack(MakeElement(2));
41 list.pushBack(MakeElement(3));
42 CHECK(!list.isEmpty());
43 CHECK(list.first()->value == 1);
44 CHECK(list.last()->value == 3);
45 CHECK((CheckList<1, 2, 3>(list)));
46
47 // Test popFront.
48
49 IntSinglyLinkedElement* e = list.popFront();
50 CHECK(e->value == 1);
51 js_delete(e);
52 CHECK(list.first()->value == 2);
53 CHECK((CheckList<2, 3>(list)));
54
55 e = list.popFront();
56 CHECK(e->value == 2);
57 js_delete(e);
58 CHECK(list.first()->value == 3);
59
60 // Test pushFront.
61
62 list.pushFront(MakeElement(2));
63 CHECK(list.first()->value == 2);
64 CHECK((CheckList<2, 3>(list)));
65
66 list.pushFront(MakeElement(1));
67 CHECK(list.first()->value == 1);
68 CHECK((CheckList<1, 2, 3>(list)));
69
70 // Test moveFrontToBack.
71
72 list.moveFrontToBack();
73 CHECK(list.first()->value == 2);
74 CHECK(list.last()->value == 1);
75 CHECK((CheckList<2, 3, 1>(list)));
76 list.moveFrontToBack();
77 list.moveFrontToBack();
78 CHECK((CheckList<1, 2, 3>(list)));
79
80 // Test move constructor and assignment.
81
82 TestList list2(std::move(list));
83 CHECK(list.isEmpty());
84 CHECK((CheckList<1, 2, 3>(list2)));
85
86 list = std::move(list2);
87 CHECK(list2.isEmpty());
88 CHECK((CheckList<1, 2, 3>(list)));
89
90 // Test release.
91
92 IntSinglyLinkedElement* head = list.release();
93 CHECK(list.isEmpty());
94 CHECK(head->value == 1);
95 CHECK(head->next->value == 2);
96 CHECK(head->next->next->value == 3);
97 CHECK(!head->next->next->next);
98
99 // Test construct from linked list.
100
101 list = TestList(head, head->next->next);
102 CHECK((CheckList<1, 2, 3>(list)));
103
104 // Test append.
105
106 CHECK(list2.isEmpty());
107 list.append(std::move(list2));
108 CHECK((CheckList<1, 2, 3>(list)));
109 CHECK(list2.isEmpty());
110
111 TestList list3;
112 list3.pushBack(MakeElement(4));
113 list3.pushBack(MakeElement(5));
114 list3.pushBack(MakeElement(6));
115 list2.append(std::move(list3));
116 CHECK((CheckList<4, 5, 6>(list2)));
117 CHECK(list3.isEmpty());
118
119 list.append(std::move(list2));
120 CHECK((CheckList<1, 2, 3, 4, 5, 6>(list)));
121 CHECK(list2.isEmpty());
122
123 // Test prepend.
124
125 CHECK(list2.isEmpty());
126 list.prepend(std::move(list2));
127 CHECK((CheckList<1, 2, 3, 4, 5, 6>(list)));
128 CHECK(list2.isEmpty());
129
130 CHECK(list3.isEmpty());
131 list3.pushBack(MakeElement(7));
132 list3.pushBack(MakeElement(8));
133 list3.pushBack(MakeElement(9));
134 list2.prepend(std::move(list3));
135 CHECK((CheckList<7, 8, 9>(list2)));
136 CHECK(list3.isEmpty());
137
138 list.prepend(std::move(list2));
139 CHECK((CheckList<7, 8, 9, 1, 2, 3, 4, 5, 6>(list)));
140 CHECK(list2.isEmpty());
141
142 // Test iterators.
143
144 TestList::Iterator iter;
145 CHECK(iter.done());
146
147 iter = list.iter();
148 CHECK(!iter.done());
149 CHECK(iter.get() == list.first());
150
151 iter = list.iterFrom(list.last());
152 CHECK(!iter.done());
153 CHECK(iter.get() == list.last());
154
155 // Test removeRange.
156
157 e = FindElement(list, 3);
158 CHECK(e);
159 list.removeRange(e, list.last());
160 CHECK((CheckList<7, 8, 9, 1, 2, 3>(list)));
161
162 e = FindElement(list, 8);
163 CHECK(e);
164 IntSinglyLinkedElement* f = FindElement(list, 2);
165 CHECK(f);
166 list.removeRange(e, f);
167 CHECK((CheckList<7, 8, 3>(list)));
168
169 // Cleanup.
170
171 while (!list.isEmpty()) {
172 js_delete(list.popFront());
173 }
174 CHECK(list.isEmpty());
175 CHECK(!list.first());
176 CHECK(!list.last());
177 CHECK(CountList(list) == 0);
178
179 return true;
180 }
181
182 IntSinglyLinkedElement* MakeElement(int value) {
183 IntSinglyLinkedElement* element = js_new<IntSinglyLinkedElement>(value);
184 MOZ_RELEASE_ASSERT(element);
185 return element;
186 }
187
188 size_t CountList(const TestList& list) {
189 size_t i = 0;
190 for (auto iter = list.iter(); !iter.done(); iter.next()) {
191 i++;
192 }
193 return i;
194 }
195
196 IntSinglyLinkedElement* FindElement(const TestList& list, int value) {
197 for (auto iter = list.iter(); !iter.done(); iter.next()) {
198 if (iter->value == value) {
199 return iter.get();
200 }
201 }
202
203 return nullptr;
204 }
205
206 template <int... Values>
207 bool CheckList(const TestList& list) {
208 int expected[] = {Values...};
209 constexpr size_t N = std::size(expected);
210
211 size_t i = 0;
212 for (auto iter = list.iter(); !iter.done(); iter.next()) {
213 CHECK(i < N);
214 CHECK(iter->value == expected[i]);
215 i++;
216 }
217
218 CHECK(i == N);
219
220 return true;
221 }
222
223 END_TEST(testSinglyLinkedList)