* test/gc: Added directory to contain Garbage Collector testcases.
authormichi <none@none>
Wed, 28 Mar 2007 23:39:26 +0000 (23:39 +0000)
committermichi <none@none>
Wed, 28 Mar 2007 23:39:26 +0000 (23:39 +0000)
* test/gc/Chain.java: Added.
* tests/gc/Simple.java: Added.
* tests/gc/Hashcode.java: Added.

--HG--
branch : exact-gc

tests/gc/Chain.java [new file with mode: 0644]
tests/gc/Hashcode.java [new file with mode: 0644]
tests/gc/Simple.java [new file with mode: 0644]

diff --git a/tests/gc/Chain.java b/tests/gc/Chain.java
new file mode 100644 (file)
index 0000000..5d03483
--- /dev/null
@@ -0,0 +1,19 @@
+public class Chain {
+       public static final int N=500000;
+
+       public Chain next;
+
+       public static void main(String args[]) {
+               Chain top = null;
+
+               System.out.println("Building chain ...");
+               for (int i=0; i<N; i++) {
+                       Chain c = new Chain();
+                       c.next = top;
+                       top = c;
+               }
+
+               System.out.println("Forcing collection ...");
+               System.gc();
+       }
+}
diff --git a/tests/gc/Hashcode.java b/tests/gc/Hashcode.java
new file mode 100644 (file)
index 0000000..09bb473
--- /dev/null
@@ -0,0 +1,66 @@
+public class Hashcode {
+       public static final boolean doPrintHashes = false;
+       public static final int N = 20;
+       public static final int N2 = N/2;
+
+       public static void compareHashes(int[] a, int[] b) {
+               boolean diff = false;
+
+               for (int i=0; i<a.length; i++)
+                       if (a[i] != b[i]) {
+                               System.out.println("\tHash #" + i + " changed!");
+                               diff = true;
+                       }
+
+               if (diff)
+                       System.out.println("\tHashes are DIFFERENT!");
+               else
+                       System.out.println("\tHashes are ok.");
+       }
+
+       public static void printHashes(int[] a) {
+               for (int i=0; i<a.length; i++)
+                       System.out.println("\t#" + i + ": " + a[i]);
+       }
+
+       public static int[] getHashes(Object[] a) {
+               int[] result = new int[a.length];
+
+               for (int i=0; i<a.length; i++)
+                       result[i] = System.identityHashCode(a[i]);
+
+               return result;
+       }
+
+       public static void main(String[] s) {
+               Object[] a = new Object[N];
+               int[] hashes1, hashes2;
+
+               System.out.println("Creating objects ...");
+               for (int i=0; i<N2; i++)
+                       a[i] = new Object();
+               for (int i=N2+1; i<N; i++)
+                       a[i] = new String("String-" + i);
+
+               System.out.println("Forgetting some objects ...");
+               for (int i=0; i<N; i+=2)
+                       a[i] = null;
+
+               System.out.println("Getting hashes before GC ...");
+               hashes1 = getHashes(a);
+               if (doPrintHashes)
+                       printHashes(hashes1);
+
+               System.out.println("Invoking the GC ...");
+               System.gc();
+               System.out.println("\tfinished.");
+
+               System.out.println("Getting hashes after GC ...");
+               hashes2 = getHashes(a);
+               if (doPrintHashes)
+                       printHashes(hashes2);
+
+               System.out.println("Comparing hashes now ...");
+               compareHashes(hashes1, hashes2);
+       }
+}
diff --git a/tests/gc/Simple.java b/tests/gc/Simple.java
new file mode 100644 (file)
index 0000000..42921a2
--- /dev/null
@@ -0,0 +1,5 @@
+public class Simple {
+       public static void main(String[] s) {
+               System.gc();
+       }
+}