xpcom/tests/TestDeque.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 /* 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 "TestHarness.h"
michael@0 7 #include "nsDeque.h"
michael@0 8 #include "nsCRT.h"
michael@0 9 #include <stdio.h>
michael@0 10
michael@0 11 /**************************************************************
michael@0 12 Now define the token deallocator class...
michael@0 13 **************************************************************/
michael@0 14 class _TestDeque {
michael@0 15 public:
michael@0 16 int Test();
michael@0 17 private:
michael@0 18 int OriginalTest();
michael@0 19 int OriginalFlaw();
michael@0 20 int AssignFlaw();
michael@0 21 int TestRemove();
michael@0 22 };
michael@0 23
michael@0 24 class _Dealloc: public nsDequeFunctor {
michael@0 25 virtual void* operator()(void* aObject) {
michael@0 26 return 0;
michael@0 27 }
michael@0 28 };
michael@0 29
michael@0 30 #define TEST(aCondition, aMsg) \
michael@0 31 if (!(aCondition)) { fail("TestDeque: "#aMsg); return 1; }
michael@0 32
michael@0 33
michael@0 34 /**
michael@0 35 * conduct automated self test for this class
michael@0 36 *
michael@0 37 * @param
michael@0 38 * @return
michael@0 39 */
michael@0 40 int _TestDeque::Test() {
michael@0 41 /* the old deque should have failed a bunch of these tests */
michael@0 42 int results=0;
michael@0 43 results+=OriginalTest();
michael@0 44 results+=OriginalFlaw();
michael@0 45 results+=AssignFlaw();
michael@0 46 results+=TestRemove();
michael@0 47 return results;
michael@0 48 }
michael@0 49
michael@0 50 int _TestDeque::OriginalTest() {
michael@0 51 const int size = 200;
michael@0 52 int ints[size];
michael@0 53 int i=0;
michael@0 54 int temp;
michael@0 55 nsDeque theDeque(new _Dealloc); //construct a simple one...
michael@0 56
michael@0 57 // ints = [0...199]
michael@0 58 for (i=0;i<size;i++) { //initialize'em
michael@0 59 ints[i]=i;
michael@0 60 }
michael@0 61 // queue = [0...69]
michael@0 62 for (i=0;i<70;i++) {
michael@0 63 theDeque.Push(&ints[i]);
michael@0 64 temp=*(int*)theDeque.Peek();
michael@0 65 TEST(temp == i, "Verify end after push #1");
michael@0 66 TEST(theDeque.GetSize() == i + 1, "Verify size after push #1");
michael@0 67 }
michael@0 68 TEST(theDeque.GetSize() == 70, "Verify overall size after pushes #1");
michael@0 69 // queue = [0...14]
michael@0 70 for (i=1;i<=55;i++) {
michael@0 71 temp=*(int*)theDeque.Pop();
michael@0 72 TEST(temp == 70-i, "Verify end after pop # 1");
michael@0 73 TEST(theDeque.GetSize() == 70 - i, "Verify size after pop # 1");
michael@0 74 }
michael@0 75 TEST(theDeque.GetSize() == 15, "Verify overall size after pops");
michael@0 76
michael@0 77 // queue = [0...14,0...54]
michael@0 78 for (i=0;i<55;i++) {
michael@0 79 theDeque.Push(&ints[i]);
michael@0 80 temp=*(int*)theDeque.Peek();
michael@0 81 TEST(temp == i, "Verify end after push #2");
michael@0 82 TEST(theDeque.GetSize() == i + 15 + 1, "Verify size after push # 2");
michael@0 83 }
michael@0 84 TEST(theDeque.GetSize() == 70, "Verify size after end of all pushes #2");
michael@0 85
michael@0 86 // queue = [0...14,0...19]
michael@0 87 for (i=1;i<=35;i++) {
michael@0 88 temp=*(int*)theDeque.Pop();
michael@0 89 TEST(temp == 55-i, "Verify end after pop # 2");
michael@0 90 TEST(theDeque.GetSize() == 70 - i, "Verify size after pop #2");
michael@0 91 }
michael@0 92 TEST(theDeque.GetSize() == 35, "Verify overall size after end of all pops #2");
michael@0 93
michael@0 94 // queue = [0...14,0...19,0...34]
michael@0 95 for (i=0;i<35;i++) {
michael@0 96 theDeque.Push(&ints[i]);
michael@0 97 temp = *(int*)theDeque.Peek();
michael@0 98 TEST(temp == i, "Verify end after push # 3");
michael@0 99 TEST(theDeque.GetSize() == 35 + 1 + i, "Verify size after push #3");
michael@0 100 }
michael@0 101
michael@0 102 // queue = [0...14,0...19]
michael@0 103 for (i=0;i<35;i++) {
michael@0 104 temp=*(int*)theDeque.Pop();
michael@0 105 TEST(temp == 34 - i, "Verify end after pop # 3");
michael@0 106 }
michael@0 107
michael@0 108 // queue = [0...14]
michael@0 109 for (i=0;i<20;i++) {
michael@0 110 temp=*(int*)theDeque.Pop();
michael@0 111 TEST(temp == 19 - i, "Verify end after pop # 4");
michael@0 112 }
michael@0 113
michael@0 114 // queue = []
michael@0 115 for (i=0;i<15;i++) {
michael@0 116 temp=*(int*)theDeque.Pop();
michael@0 117 TEST(temp == 14 - i, "Verify end after pop # 5");
michael@0 118 }
michael@0 119
michael@0 120 TEST(theDeque.GetSize() == 0, "Deque should finish empty.");
michael@0 121
michael@0 122 return 0;
michael@0 123 }
michael@0 124
michael@0 125 int _TestDeque::OriginalFlaw() {
michael@0 126 int ints[200];
michael@0 127 int i=0;
michael@0 128 int temp;
michael@0 129 nsDeque d(new _Dealloc);
michael@0 130 /**
michael@0 131 * Test 1. Origin near end, semi full, call Peek().
michael@0 132 * you start, mCapacity is 8
michael@0 133 */
michael@0 134 printf("fill array\n");
michael@0 135 for (i=0; i<30; i++)
michael@0 136 ints[i]=i;
michael@0 137
michael@0 138 for (i=0; i<6; i++) {
michael@0 139 d.Push(&ints[i]);
michael@0 140 temp = *(int*)d.Peek();
michael@0 141 TEST(temp == i, "OriginalFlaw push #1");
michael@0 142 }
michael@0 143 TEST(d.GetSize() == 6, "OriginalFlaw size check #1");
michael@0 144
michael@0 145 for (i=0; i<4; i++) {
michael@0 146 temp=*(int*)d.PopFront();
michael@0 147 TEST(temp == i, "PopFront test");
michael@0 148 }
michael@0 149 // d = [4,5]
michael@0 150 TEST(d.GetSize() == 2, "OriginalFlaw size check #2");
michael@0 151
michael@0 152 for (i=0; i<4; i++) {
michael@0 153 d.Push(&ints[6 + i]);
michael@0 154 }
michael@0 155 // d = [4...9]
michael@0 156
michael@0 157 for (i=4; i<=9; i++) {
michael@0 158 temp=*(int*)d.PopFront();
michael@0 159 TEST(temp == i, "OriginalFlaw empty check");
michael@0 160 }
michael@0 161
michael@0 162 return 0;
michael@0 163 }
michael@0 164
michael@0 165 int _TestDeque::AssignFlaw() {
michael@0 166 nsDeque src(new _Dealloc),dest(new _Dealloc);
michael@0 167 return 0;
michael@0 168 }
michael@0 169
michael@0 170 static bool VerifyContents(const nsDeque& aDeque, const int* aContents, int aLength) {
michael@0 171 for (int i=0; i<aLength; ++i) {
michael@0 172 if (*(int*)aDeque.ObjectAt(i) != aContents[i]) {
michael@0 173 return false;
michael@0 174 }
michael@0 175 }
michael@0 176 return true;
michael@0 177 }
michael@0 178
michael@0 179 int _TestDeque::TestRemove() {
michael@0 180 nsDeque d;
michael@0 181 const int count = 10;
michael@0 182 int ints[count];
michael@0 183 for (int i=0; i<count; i++) {
michael@0 184 ints[i] = i;
michael@0 185 }
michael@0 186
michael@0 187 for (int i=0; i<6; i++) {
michael@0 188 d.Push(&ints[i]);
michael@0 189 }
michael@0 190 // d = [0...5]
michael@0 191 d.PopFront();
michael@0 192 d.PopFront();
michael@0 193
michael@0 194 // d = [2,5]
michael@0 195 for (int i=2; i<=5; i++) {
michael@0 196 int t = *(int*)d.ObjectAt(i-2);
michael@0 197 TEST(t == i, "Verify ObjectAt()");
michael@0 198 }
michael@0 199
michael@0 200 d.RemoveObjectAt(1);
michael@0 201 // d == [2,4,5]
michael@0 202 static const int t1[] = {2,4,5};
michael@0 203 TEST(VerifyContents(d, t1, 3), "verify contents t1");
michael@0 204
michael@0 205 d.PushFront(&ints[1]);
michael@0 206 d.PushFront(&ints[0]);
michael@0 207 d.PushFront(&ints[7]);
michael@0 208 d.PushFront(&ints[6]);
michael@0 209 // d == [6,7,0,1,2,4,5] // (0==mOrigin)
michael@0 210 static const int t2[] = {6,7,0,1,2,4,5};
michael@0 211 TEST(VerifyContents(d, t2, 7), "verify contents t2");
michael@0 212
michael@0 213 d.RemoveObjectAt(1);
michael@0 214 // d == [6,0,1,2,4,5] // (1==mOrigin)
michael@0 215 static const int t3[] = {6,0,1,2,4,5};
michael@0 216 TEST(VerifyContents(d, t3, 6), "verify contents t3");
michael@0 217
michael@0 218 d.RemoveObjectAt(5);
michael@0 219 // d == [6,0,1,2,4] // (1==mOrigin)
michael@0 220 static const int t4[] = {6,0,1,2,4};
michael@0 221 TEST(VerifyContents(d, t4, 5), "verify contents t4");
michael@0 222
michael@0 223 d.RemoveObjectAt(0);
michael@0 224 // d == [0,1,2,4] // (2==mOrigin)
michael@0 225 static const int t5[] = {0,1,2,4};
michael@0 226 TEST(VerifyContents(d, t5, 4), "verify contents t5");
michael@0 227
michael@0 228
michael@0 229 return 0;
michael@0 230 }
michael@0 231
michael@0 232 int main (void) {
michael@0 233 ScopedXPCOM xpcom("TestTimers");
michael@0 234 NS_ENSURE_FALSE(xpcom.failed(), 1);
michael@0 235
michael@0 236 _TestDeque test;
michael@0 237 int result = test.Test();
michael@0 238 TEST(result == 0, "All tests pass");
michael@0 239 return 0;
michael@0 240 }

mercurial