xpcom/tests/TestCOMArray.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 // vim:cindent:ts=4:et:sw=4:
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "TestHarness.h"
michael@0 8 #include "nsCOMArray.h"
michael@0 9 #include "mozilla/Attributes.h"
michael@0 10
michael@0 11 // {9e70a320-be02-11d1-8031-006008159b5a}
michael@0 12 #define NS_IFOO_IID \
michael@0 13 {0x9e70a320, 0xbe02, 0x11d1, \
michael@0 14 {0x80, 0x31, 0x00, 0x60, 0x08, 0x15, 0x9b, 0x5a}}
michael@0 15
michael@0 16 class IFoo : public nsISupports {
michael@0 17 public:
michael@0 18
michael@0 19 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFOO_IID)
michael@0 20
michael@0 21 NS_IMETHOD_(MozExternalRefCountType) RefCnt() = 0;
michael@0 22 NS_IMETHOD_(int32_t) ID() = 0;
michael@0 23 };
michael@0 24
michael@0 25 NS_DEFINE_STATIC_IID_ACCESSOR(IFoo, NS_IFOO_IID)
michael@0 26
michael@0 27 class Foo MOZ_FINAL : public IFoo {
michael@0 28 public:
michael@0 29
michael@0 30 Foo(int32_t aID);
michael@0 31 ~Foo();
michael@0 32
michael@0 33 // nsISupports implementation
michael@0 34 NS_DECL_ISUPPORTS
michael@0 35
michael@0 36 // IFoo implementation
michael@0 37 NS_IMETHOD_(MozExternalRefCountType) RefCnt() { return mRefCnt; }
michael@0 38 NS_IMETHOD_(int32_t) ID() { return mID; }
michael@0 39
michael@0 40 static int32_t gCount;
michael@0 41
michael@0 42 int32_t mID;
michael@0 43 };
michael@0 44
michael@0 45 int32_t Foo::gCount = 0;
michael@0 46
michael@0 47 Foo::Foo(int32_t aID)
michael@0 48 {
michael@0 49 mID = aID;
michael@0 50 ++gCount;
michael@0 51 }
michael@0 52
michael@0 53 Foo::~Foo()
michael@0 54 {
michael@0 55 --gCount;
michael@0 56 }
michael@0 57
michael@0 58 NS_IMPL_ISUPPORTS(Foo, IFoo)
michael@0 59
michael@0 60
michael@0 61 typedef nsCOMArray<IFoo> Array;
michael@0 62
michael@0 63
michael@0 64 // {0e70a320-be02-11d1-8031-006008159b5a}
michael@0 65 #define NS_IBAR_IID \
michael@0 66 {0x0e70a320, 0xbe02, 0x11d1, \
michael@0 67 {0x80, 0x31, 0x00, 0x60, 0x08, 0x15, 0x9b, 0x5a}}
michael@0 68
michael@0 69 class IBar : public nsISupports {
michael@0 70 public:
michael@0 71
michael@0 72 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IBAR_IID)
michael@0 73 };
michael@0 74
michael@0 75 NS_DEFINE_STATIC_IID_ACCESSOR(IBar, NS_IBAR_IID)
michael@0 76
michael@0 77 class Bar MOZ_FINAL : public IBar {
michael@0 78 public:
michael@0 79
michael@0 80 explicit Bar(nsCOMArray<IBar>& aArray);
michael@0 81 ~Bar();
michael@0 82
michael@0 83 // nsISupports implementation
michael@0 84 NS_DECL_ISUPPORTS
michael@0 85
michael@0 86 static int32_t sReleaseCalled;
michael@0 87
michael@0 88 private:
michael@0 89 nsCOMArray<IBar>& mArray;
michael@0 90 };
michael@0 91
michael@0 92 int32_t Bar::sReleaseCalled = 0;
michael@0 93
michael@0 94 typedef nsCOMArray<IBar> Array2;
michael@0 95
michael@0 96 Bar::Bar(Array2& aArray)
michael@0 97 : mArray(aArray)
michael@0 98 {
michael@0 99 }
michael@0 100
michael@0 101 Bar::~Bar()
michael@0 102 {
michael@0 103 if (mArray.RemoveObject(this)) {
michael@0 104 fail("We should never manage to remove the object here");
michael@0 105 }
michael@0 106 }
michael@0 107
michael@0 108 NS_IMPL_ADDREF(Bar)
michael@0 109 NS_IMPL_QUERY_INTERFACE(Bar, IBar)
michael@0 110
michael@0 111 NS_IMETHODIMP_(MozExternalRefCountType)
michael@0 112 Bar::Release(void)
michael@0 113 {
michael@0 114 ++Bar::sReleaseCalled;
michael@0 115 MOZ_ASSERT(int32_t(mRefCnt) > 0, "dup release");
michael@0 116 NS_ASSERT_OWNINGTHREAD(_class);
michael@0 117 --mRefCnt;
michael@0 118 NS_LOG_RELEASE(this, mRefCnt, "Bar");
michael@0 119 if (mRefCnt == 0) {
michael@0 120 mRefCnt = 1; /* stabilize */
michael@0 121 delete this;
michael@0 122 return 0;
michael@0 123 }
michael@0 124 return mRefCnt;
michael@0 125 }
michael@0 126
michael@0 127
michael@0 128 int main(int argc, char **argv)
michael@0 129 {
michael@0 130 ScopedXPCOM xpcom("nsCOMArrayTests");
michael@0 131 if (xpcom.failed()) {
michael@0 132 return 1;
michael@0 133 }
michael@0 134
michael@0 135 int rv = 0;
michael@0 136
michael@0 137 Array arr;
michael@0 138
michael@0 139 for (int32_t i = 0; i < 20; ++i) {
michael@0 140 nsCOMPtr<IFoo> foo = new Foo(i);
michael@0 141 arr.AppendObject(foo);
michael@0 142 }
michael@0 143
michael@0 144 if (arr.Count() != 20 || Foo::gCount != 20) {
michael@0 145 fail("nsCOMArray::AppendObject failed");
michael@0 146 rv = 1;
michael@0 147 }
michael@0 148
michael@0 149 arr.TruncateLength(10);
michael@0 150
michael@0 151 if (arr.Count() != 10 || Foo::gCount != 10) {
michael@0 152 fail("nsCOMArray::TruncateLength shortening of array failed");
michael@0 153 rv = 1;
michael@0 154 }
michael@0 155
michael@0 156 arr.SetCount(30);
michael@0 157
michael@0 158 if (arr.Count() != 30 || Foo::gCount != 10) {
michael@0 159 fail("nsCOMArray::SetCount lengthening of array failed");
michael@0 160 rv = 1;
michael@0 161 }
michael@0 162
michael@0 163 for (int32_t i = 0; i < 10; ++i) {
michael@0 164 if (arr[i] == nullptr) {
michael@0 165 fail("nsCOMArray elements should be non-null");
michael@0 166 rv = 1;
michael@0 167 break;
michael@0 168 }
michael@0 169 }
michael@0 170
michael@0 171 for (int32_t i = 10; i < 30; ++i) {
michael@0 172 if (arr[i] != nullptr) {
michael@0 173 fail("nsCOMArray elements should be null");
michael@0 174 rv = 1;
michael@0 175 break;
michael@0 176 }
michael@0 177 }
michael@0 178
michael@0 179 int32_t base;
michael@0 180 {
michael@0 181 Array2 arr2;
michael@0 182
michael@0 183 IBar *thirdObject = nullptr,
michael@0 184 *fourthObject = nullptr,
michael@0 185 *fifthObject = nullptr,
michael@0 186 *ninthObject = nullptr;
michael@0 187 for (int32_t i = 0; i < 20; ++i) {
michael@0 188 nsCOMPtr<IBar> bar = new Bar(arr2);
michael@0 189 switch (i) {
michael@0 190 case 2:
michael@0 191 thirdObject = bar; break;
michael@0 192 case 3:
michael@0 193 fourthObject = bar; break;
michael@0 194 case 4:
michael@0 195 fifthObject = bar; break;
michael@0 196 case 8:
michael@0 197 ninthObject = bar; break;
michael@0 198 }
michael@0 199 arr2.AppendObject(bar);
michael@0 200 }
michael@0 201
michael@0 202 base = Bar::sReleaseCalled;
michael@0 203
michael@0 204 arr2.SetCount(10);
michael@0 205 if (Bar::sReleaseCalled != base + 10) {
michael@0 206 fail("Release called multiple times for SetCount");
michael@0 207 }
michael@0 208 if (arr2.Count() != 10) {
michael@0 209 fail("SetCount(10) should remove exactly ten objects");
michael@0 210 }
michael@0 211
michael@0 212 arr2.RemoveObjectAt(9);
michael@0 213 if (Bar::sReleaseCalled != base + 11) {
michael@0 214 fail("Release called multiple times for RemoveObjectAt");
michael@0 215 }
michael@0 216 if (arr2.Count() != 9) {
michael@0 217 fail("RemoveObjectAt should remove exactly one object");
michael@0 218 }
michael@0 219
michael@0 220 arr2.RemoveObject(ninthObject);
michael@0 221 if (Bar::sReleaseCalled != base + 12) {
michael@0 222 fail("Release called multiple times for RemoveObject");
michael@0 223 }
michael@0 224 if (arr2.Count() != 8) {
michael@0 225 fail("RemoveObject should remove exactly one object");
michael@0 226 }
michael@0 227
michael@0 228 arr2.RemoveObjectsAt(2, 3);
michael@0 229 if (Bar::sReleaseCalled != base + 15) {
michael@0 230 fail("Release called more or less than three times for RemoveObjectsAt");
michael@0 231 }
michael@0 232 if (arr2.Count() != 5) {
michael@0 233 fail("RemoveObjectsAt should remove exactly three objects");
michael@0 234 }
michael@0 235 for (int32_t j = 0; j < arr2.Count(); ++j) {
michael@0 236 if (arr2.ObjectAt(j) == thirdObject) {
michael@0 237 fail("RemoveObjectsAt should have removed thirdObject");
michael@0 238 }
michael@0 239 if (arr2.ObjectAt(j) == fourthObject) {
michael@0 240 fail("RemoveObjectsAt should have removed fourthObject");
michael@0 241 }
michael@0 242 if (arr2.ObjectAt(j) == fifthObject) {
michael@0 243 fail("RemoveObjectsAt should have removed fifthObject");
michael@0 244 }
michael@0 245 }
michael@0 246
michael@0 247 arr2.RemoveObjectsAt(4, 1);
michael@0 248 if (Bar::sReleaseCalled != base + 16) {
michael@0 249 fail("Release called more or less than one time for RemoveObjectsAt");
michael@0 250 }
michael@0 251 if (arr2.Count() != 4) {
michael@0 252 fail("RemoveObjectsAt should work for removing the last element");
michael@0 253 }
michael@0 254
michael@0 255 arr2.Clear();
michael@0 256 if (Bar::sReleaseCalled != base + 20) {
michael@0 257 fail("Release called multiple times for Clear");
michael@0 258 }
michael@0 259 }
michael@0 260
michael@0 261 {
michael@0 262 Array2 arr2;
michael@0 263
michael@0 264 IBar *thirdElement = nullptr,
michael@0 265 *fourthElement = nullptr,
michael@0 266 *fifthElement = nullptr,
michael@0 267 *ninthElement = nullptr;
michael@0 268 for (int32_t i = 0; i < 20; ++i) {
michael@0 269 nsCOMPtr<IBar> bar = new Bar(arr2);
michael@0 270 switch (i) {
michael@0 271 case 2:
michael@0 272 thirdElement = bar; break;
michael@0 273 case 3:
michael@0 274 fourthElement = bar; break;
michael@0 275 case 4:
michael@0 276 fifthElement = bar; break;
michael@0 277 case 8:
michael@0 278 ninthElement = bar; break;
michael@0 279 }
michael@0 280 arr2.AppendElement(bar);
michael@0 281 }
michael@0 282
michael@0 283 base = Bar::sReleaseCalled;
michael@0 284
michael@0 285 arr2.TruncateLength(10);
michael@0 286 if (Bar::sReleaseCalled != base + 10) {
michael@0 287 fail("Release called multiple times for TruncateLength");
michael@0 288 }
michael@0 289 if (arr2.Length() != 10) {
michael@0 290 fail("TruncateLength(10) should remove exactly ten objects");
michael@0 291 }
michael@0 292
michael@0 293 arr2.RemoveElementAt(9);
michael@0 294 if (Bar::sReleaseCalled != base + 11) {
michael@0 295 fail("Release called multiple times for RemoveElementAt");
michael@0 296 }
michael@0 297 if (arr2.Length() != 9) {
michael@0 298 fail("RemoveElementAt should remove exactly one object");
michael@0 299 }
michael@0 300
michael@0 301 arr2.RemoveElement(ninthElement);
michael@0 302 if (Bar::sReleaseCalled != base + 12) {
michael@0 303 fail("Release called multiple times for RemoveElement");
michael@0 304 }
michael@0 305 if (arr2.Length() != 8) {
michael@0 306 fail("RemoveElement should remove exactly one object");
michael@0 307 }
michael@0 308
michael@0 309 arr2.RemoveElementsAt(2, 3);
michael@0 310 if (Bar::sReleaseCalled != base + 15) {
michael@0 311 fail("Release called more or less than three times for RemoveElementsAt");
michael@0 312 }
michael@0 313 if (arr2.Length() != 5) {
michael@0 314 fail("RemoveElementsAt should remove exactly three objects");
michael@0 315 }
michael@0 316 for (uint32_t j = 0; j < arr2.Length(); ++j) {
michael@0 317 if (arr2.ElementAt(j) == thirdElement) {
michael@0 318 fail("RemoveElementsAt should have removed thirdElement");
michael@0 319 }
michael@0 320 if (arr2.ElementAt(j) == fourthElement) {
michael@0 321 fail("RemoveElementsAt should have removed fourthElement");
michael@0 322 }
michael@0 323 if (arr2.ElementAt(j) == fifthElement) {
michael@0 324 fail("RemoveElementsAt should have removed fifthElement");
michael@0 325 }
michael@0 326 }
michael@0 327
michael@0 328 arr2.RemoveElementsAt(4, 1);
michael@0 329 if (Bar::sReleaseCalled != base + 16) {
michael@0 330 fail("Release called more or less than one time for RemoveElementsAt");
michael@0 331 }
michael@0 332 if (arr2.Length() != 4) {
michael@0 333 fail("RemoveElementsAt should work for removing the last element");
michael@0 334 }
michael@0 335
michael@0 336 arr2.Clear();
michael@0 337 if (Bar::sReleaseCalled != base + 20) {
michael@0 338 fail("Release called multiple times for Clear");
michael@0 339 }
michael@0 340 }
michael@0 341
michael@0 342 Bar::sReleaseCalled = 0;
michael@0 343
michael@0 344 {
michael@0 345 Array2 arr2;
michael@0 346
michael@0 347 for (int32_t i = 0; i < 20; ++i) {
michael@0 348 nsCOMPtr<IBar> bar = new Bar(arr2);
michael@0 349 arr2.AppendObject(bar);
michael@0 350 }
michael@0 351
michael@0 352 base = Bar::sReleaseCalled;
michael@0 353
michael@0 354 // Let arr2 be destroyed
michael@0 355 }
michael@0 356 if (Bar::sReleaseCalled != base + 20) {
michael@0 357 fail("Release called multiple times for nsCOMArray::~nsCOMArray");
michael@0 358 }
michael@0 359
michael@0 360 return rv;
michael@0 361 }

mercurial