1 |
14 |
jlechner |
//////
|
2 |
|
|
// There should be a copyright thing here but I'm in too much of a hurry to add
|
3 |
|
|
// one right now. I don't care much what the copyright is though so if someone
|
4 |
|
|
// wants to put the appropriate one here, go right ahead (I think GPL probably
|
5 |
|
|
// unless that causes a problem with running on proprietory JVMs or testing
|
6 |
|
|
// proprietory class libraries or anything.
|
7 |
|
|
//////
|
8 |
|
|
|
9 |
|
|
import java.util.*;
|
10 |
|
|
|
11 |
|
|
/**
|
12 |
|
|
* Test of most of the methods in the Arrays class. The class prints out a
|
13 |
|
|
* single pass/fail message, and enumerates all failures. The message is in the
|
14 |
|
|
* PASS: or FAIL: format that dejagnu uses, but other than that I don't know
|
15 |
|
|
* enough to make it a "proper" testsuite.
|
16 |
|
|
*/
|
17 |
|
|
public class ArraysTest {
|
18 |
|
|
|
19 |
|
|
static int passed = 0;
|
20 |
|
|
static int failed = 0;
|
21 |
|
|
|
22 |
|
|
public static void main(String[] args) {
|
23 |
|
|
testBool();
|
24 |
|
|
testByte();
|
25 |
|
|
testChar();
|
26 |
|
|
testShort();
|
27 |
|
|
testInt();
|
28 |
|
|
testLong();
|
29 |
|
|
testFloat();
|
30 |
|
|
testDouble();
|
31 |
|
|
testObject();
|
32 |
|
|
if (failed != 0) {
|
33 |
|
|
System.out.println(" (" + failed + " fails and " + passed + " passes).");
|
34 |
|
|
} else {
|
35 |
|
|
System.out.println("PASSED: [Arrays] All " + passed + " tests.");
|
36 |
|
|
}
|
37 |
|
|
}
|
38 |
|
|
|
39 |
|
|
static void testBool() {
|
40 |
|
|
boolean[] a1 = new boolean[] {true, false, false, true, true, false, true};
|
41 |
|
|
boolean[] a2 = new boolean[] {false, false, false, true, true, true, true};
|
42 |
|
|
boolean[] a3 = new boolean[] {true, false, false, true, true, false, true};
|
43 |
|
|
passfail("boolean equals", !Arrays.equals(a1, a2) && Arrays.equals(a1, a3));
|
44 |
|
|
Arrays.fill(a1, false);
|
45 |
|
|
boolean filled = true;
|
46 |
|
|
for (int i = 0; filled && i < a1.length; i++) {
|
47 |
|
|
filled = a1[i] == false;
|
48 |
|
|
}
|
49 |
|
|
passfail("boolean fill", filled);
|
50 |
|
|
}
|
51 |
|
|
|
52 |
|
|
static void testByte() {
|
53 |
|
|
byte[] a1 = new byte[] {3, -2, 0, 1, 4, 0, -5};
|
54 |
|
|
byte[] a2 = new byte[] {-5, -2, 0, 0, 1, 3, 4};
|
55 |
|
|
boolean firstEq = Arrays.equals(a1, a2);
|
56 |
|
|
Arrays.sort(a1);
|
57 |
|
|
boolean sorted = true;
|
58 |
|
|
for (int i = 0; sorted && i < a1.length - 1; i++) {
|
59 |
|
|
sorted = !(a1[i] > a1[i+1]); // the odd way of writing <= is so that we
|
60 |
|
|
// aren't tooo mean to NaNs
|
61 |
|
|
}
|
62 |
|
|
passfail("byte sort", sorted);
|
63 |
|
|
passfail("byte search", Arrays.binarySearch(a2, (byte)1) == 4 &&
|
64 |
|
|
Arrays.binarySearch(a2, (byte)-1) == -3);
|
65 |
|
|
passfail("byte equals", !firstEq && Arrays.equals(a1, a2));
|
66 |
|
|
Arrays.fill(a1, (byte)6);
|
67 |
|
|
boolean filled = true;
|
68 |
|
|
for (int i = 0; filled && i < a1.length; i++) {
|
69 |
|
|
filled = a1[i] == (byte)6;
|
70 |
|
|
}
|
71 |
|
|
passfail("byte fill", filled);
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
static void testChar() {
|
75 |
|
|
char[] a1 = new char[] {'i', 'd', 'f', 'g', 'j', 'f', 'a'};
|
76 |
|
|
char[] a2 = new char[] {'a', 'd', 'f', 'f', 'g', 'i', 'j'};
|
77 |
|
|
boolean firstEq = Arrays.equals(a1, a2);
|
78 |
|
|
Arrays.sort(a1);
|
79 |
|
|
boolean sorted = true;
|
80 |
|
|
for (int i = 0; sorted && i < a1.length - 1; i++) {
|
81 |
|
|
sorted = !(a1[i] > a1[i+1]); // the odd way of writing <= is so that we
|
82 |
|
|
// aren't tooo mean to NaNs
|
83 |
|
|
}
|
84 |
|
|
passfail("char sort", sorted);
|
85 |
|
|
passfail("char search", Arrays.binarySearch(a2, 'i') == 5 &&
|
86 |
|
|
Arrays.binarySearch(a2, 'e') == -3);
|
87 |
|
|
passfail("char equals", !firstEq && Arrays.equals(a1, a2));
|
88 |
|
|
Arrays.fill(a1, 'Q');
|
89 |
|
|
boolean filled = true;
|
90 |
|
|
for (int i = 0; filled && i < a1.length; i++) {
|
91 |
|
|
filled = a1[i] == 'Q';
|
92 |
|
|
}
|
93 |
|
|
passfail("char fill", filled);
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
static void testShort() {
|
97 |
|
|
short[] a1 = new short[] {3, -2, 0, 1, 4, 0, -5};
|
98 |
|
|
short[] a2 = new short[] {-5, -2, 0, 0, 1, 3, 4};
|
99 |
|
|
boolean firstEq = Arrays.equals(a1, a2);
|
100 |
|
|
Arrays.sort(a1);
|
101 |
|
|
boolean sorted = true;
|
102 |
|
|
for (int i = 0; sorted && i < a1.length - 1; i++) {
|
103 |
|
|
sorted = !(a1[i] > a1[i+1]); // the odd way of writing <= is so that we
|
104 |
|
|
// aren't tooo mean to NaNs
|
105 |
|
|
}
|
106 |
|
|
passfail("short sort", sorted);
|
107 |
|
|
passfail("short search", Arrays.binarySearch(a2, (short)1) == 4 &&
|
108 |
|
|
Arrays.binarySearch(a2, (short)-1) == -3);
|
109 |
|
|
passfail("short equals", !firstEq && Arrays.equals(a1, a2));
|
110 |
|
|
Arrays.fill(a1, (short)6);
|
111 |
|
|
boolean filled = true;
|
112 |
|
|
for (int i = 0; filled && i < a1.length; i++) {
|
113 |
|
|
filled = a1[i] == (short)6;
|
114 |
|
|
}
|
115 |
|
|
passfail("short fill", filled);
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
static void testInt() {
|
119 |
|
|
int[] a1 = new int[] {3, -2, 0, 1, 4, 0, -5};
|
120 |
|
|
int[] a2 = new int[] {-5, -2, 0, 0, 1, 3, 4};
|
121 |
|
|
boolean firstEq = Arrays.equals(a1, a2);
|
122 |
|
|
Arrays.sort(a1);
|
123 |
|
|
boolean sorted = true;
|
124 |
|
|
for (int i = 0; sorted && i < a1.length - 1; i++) {
|
125 |
|
|
sorted = !(a1[i] > a1[i+1]); // the odd way of writing <= is so that we
|
126 |
|
|
// aren't tooo mean to NaNs
|
127 |
|
|
}
|
128 |
|
|
passfail("int sort", sorted);
|
129 |
|
|
passfail("int search", Arrays.binarySearch(a2, 1) == 4 &&
|
130 |
|
|
Arrays.binarySearch(a2, -1) == -3);
|
131 |
|
|
passfail("int equals", !firstEq && Arrays.equals(a1, a2));
|
132 |
|
|
Arrays.fill(a1, 6);
|
133 |
|
|
boolean filled = true;
|
134 |
|
|
for (int i = 0; filled && i < a1.length; i++) {
|
135 |
|
|
filled = a1[i] == 6;
|
136 |
|
|
}
|
137 |
|
|
passfail("int fill", filled);
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
static void testLong() {
|
141 |
|
|
long[] a1 = new long[] {3, -2, 0, 1, 4, 0, -5};
|
142 |
|
|
long[] a2 = new long[] {-5, -2, 0, 0, 1, 3, 4};
|
143 |
|
|
boolean firstEq = Arrays.equals(a1, a2);
|
144 |
|
|
Arrays.sort(a1);
|
145 |
|
|
boolean sorted = true;
|
146 |
|
|
for (int i = 0; sorted && i < a1.length - 1; i++) {
|
147 |
|
|
sorted = !(a1[i] > a1[i+1]); // the odd way of writing <= is so that we
|
148 |
|
|
// aren't tooo mean to NaNs
|
149 |
|
|
}
|
150 |
|
|
passfail("long sort", sorted);
|
151 |
|
|
passfail("long search", Arrays.binarySearch(a2, 1L) == 4 &&
|
152 |
|
|
Arrays.binarySearch(a2, -1L) == -3);
|
153 |
|
|
passfail("long equals", !firstEq && Arrays.equals(a1, a2));
|
154 |
|
|
Arrays.fill(a1, 6L);
|
155 |
|
|
boolean filled = true;
|
156 |
|
|
for (int i = 0; filled && i < a1.length; i++) {
|
157 |
|
|
filled = a1[i] == 6L;
|
158 |
|
|
}
|
159 |
|
|
passfail("long fill", filled);
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
static void testFloat() {
|
163 |
|
|
float[] a1 = new float[] {-4.0f, 75.3f, Float.POSITIVE_INFINITY, -0.0f,
|
164 |
|
|
-3324.342f, 0.0f, 3.14f, 2.5f, 1.0f, 1.0f};
|
165 |
|
|
float[] a2 = new float[] {-3324.342f, -4.0f, -0.0f, 0.0f, 1.0f, 1.0f, 2.5f,
|
166 |
|
|
3.14f, 75.3f, Float.POSITIVE_INFINITY};
|
167 |
|
|
boolean firstEq = Arrays.equals(a1, a2);
|
168 |
|
|
Arrays.sort(a1);
|
169 |
|
|
boolean sorted = true;
|
170 |
|
|
for (int i = 0; sorted && i < a1.length - 1; i++) {
|
171 |
|
|
sorted = !(a1[i] > a1[i+1]); // the odd way of writing <= is so that we
|
172 |
|
|
// aren't tooo mean to NaNs
|
173 |
|
|
}
|
174 |
|
|
passfail("float sort", sorted);
|
175 |
|
|
passfail("float search", Arrays.binarySearch(a2, 3.14f) == 7 &&
|
176 |
|
|
Arrays.binarySearch(a2, -1.0f) == -3);
|
177 |
|
|
passfail("float equals", !firstEq && Arrays.equals(a1, a2));
|
178 |
|
|
Arrays.fill(a1, 27.0f);
|
179 |
|
|
boolean filled = true;
|
180 |
|
|
for (int i = 0; filled && i < a1.length; i++) {
|
181 |
|
|
filled = a1[i] == 27.0f;
|
182 |
|
|
}
|
183 |
|
|
passfail("float fill", filled);
|
184 |
|
|
}
|
185 |
|
|
|
186 |
|
|
static void testDouble() {
|
187 |
|
|
double[] a1 = new double[] {-4.0d, 75.3d, Double.POSITIVE_INFINITY, -0.0d,
|
188 |
|
|
-3324.342d, 0.0d, 3.14d, 2.5d, 1.0d, 1.0d};
|
189 |
|
|
double[] a2 = new double[] {-3324.342d, -4.0d, -0.0d, 0.0d, 1.0d, 1.0d,
|
190 |
|
|
2.5d, 3.14d, 75.3d, Double.POSITIVE_INFINITY};
|
191 |
|
|
boolean firstEq = Arrays.equals(a1, a2);
|
192 |
|
|
Arrays.sort(a1);
|
193 |
|
|
boolean sorted = true;
|
194 |
|
|
for (int i = 0; sorted && i < a1.length - 1; i++) {
|
195 |
|
|
sorted = !(a1[i] > a1[i+1]); // the odd way of writing <= is so that we
|
196 |
|
|
// aren't tooo mean to NaNs
|
197 |
|
|
}
|
198 |
|
|
passfail("double sort", sorted);
|
199 |
|
|
passfail("double search", Arrays.binarySearch(a2, 3.14d) == 7 &&
|
200 |
|
|
Arrays.binarySearch(a2, -1.0d) == -3);
|
201 |
|
|
passfail("double equals", !firstEq && Arrays.equals(a1, a2));
|
202 |
|
|
Arrays.fill(a1, 27.0d);
|
203 |
|
|
boolean filled = true;
|
204 |
|
|
for (int i = 0; filled && i < a1.length; i++) {
|
205 |
|
|
filled = a1[i] == 27.0d;
|
206 |
|
|
}
|
207 |
|
|
passfail("double fill", filled);
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
static void testObject() {
|
211 |
|
|
String[] a1 = new String[] {"this", "is", "a", "string", "test", "which",
|
212 |
|
|
"will", "hopefully", "demonstrate", "that",
|
213 |
|
|
"sorting", "works"};
|
214 |
|
|
String[] a2 = new String[] {"a", "demonstrate", "hopefully", "is",
|
215 |
|
|
"sorting", "string", "test", "that", "this",
|
216 |
|
|
"which", "will", "works"};
|
217 |
|
|
String[] a3 = new String[] {"this", "is", "a", "reverse", "string", "test",
|
218 |
|
|
"which", "will", "hopefully", "demonstrate",
|
219 |
|
|
"that", "sorting", "works", "with",
|
220 |
|
|
"comparators"};
|
221 |
|
|
String[] a4 = new String[] {"works", "with", "will", "which", "this",
|
222 |
|
|
"that", "test", "string", "sorting", "reverse",
|
223 |
|
|
"is", "hopefully", "demonstrate", "comparators",
|
224 |
|
|
"a"};
|
225 |
|
|
final String list = "[works, with, will, which, this, that, test, string," +
|
226 |
|
|
" sorting, reverse, is, hopefully, demonstrate," +
|
227 |
|
|
" comparators, a]";
|
228 |
|
|
boolean firstEq = Arrays.equals(a1, a2);
|
229 |
|
|
Arrays.sort(a1);
|
230 |
|
|
boolean sorted = true;
|
231 |
|
|
for (int i = 0; sorted && i < a1.length - 1; i++) {
|
232 |
|
|
sorted = a1[i].compareTo(a1[i+1]) <= 0;
|
233 |
|
|
}
|
234 |
|
|
passfail("object sort", sorted);
|
235 |
|
|
passfail("object search", Arrays.binarySearch(a2, "hopefully") == 2 &&
|
236 |
|
|
Arrays.binarySearch(a2, "strange") == -6);
|
237 |
|
|
passfail("object equals", !firstEq && Arrays.equals(a1, a2));
|
238 |
|
|
Arrays.fill(a1, "blah");
|
239 |
|
|
boolean filled = true;
|
240 |
|
|
for (int i = 0; filled && i < a1.length; i++) {
|
241 |
|
|
filled = a1[i].equals("blah");
|
242 |
|
|
}
|
243 |
|
|
passfail("object fill", filled);
|
244 |
|
|
Comparator c = new ReverseOrder();
|
245 |
|
|
Arrays.sort(a3, c);
|
246 |
|
|
passfail("comparator sort", Arrays.equals(a3, a4));
|
247 |
|
|
passfail("comparator search", Arrays.binarySearch(a4, "sorting", c) == 8 &&
|
248 |
|
|
Arrays.binarySearch(a4, "nice", c) == -11);
|
249 |
|
|
|
250 |
|
|
// toList doesn't exist -gcb
|
251 |
|
|
// passfail("toList toString", Arrays.toList(a4).toString().equals(list));
|
252 |
|
|
}
|
253 |
|
|
|
254 |
|
|
static void passfail(String desc, boolean didpass) {
|
255 |
|
|
if (didpass) {
|
256 |
|
|
passed++;
|
257 |
|
|
} else {
|
258 |
|
|
if (failed++ != 0) {
|
259 |
|
|
System.out.print(", " + desc);
|
260 |
|
|
} else {
|
261 |
|
|
System.out.print("FAILED: [Arrays] " + desc);
|
262 |
|
|
}
|
263 |
|
|
}
|
264 |
|
|
}
|
265 |
|
|
}
|
266 |
|
|
|
267 |
|
|
class ReverseOrder implements Comparator {
|
268 |
|
|
public int compare(Object a, Object b) {
|
269 |
|
|
return -((Comparable)a).compareTo(b);
|
270 |
|
|
}
|
271 |
|
|
}
|