Back to index

lightning-sunbird  0.9+nobinonly
TestArray.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; 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  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include <stdio.h>
00040 #include <stdlib.h>
00041 #include "nsISupportsArray.h"
00042 
00043 // {9e70a320-be02-11d1-8031-006008159b5a}
00044 #define NS_IFOO_IID \
00045   {0x9e70a320, 0xbe02, 0x11d1,    \
00046     {0x80, 0x31, 0x00, 0x60, 0x08, 0x15, 0x9b, 0x5a}}
00047 
00048 static const PRBool kExitOnError = PR_TRUE;
00049 
00050 class IFoo : public nsISupports {
00051 public:
00052 
00053   NS_DEFINE_STATIC_IID_ACCESSOR(NS_IFOO_IID)
00054 
00055   NS_IMETHOD_(nsrefcnt) RefCnt() = 0;
00056   NS_IMETHOD_(PRInt32) ID() = 0;
00057 };
00058 
00059 class Foo : public IFoo {
00060 public:
00061 
00062   Foo(PRInt32 aID);
00063 
00064   // nsISupports implementation
00065   NS_DECL_ISUPPORTS
00066 
00067   // IFoo implementation
00068   NS_IMETHOD_(nsrefcnt) RefCnt() { return mRefCnt; }
00069   NS_IMETHOD_(PRInt32) ID() { return mID; }
00070 
00071   static PRInt32 gCount;
00072 
00073   PRInt32 mID;
00074 
00075 private:
00076   ~Foo();
00077 };
00078 
00079 PRInt32 Foo::gCount;
00080 
00081 Foo::Foo(PRInt32 aID)
00082 {
00083   mID = aID;
00084   ++gCount;
00085   fprintf(stdout, "init: %d (%p), %d total)\n", mID, this, gCount);
00086 }
00087 
00088 Foo::~Foo()
00089 {
00090   --gCount;
00091   fprintf(stdout, "destruct: %d (%p), %d remain)\n", mID, this, gCount);
00092 }
00093 
00094 NS_IMPL_ISUPPORTS1(Foo, IFoo)
00095 
00096 const char* AssertEqual(PRInt32 aValue1, PRInt32 aValue2)
00097 {
00098   if (aValue1 == aValue2) {
00099     return "OK";
00100   }
00101   if (kExitOnError) {
00102     exit(1);
00103   }
00104   return "ERROR";
00105 }
00106 
00107 void DumpArray(nsISupportsArray* aArray, PRInt32 aExpectedCount, PRInt32 aElementIDs[], PRInt32 aExpectedTotal)
00108 {
00109   PRUint32 cnt = 0;
00110   nsresult rv = aArray->Count(&cnt);
00111   NS_ASSERTION(NS_SUCCEEDED(rv), "Count failed");
00112   PRInt32 count = cnt;
00113   PRInt32 index;
00114 
00115   fprintf(stdout, "object count %d = %d %s\n", Foo::gCount, aExpectedTotal, 
00116           AssertEqual(Foo::gCount, aExpectedTotal));
00117   fprintf(stdout, "array count %d = %d %s\n", count, aExpectedCount,
00118           AssertEqual(count, aExpectedCount));
00119   
00120   for (index = 0; (index < count) && (index < aExpectedCount); index++) {
00121     IFoo* foo = (IFoo*)(aArray->ElementAt(index));
00122     fprintf(stdout, "%2d: %d=%d (%p) c: %d %s\n", 
00123             index, aElementIDs[index], foo->ID(), foo, foo->RefCnt() - 1,
00124             AssertEqual(foo->ID(), aElementIDs[index]));
00125     foo->Release();
00126   }
00127 }
00128 
00129 void FillArray(nsISupportsArray* aArray, PRInt32 aCount)
00130 {
00131   PRInt32 index;
00132   for (index = 0; index < aCount; index++) {
00133     nsCOMPtr<IFoo> foo = new Foo(index);
00134     aArray->AppendElement(foo);
00135   }
00136 }
00137 
00138 int main(int argc, char *argv[])
00139 {
00140   nsISupportsArray* array;
00141   nsresult  rv;
00142   
00143   if (NS_OK == (rv = NS_NewISupportsArray(&array))) {
00144     FillArray(array, 10);
00145     fprintf(stdout, "Array created:\n");
00146     PRInt32   fillResult[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
00147     DumpArray(array, 10, fillResult, 10);
00148 
00149     // test insert
00150     IFoo* foo = (IFoo*)array->ElementAt(3);
00151     foo->Release();  // pre-release to fix ref count for dumps
00152     array->InsertElementAt(foo, 5);
00153     fprintf(stdout, "insert 3 at 5:\n");
00154     PRInt32   insertResult[11] = {0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9};
00155     DumpArray(array, 11, insertResult, 10);
00156     fprintf(stdout, "insert 3 at 0:\n");
00157     array->InsertElementAt(foo, 0);
00158     PRInt32   insertResult2[12] = {3, 0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9};
00159     DumpArray(array, 12, insertResult2, 10);
00160     fprintf(stdout, "append 3:\n");
00161     array->AppendElement(foo);
00162     PRInt32   appendResult[13] = {3, 0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9, 3};
00163     DumpArray(array, 13, appendResult, 10);
00164 
00165 
00166     // test IndexOf && LastIndexOf
00167     PRInt32 expectedIndex[5] = {0, 4, 6, 12, -1};
00168     PRInt32 count = 0;
00169     PRInt32 index = array->IndexOf(foo);
00170     fprintf(stdout, "IndexOf(foo): %d=%d %s\n", index, expectedIndex[count], 
00171             AssertEqual(index, expectedIndex[count]));
00172     while (-1 != index) {
00173       count++;
00174       index = array->IndexOfStartingAt(foo, index + 1);
00175       if (-1 != index)
00176         fprintf(stdout, "IndexOf(foo): %d=%d %s\n", index, expectedIndex[count], 
00177                 AssertEqual(index, expectedIndex[count]));
00178     }
00179     index = array->LastIndexOf(foo);
00180     count--;
00181     fprintf(stdout, "LastIndexOf(foo): %d=%d %s\n", index, expectedIndex[count], 
00182             AssertEqual(index, expectedIndex[count]));
00183 
00184     // test ReplaceElementAt
00185     fprintf(stdout, "ReplaceElementAt(8):\n");
00186     array->ReplaceElementAt(foo, 8);
00187     PRInt32   replaceResult[13] = {3, 0, 1, 2, 3, 4, 3, 5, 3, 7, 8, 9, 3};
00188     DumpArray(array, 13, replaceResult, 9);
00189 
00190     // test RemoveElementAt, RemoveElement RemoveLastElement
00191     fprintf(stdout, "RemoveElementAt(0):\n");
00192     array->RemoveElementAt(0);
00193     PRInt32   removeResult[12] = {0, 1, 2, 3, 4, 3, 5, 3, 7, 8, 9, 3};
00194     DumpArray(array, 12, removeResult, 9);
00195     fprintf(stdout, "RemoveElementAt(7):\n");
00196     array->RemoveElementAt(7);
00197     PRInt32   removeResult2[11] = {0, 1, 2, 3, 4, 3, 5, 7, 8, 9, 3};
00198     DumpArray(array, 11, removeResult2, 9);
00199     fprintf(stdout, "RemoveElement(foo):\n");
00200     array->RemoveElement(foo);
00201     PRInt32   removeResult3[10] = {0, 1, 2, 4, 3, 5, 7, 8, 9, 3};
00202     DumpArray(array, 10, removeResult3, 9);
00203     fprintf(stdout, "RemoveLastElement(foo):\n");
00204     array->RemoveLastElement(foo);
00205     PRInt32   removeResult4[9] = {0, 1, 2, 4, 3, 5, 7, 8, 9};
00206     DumpArray(array, 9, removeResult4, 9);
00207 
00208     // test clear
00209     fprintf(stdout, "clear array:\n");
00210     array->Clear();
00211     DumpArray(array, 0, 0, 0);
00212     fprintf(stdout, "add 4 new:\n");
00213     FillArray(array, 4);
00214     DumpArray(array, 4, fillResult, 4);
00215 
00216     // test compact
00217     fprintf(stdout, "compact array:\n");
00218     array->Compact();
00219     DumpArray(array, 4, fillResult, 4);
00220 
00221     // test delete
00222     fprintf(stdout, "release array:\n");
00223     NS_RELEASE(array);
00224   }
00225   else {
00226     fprintf(stdout, "error can't create array: %x\n", rv);
00227   }
00228 
00229   return 0;
00230 }