Back to index

lightning-sunbird  0.9+nobinonly
TestArray.java
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Java XPCOM Bindings.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * IBM Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2005
00019  * IBM Corporation. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Javier Pedemonte (jhpedemonte@gmail.com)
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * 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 import org.mozilla.xpcom.*;
00039 import java.io.*;
00040 
00063 public class TestArray {
00064   public static final String NS_ARRAY_CONTRACTID = "@mozilla.org/array;1";
00065 
00066   public static void main(String [] args) {
00067     System.loadLibrary("javaxpcom");
00068 
00069     String mozillaPath = System.getProperty("MOZILLA_FIVE_HOME");
00070     if (mozillaPath == null) {
00071       throw new RuntimeException("MOZILLA_FIVE_HOME system property not set.");
00072     }
00073 
00074     File localFile = new File(mozillaPath);
00075     XPCOM.initXPCOM(localFile, null);
00076     // XPCOM.initXPCOM() only initializes XPCOM.  If you want to initialize
00077     // Gecko, you would do the following instead:
00078     //    GeckoEmbed.initEmbedding(localFile, null);
00079 
00080     nsIComponentManager componentManager = XPCOM.getComponentManager();
00081     nsIMutableArray array = (nsIMutableArray)
00082       componentManager.createInstanceByContractID(NS_ARRAY_CONTRACTID, null,
00083                                                   nsIMutableArray.NS_IMUTABLEARRAY_IID);
00084     if (array == null) {
00085       throw new RuntimeException("Failed to create nsIMutableArray.");
00086     }
00087 
00088     fillArray(array, 10);
00089     System.out.println("Array created:");
00090     int fillResult[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
00091     dumpArray(array, 10, fillResult, 10);
00092 
00093     // test insert
00094     IFoo foo = (IFoo) array.queryElementAt(3, IFoo.IFOO_IID);
00095     array.insertElementAt(foo, 5, false);
00096     System.out.println("insert 3 at 5:");
00097     int insertResult[] = {0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9};
00098     dumpArray(array, 11, insertResult, 10);
00099     System.out.println("insert 3 at 0:");
00100     array.insertElementAt(foo, 0, false);
00101     int insertResult2[] = {3, 0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9};
00102     dumpArray(array, 12, insertResult2, 10);
00103     System.out.println("append 3:");
00104     array.appendElement(foo, false);
00105     int appendResult[] = {3, 0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9, 3};
00106     dumpArray(array, 13, appendResult, 10);
00107 
00108     // test IndexOf && LastIndexOf
00109     int expectedIndex[] = {0, 4, 6, 12, -1};
00110     int count = 0;
00111     long index = array.indexOf(0, foo);
00112     System.out.println("IndexOf(foo): " + index + "=" + expectedIndex[count] +
00113                        " " + assertEqual(index, expectedIndex[count]));
00114     try {
00115       do {
00116         count++;
00117         index = array.indexOf(index + 1, foo);
00118         System.out.println("IndexOf(foo): " + index + "=" +
00119                            expectedIndex[count] + " " +
00120                            assertEqual(index, expectedIndex[count]));
00121       } while (true);
00122     } catch (XPCOMException e) {
00123       // If array.indexOf() did not find the element, it returns
00124       // NS_ERROR_FAILURE.
00125       if (e.errorcode != XPCOM.NS_ERROR_FAILURE) {
00126         throw e;
00127       }
00128     }
00129 
00130     index = lastIndexOf(array, foo);
00131     count--;
00132     System.out.println("LastIndexOf(foo): " + index + "=" +
00133                        expectedIndex[count] + " " + 
00134                        assertEqual(index, expectedIndex[count]));
00135 
00136     System.out.println("ReplaceElementAt(8):");
00137     replaceElementAt(array, foo, 8);
00138     System.gc();
00139     int replaceResult[] = {3, 0, 1, 2, 3, 4, 3, 5, 3, 7, 8, 9, 3};
00140     dumpArray(array, 13, replaceResult, 9);
00141 
00142     System.out.println("RemoveElementAt(0):");
00143     array.removeElementAt(0);
00144     System.gc();
00145     int removeResult[] = {0, 1, 2, 3, 4, 3, 5, 3, 7, 8, 9, 3};
00146     dumpArray(array, 12, removeResult, 9);
00147     System.out.println("RemoveElementAt(7):");
00148     array.removeElementAt(7);
00149     System.gc();
00150     int removeResult2[] = {0, 1, 2, 3, 4, 3, 5, 7, 8, 9, 3};
00151     dumpArray(array, 11, removeResult2, 9);
00152     System.out.println("RemoveElement(foo):");
00153     removeElement(array, foo);
00154     System.gc();
00155     int removeResult3[] = {0, 1, 2, 4, 3, 5, 7, 8, 9, 3};
00156     dumpArray(array, 10, removeResult3, 9);
00157     System.out.println("RemoveLastElement(foo):");
00158     removeLastElement(array, foo);
00159     System.gc();
00160     int removeResult4[] = {0, 1, 2, 4, 3, 5, 7, 8, 9};
00161     dumpArray(array, 9, removeResult4, 9);
00162 
00163     // test clear
00164     foo = null;  // remove ref now, so everything is cleared
00165     System.out.println("clear array:");
00166     array.clear();
00167     System.gc();
00168     dumpArray(array, 0, null, 0);
00169     System.out.println("add 4 new:");
00170     fillArray(array, 4);
00171     dumpArray(array, 4, fillResult, 4);
00172 
00173     // test deleting of array
00174     System.out.println("release array:");
00175     array = null;
00176     System.gc();
00177     dumpArray(array, 0, null, 0);
00178 
00179     localFile = null;
00180     componentManager = null;
00181     System.gc();
00182     XPCOM.shutdownXPCOM(null);
00183     //    GeckoEmbed.termEmbedding();
00184 
00185     System.out.println("Test Passed.");
00186   }
00187 
00188   static void fillArray(nsIMutableArray aArray, int aCount)
00189   {
00190     for (int index = 0; index < aCount; index++) {
00191       IFoo foo = new Foo(index);
00192       aArray.appendElement(foo, false);
00193     }
00194   }
00195 
00196   static String assertEqual(long aValue1, long aValue2)
00197   {
00198     if (aValue1 == aValue2)
00199       return "OK";
00200     return "ERROR";
00201   }
00202   
00203   static void dumpArray(nsIMutableArray aArray, int aExpectedCount,
00204                         int[] aElementIDs, int aExpectedTotal)
00205   {
00206     long count = 0;
00207     if (aArray != null)
00208       count = aArray.getLength();
00209 
00210     System.out.println("object count " + Foo.gCount + " = " + aExpectedTotal +
00211                        " " + assertEqual(Foo.gCount, aExpectedTotal));
00212     System.out.println("array count " + count + " = " + aExpectedCount + " " +
00213                        assertEqual(count, aExpectedCount));
00214 
00215     for (int index = 0; (index < count) && (index < aExpectedCount); index++) {
00216       IFoo foo = (IFoo) aArray.queryElementAt(index, IFoo.IFOO_IID);
00217       System.out.println(index + ": " + aElementIDs[index] + "=" +
00218                          foo.getId() + " (" +
00219                          Integer.toHexString(foo.hashCode()) + ") " + 
00220                          assertEqual(foo.getId(), aElementIDs[index]));
00221       foo = null;
00222     }
00223   }
00224 
00225   static long lastIndexOf(nsIMutableArray aArray, nsISupports aElement)
00226   {
00227     for (long i = aArray.getLength() - 1; i >= 0; i--) {
00228       IFoo foo = (IFoo) aArray.queryElementAt(i, IFoo.IFOO_IID);
00229       if (foo == aElement)
00230         return i;
00231     }
00232     return -1;
00233   }
00234 
00235   static void replaceElementAt(nsIMutableArray aArray, nsISupports aElement,
00236                                int aIndex)
00237   {
00238     aArray.removeElementAt(aIndex);
00239     aArray.insertElementAt(aElement, aIndex, false);
00240   }
00241 
00242   /* Removes first instance of given element */
00243   static void removeElement(nsIMutableArray aArray, nsISupports aElement)
00244   {
00245     long index = aArray.indexOf(0, aElement);
00246     aArray.removeElementAt(index);
00247   }
00248 
00249   static void removeLastElement(nsIMutableArray aArray, nsISupports aElement)
00250   {
00251     long index = lastIndexOf(aArray, aElement);
00252     aArray.removeElementAt(index);
00253   }
00254 }
00255