Back to index

lightning-sunbird  0.9+nobinonly
TestDeque.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsDeque.h"
00039 #include "nsCRT.h"
00040 #include <stdio.h>
00041 
00042 /**************************************************************
00043   Now define the token deallocator class...
00044  **************************************************************/
00045 class _TestDeque {
00046 public:
00047   _TestDeque() {
00048     SelfTest();
00049   }
00050   int SelfTest();
00051   nsresult OriginalTest();
00052   nsresult OriginalFlaw();
00053   nsresult AssignFlaw();
00054 };
00055 static _TestDeque sTestDeque;
00056 
00057 class _Dealloc: public nsDequeFunctor {
00058   virtual void* operator()(void* aObject) {
00059     return 0;
00060   }
00061 };
00062 
00069 int _TestDeque::SelfTest() {
00070   /* the old deque should have failed a bunch of these tests */
00071   int results=0;
00072   results+=OriginalTest();
00073   results+=OriginalFlaw();
00074   results+=AssignFlaw();
00075   return results;
00076 }
00077 
00078 nsresult _TestDeque::OriginalTest() {
00079   int ints[200];
00080   int count=sizeof(ints)/sizeof(int);
00081   int i=0;
00082   int* temp;
00083   nsDeque theDeque(new _Dealloc); //construct a simple one...
00084  
00085   for (i=0;i<count;i++) { //initialize'em
00086     ints[i]=10*(1+i);
00087   }
00088   for (i=0;i<70;i++) {
00089     theDeque.Push(&ints[i]);
00090   }
00091   for (i=0;i<56;i++) {
00092     temp=(int*)theDeque.Pop();
00093   }
00094   for (i=0;i<55;i++) {
00095     theDeque.Push(&ints[i]);
00096   }
00097   for (i=0;i<35;i++) {
00098     temp=(int*)theDeque.Pop();
00099   }
00100   for (i=0;i<35;i++) {
00101     theDeque.Push(&ints[i]);
00102   }
00103   for (i=0;i<38;i++) {
00104     temp=(int*)theDeque.Pop();
00105   }
00106   return NS_OK;
00107 }
00108 
00109 nsresult _TestDeque::OriginalFlaw() {
00110   int ints[200];
00111   int i=0;
00112   int* temp;
00113   nsDeque secondDeque(new _Dealloc);
00118   printf("fill array\n");
00119   for (i=32; i; --i)
00120     ints[i]=i*3+10;
00121   printf("push 6 times\n");
00122   for (i=0; i<6; i++)
00123     secondDeque.Push(&ints[i]);
00124   printf("popfront 4 times:\n");
00125   for (i=4; i; --i) {
00126     temp=(int*)secondDeque.PopFront();
00127     printf("%d\t",*temp);
00128   }
00129   printf("push 4 times\n");
00130   for (int j=4; j; --j)
00131     secondDeque.Push(&ints[++i]);
00132   printf("origin should now be about 4\n");
00133   printf("and size should be 6\n");
00134   printf("origin+size>capacity\n");
00135 
00136    /*<akk> Oh, I see ... it's a circular buffer */
00137   printf("but the old code wasn't behaving accordingly.\n");
00138 
00139    /*right*/
00140   printf("we shouldn't crash or anything interesting, ");
00141 
00142   temp=(int*)secondDeque.Peek();
00143   printf("peek: %d\n",*temp);
00144   return NS_OK;
00145 }
00146 
00147 nsresult _TestDeque::AssignFlaw() {
00148   nsDeque src(new _Dealloc),dest(new _Dealloc);
00149   return NS_OK;
00150 }
00151 
00152 int main (void) {
00153   _TestDeque test;
00154   return 0;
00155 }