KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > test > unit > TestIntArray


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.test.unit;
6
7 import java.util.Arrays JavaDoc;
8 import java.util.Random JavaDoc;
9
10 import org.h2.test.TestBase;
11 import org.h2.util.IntArray;
12
13 public class TestIntArray extends TestBase {
14
15     public void test() throws Exception JavaDoc {
16         IntArray array = new IntArray();
17         int[] test = new int[0];
18         Random JavaDoc random = new Random JavaDoc(1);
19         for(int i=0; i<10000; i++) {
20             int idx = test.length == 0 ? 0 : random.nextInt(test.length);
21             int v = random.nextInt(100);
22             int op = random.nextInt(9);
23             switch(op) {
24             case 0:
25                 array.add(idx, v);
26                 test = add(test, idx, v);
27                 break;
28             case 1:
29                 array.add(v);
30                 test = add(test, v);
31                 break;
32             case 2:
33                 array.sort();
34                 test = sort(test);
35                 array.addValueSorted(v);
36                 test = addValueSorted(test, v);
37                 break;
38             case 3:
39                 array.sort();
40                 test = sort(test);
41                 int a = array.findNextValueIndex(v);
42                 int b = findNextValueIndex(test, v);
43                 check(a, b);
44                 break;
45             case 4:
46                 if(test.length > idx) {
47                     check(array.get(idx), get(test, idx));
48                 }
49                 break;
50             case 5:
51                 array.remove(idx);
52                 test = remove(test, idx);
53                 break;
54             case 6:
55                 if(test.length > idx) {
56                     v = test[idx];
57                     array.removeValue(v);
58                     test = removeValue(test, v);
59                 }
60                 break;
61             case 7:
62                 array.set(idx, v);
63                 test = set(test, idx, v);
64                 break;
65             case 8:
66                 check(array.size(), test.length);
67                 break;
68             }
69             check(array.size(), test.length);
70             for(int j=0; j<test.length; j++) {
71                 check(test[j], array.get(j));
72             }
73                 
74         }
75     }
76     
77     int[] add(int[] array, int i, int value) {
78         int[] a2 = new int[array.length+1];
79         System.arraycopy(array, 0, a2, 0, array.length);
80         if(i < array.length) {
81             System.arraycopy(a2, i, a2, i+1, a2.length - i - 1);
82         }
83         array = a2;
84         array[i] = value;
85         return array;
86     }
87     
88     int[] add(int[] array, int value) {
89         return add(array, array.length, value);
90     }
91     
92     int[] addValueSorted(int[] array, int value) {
93         for(int i=0; i<array.length; i++) {
94             if(array[i] < value) {
95                 continue;
96             }
97             if(array[i] == value) {
98                 return array;
99             } else {
100                 return add(array, i, value);
101             }
102         }
103         return add(array, value);
104     }
105     
106     int findNextValueIndex(int[] array, int value) {
107         for(int i=0; i<array.length; i++) {
108             if(array[i] >= value) {
109                 return i;
110             }
111         }
112         return array.length;
113     }
114     
115     int get(int[] array, int i) {
116         return array[i];
117     }
118     
119     int[] remove(int[] array, int i) {
120         int[] a2 = new int[array.length-1];
121         System.arraycopy(array, 0, a2, 0, i);
122         if(i < a2.length) {
123             System.arraycopy(array, i+1, a2, i, array.length - i - 1);
124         }
125         return a2;
126     }
127     
128     int[] removeValue(int[] array, int value) {
129         for(int i=0; i<array.length; i++) {
130             if(array[i] == value) {
131                 return remove(array, i);
132             }
133         }
134         return array;
135     }
136
137     int[] set(int[] array, int i, int value) {
138         array[i] = value;
139         return array;
140     }
141     
142     int size(int[] array) {
143         return array.length;
144     }
145     
146     int[] sort(int[] array) {
147         Arrays.sort(array);
148         return array;
149     }
150     
151 }
152
Popular Tags