1 //Tags: JDK1.2 |
|
2 |
|
3 //Copyright (C) 2004 David Gilbert <david.gilbert@object-refinery.com> |
|
4 |
|
5 //This file is part of Mauve. |
|
6 |
|
7 //Mauve is free software; you can redistribute it and/or modify |
|
8 //it under the terms of the GNU General Public License as published by |
|
9 //the Free Software Foundation; either version 2, or (at your option) |
|
10 //any later version. |
|
11 |
|
12 //Mauve is distributed in the hope that it will be useful, |
|
13 //but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15 //GNU General Public License for more details. |
|
16 |
|
17 //You should have received a copy of the GNU General Public License |
|
18 //along with Mauve; see the file COPYING. If not, write to |
|
19 //the Free Software Foundation, 59 Temple Place - Suite 330, |
|
20 //Boston, MA 02111-1307, USA. |
|
21 |
|
22 package gnu.testlet.java.util.Arrays; |
|
23 |
|
24 import gnu.testlet.TestHarness; |
|
25 import gnu.testlet.Testlet; |
|
26 |
|
27 import java.util.Arrays; |
|
28 import java.util.Comparator; |
|
29 |
|
30 /** |
|
31 * Some tests for the binarySearch() method in the {@link Arrays} class. |
|
32 */ |
|
33 public class binarySearch implements Testlet |
|
34 { |
|
35 |
|
36 /** |
|
37 * Runs the test using the specified harness. |
|
38 * |
|
39 * @param harness the test harness (<code>null</code> not permitted). |
|
40 */ |
|
41 public void test(TestHarness harness) { |
|
42 testByte(harness); |
|
43 testChar(harness); |
|
44 testDouble(harness); |
|
45 testFloat(harness); |
|
46 testInt(harness); |
|
47 testLong(harness); |
|
48 testObject(harness); |
|
49 testShort(harness); |
|
50 } |
|
51 |
|
52 private void testByte(TestHarness harness) |
|
53 { |
|
54 harness.checkPoint("Arrays.binarySearch(byte[], byte)"); |
|
55 byte[] b1 = new byte[] {1, 2, 3}; |
|
56 harness.check(Arrays.binarySearch(b1, (byte) 0) == -1); |
|
57 harness.check(Arrays.binarySearch(b1, (byte) 1) == 0); |
|
58 harness.check(Arrays.binarySearch(b1, (byte) 2) == 1); |
|
59 harness.check(Arrays.binarySearch(b1, (byte) 3) == 2); |
|
60 harness.check(Arrays.binarySearch(b1, (byte) 4) == -4); |
|
61 |
|
62 boolean pass = false; |
|
63 try |
|
64 { |
|
65 Arrays.binarySearch((byte[]) null, (byte) 0); |
|
66 } |
|
67 catch (NullPointerException e) |
|
68 { |
|
69 pass = true; |
|
70 } |
|
71 harness.check(pass); |
|
72 |
|
73 b1 = new byte[0]; |
|
74 harness.check(Arrays.binarySearch(b1, (byte)0), -1); |
|
75 } |
|
76 |
|
77 private void testChar(TestHarness harness) |
|
78 { |
|
79 harness.checkPoint("Arrays.binarySearch(char[], char)"); |
|
80 char[] b1 = new char[] {'1', '2', '3'}; |
|
81 harness.check(Arrays.binarySearch(b1, '0') == -1); |
|
82 harness.check(Arrays.binarySearch(b1, '1') == 0); |
|
83 harness.check(Arrays.binarySearch(b1, '2') == 1); |
|
84 harness.check(Arrays.binarySearch(b1, '3') == 2); |
|
85 harness.check(Arrays.binarySearch(b1, '4') == -4); |
|
86 |
|
87 boolean pass = false; |
|
88 try |
|
89 { |
|
90 Arrays.binarySearch((char[]) null, '0'); |
|
91 } |
|
92 catch (NullPointerException e) |
|
93 { |
|
94 pass = true; |
|
95 } |
|
96 harness.check(pass); |
|
97 |
|
98 b1 = new char[0]; |
|
99 harness.check(Arrays.binarySearch(b1, '0'), -1); |
|
100 } |
|
101 |
|
102 private void testDouble(TestHarness harness) |
|
103 { |
|
104 harness.checkPoint("Arrays.binarySearch(double[], double)"); |
|
105 double[] b1 = new double[] {1.0, 2.0, 3.0}; |
|
106 harness.check(Arrays.binarySearch(b1, 0.0) == -1); |
|
107 harness.check(Arrays.binarySearch(b1, 1.0) == 0); |
|
108 harness.check(Arrays.binarySearch(b1, 2.0) == 1); |
|
109 harness.check(Arrays.binarySearch(b1, 3.0) == 2); |
|
110 harness.check(Arrays.binarySearch(b1, 4.0) == -4); |
|
111 |
|
112 boolean pass = false; |
|
113 try |
|
114 { |
|
115 Arrays.binarySearch((double[]) null, 0.0); |
|
116 } |
|
117 catch (NullPointerException e) |
|
118 { |
|
119 pass = true; |
|
120 } |
|
121 harness.check(pass); |
|
122 |
|
123 b1 = new double[0]; |
|
124 harness.check(Arrays.binarySearch(b1, 0.0), -1); |
|
125 } |
|
126 |
|
127 private void testFloat(TestHarness harness) |
|
128 { |
|
129 harness.checkPoint("Arrays.binarySearch(float[], float)"); |
|
130 float[] b1 = new float[] {1.0f, 2.0f, 3.0f}; |
|
131 harness.check(Arrays.binarySearch(b1, 0.0f) == -1); |
|
132 harness.check(Arrays.binarySearch(b1, 1.0f) == 0); |
|
133 harness.check(Arrays.binarySearch(b1, 2.0f) == 1); |
|
134 harness.check(Arrays.binarySearch(b1, 3.0f) == 2); |
|
135 harness.check(Arrays.binarySearch(b1, 4.0f) == -4); |
|
136 |
|
137 boolean pass = false; |
|
138 try |
|
139 { |
|
140 Arrays.binarySearch((float[]) null, 0.0f); |
|
141 } |
|
142 catch (NullPointerException e) |
|
143 { |
|
144 pass = true; |
|
145 } |
|
146 harness.check(pass); |
|
147 |
|
148 b1 = new float[0]; |
|
149 harness.check(Arrays.binarySearch(b1, 0.0f), -1); |
|
150 } |
|
151 |
|
152 private void testInt(TestHarness harness) |
|
153 { |
|
154 harness.checkPoint("Arrays.binarySearch(int[], int)"); |
|
155 int[] b1 = new int[] {1, 2, 3}; |
|
156 harness.check(Arrays.binarySearch(b1, 0) == -1); |
|
157 harness.check(Arrays.binarySearch(b1, 1) == 0); |
|
158 harness.check(Arrays.binarySearch(b1, 2) == 1); |
|
159 harness.check(Arrays.binarySearch(b1, 3) == 2); |
|
160 harness.check(Arrays.binarySearch(b1, 4) == -4); |
|
161 |
|
162 boolean pass = false; |
|
163 try |
|
164 { |
|
165 Arrays.binarySearch((int[]) null, 0); |
|
166 } |
|
167 catch (NullPointerException e) |
|
168 { |
|
169 pass = true; |
|
170 } |
|
171 harness.check(pass); |
|
172 |
|
173 b1 = new int[0]; |
|
174 harness.check(Arrays.binarySearch(b1, 0), -1); |
|
175 } |
|
176 |
|
177 private void testLong(TestHarness harness) |
|
178 { |
|
179 harness.checkPoint("Arrays.binarySearch(long[], long)"); |
|
180 long[] b1 = new long[] {1, 2, 3}; |
|
181 harness.check(Arrays.binarySearch(b1, 0) == -1); |
|
182 harness.check(Arrays.binarySearch(b1, 1) == 0); |
|
183 harness.check(Arrays.binarySearch(b1, 2) == 1); |
|
184 harness.check(Arrays.binarySearch(b1, 3) == 2); |
|
185 harness.check(Arrays.binarySearch(b1, 4) == -4); |
|
186 |
|
187 boolean pass = false; |
|
188 try |
|
189 { |
|
190 Arrays.binarySearch((long[]) null, 0); |
|
191 } |
|
192 catch (NullPointerException e) |
|
193 { |
|
194 pass = true; |
|
195 } |
|
196 harness.check(pass); |
|
197 |
|
198 b1 = new long[0]; |
|
199 harness.check(Arrays.binarySearch(b1, 0), -1); |
|
200 } |
|
201 |
|
202 private void testObject(TestHarness harness) |
|
203 { |
|
204 harness.checkPoint("Arrays.binarySearch(Object[], Object)"); |
|
205 Object[] b1 = new Object[] {"1", "2", "3"}; |
|
206 harness.check(Arrays.binarySearch(b1, "0") == -1); |
|
207 harness.check(Arrays.binarySearch(b1, "1") == 0); |
|
208 harness.check(Arrays.binarySearch(b1, "2") == 1); |
|
209 harness.check(Arrays.binarySearch(b1, "3") == 2); |
|
210 harness.check(Arrays.binarySearch(b1, "4") == -4); |
|
211 |
|
212 // searching for null throws NullPointerException |
|
213 boolean pass = false; |
|
214 try |
|
215 { |
|
216 Arrays.binarySearch(b1, null); |
|
217 } |
|
218 catch (NullPointerException e) |
|
219 { |
|
220 pass = true; |
|
221 } |
|
222 harness.check(pass); |
|
223 |
|
224 pass = false; |
|
225 try |
|
226 { |
|
227 Arrays.binarySearch((Object[]) null, "0"); |
|
228 } |
|
229 catch (NullPointerException e) |
|
230 { |
|
231 pass = true; |
|
232 } |
|
233 harness.check(pass); |
|
234 |
|
235 harness.checkPoint("Arrays.binarySearch(Object[], Object, Comparator)"); |
|
236 harness.check(Arrays.binarySearch(b1, "0", (Comparator) null) == -1); |
|
237 harness.check(Arrays.binarySearch(b1, "1", (Comparator) null) == 0); |
|
238 harness.check(Arrays.binarySearch(b1, "2", (Comparator) null) == 1); |
|
239 harness.check(Arrays.binarySearch(b1, "3", (Comparator) null) == 2); |
|
240 harness.check(Arrays.binarySearch(b1, "4", (Comparator) null) == -4); |
|
241 |
|
242 Arrays.sort(b1, new ReverseComparator()); |
|
243 harness.check(Arrays.binarySearch(b1, "0", new ReverseComparator()) == -4); |
|
244 harness.check(Arrays.binarySearch(b1, "1", new ReverseComparator()) == 2); |
|
245 harness.check(Arrays.binarySearch(b1, "2", new ReverseComparator()) == 1); |
|
246 harness.check(Arrays.binarySearch(b1, "3", new ReverseComparator()) == 0); |
|
247 harness.check(Arrays.binarySearch(b1, "4", new ReverseComparator()) == -1); |
|
248 |
|
249 b1 = new Object[0]; |
|
250 harness.check(Arrays.binarySearch(b1, ""), -1); |
|
251 } |
|
252 |
|
253 private void testShort(TestHarness harness) |
|
254 { |
|
255 harness.checkPoint("Arrays.binarySearch(short[], short)"); |
|
256 short[] b1 = new short[] {1, 2, 3}; |
|
257 harness.check(Arrays.binarySearch(b1, (short) 0) == -1); |
|
258 harness.check(Arrays.binarySearch(b1, (short) 1) == 0); |
|
259 harness.check(Arrays.binarySearch(b1, (short) 2) == 1); |
|
260 harness.check(Arrays.binarySearch(b1, (short) 3) == 2); |
|
261 harness.check(Arrays.binarySearch(b1, (short) 4) == -4); |
|
262 |
|
263 boolean pass = false; |
|
264 try |
|
265 { |
|
266 Arrays.binarySearch((short[]) null, (short) 0); |
|
267 } |
|
268 catch (NullPointerException e) |
|
269 { |
|
270 pass = true; |
|
271 } |
|
272 harness.check(pass); |
|
273 |
|
274 b1 = new short[0]; |
|
275 harness.check(Arrays.binarySearch(b1, (short)0), -1); |
|
276 } |
|
277 |
|
278 static class ReverseComparator implements Comparator { |
|
279 public int compare(Object o1, Object o2) { |
|
280 int i1 = Integer.valueOf(o1.toString()).intValue(); |
|
281 int i2 = Integer.valueOf(o2.toString()).intValue(); |
|
282 return (i2 - i1); |
|
283 } |
|
284 } |
|
285 |
|
286 } |
|