| 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 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 5 |
* You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "mozilla/SmallPointerArray.h" |
| 8 |
|
| 9 |
#define PTR1 (void*)0x4 |
| 10 |
#define PTR2 (void*)0x5 |
| 11 |
#define PTR3 (void*)0x6 |
| 12 |
|
| 13 |
// We explicitly test sizes up to 3 here, as that is when SmallPointerArray<> |
| 14 |
// switches to the storage method used for larger arrays. |
| 15 |
void TestArrayManipulation() { |
| 16 |
using namespace mozilla; |
| 17 |
SmallPointerArray<void> testArray; |
| 18 |
|
| 19 |
MOZ_RELEASE_ASSERT(testArray.Length() == 0); |
| 20 |
MOZ_RELEASE_ASSERT(sizeof(testArray) == 2 * sizeof(void*)); |
| 21 |
MOZ_RELEASE_ASSERT(!testArray.Contains(PTR1)); |
| 22 |
|
| 23 |
testArray.AppendElement(PTR1); |
| 24 |
|
| 25 |
MOZ_RELEASE_ASSERT(testArray.Length() == 1); |
| 26 |
MOZ_RELEASE_ASSERT(testArray[0] == PTR1); |
| 27 |
MOZ_RELEASE_ASSERT(testArray.ElementAt(0) == PTR1); |
| 28 |
MOZ_RELEASE_ASSERT(testArray.Contains(PTR1)); |
| 29 |
|
| 30 |
testArray.AppendElement(PTR2); |
| 31 |
|
| 32 |
MOZ_RELEASE_ASSERT(testArray.Length() == 2); |
| 33 |
MOZ_RELEASE_ASSERT(testArray[0] == PTR1); |
| 34 |
MOZ_RELEASE_ASSERT(testArray.ElementAt(0) == PTR1); |
| 35 |
MOZ_RELEASE_ASSERT(testArray[1] == PTR2); |
| 36 |
MOZ_RELEASE_ASSERT(testArray.ElementAt(1) == PTR2); |
| 37 |
MOZ_RELEASE_ASSERT(testArray.Contains(PTR2)); |
| 38 |
|
| 39 |
MOZ_RELEASE_ASSERT(testArray.RemoveElement(PTR1)); |
| 40 |
MOZ_RELEASE_ASSERT(!testArray.RemoveElement(PTR1)); |
| 41 |
|
| 42 |
MOZ_RELEASE_ASSERT(testArray.Length() == 1); |
| 43 |
MOZ_RELEASE_ASSERT(testArray[0] == PTR2); |
| 44 |
MOZ_RELEASE_ASSERT(testArray.ElementAt(0) == PTR2); |
| 45 |
MOZ_RELEASE_ASSERT(!testArray.Contains(PTR1)); |
| 46 |
|
| 47 |
testArray.AppendElement(PTR1); |
| 48 |
|
| 49 |
MOZ_RELEASE_ASSERT(testArray.Length() == 2); |
| 50 |
MOZ_RELEASE_ASSERT(testArray[0] == PTR2); |
| 51 |
MOZ_RELEASE_ASSERT(testArray.ElementAt(0) == PTR2); |
| 52 |
MOZ_RELEASE_ASSERT(testArray[1] == PTR1); |
| 53 |
MOZ_RELEASE_ASSERT(testArray.ElementAt(1) == PTR1); |
| 54 |
MOZ_RELEASE_ASSERT(testArray.Contains(PTR1)); |
| 55 |
|
| 56 |
testArray.AppendElement(PTR3); |
| 57 |
|
| 58 |
MOZ_RELEASE_ASSERT(testArray.Length() == 3); |
| 59 |
MOZ_RELEASE_ASSERT(testArray[0] == PTR2); |
| 60 |
MOZ_RELEASE_ASSERT(testArray.ElementAt(0) == PTR2); |
| 61 |
MOZ_RELEASE_ASSERT(testArray[1] == PTR1); |
| 62 |
MOZ_RELEASE_ASSERT(testArray.ElementAt(1) == PTR1); |
| 63 |
MOZ_RELEASE_ASSERT(testArray[2] == PTR3); |
| 64 |
MOZ_RELEASE_ASSERT(testArray.ElementAt(2) == PTR3); |
| 65 |
MOZ_RELEASE_ASSERT(testArray.Contains(PTR3)); |
| 66 |
|
| 67 |
MOZ_RELEASE_ASSERT(testArray.RemoveElement(PTR1)); |
| 68 |
|
| 69 |
MOZ_RELEASE_ASSERT(testArray.Length() == 2); |
| 70 |
MOZ_RELEASE_ASSERT(testArray[0] == PTR2); |
| 71 |
MOZ_RELEASE_ASSERT(testArray.ElementAt(0) == PTR2); |
| 72 |
MOZ_RELEASE_ASSERT(testArray[1] == PTR3); |
| 73 |
MOZ_RELEASE_ASSERT(testArray.ElementAt(1) == PTR3); |
| 74 |
|
| 75 |
MOZ_RELEASE_ASSERT(testArray.RemoveElement(PTR2)); |
| 76 |
|
| 77 |
MOZ_RELEASE_ASSERT(testArray.Length() == 1); |
| 78 |
MOZ_RELEASE_ASSERT(testArray[0] == PTR3); |
| 79 |
MOZ_RELEASE_ASSERT(testArray.ElementAt(0) == PTR3); |
| 80 |
|
| 81 |
MOZ_RELEASE_ASSERT(testArray.RemoveElement(PTR3)); |
| 82 |
|
| 83 |
MOZ_RELEASE_ASSERT(testArray.Length() == 0); |
| 84 |
|
| 85 |
testArray.Clear(); |
| 86 |
|
| 87 |
MOZ_RELEASE_ASSERT(testArray.Length() == 0); |
| 88 |
|
| 89 |
testArray.AppendElement(PTR1); |
| 90 |
|
| 91 |
MOZ_RELEASE_ASSERT(testArray.Length() == 1); |
| 92 |
MOZ_RELEASE_ASSERT(testArray[0] == PTR1); |
| 93 |
MOZ_RELEASE_ASSERT(testArray.ElementAt(0) == PTR1); |
| 94 |
|
| 95 |
testArray.AppendElement(PTR2); |
| 96 |
|
| 97 |
MOZ_RELEASE_ASSERT(testArray.Length() == 2); |
| 98 |
MOZ_RELEASE_ASSERT(testArray[0] == PTR1); |
| 99 |
MOZ_RELEASE_ASSERT(testArray.ElementAt(0) == PTR1); |
| 100 |
MOZ_RELEASE_ASSERT(testArray[1] == PTR2); |
| 101 |
MOZ_RELEASE_ASSERT(testArray.ElementAt(1) == PTR2); |
| 102 |
|
| 103 |
MOZ_RELEASE_ASSERT(testArray.RemoveElement(PTR2)); |
| 104 |
|
| 105 |
MOZ_RELEASE_ASSERT(testArray.Length() == 1); |
| 106 |
MOZ_RELEASE_ASSERT(testArray[0] == PTR1); |
| 107 |
MOZ_RELEASE_ASSERT(testArray.ElementAt(0) == PTR1); |
| 108 |
|
| 109 |
MOZ_RELEASE_ASSERT(!testArray.RemoveElement(PTR3)); |
| 110 |
|
| 111 |
MOZ_RELEASE_ASSERT(testArray.Length() == 1); |
| 112 |
MOZ_RELEASE_ASSERT(testArray[0] == PTR1); |
| 113 |
MOZ_RELEASE_ASSERT(testArray.ElementAt(0) == PTR1); |
| 114 |
} |
| 115 |
|
| 116 |
void TestRangeBasedLoops() { |
| 117 |
using namespace mozilla; |
| 118 |
SmallPointerArray<void> testArray; |
| 119 |
void* verification[3]; |
| 120 |
uint32_t entries = 0; |
| 121 |
|
| 122 |
for (void* test : testArray) { |
| 123 |
verification[entries++] = test; |
| 124 |
} |
| 125 |
|
| 126 |
MOZ_RELEASE_ASSERT(entries == 0); |
| 127 |
|
| 128 |
testArray.AppendElement(PTR1); |
| 129 |
|
| 130 |
for (void* test : testArray) { |
| 131 |
verification[entries++] = test; |
| 132 |
} |
| 133 |
|
| 134 |
MOZ_RELEASE_ASSERT(entries == 1); |
| 135 |
MOZ_RELEASE_ASSERT(verification[0] == PTR1); |
| 136 |
|
| 137 |
entries = 0; |
| 138 |
|
| 139 |
testArray.AppendElement(PTR2); |
| 140 |
|
| 141 |
for (void* test : testArray) { |
| 142 |
verification[entries++] = test; |
| 143 |
} |
| 144 |
|
| 145 |
MOZ_RELEASE_ASSERT(entries == 2); |
| 146 |
MOZ_RELEASE_ASSERT(verification[0] == PTR1); |
| 147 |
MOZ_RELEASE_ASSERT(verification[1] == PTR2); |
| 148 |
|
| 149 |
entries = 0; |
| 150 |
|
| 151 |
testArray.RemoveElement(PTR1); |
| 152 |
|
| 153 |
for (void* test : testArray) { |
| 154 |
verification[entries++] = test; |
| 155 |
} |
| 156 |
|
| 157 |
MOZ_RELEASE_ASSERT(entries == 1); |
| 158 |
MOZ_RELEASE_ASSERT(verification[0] == PTR2); |
| 159 |
|
| 160 |
entries = 0; |
| 161 |
|
| 162 |
testArray.AppendElement(PTR1); |
| 163 |
testArray.AppendElement(PTR3); |
| 164 |
|
| 165 |
for (void* test : testArray) { |
| 166 |
verification[entries++] = test; |
| 167 |
} |
| 168 |
|
| 169 |
MOZ_RELEASE_ASSERT(entries == 3); |
| 170 |
MOZ_RELEASE_ASSERT(verification[0] == PTR2); |
| 171 |
MOZ_RELEASE_ASSERT(verification[1] == PTR1); |
| 172 |
MOZ_RELEASE_ASSERT(verification[2] == PTR3); |
| 173 |
|
| 174 |
entries = 0; |
| 175 |
|
| 176 |
testArray.RemoveElement(PTR1); |
| 177 |
testArray.RemoveElement(PTR2); |
| 178 |
testArray.RemoveElement(PTR3); |
| 179 |
|
| 180 |
for (void* test : testArray) { |
| 181 |
verification[entries++] = test; |
| 182 |
} |
| 183 |
|
| 184 |
MOZ_RELEASE_ASSERT(entries == 0); |
| 185 |
|
| 186 |
testArray.Clear(); |
| 187 |
|
| 188 |
for (void* test : testArray) { |
| 189 |
verification[entries++] = test; |
| 190 |
} |
| 191 |
|
| 192 |
MOZ_RELEASE_ASSERT(entries == 0); |
| 193 |
} |
| 194 |
|
| 195 |
void TestMove() { |
| 196 |
using namespace mozilla; |
| 197 |
|
| 198 |
SmallPointerArray<void> testArray; |
| 199 |
testArray.AppendElement(PTR1); |
| 200 |
testArray.AppendElement(PTR2); |
| 201 |
|
| 202 |
SmallPointerArray<void> moved = std::move(testArray); |
| 203 |
|
| 204 |
MOZ_RELEASE_ASSERT(testArray.IsEmpty()); |
| 205 |
MOZ_RELEASE_ASSERT(moved.Length() == 2); |
| 206 |
MOZ_RELEASE_ASSERT(moved[0] == PTR1); |
| 207 |
MOZ_RELEASE_ASSERT(moved[1] == PTR2); |
| 208 |
|
| 209 |
// Heap case. |
| 210 |
moved.AppendElement(PTR3); |
| 211 |
|
| 212 |
SmallPointerArray<void> another = std::move(moved); |
| 213 |
|
| 214 |
MOZ_RELEASE_ASSERT(testArray.IsEmpty()); |
| 215 |
MOZ_RELEASE_ASSERT(moved.IsEmpty()); |
| 216 |
MOZ_RELEASE_ASSERT(another.Length() == 3); |
| 217 |
MOZ_RELEASE_ASSERT(another[0] == PTR1); |
| 218 |
MOZ_RELEASE_ASSERT(another[1] == PTR2); |
| 219 |
MOZ_RELEASE_ASSERT(another[2] == PTR3); |
| 220 |
|
| 221 |
// Move assignment. |
| 222 |
testArray = std::move(another); |
| 223 |
|
| 224 |
MOZ_RELEASE_ASSERT(moved.IsEmpty()); |
| 225 |
MOZ_RELEASE_ASSERT(another.IsEmpty()); |
| 226 |
MOZ_RELEASE_ASSERT(testArray.Length() == 3); |
| 227 |
MOZ_RELEASE_ASSERT(testArray[0] == PTR1); |
| 228 |
MOZ_RELEASE_ASSERT(testArray[1] == PTR2); |
| 229 |
MOZ_RELEASE_ASSERT(testArray[2] == PTR3); |
| 230 |
} |
| 231 |
|
| 232 |
int main() { |
| 233 |
TestArrayManipulation(); |
| 234 |
TestRangeBasedLoops(); |
| 235 |
TestMove(); |
| 236 |
return 0; |
| 237 |
} |