xpcom/tests/TestArray.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: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include <stdio.h>
michael@0 7 #include <stdlib.h>
michael@0 8 #include "nsISupportsArray.h"
michael@0 9
michael@0 10 // {9e70a320-be02-11d1-8031-006008159b5a}
michael@0 11 #define NS_IFOO_IID \
michael@0 12 {0x9e70a320, 0xbe02, 0x11d1, \
michael@0 13 {0x80, 0x31, 0x00, 0x60, 0x08, 0x15, 0x9b, 0x5a}}
michael@0 14
michael@0 15 namespace TestArray {
michael@0 16
michael@0 17 static const bool kExitOnError = true;
michael@0 18
michael@0 19 class IFoo : public nsISupports {
michael@0 20 public:
michael@0 21
michael@0 22 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFOO_IID)
michael@0 23
michael@0 24 NS_IMETHOD_(nsrefcnt) RefCnt() = 0;
michael@0 25 NS_IMETHOD_(int32_t) ID() = 0;
michael@0 26 };
michael@0 27
michael@0 28 NS_DEFINE_STATIC_IID_ACCESSOR(IFoo, NS_IFOO_IID)
michael@0 29
michael@0 30 class Foo : public IFoo {
michael@0 31 public:
michael@0 32
michael@0 33 Foo(int32_t aID);
michael@0 34
michael@0 35 // nsISupports implementation
michael@0 36 NS_DECL_ISUPPORTS
michael@0 37
michael@0 38 // IFoo implementation
michael@0 39 NS_IMETHOD_(nsrefcnt) RefCnt() { return mRefCnt; }
michael@0 40 NS_IMETHOD_(int32_t) ID() { return mID; }
michael@0 41
michael@0 42 static int32_t gCount;
michael@0 43
michael@0 44 int32_t mID;
michael@0 45
michael@0 46 private:
michael@0 47 ~Foo();
michael@0 48 };
michael@0 49
michael@0 50 int32_t Foo::gCount;
michael@0 51
michael@0 52 Foo::Foo(int32_t aID)
michael@0 53 {
michael@0 54 mID = aID;
michael@0 55 ++gCount;
michael@0 56 fprintf(stdout, "init: %d (%p), %d total)\n",
michael@0 57 mID, static_cast<void*>(this), gCount);
michael@0 58 }
michael@0 59
michael@0 60 Foo::~Foo()
michael@0 61 {
michael@0 62 --gCount;
michael@0 63 fprintf(stdout, "destruct: %d (%p), %d remain)\n",
michael@0 64 mID, static_cast<void*>(this), gCount);
michael@0 65 }
michael@0 66
michael@0 67 NS_IMPL_ISUPPORTS(Foo, IFoo)
michael@0 68
michael@0 69 const char* AssertEqual(int32_t aValue1, int32_t aValue2)
michael@0 70 {
michael@0 71 if (aValue1 == aValue2) {
michael@0 72 return "OK";
michael@0 73 }
michael@0 74 if (kExitOnError) {
michael@0 75 exit(1);
michael@0 76 }
michael@0 77 return "ERROR";
michael@0 78 }
michael@0 79
michael@0 80 void DumpArray(nsISupportsArray* aArray, int32_t aExpectedCount, int32_t aElementIDs[], int32_t aExpectedTotal)
michael@0 81 {
michael@0 82 uint32_t cnt = 0;
michael@0 83 #ifdef DEBUG
michael@0 84 nsresult rv =
michael@0 85 #endif
michael@0 86 aArray->Count(&cnt);
michael@0 87 NS_ASSERTION(NS_SUCCEEDED(rv), "Count failed");
michael@0 88 int32_t count = cnt;
michael@0 89 int32_t index;
michael@0 90
michael@0 91 fprintf(stdout, "object count %d = %d %s\n", Foo::gCount, aExpectedTotal,
michael@0 92 AssertEqual(Foo::gCount, aExpectedTotal));
michael@0 93 fprintf(stdout, "array count %d = %d %s\n", count, aExpectedCount,
michael@0 94 AssertEqual(count, aExpectedCount));
michael@0 95
michael@0 96 for (index = 0; (index < count) && (index < aExpectedCount); index++) {
michael@0 97 IFoo* foo = (IFoo*)(aArray->ElementAt(index));
michael@0 98 fprintf(stdout, "%2d: %d=%d (%p) c: %d %s\n",
michael@0 99 index, aElementIDs[index], foo->ID(),
michael@0 100 static_cast<void*>(foo), foo->RefCnt() - 1,
michael@0 101 AssertEqual(foo->ID(), aElementIDs[index]));
michael@0 102 foo->Release();
michael@0 103 }
michael@0 104 }
michael@0 105
michael@0 106 void FillArray(nsISupportsArray* aArray, int32_t aCount)
michael@0 107 {
michael@0 108 int32_t index;
michael@0 109 for (index = 0; index < aCount; index++) {
michael@0 110 nsCOMPtr<IFoo> foo = new Foo(index);
michael@0 111 aArray->AppendElement(foo);
michael@0 112 }
michael@0 113 }
michael@0 114
michael@0 115 }
michael@0 116
michael@0 117 using namespace TestArray;
michael@0 118
michael@0 119 int main(int argc, char *argv[])
michael@0 120 {
michael@0 121 nsISupportsArray* array;
michael@0 122 nsresult rv;
michael@0 123
michael@0 124 if (NS_OK == (rv = NS_NewISupportsArray(&array))) {
michael@0 125 FillArray(array, 10);
michael@0 126 fprintf(stdout, "Array created:\n");
michael@0 127 int32_t fillResult[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
michael@0 128 DumpArray(array, 10, fillResult, 10);
michael@0 129
michael@0 130 // test insert
michael@0 131 IFoo* foo = (IFoo*)array->ElementAt(3);
michael@0 132 foo->Release(); // pre-release to fix ref count for dumps
michael@0 133 array->InsertElementAt(foo, 5);
michael@0 134 fprintf(stdout, "insert 3 at 5:\n");
michael@0 135 int32_t insertResult[11] = {0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9};
michael@0 136 DumpArray(array, 11, insertResult, 10);
michael@0 137 fprintf(stdout, "insert 3 at 0:\n");
michael@0 138 array->InsertElementAt(foo, 0);
michael@0 139 int32_t insertResult2[12] = {3, 0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9};
michael@0 140 DumpArray(array, 12, insertResult2, 10);
michael@0 141 fprintf(stdout, "append 3:\n");
michael@0 142 array->AppendElement(foo);
michael@0 143 int32_t appendResult[13] = {3, 0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9, 3};
michael@0 144 DumpArray(array, 13, appendResult, 10);
michael@0 145
michael@0 146
michael@0 147 // test IndexOf && LastIndexOf
michael@0 148 int32_t expectedIndex[5] = {0, 4, 6, 12, -1};
michael@0 149 int32_t count = 0;
michael@0 150 int32_t index = array->IndexOf(foo);
michael@0 151 fprintf(stdout, "IndexOf(foo): %d=%d %s\n", index, expectedIndex[count],
michael@0 152 AssertEqual(index, expectedIndex[count]));
michael@0 153 while (-1 != index) {
michael@0 154 count++;
michael@0 155 index = array->IndexOfStartingAt(foo, index + 1);
michael@0 156 if (-1 != index)
michael@0 157 fprintf(stdout, "IndexOf(foo): %d=%d %s\n", index, expectedIndex[count],
michael@0 158 AssertEqual(index, expectedIndex[count]));
michael@0 159 }
michael@0 160 index = array->LastIndexOf(foo);
michael@0 161 count--;
michael@0 162 fprintf(stdout, "LastIndexOf(foo): %d=%d %s\n", index, expectedIndex[count],
michael@0 163 AssertEqual(index, expectedIndex[count]));
michael@0 164
michael@0 165 // test ReplaceElementAt
michael@0 166 fprintf(stdout, "ReplaceElementAt(8):\n");
michael@0 167 array->ReplaceElementAt(foo, 8);
michael@0 168 int32_t replaceResult[13] = {3, 0, 1, 2, 3, 4, 3, 5, 3, 7, 8, 9, 3};
michael@0 169 DumpArray(array, 13, replaceResult, 9);
michael@0 170
michael@0 171 // test RemoveElementAt, RemoveElement RemoveLastElement
michael@0 172 fprintf(stdout, "RemoveElementAt(0):\n");
michael@0 173 array->RemoveElementAt(0);
michael@0 174 int32_t removeResult[12] = {0, 1, 2, 3, 4, 3, 5, 3, 7, 8, 9, 3};
michael@0 175 DumpArray(array, 12, removeResult, 9);
michael@0 176 fprintf(stdout, "RemoveElementAt(7):\n");
michael@0 177 array->RemoveElementAt(7);
michael@0 178 int32_t removeResult2[11] = {0, 1, 2, 3, 4, 3, 5, 7, 8, 9, 3};
michael@0 179 DumpArray(array, 11, removeResult2, 9);
michael@0 180 fprintf(stdout, "RemoveElement(foo):\n");
michael@0 181 array->RemoveElement(foo);
michael@0 182 int32_t removeResult3[10] = {0, 1, 2, 4, 3, 5, 7, 8, 9, 3};
michael@0 183 DumpArray(array, 10, removeResult3, 9);
michael@0 184 fprintf(stdout, "RemoveLastElement(foo):\n");
michael@0 185 array->RemoveLastElement(foo);
michael@0 186 int32_t removeResult4[9] = {0, 1, 2, 4, 3, 5, 7, 8, 9};
michael@0 187 DumpArray(array, 9, removeResult4, 9);
michael@0 188
michael@0 189 // test clear
michael@0 190 fprintf(stdout, "clear array:\n");
michael@0 191 array->Clear();
michael@0 192 DumpArray(array, 0, 0, 0);
michael@0 193 fprintf(stdout, "add 4 new:\n");
michael@0 194 FillArray(array, 4);
michael@0 195 DumpArray(array, 4, fillResult, 4);
michael@0 196
michael@0 197 // test compact
michael@0 198 fprintf(stdout, "compact array:\n");
michael@0 199 array->Compact();
michael@0 200 DumpArray(array, 4, fillResult, 4);
michael@0 201
michael@0 202 // test delete
michael@0 203 fprintf(stdout, "release array:\n");
michael@0 204 NS_RELEASE(array);
michael@0 205 }
michael@0 206 else {
michael@0 207 fprintf(stdout, "error can't create array: %x\n", rv);
michael@0 208 }
michael@0 209
michael@0 210 return 0;
michael@0 211 }

mercurial