Debian Codegraph @

js/src/jsapi-tests/testLinkedList.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 "mozilla/ScopeExit.h"
9
10 #include "ds/SlimLinkedList.h"
11
12 #include "jsapi-tests/tests.h"
13
14 using namespace js;
15
16 struct IntElement : public SlimLinkedListElement<IntElement> {
17 int value;
18 explicit IntElement(int value = 0) : value(value) {}
19 void incr() { ++value; }
20 };
21
22 BEGIN_TEST(testSlimLinkedList) {
23 CHECK(TestList());
24 CHECK(TestMove());
25 CHECK(TestExtendLists());
26 return true;
27 }
28
29 template <size_t N>
30 [[nodiscard]] bool PushListValues(SlimLinkedList<IntElement>& list,
31 const std::array<int, N>& values) {
32 for (int value : values) {
33 IntElement* element = new IntElement(value);
34 CHECK(element);
35 list.pushBack(element);
36 }
37
38 return true;
39 }
40
41 template <size_t N>
42 [[nodiscard]] bool CheckListValues(const SlimLinkedList<IntElement>& list,
43 const std::array<int, N>& expected) {
44 size_t count = 0;
45 for (const IntElement* x : list) {
46 CHECK(x->value == expected[count]);
47 ++count;
48 }
49 CHECK(count == N);
50
51 return true;
52 }
53
54 bool TestList() {
55 SlimLinkedList<IntElement> list;
56 const SlimLinkedList<IntElement>& constList = list;
57
58 IntElement one(1), two(2), three(3);
59
60 auto guard = mozilla::MakeScopeExit([&list]() { list.clear(); });
61
62 // Test empty list.
63 CHECK(list.isEmpty());
64 CHECK(list.length() == 0);
65 CHECK(!list.getFirst());
66 CHECK(!list.getLast());
67 CHECK(!list.popFirst());
68 CHECK(!list.popLast());
69 CHECK(list.begin() == list.end());
70 CHECK(constList.begin() == constList.end());
71 CHECK(CheckListValues(list, std::array<int, 0>{}));
72
73 // Test pushFront.
74 list.pushFront(&one);
75 CHECK(!list.isEmpty());
76 CHECK(list.length() == 1);
77 CHECK(list.getFirst() == &one);
78 CHECK(list.getLast() == &one);
79 CHECK(*list.begin() == &one);
80 list.pushFront(&two);
81 CHECK(list.length() == 2);
82 CHECK(list.getFirst() == &two);
83 CHECK(list.getLast() == &one);
84 CHECK(*list.begin() == &two);
85 CHECK(CheckListValues(list, std::array{2, 1}));
86 CHECK(list.contains(&one));
87 CHECK(!list.contains(&three));
88
89 // Test popFirst.
90 IntElement* element = list.popFirst();
91 CHECK(element == &two);
92 CHECK(list.length() == 1);
93 element = list.popFirst();
94 CHECK(element == &one);
95 CHECK(list.isEmpty());
96
97 // Test pushBack.
98 list.pushBack(&one);
99 CHECK(!list.isEmpty());
100 CHECK(list.length() == 1);
101 CHECK(list.getFirst() == &one);
102 CHECK(list.getLast() == &one);
103 CHECK(*list.begin() == &one);
104 list.pushBack(&two);
105 CHECK(list.length() == 2);
106 CHECK(list.getFirst() == &one);
107 CHECK(list.getLast() == &two);
108 CHECK(*list.begin() == &one);
109 CHECK(CheckListValues(list, std::array{1, 2}));
110 CHECK(list.contains(&one));
111 CHECK(!list.contains(&three));
112
113 // Test popLast.
114 element = list.popLast();
115 CHECK(element == &two);
116 CHECK(list.length() == 1);
117 element = list.popLast();
118 CHECK(element == &one);
119 CHECK(list.isEmpty());
120
121 // Test remove.
122 list.pushBack(&one);
123 list.pushBack(&two);
124 list.pushBack(&three);
125 list.remove(&one);
126 CHECK(CheckListValues(list, std::array{2, 3}));
127 list.pushFront(&one);
128 list.remove(&three);
129 CHECK(CheckListValues(list, std::array{1, 2}));
130 list.pushBack(&three);
131 list.remove(&two);
132 CHECK(CheckListValues(list, std::array{1, 3}));
133
134 // Test clear.
135 list.clear();
136 CHECK(list.isEmpty());
137 list.clear();
138 CHECK(list.isEmpty());
139
140 return true;
141 }
142
143 bool TestExtendLists() {
144 SlimLinkedList<IntElement> list1;
145 auto guard =
146 mozilla::MakeScopeExit([&]() { list1.drain([](auto* e) { delete e; }); });
147
148 CHECK(PushListValues(list1, std::array{0, 1, 2}));
149 CHECK(CheckListValues(list1, std::array{0, 1, 2}));
150
151 // Test extending with empty list.
152 list1.append(SlimLinkedList<IntElement>());
153 CHECK(CheckListValues(list1, std::array{0, 1, 2}));
154 list1.prepend(SlimLinkedList<IntElement>());
155 CHECK(CheckListValues(list1, std::array{0, 1, 2}));
156
157 // Test extending empty list.
158 SlimLinkedList<IntElement> list2;
159 list2.append(std::move(list1));
160 CHECK(list1.isEmpty());
161 CHECK(CheckListValues(list2, std::array{0, 1, 2}));
162 list1.prepend(std::move(list2));
163 CHECK(list2.isEmpty());
164 CHECK(CheckListValues(list1, std::array{0, 1, 2}));
165
166 // Test append.
167 CHECK(PushListValues(list2, std::array{3, 4, 5}));
168 CHECK(CheckListValues(list2, std::array{3, 4, 5}));
169 list1.append(std::move(list2));
170 CHECK(CheckListValues(list1, std::array{0, 1, 2, 3, 4, 5}));
171 CHECK(list2.isEmpty());
172
173 // Test prepend.
174 CHECK(PushListValues(list2, std::array{6, 7, 8}));
175 CHECK(CheckListValues(list2, std::array{6, 7, 8}));
176 list1.prepend(std::move(list2));
177 CHECK(CheckListValues(list1, std::array{6, 7, 8, 0, 1, 2, 3, 4, 5}));
178 CHECK(list2.isEmpty());
179
180 return true;
181 }
182
183 bool TestMove() {
184 // Test move constructor for the element.
185 IntElement c1(IntElement(1));
186 CHECK(c1.value == 1);
187
188 // Test move assignment from an element not in a list.
189 IntElement c2;
190 c2 = IntElement(2);
191 CHECK(c2.value == 2);
192
193 SlimLinkedList<IntElement> list1;
194 list1.pushBack(&c1);
195 list1.pushBack(&c2);
196
197 // Test move constructor for the list.
198 SlimLinkedList<IntElement> list2(std::move(list1));
199 CHECK(CheckListValues(list2, std::array{1, 2}));
200 CHECK(list1.isEmpty());
201
202 // Test move assignment for the list.
203 SlimLinkedList<IntElement> list3;
204 list3 = std::move(list2);
205 CHECK(CheckListValues(list3, std::array{1, 2}));
206 CHECK(list2.isEmpty());
207
208 list3.clear();
209
210 return true;
211 }
212 END_TEST(testSlimLinkedList)