xpcom/tests/TestDeque.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/tests/TestDeque.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,240 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "TestHarness.h"
    1.10 +#include "nsDeque.h"
    1.11 +#include "nsCRT.h"
    1.12 +#include <stdio.h>
    1.13 +
    1.14 +/**************************************************************
    1.15 +  Now define the token deallocator class...
    1.16 + **************************************************************/
    1.17 +class _TestDeque {
    1.18 +public:
    1.19 +  int Test();
    1.20 +private:
    1.21 +  int OriginalTest();
    1.22 +  int OriginalFlaw();
    1.23 +  int AssignFlaw();
    1.24 +  int TestRemove();
    1.25 +};
    1.26 +
    1.27 +class _Dealloc: public nsDequeFunctor {
    1.28 +  virtual void* operator()(void* aObject) {
    1.29 +    return 0;
    1.30 +  }
    1.31 +};
    1.32 +
    1.33 +#define TEST(aCondition, aMsg) \
    1.34 +  if (!(aCondition)) { fail("TestDeque: "#aMsg); return 1; }
    1.35 +
    1.36 +
    1.37 +/**
    1.38 + * conduct automated self test for this class
    1.39 + *
    1.40 + * @param
    1.41 + * @return
    1.42 + */
    1.43 +int _TestDeque::Test() {
    1.44 +  /* the old deque should have failed a bunch of these tests */
    1.45 +  int results=0;
    1.46 +  results+=OriginalTest();
    1.47 +  results+=OriginalFlaw();
    1.48 +  results+=AssignFlaw();
    1.49 +  results+=TestRemove();
    1.50 +  return results;
    1.51 +}
    1.52 +
    1.53 +int _TestDeque::OriginalTest() {
    1.54 +  const int size = 200;
    1.55 +  int ints[size];
    1.56 +  int i=0;
    1.57 +  int temp;
    1.58 +  nsDeque theDeque(new _Dealloc); //construct a simple one...
    1.59 + 
    1.60 +  // ints = [0...199]
    1.61 +  for (i=0;i<size;i++) { //initialize'em
    1.62 +    ints[i]=i;
    1.63 +  }
    1.64 +  // queue = [0...69]
    1.65 +  for (i=0;i<70;i++) {
    1.66 +    theDeque.Push(&ints[i]);
    1.67 +    temp=*(int*)theDeque.Peek();
    1.68 +    TEST(temp == i, "Verify end after push #1");
    1.69 +    TEST(theDeque.GetSize() == i + 1, "Verify size after push #1");
    1.70 +  }
    1.71 +  TEST(theDeque.GetSize() == 70, "Verify overall size after pushes #1");
    1.72 +  // queue = [0...14]
    1.73 +  for (i=1;i<=55;i++) {
    1.74 +    temp=*(int*)theDeque.Pop();
    1.75 +    TEST(temp == 70-i, "Verify end after pop # 1");
    1.76 +    TEST(theDeque.GetSize() == 70 - i, "Verify size after pop # 1");
    1.77 +  }
    1.78 +  TEST(theDeque.GetSize() == 15, "Verify overall size after pops");
    1.79 +
    1.80 +  // queue = [0...14,0...54]
    1.81 +  for (i=0;i<55;i++) {
    1.82 +    theDeque.Push(&ints[i]);
    1.83 +    temp=*(int*)theDeque.Peek();
    1.84 +    TEST(temp == i, "Verify end after push #2");
    1.85 +    TEST(theDeque.GetSize() == i + 15 + 1, "Verify size after push # 2");
    1.86 +  }
    1.87 +  TEST(theDeque.GetSize() == 70, "Verify size after end of all pushes #2");
    1.88 +
    1.89 +  // queue = [0...14,0...19]
    1.90 +  for (i=1;i<=35;i++) {
    1.91 +    temp=*(int*)theDeque.Pop();
    1.92 +    TEST(temp == 55-i, "Verify end after pop # 2");
    1.93 +    TEST(theDeque.GetSize() == 70 - i, "Verify size after pop #2");
    1.94 +  }
    1.95 +  TEST(theDeque.GetSize() == 35, "Verify overall size after end of all pops #2");
    1.96 +
    1.97 +  // queue = [0...14,0...19,0...34]
    1.98 +  for (i=0;i<35;i++) {
    1.99 +    theDeque.Push(&ints[i]);
   1.100 +    temp = *(int*)theDeque.Peek();
   1.101 +    TEST(temp == i, "Verify end after push # 3");
   1.102 +    TEST(theDeque.GetSize() == 35 + 1 + i, "Verify size after push #3");
   1.103 +  }
   1.104 +
   1.105 +  // queue = [0...14,0...19]
   1.106 +  for (i=0;i<35;i++) {
   1.107 +    temp=*(int*)theDeque.Pop();
   1.108 +    TEST(temp == 34 - i, "Verify end after pop # 3");
   1.109 +  }
   1.110 +
   1.111 +  // queue = [0...14]
   1.112 +  for (i=0;i<20;i++) {
   1.113 +    temp=*(int*)theDeque.Pop();
   1.114 +    TEST(temp == 19 - i, "Verify end after pop # 4");
   1.115 +  }
   1.116 +
   1.117 +  // queue = []
   1.118 +  for (i=0;i<15;i++) {
   1.119 +    temp=*(int*)theDeque.Pop();
   1.120 +    TEST(temp == 14 - i, "Verify end after pop # 5");
   1.121 +  }
   1.122 +
   1.123 +  TEST(theDeque.GetSize() == 0, "Deque should finish empty.");
   1.124 +
   1.125 +  return 0;
   1.126 +}
   1.127 +
   1.128 +int _TestDeque::OriginalFlaw() {
   1.129 +  int ints[200];
   1.130 +  int i=0;
   1.131 +  int temp;
   1.132 +  nsDeque d(new _Dealloc);
   1.133 +  /**
   1.134 +   * Test 1. Origin near end, semi full, call Peek().
   1.135 +   * you start, mCapacity is 8
   1.136 +   */
   1.137 +  printf("fill array\n");
   1.138 +  for (i=0; i<30; i++)
   1.139 +    ints[i]=i;
   1.140 +
   1.141 +  for (i=0; i<6; i++) {
   1.142 +    d.Push(&ints[i]);
   1.143 +    temp = *(int*)d.Peek();
   1.144 +    TEST(temp == i, "OriginalFlaw push #1");
   1.145 +  }
   1.146 +  TEST(d.GetSize() == 6, "OriginalFlaw size check #1");
   1.147 +
   1.148 +  for (i=0; i<4; i++) {
   1.149 +    temp=*(int*)d.PopFront();
   1.150 +    TEST(temp == i, "PopFront test");
   1.151 +  }
   1.152 +  // d = [4,5]
   1.153 +  TEST(d.GetSize() == 2, "OriginalFlaw size check #2");
   1.154 +
   1.155 +  for (i=0; i<4; i++) {
   1.156 +    d.Push(&ints[6 + i]);
   1.157 +  }
   1.158 +  // d = [4...9]
   1.159 +
   1.160 +  for (i=4; i<=9; i++) {
   1.161 +    temp=*(int*)d.PopFront();
   1.162 +    TEST(temp == i, "OriginalFlaw empty check");
   1.163 +  }
   1.164 +
   1.165 +  return 0;
   1.166 +}
   1.167 +
   1.168 +int _TestDeque::AssignFlaw() {
   1.169 +  nsDeque src(new _Dealloc),dest(new _Dealloc);
   1.170 +  return 0;
   1.171 +}
   1.172 +
   1.173 +static bool VerifyContents(const nsDeque& aDeque, const int* aContents, int aLength) {
   1.174 +  for (int i=0; i<aLength; ++i) {
   1.175 +    if (*(int*)aDeque.ObjectAt(i) != aContents[i]) {
   1.176 +      return false;
   1.177 +    }
   1.178 +  }
   1.179 +  return true;
   1.180 +}
   1.181 +
   1.182 +int _TestDeque::TestRemove() {
   1.183 +  nsDeque d;
   1.184 +  const int count = 10;
   1.185 +  int ints[count];
   1.186 +  for (int i=0; i<count; i++) {
   1.187 +    ints[i] = i;
   1.188 +  }
   1.189 +
   1.190 +  for (int i=0; i<6; i++) {
   1.191 +    d.Push(&ints[i]);
   1.192 +  }
   1.193 +  // d = [0...5]
   1.194 +  d.PopFront();
   1.195 +  d.PopFront();
   1.196 +
   1.197 +  // d = [2,5]
   1.198 +  for (int i=2; i<=5; i++) {
   1.199 +    int t = *(int*)d.ObjectAt(i-2);
   1.200 +    TEST(t == i, "Verify ObjectAt()");
   1.201 +  }
   1.202 +
   1.203 +  d.RemoveObjectAt(1);
   1.204 +  // d == [2,4,5]
   1.205 +  static const int t1[] = {2,4,5};
   1.206 +  TEST(VerifyContents(d, t1, 3), "verify contents t1");
   1.207 +
   1.208 +  d.PushFront(&ints[1]);
   1.209 +  d.PushFront(&ints[0]);
   1.210 +  d.PushFront(&ints[7]);
   1.211 +  d.PushFront(&ints[6]);
   1.212 +  //  d == [6,7,0,1,2,4,5] // (0==mOrigin)
   1.213 +  static const int t2[] = {6,7,0,1,2,4,5};
   1.214 +  TEST(VerifyContents(d, t2, 7), "verify contents t2");
   1.215 +
   1.216 +  d.RemoveObjectAt(1);
   1.217 +  //  d == [6,0,1,2,4,5] // (1==mOrigin)
   1.218 +  static const int t3[] = {6,0,1,2,4,5};
   1.219 +  TEST(VerifyContents(d, t3, 6), "verify contents t3");
   1.220 +
   1.221 +  d.RemoveObjectAt(5);
   1.222 +  //  d == [6,0,1,2,4] // (1==mOrigin)
   1.223 +  static const int t4[] = {6,0,1,2,4};
   1.224 +  TEST(VerifyContents(d, t4, 5), "verify contents t4");
   1.225 +
   1.226 +  d.RemoveObjectAt(0);
   1.227 +  //  d == [0,1,2,4] // (2==mOrigin)
   1.228 +  static const int t5[] = {0,1,2,4};
   1.229 +  TEST(VerifyContents(d, t5, 4), "verify contents t5");
   1.230 +
   1.231 +
   1.232 +  return 0;
   1.233 +}
   1.234 +
   1.235 +int main (void) {
   1.236 +  ScopedXPCOM xpcom("TestTimers");
   1.237 +  NS_ENSURE_FALSE(xpcom.failed(), 1);
   1.238 +
   1.239 +  _TestDeque test;
   1.240 +  int result = test.Test();
   1.241 +  TEST(result == 0, "All tests pass");
   1.242 +  return 0;
   1.243 +}

mercurial