• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 package tests.api.java.util;
18 
19 import java.util.Arrays;
20 import java.util.Comparator;
21 import java.util.LinkedList;
22 import java.util.List;
23 import java.util.Random;
24 
25 import tests.support.Support_UnmodifiableCollectionTest;
26 
27 public class ArraysTest extends junit.framework.TestCase {
28 
29     public static class ReversedIntegerComparator implements Comparator {
compare(Object o1, Object o2)30         public int compare(Object o1, Object o2) {
31             return -(((Integer) o1).compareTo((Integer) o2));
32         }
33 
equals(Object o1, Object o2)34         public boolean equals(Object o1, Object o2) {
35             return ((Integer) o1).compareTo((Integer) o2) == 0;
36         }
37     }
38 
39     final static int arraySize = 100;
40 
41     Object[] objArray;
42 
43     boolean[] booleanArray;
44 
45     byte[] byteArray;
46 
47     char[] charArray;
48 
49     double[] doubleArray;
50 
51     float[] floatArray;
52 
53     int[] intArray;
54 
55     long[] longArray;
56 
57     Object[] objectArray;
58 
59     short[] shortArray;
60 
61     /**
62      * java.util.Arrays#asList(java.lang.Object[])
63      */
test_asList$Ljava_lang_Object()64     public void test_asList$Ljava_lang_Object() {
65         // Test for method java.util.List
66         // java.util.Arrays.asList(java.lang.Object [])
67         List convertedList = Arrays.asList(objectArray);
68         for (int counter = 0; counter < arraySize; counter++) {
69             assertTrue(
70                     "Array and List converted from array do not contain identical elements",
71                     convertedList.get(counter) == objectArray[counter]);
72         }
73         convertedList.set(50, new Integer(1000));
74         assertTrue("set/get did not work on coverted list", convertedList.get(
75                 50).equals(new Integer(1000)));
76         convertedList.set(50, new Integer(50));
77         new Support_UnmodifiableCollectionTest("", convertedList).runTest();
78 
79         Object[] myArray = (Object[]) (objectArray.clone());
80         myArray[30] = null;
81         myArray[60] = null;
82         convertedList = Arrays.asList(myArray);
83         for (int counter = 0; counter < arraySize; counter++) {
84             assertTrue(
85                     "Array and List converted from array do not contain identical elements",
86                     convertedList.get(counter) == myArray[counter]);
87         }
88 
89         try {
90             Arrays.asList((Object[])null);
91             fail("asList with null arg didn't throw NPE");
92         } catch (NullPointerException e) {
93             // Expected
94         }
95     }
96 
97     /**
98      * java.util.Arrays#binarySearch(byte[], byte)
99      */
test_binarySearch$BB()100     public void test_binarySearch$BB() {
101         // Test for method int java.util.Arrays.binarySearch(byte [], byte)
102         for (byte counter = 0; counter < arraySize; counter++)
103             assertTrue("Binary search on byte[] answered incorrect position",
104                     Arrays.binarySearch(byteArray, counter) == counter);
105         assertEquals("Binary search succeeded for value not present in array 1",
106                 -1, Arrays.binarySearch(intArray, (byte) -1));
107         assertTrue(
108                 "Binary search succeeded for value not present in array 2",
109                 Arrays.binarySearch(intArray, (byte) arraySize) == -(arraySize + 1));
110         for (byte counter = 0; counter < arraySize; counter++)
111             byteArray[counter] -= 50;
112         for (byte counter = 0; counter < arraySize; counter++)
113             assertTrue(
114                     "Binary search on byte[] involving negative numbers answered incorrect position",
115                     Arrays.binarySearch(byteArray, (byte) (counter - 50)) == counter);
116     }
117 
118     /**
119      * java.util.Arrays#binarySearch(char[], char)
120      */
test_binarySearch$CC()121     public void test_binarySearch$CC() {
122         // Test for method int java.util.Arrays.binarySearch(char [], char)
123         for (char counter = 0; counter < arraySize; counter++)
124             assertTrue(
125                     "Binary search on char[] answered incorrect position",
126                     Arrays.binarySearch(charArray, (char) (counter + 1)) == counter);
127         assertEquals("Binary search succeeded for value not present in array 1",
128                 -1, Arrays.binarySearch(charArray, '\u0000'));
129         assertTrue(
130                 "Binary search succeeded for value not present in array 2",
131                 Arrays.binarySearch(charArray, (char) (arraySize + 1)) == -(arraySize + 1));
132     }
133 
134     /**
135      * java.util.Arrays#binarySearch(double[], double)
136      */
test_binarySearch$DD()137     public void test_binarySearch$DD() {
138         // Test for method int java.util.Arrays.binarySearch(double [], double)
139         for (int counter = 0; counter < arraySize; counter++)
140             assertTrue(
141                     "Binary search on double[] answered incorrect position",
142                     Arrays.binarySearch(doubleArray, (double) counter) == (double) counter);
143         assertEquals("Binary search succeeded for value not present in array 1",
144                 -1, Arrays.binarySearch(doubleArray, (double) -1));
145         assertTrue(
146                 "Binary search succeeded for value not present in array 2",
147                 Arrays.binarySearch(doubleArray, (double) arraySize) == -(arraySize + 1));
148         for (int counter = 0; counter < arraySize; counter++)
149             doubleArray[counter] -= (double) 50;
150         for (int counter = 0; counter < arraySize; counter++)
151             assertTrue(
152                     "Binary search on double[] involving negative numbers answered incorrect position",
153                     Arrays.binarySearch(doubleArray, (double) (counter - 50)) == (double) counter);
154 
155         double[] specials = new double[] { Double.NEGATIVE_INFINITY,
156                 -Double.MAX_VALUE, -2d, -Double.MIN_VALUE, -0d, 0d,
157                 Double.MIN_VALUE, 2d, Double.MAX_VALUE,
158                 Double.POSITIVE_INFINITY, Double.NaN };
159         for (int i = 0; i < specials.length; i++) {
160             int result = Arrays.binarySearch(specials, specials[i]);
161             assertTrue(specials[i] + " invalid: " + result, result == i);
162         }
163         assertEquals("-1d", -4, Arrays.binarySearch(specials, -1d));
164         assertEquals("1d", -8, Arrays.binarySearch(specials, 1d));
165 
166     }
167 
168     /**
169      * java.util.Arrays#binarySearch(float[], float)
170      */
test_binarySearch$FF()171     public void test_binarySearch$FF() {
172         // Test for method int java.util.Arrays.binarySearch(float [], float)
173         for (int counter = 0; counter < arraySize; counter++)
174             assertTrue(
175                     "Binary search on float[] answered incorrect position",
176                     Arrays.binarySearch(floatArray, (float) counter) == (float) counter);
177         assertEquals("Binary search succeeded for value not present in array 1",
178                 -1, Arrays.binarySearch(floatArray, (float) -1));
179         assertTrue(
180                 "Binary search succeeded for value not present in array 2",
181                 Arrays.binarySearch(floatArray, (float) arraySize) == -(arraySize + 1));
182         for (int counter = 0; counter < arraySize; counter++)
183             floatArray[counter] -= (float) 50;
184         for (int counter = 0; counter < arraySize; counter++)
185             assertTrue(
186                     "Binary search on float[] involving negative numbers answered incorrect position",
187                     Arrays.binarySearch(floatArray, (float) counter - 50) == (float) counter);
188 
189         float[] specials = new float[] { Float.NEGATIVE_INFINITY,
190                 -Float.MAX_VALUE, -2f, -Float.MIN_VALUE, -0f, 0f,
191                 Float.MIN_VALUE, 2f, Float.MAX_VALUE, Float.POSITIVE_INFINITY,
192                 Float.NaN };
193         for (int i = 0; i < specials.length; i++) {
194             int result = Arrays.binarySearch(specials, specials[i]);
195             assertTrue(specials[i] + " invalid: " + result, result == i);
196         }
197         assertEquals("-1f", -4, Arrays.binarySearch(specials, -1f));
198         assertEquals("1f", -8, Arrays.binarySearch(specials, 1f));
199     }
200 
201     /**
202      * java.util.Arrays#binarySearch(int[], int)
203      */
test_binarySearch$II()204     public void test_binarySearch$II() {
205         // Test for method int java.util.Arrays.binarySearch(int [], int)
206         for (int counter = 0; counter < arraySize; counter++)
207             assertTrue("Binary search on int[] answered incorrect position",
208                     Arrays.binarySearch(intArray, counter) == counter);
209         assertEquals("Binary search succeeded for value not present in array 1",
210                 -1, Arrays.binarySearch(intArray, -1));
211         assertTrue("Binary search succeeded for value not present in array 2",
212                 Arrays.binarySearch(intArray, arraySize) == -(arraySize + 1));
213         for (int counter = 0; counter < arraySize; counter++)
214             intArray[counter] -= 50;
215         for (int counter = 0; counter < arraySize; counter++)
216             assertTrue(
217                     "Binary search on int[] involving negative numbers answered incorrect position",
218                     Arrays.binarySearch(intArray, counter - 50) == counter);
219     }
220 
221     /**
222      * java.util.Arrays#binarySearch(long[], long)
223      */
test_binarySearch$JJ()224     public void test_binarySearch$JJ() {
225         // Test for method int java.util.Arrays.binarySearch(long [], long)
226         for (long counter = 0; counter < arraySize; counter++)
227             assertTrue("Binary search on long[] answered incorrect position",
228                     Arrays.binarySearch(longArray, counter) == counter);
229         assertEquals("Binary search succeeded for value not present in array 1",
230                 -1, Arrays.binarySearch(longArray, (long) -1));
231         assertTrue(
232                 "Binary search succeeded for value not present in array 2",
233                 Arrays.binarySearch(longArray, (long) arraySize) == -(arraySize + 1));
234         for (long counter = 0; counter < arraySize; counter++)
235             longArray[(int) counter] -= (long) 50;
236         for (long counter = 0; counter < arraySize; counter++)
237             assertTrue(
238                     "Binary search on long[] involving negative numbers answered incorrect position",
239                     Arrays.binarySearch(longArray, counter - (long) 50) == counter);
240     }
241 
242     /**
243      * java.util.Arrays#binarySearch(java.lang.Object[],
244      *        java.lang.Object)
245      */
test_binarySearch$Ljava_lang_ObjectLjava_lang_Object()246     public void test_binarySearch$Ljava_lang_ObjectLjava_lang_Object() {
247         // Test for method int java.util.Arrays.binarySearch(java.lang.Object
248         // [], java.lang.Object)
249         assertEquals(
250                 "Binary search succeeded for non-comparable value in empty array",
251                 -1, Arrays.binarySearch(new Object[] {}, new Object()));
252         assertEquals(
253                 "Binary search succeeded for comparable value in empty array",
254                 -1, Arrays.binarySearch(new Object[] {}, new Integer(-1)));
255         for (int counter = 0; counter < arraySize; counter++)
256             assertTrue(
257                     "Binary search on Object[] answered incorrect position",
258                     Arrays.binarySearch(objectArray, objArray[counter]) == counter);
259         assertEquals("Binary search succeeded for value not present in array 1",
260                 -1, Arrays.binarySearch(objectArray, new Integer(-1)));
261         assertTrue(
262                 "Binary search succeeded for value not present in array 2",
263                 Arrays.binarySearch(objectArray, new Integer(arraySize)) == -(arraySize + 1));
264 
265         String[] sArray = new String[]{"1", "2", "3", "4", ""};
266         Object[] oArray = sArray;
267 
268         try {
269             Arrays.binarySearch(oArray, new Integer(10));
270             fail("ClassCastException expected");
271         } catch (ClassCastException e) {
272             //expected
273         }
274     }
275 
276     /**
277      * java.util.Arrays#binarySearch(java.lang.Object[],
278      *        java.lang.Object, java.util.Comparator)
279      */
test_binarySearch$Ljava_lang_ObjectLjava_lang_ObjectLjava_util_Comparator()280     public void test_binarySearch$Ljava_lang_ObjectLjava_lang_ObjectLjava_util_Comparator() {
281         // Test for method int java.util.Arrays.binarySearch(java.lang.Object
282         // [], java.lang.Object, java.util.Comparator)
283         Comparator comp = new ReversedIntegerComparator();
284         for (int counter = 0; counter < arraySize; counter++)
285             objectArray[counter] = objArray[arraySize - counter - 1];
286         assertTrue(
287                 "Binary search succeeded for value not present in array 1",
288                 Arrays.binarySearch(objectArray, new Integer(-1), comp) == -(arraySize + 1));
289         assertEquals("Binary search succeeded for value not present in array 2",
290                 -1, Arrays.binarySearch(objectArray, new Integer(arraySize), comp));
291         for (int counter = 0; counter < arraySize; counter++)
292             assertTrue(
293                     "Binary search on Object[] with custom comparator answered incorrect position",
294                     Arrays.binarySearch(objectArray, objArray[counter], comp) == arraySize
295                             - counter - 1);
296     }
297 
298     /**
299      * java.util.Arrays#binarySearch(short[], short)
300      */
test_binarySearch$SS()301     public void test_binarySearch$SS() {
302         // Test for method int java.util.Arrays.binarySearch(short [], short)
303         for (short counter = 0; counter < arraySize; counter++)
304             assertTrue("Binary search on short[] answered incorrect position",
305                     Arrays.binarySearch(shortArray, counter) == counter);
306         assertEquals("Binary search succeeded for value not present in array 1",
307                 -1, Arrays.binarySearch(intArray, (short) -1));
308         assertTrue(
309                 "Binary search succeeded for value not present in array 2",
310                 Arrays.binarySearch(intArray, (short) arraySize) == -(arraySize + 1));
311         for (short counter = 0; counter < arraySize; counter++)
312             shortArray[counter] -= 50;
313         for (short counter = 0; counter < arraySize; counter++)
314             assertTrue(
315                     "Binary search on short[] involving negative numbers answered incorrect position",
316                     Arrays.binarySearch(shortArray, (short) (counter - 50)) == counter);
317     }
318 
319     /**
320      * java.util.Arrays#fill(byte[], byte)
321      */
test_fill$BB()322     public void test_fill$BB() {
323         // Test for method void java.util.Arrays.fill(byte [], byte)
324 
325         byte d[] = new byte[1000];
326         Arrays.fill(d, Byte.MAX_VALUE);
327         for (int i = 0; i < d.length; i++)
328             assertTrue("Failed to fill byte array correctly",
329                     d[i] == Byte.MAX_VALUE);
330     }
331 
332     /**
333      * java.util.Arrays#fill(byte[], int, int, byte)
334      */
test_fill$BIIB()335     public void test_fill$BIIB() {
336         // Test for method void java.util.Arrays.fill(byte [], int, int, byte)
337         byte val = Byte.MAX_VALUE;
338         byte d[] = new byte[1000];
339         Arrays.fill(d, 400, d.length, val);
340         for (int i = 0; i < 400; i++)
341             assertTrue("Filled elements not in range", !(d[i] == val));
342         for (int i = 400; i < d.length; i++)
343             assertTrue("Failed to fill byte array correctly", d[i] == val);
344 
345         int result;
346         try {
347             Arrays.fill(new byte[2], 2, 1, (byte) 27);
348             result = 0;
349         } catch (ArrayIndexOutOfBoundsException e) {
350             result = 1;
351         } catch (IllegalArgumentException e) {
352             result = 2;
353         }
354         assertEquals("Wrong exception1", 2, result);
355         try {
356             Arrays.fill(new byte[2], -1, 1, (byte) 27);
357             result = 0;
358         } catch (ArrayIndexOutOfBoundsException e) {
359             result = 1;
360         } catch (IllegalArgumentException e) {
361             result = 2;
362         }
363         assertEquals("Wrong exception2", 1, result);
364         try {
365             Arrays.fill(new byte[2], 1, 4, (byte) 27);
366             result = 0;
367         } catch (ArrayIndexOutOfBoundsException e) {
368             result = 1;
369         } catch (IllegalArgumentException e) {
370             result = 2;
371         }
372         assertEquals("Wrong exception", 1, result);
373     }
374 
375     /**
376      * java.util.Arrays#fill(short[], short)
377      */
test_fill$SS()378     public void test_fill$SS() {
379         // Test for method void java.util.Arrays.fill(short [], short)
380 
381         short d[] = new short[1000];
382         Arrays.fill(d, Short.MAX_VALUE);
383         for (int i = 0; i < d.length; i++)
384             assertTrue("Failed to fill short array correctly",
385                     d[i] == Short.MAX_VALUE);
386     }
387 
388     /**
389      * java.util.Arrays#fill(short[], int, int, short)
390      */
test_fill$SIIS()391     public void test_fill$SIIS() {
392         // Test for method void java.util.Arrays.fill(short [], int, int, short)
393         short val = Short.MAX_VALUE;
394         short d[] = new short[1000];
395         Arrays.fill(d, 400, d.length, val);
396         for (int i = 0; i < 400; i++)
397             assertTrue("Filled elements not in range", !(d[i] == val));
398         for (int i = 400; i < d.length; i++)
399             assertTrue("Failed to fill short array correctly", d[i] == val);
400 
401         try {
402             Arrays.fill(d, 10, 0, val);
403             fail("IllegalArgumentException expected");
404         } catch (IllegalArgumentException e) {
405             //expected
406         }
407 
408         try {
409             Arrays.fill(d, -10, 0, val);
410             fail("ArrayIndexOutOfBoundsException expected");
411         } catch (ArrayIndexOutOfBoundsException e) {
412             //expected
413         }
414 
415         try {
416             Arrays.fill(d, 10, d.length+1, val);
417             fail("ArrayIndexOutOfBoundsException expected");
418         } catch (ArrayIndexOutOfBoundsException e) {
419             //expected
420         }
421     }
422 
423     /**
424      * java.util.Arrays#fill(char[], char)
425      */
test_fill$CC()426     public void test_fill$CC() {
427         // Test for method void java.util.Arrays.fill(char [], char)
428 
429         char d[] = new char[1000];
430         Arrays.fill(d, 'V');
431         for (int i = 0; i < d.length; i++)
432             assertEquals("Failed to fill char array correctly", 'V', d[i]);
433     }
434 
435     /**
436      * java.util.Arrays#fill(char[], int, int, char)
437      */
test_fill$CIIC()438     public void test_fill$CIIC() {
439         // Test for method void java.util.Arrays.fill(char [], int, int, char)
440         char val = 'T';
441         char d[] = new char[1000];
442         Arrays.fill(d, 400, d.length, val);
443         for (int i = 0; i < 400; i++)
444             assertTrue("Filled elements not in range", !(d[i] == val));
445         for (int i = 400; i < d.length; i++)
446             assertTrue("Failed to fill char array correctly", d[i] == val);
447 
448         try {
449             Arrays.fill(d, 10, 0, val);
450             fail("IllegalArgumentException expected");
451         } catch (IllegalArgumentException e) {
452             //expected
453         }
454 
455         try {
456             Arrays.fill(d, -10, 0, val);
457             fail("ArrayIndexOutOfBoundsException expected");
458         } catch (ArrayIndexOutOfBoundsException e) {
459             //expected
460         }
461 
462         try {
463             Arrays.fill(d, 10, d.length+1, val);
464             fail("ArrayIndexOutOfBoundsException expected");
465         } catch (ArrayIndexOutOfBoundsException e) {
466             //expected
467         }
468     }
469 
470     /**
471      * java.util.Arrays#fill(int[], int)
472      */
test_fill$II()473     public void test_fill$II() {
474         // Test for method void java.util.Arrays.fill(int [], int)
475 
476         int d[] = new int[1000];
477         Arrays.fill(d, Integer.MAX_VALUE);
478         for (int i = 0; i < d.length; i++)
479             assertTrue("Failed to fill int array correctly",
480                     d[i] == Integer.MAX_VALUE);
481     }
482 
483     /**
484      * java.util.Arrays#fill(int[], int, int, int)
485      */
test_fill$IIII()486     public void test_fill$IIII() {
487         // Test for method void java.util.Arrays.fill(int [], int, int, int)
488         int val = Integer.MAX_VALUE;
489         int d[] = new int[1000];
490         Arrays.fill(d, 400, d.length, val);
491         for (int i = 0; i < 400; i++)
492             assertTrue("Filled elements not in range", !(d[i] == val));
493         for (int i = 400; i < d.length; i++)
494             assertTrue("Failed to fill int array correctly", d[i] == val);
495 
496         try {
497             Arrays.fill(d, 10, 0, val);
498             fail("IllegalArgumentException expected");
499         } catch (IllegalArgumentException e) {
500             //expected
501         }
502 
503         try {
504             Arrays.fill(d, -10, 0, val);
505             fail("ArrayIndexOutOfBoundsException expected");
506         } catch (ArrayIndexOutOfBoundsException e) {
507             //expected
508         }
509 
510         try {
511             Arrays.fill(d, 10, d.length+1, val);
512             fail("ArrayIndexOutOfBoundsException expected");
513         } catch (ArrayIndexOutOfBoundsException e) {
514             //expected
515         }
516     }
517 
518     /**
519      * java.util.Arrays#fill(long[], long)
520      */
test_fill$JJ()521     public void test_fill$JJ() {
522         // Test for method void java.util.Arrays.fill(long [], long)
523 
524         long d[] = new long[1000];
525         Arrays.fill(d, Long.MAX_VALUE);
526         for (int i = 0; i < d.length; i++)
527             assertTrue("Failed to fill long array correctly",
528                     d[i] == Long.MAX_VALUE);
529     }
530 
531     /**
532      * java.util.Arrays#fill(long[], int, int, long)
533      */
test_fill$JIIJ()534     public void test_fill$JIIJ() {
535         // Test for method void java.util.Arrays.fill(long [], int, int, long)
536         long d[] = new long[1000];
537         Arrays.fill(d, 400, d.length, Long.MAX_VALUE);
538         for (int i = 0; i < 400; i++)
539             assertTrue("Filled elements not in range", !(d[i] == Long.MAX_VALUE));
540         for (int i = 400; i < d.length; i++)
541             assertTrue("Failed to fill long array correctly",
542                     d[i] == Long.MAX_VALUE);
543 
544         try {
545             Arrays.fill(d, 10, 0, Long.MIN_VALUE);
546             fail("IllegalArgumentException expected");
547         } catch (IllegalArgumentException e) {
548             //expected
549         }
550 
551         try {
552             Arrays.fill(d, -10, 0, Long.MAX_VALUE);
553             fail("ArrayIndexOutOfBoundsException expected");
554         } catch (ArrayIndexOutOfBoundsException e) {
555             //expected
556         }
557 
558         try {
559             Arrays.fill(d, 10, d.length+1, Long.MAX_VALUE);
560             fail("ArrayIndexOutOfBoundsException expected");
561         } catch (ArrayIndexOutOfBoundsException e) {
562             //expected
563         }
564     }
565 
566     /**
567      * java.util.Arrays#fill(float[], float)
568      */
test_fill$FF()569     public void test_fill$FF() {
570         // Test for method void java.util.Arrays.fill(float [], float)
571         float d[] = new float[1000];
572         Arrays.fill(d, Float.MAX_VALUE);
573         for (int i = 0; i < d.length; i++)
574             assertTrue("Failed to fill float array correctly",
575                     d[i] == Float.MAX_VALUE);
576     }
577 
578     /**
579      * java.util.Arrays#fill(float[], int, int, float)
580      */
test_fill$FIIF()581     public void test_fill$FIIF() {
582         // Test for method void java.util.Arrays.fill(float [], int, int, float)
583         float val = Float.MAX_VALUE;
584         float d[] = new float[1000];
585         Arrays.fill(d, 400, d.length, val);
586         for (int i = 0; i < 400; i++)
587             assertTrue("Filled elements not in range", !(d[i] == val));
588         for (int i = 400; i < d.length; i++)
589             assertTrue("Failed to fill float array correctly", d[i] == val);
590 
591         try {
592             Arrays.fill(d, 10, 0, val);
593             fail("IllegalArgumentException expected");
594         } catch (IllegalArgumentException e) {
595             //expected
596         }
597 
598         try {
599             Arrays.fill(d, -10, 0, val);
600             fail("ArrayIndexOutOfBoundsException expected");
601         } catch (ArrayIndexOutOfBoundsException e) {
602             //expected
603         }
604 
605         try {
606             Arrays.fill(d, 10, d.length+1, val);
607             fail("ArrayIndexOutOfBoundsException expected");
608         } catch (ArrayIndexOutOfBoundsException e) {
609             //expected
610         }
611     }
612 
613     /**
614      * java.util.Arrays#fill(double[], double)
615      */
test_fill$DD()616     public void test_fill$DD() {
617         // Test for method void java.util.Arrays.fill(double [], double)
618 
619         double d[] = new double[1000];
620         Arrays.fill(d, Double.MAX_VALUE);
621         for (int i = 0; i < d.length; i++)
622             assertTrue("Failed to fill double array correctly",
623                     d[i] == Double.MAX_VALUE);
624     }
625 
626     /**
627      * java.util.Arrays#fill(double[], int, int, double)
628      */
test_fill$DIID()629     public void test_fill$DIID() {
630         // Test for method void java.util.Arrays.fill(double [], int, int,
631         // double)
632         double val = Double.MAX_VALUE;
633         double d[] = new double[1000];
634         Arrays.fill(d, 400, d.length, val);
635         for (int i = 0; i < 400; i++)
636             assertTrue("Filled elements not in range", !(d[i] == val));
637         for (int i = 400; i < d.length; i++)
638             assertTrue("Failed to fill double array correctly", d[i] == val);
639 
640         try {
641             Arrays.fill(d, 10, 0, val);
642             fail("IllegalArgumentException expected");
643         } catch (IllegalArgumentException e) {
644             //expected
645         }
646 
647         try {
648             Arrays.fill(d, -10, 0, val);
649             fail("ArrayIndexOutOfBoundsException expected");
650         } catch (ArrayIndexOutOfBoundsException e) {
651             //expected
652         }
653 
654         try {
655             Arrays.fill(d, 10, d.length+1, val);
656             fail("ArrayIndexOutOfBoundsException expected");
657         } catch (ArrayIndexOutOfBoundsException e) {
658             //expected
659         }
660     }
661 
662     /**
663      * java.util.Arrays#fill(boolean[], boolean)
664      */
test_fill$ZZ()665     public void test_fill$ZZ() {
666         // Test for method void java.util.Arrays.fill(boolean [], boolean)
667 
668         boolean d[] = new boolean[1000];
669         Arrays.fill(d, true);
670         for (int i = 0; i < d.length; i++)
671             assertTrue("Failed to fill boolean array correctly", d[i]);
672     }
673 
674     /**
675      * java.util.Arrays#fill(boolean[], int, int, boolean)
676      */
test_fill$ZIIZ()677     public void test_fill$ZIIZ() {
678         // Test for method void java.util.Arrays.fill(boolean [], int, int,
679         // boolean)
680         boolean val = true;
681         boolean d[] = new boolean[1000];
682         Arrays.fill(d, 400, d.length, val);
683         for (int i = 0; i < 400; i++)
684             assertTrue("Filled elements not in range", !(d[i] == val));
685         for (int i = 400; i < d.length; i++)
686             assertTrue("Failed to fill boolean array correctly", d[i] == val);
687 
688         try {
689             Arrays.fill(d, 10, 0, val);
690             fail("IllegalArgumentException expected");
691         } catch (IllegalArgumentException e) {
692             //expected
693         }
694 
695         try {
696             Arrays.fill(d, -10, 0, val);
697             fail("ArrayIndexOutOfBoundsException expected");
698         } catch (ArrayIndexOutOfBoundsException e) {
699             //expected
700         }
701 
702         try {
703             Arrays.fill(d, 10, d.length+1, val);
704             fail("ArrayIndexOutOfBoundsException expected");
705         } catch (ArrayIndexOutOfBoundsException e) {
706             //expected
707         }
708     }
709 
710     /**
711      * java.util.Arrays#fill(java.lang.Object[], java.lang.Object)
712      */
test_fill$Ljava_lang_ObjectLjava_lang_Object()713     public void test_fill$Ljava_lang_ObjectLjava_lang_Object() {
714         // Test for method void java.util.Arrays.fill(java.lang.Object [],
715         // java.lang.Object)
716         Object val = new Object();
717         Object d[] = new Object[1000];
718         Arrays.fill(d, 0, d.length, val);
719         for (int i = 0; i < d.length; i++)
720             assertTrue("Failed to fill Object array correctly", d[i] == val);
721     }
722 
723     /**
724      * java.util.Arrays#fill(java.lang.Object[], int, int,
725      *        java.lang.Object)
726      */
test_fill$Ljava_lang_ObjectIILjava_lang_Object()727     public void test_fill$Ljava_lang_ObjectIILjava_lang_Object() {
728         // Test for method void java.util.Arrays.fill(java.lang.Object [], int,
729         // int, java.lang.Object)
730         Object val = new Object();
731         Object d[] = new Object[1000];
732         Arrays.fill(d, 400, d.length, val);
733         for (int i = 0; i < 400; i++)
734             assertTrue("Filled elements not in range", !(d[i] == val));
735         for (int i = 400; i < d.length; i++)
736             assertTrue("Failed to fill Object array correctly", d[i] == val);
737 
738         Arrays.fill(d, 400, d.length, null);
739         for (int i = 400; i < d.length; i++)
740             assertNull("Failed to fill Object array correctly with nulls",
741                     d[i]);
742 
743         try {
744             Arrays.fill(d, 10, 0, val);
745             fail("IllegalArgumentException expected");
746         } catch (IllegalArgumentException e) {
747             //expected
748         }
749 
750         try {
751             Arrays.fill(d, -10, 0, val);
752             fail("ArrayIndexOutOfBoundsException expected");
753         } catch (ArrayIndexOutOfBoundsException e) {
754             //expected
755         }
756 
757         try {
758             Arrays.fill(d, 10, d.length+1, val);
759             fail("ArrayIndexOutOfBoundsException expected");
760         } catch (ArrayIndexOutOfBoundsException e) {
761             //expected
762         }
763     }
764 
765     /**
766      * java.util.Arrays#equals(byte[], byte[])
767      */
test_equals$B$B()768     public void test_equals$B$B() {
769         // Test for method boolean java.util.Arrays.equals(byte [], byte [])
770         byte d[] = new byte[1000];
771         byte x[] = new byte[1000];
772         Arrays.fill(d, Byte.MAX_VALUE);
773         Arrays.fill(x, Byte.MIN_VALUE);
774         assertTrue("Inequal arrays returned true", !Arrays.equals(d, x));
775         Arrays.fill(x, Byte.MAX_VALUE);
776         assertTrue("equal arrays returned false", Arrays.equals(d, x));
777     }
778 
779     /**
780      * java.util.Arrays#equals(short[], short[])
781      */
test_equals$S$S()782     public void test_equals$S$S() {
783         // Test for method boolean java.util.Arrays.equals(short [], short [])
784         short d[] = new short[1000];
785         short x[] = new short[1000];
786         Arrays.fill(d, Short.MAX_VALUE);
787         Arrays.fill(x, Short.MIN_VALUE);
788         assertTrue("Inequal arrays returned true", !Arrays.equals(d, x));
789         Arrays.fill(x, Short.MAX_VALUE);
790         assertTrue("equal arrays returned false", Arrays.equals(d, x));
791     }
792 
793     /**
794      * java.util.Arrays#equals(char[], char[])
795      */
test_equals$C$C()796     public void test_equals$C$C() {
797         // Test for method boolean java.util.Arrays.equals(char [], char [])
798         char d[] = new char[1000];
799         char x[] = new char[1000];
800         char c = 'T';
801         Arrays.fill(d, c);
802         Arrays.fill(x, 'L');
803         assertTrue("Inequal arrays returned true", !Arrays.equals(d, x));
804         Arrays.fill(x, c);
805         assertTrue("equal arrays returned false", Arrays.equals(d, x));
806     }
807 
808     /**
809      * java.util.Arrays#equals(int[], int[])
810      */
test_equals$I$I()811     public void test_equals$I$I() {
812         // Test for method boolean java.util.Arrays.equals(int [], int [])
813         int d[] = new int[1000];
814         int x[] = new int[1000];
815         Arrays.fill(d, Integer.MAX_VALUE);
816         Arrays.fill(x, Integer.MIN_VALUE);
817         assertTrue("Inequal arrays returned true", !Arrays.equals(d, x));
818         Arrays.fill(x, Integer.MAX_VALUE);
819         assertTrue("equal arrays returned false", Arrays.equals(d, x));
820 
821         assertTrue("wrong result for null array1", !Arrays.equals(new int[2],
822                 null));
823         assertTrue("wrong result for null array2", !Arrays.equals(null,
824                 new int[2]));
825     }
826 
827     /**
828      * java.util.Arrays#equals(long[], long[])
829      */
test_equals$J$J()830     public void test_equals$J$J() {
831         // Test for method boolean java.util.Arrays.equals(long [], long [])
832         long d[] = new long[1000];
833         long x[] = new long[1000];
834         Arrays.fill(d, Long.MAX_VALUE);
835         Arrays.fill(x, Long.MIN_VALUE);
836         assertTrue("Inequal arrays returned true", !Arrays.equals(d, x));
837         Arrays.fill(x, Long.MAX_VALUE);
838         assertTrue("equal arrays returned false", Arrays.equals(d, x));
839 
840         assertTrue("should be false", !Arrays.equals(
841                 new long[] { 0x100000000L }, new long[] { 0x200000000L }));
842 
843     }
844 
845     /**
846      * java.util.Arrays#equals(float[], float[])
847      */
test_equals$F$F()848     public void test_equals$F$F() {
849         // Test for method boolean java.util.Arrays.equals(float [], float [])
850         float d[] = new float[1000];
851         float x[] = new float[1000];
852         Arrays.fill(d, Float.MAX_VALUE);
853         Arrays.fill(x, Float.MIN_VALUE);
854         assertTrue("Inequal arrays returned true", !Arrays.equals(d, x));
855         Arrays.fill(x, Float.MAX_VALUE);
856         assertTrue("equal arrays returned false", Arrays.equals(d, x));
857 
858         assertTrue("NaN not equals", Arrays.equals(new float[] { Float.NaN },
859                 new float[] { Float.NaN }));
860         assertTrue("0f equals -0f", !Arrays.equals(new float[] { 0f },
861                 new float[] { -0f }));
862     }
863 
864     /**
865      * java.util.Arrays#equals(double[], double[])
866      */
test_equals$D$D()867     public void test_equals$D$D() {
868         // Test for method boolean java.util.Arrays.equals(double [], double [])
869         double d[] = new double[1000];
870         double x[] = new double[1000];
871         Arrays.fill(d, Double.MAX_VALUE);
872         Arrays.fill(x, Double.MIN_VALUE);
873         assertTrue("Inequal arrays returned true", !Arrays.equals(d, x));
874         Arrays.fill(x, Double.MAX_VALUE);
875         assertTrue("equal arrays returned false", Arrays.equals(d, x));
876 
877         assertTrue("should be false", !Arrays.equals(new double[] { 1.0 },
878                 new double[] { 2.0 }));
879 
880         assertTrue("NaN not equals", Arrays.equals(new double[] { Double.NaN },
881                 new double[] { Double.NaN }));
882         assertTrue("0d equals -0d", !Arrays.equals(new double[] { 0d },
883                 new double[] { -0d }));
884     }
885 
886     /**
887      * java.util.Arrays#equals(boolean[], boolean[])
888      */
test_equals$Z$Z()889     public void test_equals$Z$Z() {
890         // Test for method boolean java.util.Arrays.equals(boolean [], boolean
891         // [])
892         boolean d[] = new boolean[1000];
893         boolean x[] = new boolean[1000];
894         Arrays.fill(d, true);
895         Arrays.fill(x, false);
896         assertTrue("Inequal arrays returned true", !Arrays.equals(d, x));
897         Arrays.fill(x, true);
898         assertTrue("equal arrays returned false", Arrays.equals(d, x));
899     }
900 
901     /**
902      * java.util.Arrays#equals(java.lang.Object[], java.lang.Object[])
903      */
test_equals$Ljava_lang_Object$Ljava_lang_Object()904     public void test_equals$Ljava_lang_Object$Ljava_lang_Object() {
905         // Test for method boolean java.util.Arrays.equals(java.lang.Object [],
906         // java.lang.Object [])
907         Object d[] = new Object[1000];
908         Object x[] = new Object[1000];
909         Object o = new Object();
910         Arrays.fill(d, o);
911         Arrays.fill(x, new Object());
912         assertTrue("Inequal arrays returned true", !Arrays.equals(d, x));
913         Arrays.fill(x, o);
914         d[50] = null;
915         x[50] = null;
916         assertTrue("equal arrays returned false", Arrays.equals(d, x));
917     }
918 
919     /**
920      * java.util.Arrays#sort(byte[])
921      */
test_sort$B()922     public void test_sort$B() {
923         // Test for method void java.util.Arrays.sort(byte [])
924         byte[] reversedArray = new byte[arraySize];
925         for (int counter = 0; counter < arraySize; counter++)
926             reversedArray[counter] = (byte) (arraySize - counter - 1);
927         Arrays.sort(reversedArray);
928         for (int counter = 0; counter < arraySize; counter++)
929             assertTrue("Resulting array not sorted",
930                     reversedArray[counter] == (byte) counter);
931     }
932 
933     /**
934      * java.util.Arrays#sort(byte[], int, int)
935      */
test_sort$BII()936     public void test_sort$BII() {
937         // Test for method void java.util.Arrays.sort(byte [], int, int)
938         int startIndex = arraySize / 4;
939         int endIndex = 3 * arraySize / 4;
940         byte[] reversedArray = new byte[arraySize];
941         byte[] originalReversedArray = new byte[arraySize];
942         for (int counter = 0; counter < arraySize; counter++) {
943             reversedArray[counter] = (byte) (arraySize - counter - 1);
944             originalReversedArray[counter] = reversedArray[counter];
945         }
946         Arrays.sort(reversedArray, startIndex, endIndex);
947         for (int counter = 0; counter < startIndex; counter++)
948             assertTrue("Array modified outside of bounds",
949                     reversedArray[counter] == originalReversedArray[counter]);
950         for (int counter = startIndex; counter < endIndex - 1; counter++)
951             assertTrue("Array not sorted within bounds",
952                     reversedArray[counter] <= reversedArray[counter + 1]);
953         for (int counter = endIndex; counter < arraySize; counter++)
954             assertTrue("Array modified outside of bounds",
955                     reversedArray[counter] == originalReversedArray[counter]);
956 
957         //exception testing
958         try {
959             Arrays.sort(reversedArray, startIndex + 1, startIndex);
960             fail("IllegalArgumentException expected");
961         } catch (IllegalArgumentException ignore) {
962         }
963 
964         try {
965             Arrays.sort(reversedArray, -1, startIndex);
966             fail("ArrayIndexOutOfBoundsException expected (1)");
967         } catch (ArrayIndexOutOfBoundsException ignore) {
968         }
969 
970         try {
971             Arrays.sort(reversedArray, startIndex, reversedArray.length + 1);
972             fail("ArrayIndexOutOfBoundsException expected (2)");
973         } catch (ArrayIndexOutOfBoundsException ignore) {
974         }
975     }
976 
977     /**
978      * java.util.Arrays#sort(char[])
979      */
test_sort$C()980     public void test_sort$C() {
981         // Test for method void java.util.Arrays.sort(char [])
982         char[] reversedArray = new char[arraySize];
983         for (int counter = 0; counter < arraySize; counter++)
984             reversedArray[counter] = (char) (arraySize - counter - 1);
985         Arrays.sort(reversedArray);
986         for (int counter = 0; counter < arraySize; counter++)
987             assertTrue("Resulting array not sorted",
988                     reversedArray[counter] == (char) counter);
989 
990     }
991 
992     /**
993      * java.util.Arrays#sort(char[], int, int)
994      */
test_sort$CII()995     public void test_sort$CII() {
996         // Test for method void java.util.Arrays.sort(char [], int, int)
997         int startIndex = arraySize / 4;
998         int endIndex = 3 * arraySize / 4;
999         char[] reversedArray = new char[arraySize];
1000         char[] originalReversedArray = new char[arraySize];
1001         for (int counter = 0; counter < arraySize; counter++) {
1002             reversedArray[counter] = (char) (arraySize - counter - 1);
1003             originalReversedArray[counter] = reversedArray[counter];
1004         }
1005         Arrays.sort(reversedArray, startIndex, endIndex);
1006         for (int counter = 0; counter < startIndex; counter++)
1007             assertTrue("Array modified outside of bounds",
1008                     reversedArray[counter] == originalReversedArray[counter]);
1009         for (int counter = startIndex; counter < endIndex - 1; counter++)
1010             assertTrue("Array not sorted within bounds",
1011                     reversedArray[counter] <= reversedArray[counter + 1]);
1012         for (int counter = endIndex; counter < arraySize; counter++)
1013             assertTrue("Array modified outside of bounds",
1014                     reversedArray[counter] == originalReversedArray[counter]);
1015 
1016         //exception testing
1017         try {
1018             Arrays.sort(reversedArray, startIndex + 1, startIndex);
1019             fail("IllegalArgumentException expected");
1020         } catch (IllegalArgumentException ignore) {
1021         }
1022 
1023         try {
1024             Arrays.sort(reversedArray, -1, startIndex);
1025             fail("ArrayIndexOutOfBoundsException expected (1)");
1026         } catch (ArrayIndexOutOfBoundsException ignore) {
1027         }
1028 
1029         try {
1030             Arrays.sort(reversedArray, startIndex, reversedArray.length + 1);
1031             fail("ArrayIndexOutOfBoundsException expected (2)");
1032         } catch (ArrayIndexOutOfBoundsException ignore) {
1033         }
1034     }
1035 
1036     /**
1037      * java.util.Arrays#sort(double[])
1038      */
test_sort$D()1039     public void test_sort$D() {
1040         // Test for method void java.util.Arrays.sort(double [])
1041         double[] reversedArray = new double[arraySize];
1042         for (int counter = 0; counter < arraySize; counter++)
1043             reversedArray[counter] = (double) (arraySize - counter - 1);
1044         Arrays.sort(reversedArray);
1045         for (int counter = 0; counter < arraySize; counter++)
1046             assertTrue("Resulting array not sorted",
1047                     reversedArray[counter] == (double) counter);
1048 
1049         double[] specials1 = new double[] { Double.NaN, Double.MAX_VALUE,
1050                 Double.MIN_VALUE, 0d, -0d, Double.POSITIVE_INFINITY,
1051                 Double.NEGATIVE_INFINITY };
1052         double[] specials2 = new double[] { 0d, Double.POSITIVE_INFINITY, -0d,
1053                 Double.NEGATIVE_INFINITY, Double.MIN_VALUE, Double.NaN,
1054                 Double.MAX_VALUE };
1055         double[] answer = new double[] { Double.NEGATIVE_INFINITY, -0d, 0d,
1056                 Double.MIN_VALUE, Double.MAX_VALUE, Double.POSITIVE_INFINITY,
1057                 Double.NaN };
1058 
1059         Arrays.sort(specials1);
1060         Object[] print1 = new Object[specials1.length];
1061         for (int i = 0; i < specials1.length; i++)
1062             print1[i] = new Double(specials1[i]);
1063         assertTrue("specials sort incorrectly 1: " + Arrays.asList(print1),
1064                 Arrays.equals(specials1, answer));
1065 
1066         Arrays.sort(specials2);
1067         Object[] print2 = new Object[specials2.length];
1068         for (int i = 0; i < specials2.length; i++)
1069             print2[i] = new Double(specials2[i]);
1070         assertTrue("specials sort incorrectly 2: " + Arrays.asList(print2),
1071                 Arrays.equals(specials2, answer));
1072     }
1073 
1074     /**
1075      * java.util.Arrays#sort(double[], int, int)
1076      */
test_sort$DII()1077     public void test_sort$DII() {
1078         // Test for method void java.util.Arrays.sort(double [], int, int)
1079         int startIndex = arraySize / 4;
1080         int endIndex = 3 * arraySize / 4;
1081         double[] reversedArray = new double[arraySize];
1082         double[] originalReversedArray = new double[arraySize];
1083         for (int counter = 0; counter < arraySize; counter++) {
1084             reversedArray[counter] = (double) (arraySize - counter - 1);
1085             originalReversedArray[counter] = reversedArray[counter];
1086         }
1087         Arrays.sort(reversedArray, startIndex, endIndex);
1088         for (int counter = 0; counter < startIndex; counter++)
1089             assertTrue("Array modified outside of bounds",
1090                     reversedArray[counter] == originalReversedArray[counter]);
1091         for (int counter = startIndex; counter < endIndex - 1; counter++)
1092             assertTrue("Array not sorted within bounds",
1093                     reversedArray[counter] <= reversedArray[counter + 1]);
1094         for (int counter = endIndex; counter < arraySize; counter++)
1095             assertTrue("Array modified outside of bounds",
1096                     reversedArray[counter] == originalReversedArray[counter]);
1097 
1098         //exception testing
1099         try {
1100             Arrays.sort(reversedArray, startIndex + 1, startIndex);
1101             fail("IllegalArgumentException expected");
1102         } catch (IllegalArgumentException ignore) {
1103         }
1104 
1105         try {
1106             Arrays.sort(reversedArray, -1, startIndex);
1107             fail("ArrayIndexOutOfBoundsException expected (1)");
1108         } catch (ArrayIndexOutOfBoundsException ignore) {
1109         }
1110 
1111         try {
1112             Arrays.sort(reversedArray, startIndex, reversedArray.length + 1);
1113             fail("ArrayIndexOutOfBoundsException expected (2)");
1114         } catch (ArrayIndexOutOfBoundsException ignore) {
1115         }
1116     }
1117 
1118     /**
1119      * java.util.Arrays#sort(float[])
1120      */
test_sort$F()1121     public void test_sort$F() {
1122         // Test for method void java.util.Arrays.sort(float [])
1123         float[] reversedArray = new float[arraySize];
1124         for (int counter = 0; counter < arraySize; counter++)
1125             reversedArray[counter] = (float) (arraySize - counter - 1);
1126         Arrays.sort(reversedArray);
1127         for (int counter = 0; counter < arraySize; counter++)
1128             assertTrue("Resulting array not sorted",
1129                     reversedArray[counter] == (float) counter);
1130 
1131         float[] specials1 = new float[] { Float.NaN, Float.MAX_VALUE,
1132                 Float.MIN_VALUE, 0f, -0f, Float.POSITIVE_INFINITY,
1133                 Float.NEGATIVE_INFINITY };
1134         float[] specials2 = new float[] { 0f, Float.POSITIVE_INFINITY, -0f,
1135                 Float.NEGATIVE_INFINITY, Float.MIN_VALUE, Float.NaN,
1136                 Float.MAX_VALUE };
1137         float[] answer = new float[] { Float.NEGATIVE_INFINITY, -0f, 0f,
1138                 Float.MIN_VALUE, Float.MAX_VALUE, Float.POSITIVE_INFINITY,
1139                 Float.NaN };
1140 
1141         Arrays.sort(specials1);
1142         Object[] print1 = new Object[specials1.length];
1143         for (int i = 0; i < specials1.length; i++)
1144             print1[i] = new Float(specials1[i]);
1145         assertTrue("specials sort incorrectly 1: " + Arrays.asList(print1),
1146                 Arrays.equals(specials1, answer));
1147 
1148         Arrays.sort(specials2);
1149         Object[] print2 = new Object[specials2.length];
1150         for (int i = 0; i < specials2.length; i++)
1151             print2[i] = new Float(specials2[i]);
1152         assertTrue("specials sort incorrectly 2: " + Arrays.asList(print2),
1153                 Arrays.equals(specials2, answer));
1154     }
1155 
1156     /**
1157      * java.util.Arrays#sort(float[], int, int)
1158      */
test_sort$FII()1159     public void test_sort$FII() {
1160         // Test for method void java.util.Arrays.sort(float [], int, int)
1161         int startIndex = arraySize / 4;
1162         int endIndex = 3 * arraySize / 4;
1163         float[] reversedArray = new float[arraySize];
1164         float[] originalReversedArray = new float[arraySize];
1165         for (int counter = 0; counter < arraySize; counter++) {
1166             reversedArray[counter] = (float) (arraySize - counter - 1);
1167             originalReversedArray[counter] = reversedArray[counter];
1168         }
1169         Arrays.sort(reversedArray, startIndex, endIndex);
1170         for (int counter = 0; counter < startIndex; counter++)
1171             assertTrue("Array modified outside of bounds",
1172                     reversedArray[counter] == originalReversedArray[counter]);
1173         for (int counter = startIndex; counter < endIndex - 1; counter++)
1174             assertTrue("Array not sorted within bounds",
1175                     reversedArray[counter] <= reversedArray[counter + 1]);
1176         for (int counter = endIndex; counter < arraySize; counter++)
1177             assertTrue("Array modified outside of bounds",
1178                     reversedArray[counter] == originalReversedArray[counter]);
1179 
1180         //exception testing
1181         try {
1182             Arrays.sort(reversedArray, startIndex + 1, startIndex);
1183             fail("IllegalArgumentException expected");
1184         } catch (IllegalArgumentException ignore) {
1185         }
1186 
1187         try {
1188             Arrays.sort(reversedArray, -1, startIndex);
1189             fail("ArrayIndexOutOfBoundsException expected (1)");
1190         } catch (ArrayIndexOutOfBoundsException ignore) {
1191         }
1192 
1193         try {
1194             Arrays.sort(reversedArray, startIndex, reversedArray.length + 1);
1195             fail("ArrayIndexOutOfBoundsException expected (2)");
1196         } catch (ArrayIndexOutOfBoundsException ignore) {
1197         }
1198     }
1199 
1200     /**
1201      * java.util.Arrays#sort(int[])
1202      */
test_sort$I()1203     public void test_sort$I() {
1204         // Test for method void java.util.Arrays.sort(int [])
1205         int[] reversedArray = new int[arraySize];
1206         for (int counter = 0; counter < arraySize; counter++)
1207             reversedArray[counter] = arraySize - counter - 1;
1208         Arrays.sort(reversedArray);
1209         for (int counter = 0; counter < arraySize; counter++)
1210             assertTrue("Resulting array not sorted",
1211                     reversedArray[counter] == counter);
1212     }
1213 
1214     /**
1215      * java.util.Arrays#sort(int[], int, int)
1216      */
test_sort$III()1217     public void test_sort$III() {
1218         // Test for method void java.util.Arrays.sort(int [], int, int)
1219         int startIndex = arraySize / 4;
1220         int endIndex = 3 * arraySize / 4;
1221         int[] reversedArray = new int[arraySize];
1222         int[] originalReversedArray = new int[arraySize];
1223         for (int counter = 0; counter < arraySize; counter++) {
1224             reversedArray[counter] = arraySize - counter - 1;
1225             originalReversedArray[counter] = reversedArray[counter];
1226         }
1227         Arrays.sort(reversedArray, startIndex, endIndex);
1228         for (int counter = 0; counter < startIndex; counter++)
1229             assertTrue("Array modified outside of bounds",
1230                     reversedArray[counter] == originalReversedArray[counter]);
1231         for (int counter = startIndex; counter < endIndex - 1; counter++)
1232             assertTrue("Array not sorted within bounds",
1233                     reversedArray[counter] <= reversedArray[counter + 1]);
1234         for (int counter = endIndex; counter < arraySize; counter++)
1235             assertTrue("Array modified outside of bounds",
1236                     reversedArray[counter] == originalReversedArray[counter]);
1237 
1238         //exception testing
1239         try {
1240             Arrays.sort(reversedArray, startIndex + 1, startIndex);
1241             fail("IllegalArgumentException expected");
1242         } catch (IllegalArgumentException ignore) {
1243         }
1244 
1245         try {
1246             Arrays.sort(reversedArray, -1, startIndex);
1247             fail("ArrayIndexOutOfBoundsException expected (1)");
1248         } catch (ArrayIndexOutOfBoundsException ignore) {
1249         }
1250 
1251         try {
1252             Arrays.sort(reversedArray, startIndex, reversedArray.length + 1);
1253             fail("ArrayIndexOutOfBoundsException expected (2)");
1254         } catch (ArrayIndexOutOfBoundsException ignore) {
1255         }
1256     }
1257 
1258     /**
1259      * java.util.Arrays#sort(long[])
1260      */
test_sort$J()1261     public void test_sort$J() {
1262         // Test for method void java.util.Arrays.sort(long [])
1263         long[] reversedArray = new long[arraySize];
1264         for (int counter = 0; counter < arraySize; counter++)
1265             reversedArray[counter] = (long) (arraySize - counter - 1);
1266         Arrays.sort(reversedArray);
1267         for (int counter = 0; counter < arraySize; counter++)
1268             assertTrue("Resulting array not sorted",
1269                     reversedArray[counter] == (long) counter);
1270 
1271     }
1272 
1273     /**
1274      * java.util.Arrays#sort(long[], int, int)
1275      */
test_sort$JII()1276     public void test_sort$JII() {
1277         // Test for method void java.util.Arrays.sort(long [], int, int)
1278         int startIndex = arraySize / 4;
1279         int endIndex = 3 * arraySize / 4;
1280         long[] reversedArray = new long[arraySize];
1281         long[] originalReversedArray = new long[arraySize];
1282         for (int counter = 0; counter < arraySize; counter++) {
1283             reversedArray[counter] = (long) (arraySize - counter - 1);
1284             originalReversedArray[counter] = reversedArray[counter];
1285         }
1286         Arrays.sort(reversedArray, startIndex, endIndex);
1287         for (int counter = 0; counter < startIndex; counter++)
1288             assertTrue("Array modified outside of bounds",
1289                     reversedArray[counter] == originalReversedArray[counter]);
1290         for (int counter = startIndex; counter < endIndex - 1; counter++)
1291             assertTrue("Array not sorted within bounds",
1292                     reversedArray[counter] <= reversedArray[counter + 1]);
1293         for (int counter = endIndex; counter < arraySize; counter++)
1294             assertTrue("Array modified outside of bounds",
1295                     reversedArray[counter] == originalReversedArray[counter]);
1296 
1297         //exception testing
1298         try {
1299             Arrays.sort(reversedArray, startIndex + 1, startIndex);
1300             fail("IllegalArgumentException expected");
1301         } catch (IllegalArgumentException ignore) {
1302         }
1303 
1304         try {
1305             Arrays.sort(reversedArray, -1, startIndex);
1306             fail("ArrayIndexOutOfBoundsException expected (1)");
1307         } catch (ArrayIndexOutOfBoundsException ignore) {
1308         }
1309 
1310         try {
1311             Arrays.sort(reversedArray, startIndex, reversedArray.length + 1);
1312             fail("ArrayIndexOutOfBoundsException expected (2)");
1313         } catch (ArrayIndexOutOfBoundsException ignore) {
1314         }
1315     }
1316 
1317     /**
1318      * java.util.Arrays#sort(java.lang.Object[])
1319      */
test_sort$Ljava_lang_Object()1320     public void test_sort$Ljava_lang_Object() {
1321         // Test for method void java.util.Arrays.sort(java.lang.Object [])
1322         Object[] reversedArray = new Object[arraySize];
1323         for (int counter = 0; counter < arraySize; counter++)
1324             reversedArray[counter] = objectArray[arraySize - counter - 1];
1325         Arrays.sort(reversedArray);
1326         for (int counter = 0; counter < arraySize; counter++)
1327             assertTrue("Resulting array not sorted",
1328                     reversedArray[counter] == objectArray[counter]);
1329 
1330         Arrays.fill(reversedArray, 0, reversedArray.length/2, "String");
1331         Arrays.fill(reversedArray, reversedArray.length/2, reversedArray.length, new Integer(1));
1332 
1333         try {
1334             Arrays.sort(reversedArray);
1335             fail("ClassCastException expected");
1336         } catch (ClassCastException e) {
1337             //expected
1338         }
1339     }
1340 
1341     /**
1342      * java.util.Arrays#sort(java.lang.Object[], int, int)
1343      */
test_sort$Ljava_lang_ObjectII()1344     public void test_sort$Ljava_lang_ObjectII() {
1345         // Test for method void java.util.Arrays.sort(java.lang.Object [], int,
1346         // int)
1347         int startIndex = arraySize / 4;
1348         int endIndex = 3 * arraySize / 4;
1349         Object[] reversedArray = new Object[arraySize];
1350         Object[] originalReversedArray = new Object[arraySize];
1351         for (int counter = 0; counter < arraySize; counter++) {
1352             reversedArray[counter] = objectArray[arraySize - counter - 1];
1353             originalReversedArray[counter] = reversedArray[counter];
1354         }
1355         Arrays.sort(reversedArray, startIndex, endIndex);
1356         for (int counter = 0; counter < startIndex; counter++)
1357             assertTrue("Array modified outside of bounds",
1358                     reversedArray[counter] == originalReversedArray[counter]);
1359         for (int counter = startIndex; counter < endIndex - 1; counter++)
1360             assertTrue("Array not sorted within bounds",
1361                     ((Comparable) reversedArray[counter])
1362                             .compareTo(reversedArray[counter + 1]) <= 0);
1363         for (int counter = endIndex; counter < arraySize; counter++)
1364             assertTrue("Array modified outside of bounds",
1365                     reversedArray[counter] == originalReversedArray[counter]);
1366 
1367         //exception testing
1368         try {
1369             Arrays.sort(reversedArray, startIndex + 1, startIndex);
1370             fail("IllegalArgumentException expected");
1371         } catch (IllegalArgumentException ignore) {
1372         }
1373 
1374         try {
1375             Arrays.sort(reversedArray, -1, startIndex);
1376             fail("ArrayIndexOutOfBoundsException expected (1)");
1377         } catch (ArrayIndexOutOfBoundsException ignore) {
1378         }
1379 
1380         try {
1381             Arrays.sort(reversedArray, startIndex, reversedArray.length + 1);
1382             fail("ArrayIndexOutOfBoundsException expected (2)");
1383         } catch (ArrayIndexOutOfBoundsException ignore) {
1384         }
1385 
1386         Arrays.fill(reversedArray, 0, reversedArray.length/2, "String");
1387         Arrays.fill(reversedArray, reversedArray.length/2, reversedArray.length, new Integer(1));
1388 
1389         try {
1390             Arrays.sort(reversedArray, reversedArray.length/4, 3*reversedArray.length/4);
1391             fail("ClassCastException expected");
1392         } catch (ClassCastException e) {
1393             //expected
1394         }
1395 
1396         Arrays.sort(reversedArray, 0, reversedArray.length/4);
1397         Arrays.sort(reversedArray, 3*reversedArray.length/4, reversedArray.length);
1398     }
1399 
1400     /**
1401      * java.util.Arrays#sort(java.lang.Object[], int, int,
1402      *        java.util.Comparator)
1403      */
test_sort$Ljava_lang_ObjectIILjava_util_Comparator()1404     public void test_sort$Ljava_lang_ObjectIILjava_util_Comparator() {
1405         // Test for method void java.util.Arrays.sort(java.lang.Object [], int,
1406         // int, java.util.Comparator)
1407         int startIndex = arraySize / 4;
1408         int endIndex = 3 * arraySize / 4;
1409         ReversedIntegerComparator comp = new ReversedIntegerComparator();
1410         Object[] originalArray = new Object[arraySize];
1411         for (int counter = 0; counter < arraySize; counter++)
1412             originalArray[counter] = objectArray[counter];
1413         Arrays.sort(objectArray, startIndex, endIndex, comp);
1414         for (int counter = 0; counter < startIndex; counter++)
1415             assertTrue("Array modified outside of bounds",
1416                     objectArray[counter] == originalArray[counter]);
1417         for (int counter = startIndex; counter < endIndex - 1; counter++)
1418             assertTrue("Array not sorted within bounds", comp.compare(
1419                     objectArray[counter], objectArray[counter + 1]) <= 0);
1420         for (int counter = endIndex; counter < arraySize; counter++)
1421             assertTrue("Array modified outside of bounds",
1422                     objectArray[counter] == originalArray[counter]);
1423 
1424         Arrays.fill(originalArray, 0, originalArray.length/2, "String");
1425         Arrays.fill(originalArray, originalArray.length/2, originalArray.length, new Integer(1));
1426 
1427         try {
1428             Arrays.sort(originalArray, startIndex, endIndex, comp);
1429             fail("ClassCastException expected");
1430         } catch (ClassCastException e) {
1431             //expected
1432         }
1433 
1434         Arrays.sort(originalArray, endIndex, originalArray.length, comp);
1435 
1436         try {
1437             Arrays.sort(originalArray, endIndex, originalArray.length + 1, comp);
1438             fail("ArrayIndexOutOfBoundsException expected");
1439         } catch(ArrayIndexOutOfBoundsException e) {
1440             //expected
1441         }
1442 
1443         try {
1444             Arrays.sort(originalArray, -1, startIndex, comp);
1445             fail("ArrayIndexOutOfBoundsException expected");
1446         } catch(ArrayIndexOutOfBoundsException e) {
1447             //expected
1448         }
1449 
1450         try {
1451             Arrays.sort(originalArray, originalArray.length, endIndex, comp);
1452             fail("IllegalArgumentException expected");
1453         } catch(IllegalArgumentException e) {
1454             //expected
1455         }
1456     }
1457 
1458     /**
1459      * java.util.Arrays#sort(java.lang.Object[], java.util.Comparator)
1460      */
test_sort$Ljava_lang_ObjectLjava_util_Comparator()1461     public void test_sort$Ljava_lang_ObjectLjava_util_Comparator() {
1462         // Test for method void java.util.Arrays.sort(java.lang.Object [],
1463         // java.util.Comparator)
1464         ReversedIntegerComparator comp = new ReversedIntegerComparator();
1465         Arrays.sort(objectArray, comp);
1466         for (int counter = 0; counter < arraySize - 1; counter++)
1467             assertTrue("Array not sorted correctly with custom comparator",
1468                     comp
1469                             .compare(objectArray[counter],
1470                                     objectArray[counter + 1]) <= 0);
1471 
1472         Arrays.fill(objectArray, 0, objectArray.length/2, "String");
1473         Arrays.fill(objectArray, objectArray.length/2, objectArray.length, new Integer(1));
1474 
1475         try {
1476             Arrays.sort(objectArray, comp);
1477             fail("ClassCastException expected");
1478         } catch (ClassCastException e) {
1479             //expected
1480         }
1481     }
1482 
1483     /**
1484      * java.util.Arrays#sort(short[])
1485      */
test_sort$S()1486     public void test_sort$S() {
1487         // Test for method void java.util.Arrays.sort(short [])
1488         short[] reversedArray = new short[arraySize];
1489         for (int counter = 0; counter < arraySize; counter++)
1490             reversedArray[counter] = (short) (arraySize - counter - 1);
1491         Arrays.sort(reversedArray);
1492         for (int counter = 0; counter < arraySize; counter++)
1493             assertTrue("Resulting array not sorted",
1494                     reversedArray[counter] == (short) counter);
1495     }
1496 
1497     /**
1498      * java.util.Arrays#sort(short[], int, int)
1499      */
test_sort$SII()1500     public void test_sort$SII() {
1501         // Test for method void java.util.Arrays.sort(short [], int, int)
1502         int startIndex = arraySize / 4;
1503         int endIndex = 3 * arraySize / 4;
1504         short[] reversedArray = new short[arraySize];
1505         short[] originalReversedArray = new short[arraySize];
1506         for (int counter = 0; counter < arraySize; counter++) {
1507             reversedArray[counter] = (short) (arraySize - counter - 1);
1508             originalReversedArray[counter] = reversedArray[counter];
1509         }
1510         Arrays.sort(reversedArray, startIndex, endIndex);
1511         for (int counter = 0; counter < startIndex; counter++)
1512             assertTrue("Array modified outside of bounds",
1513                     reversedArray[counter] == originalReversedArray[counter]);
1514         for (int counter = startIndex; counter < endIndex - 1; counter++)
1515             assertTrue("Array not sorted within bounds",
1516                     reversedArray[counter] <= reversedArray[counter + 1]);
1517         for (int counter = endIndex; counter < arraySize; counter++)
1518             assertTrue("Array modified outside of bounds",
1519                     reversedArray[counter] == originalReversedArray[counter]);
1520 
1521         //exception testing
1522         try {
1523             Arrays.sort(reversedArray, startIndex + 1, startIndex);
1524             fail("IllegalArgumentException expected");
1525         } catch (IllegalArgumentException ignore) {
1526         }
1527 
1528         try {
1529             Arrays.sort(reversedArray, -1, startIndex);
1530             fail("ArrayIndexOutOfBoundsException expected (1)");
1531         } catch (ArrayIndexOutOfBoundsException ignore) {
1532         }
1533 
1534         try {
1535             Arrays.sort(reversedArray, startIndex, reversedArray.length + 1);
1536             fail("ArrayIndexOutOfBoundsException expected (2)");
1537         } catch (ArrayIndexOutOfBoundsException ignore) {
1538         }
1539     }
1540 
1541     /**
1542      * java.util.Arrays#sort(byte[], int, int)
1543      */
test_java_util_Arrays_sort_byte_array_NPE()1544     public void test_java_util_Arrays_sort_byte_array_NPE() {
1545         byte[] byte_array_null = null;
1546         try {
1547             java.util.Arrays.sort(byte_array_null);
1548             fail("Should throw java.lang.NullPointerException");
1549         } catch (NullPointerException e) {
1550             // Expected
1551         }
1552         try {
1553             // Regression for HARMONY-378
1554             java.util.Arrays.sort(byte_array_null, (int) -1, (int) 1);
1555             fail("Should throw java.lang.NullPointerException");
1556         } catch (NullPointerException e) {
1557             // Expected
1558         }
1559     }
1560 
1561     /**
1562      * java.util.Arrays#sort(char[], int, int)
1563      */
test_java_util_Arrays_sort_char_array_NPE()1564     public void test_java_util_Arrays_sort_char_array_NPE() {
1565         char[] char_array_null = null;
1566         try {
1567             java.util.Arrays.sort(char_array_null);
1568             fail("Should throw java.lang.NullPointerException");
1569         } catch (NullPointerException e) {
1570             // Expected
1571         }
1572         try {
1573             // Regression for HARMONY-378
1574             java.util.Arrays.sort(char_array_null, (int) -1, (int) 1);
1575             fail("Should throw java.lang.NullPointerException");
1576         } catch (NullPointerException e) {
1577             // Expected
1578         }
1579     }
1580 
1581     /**
1582      * java.util.Arrays#sort(double[], int, int)
1583      */
test_java_util_Arrays_sort_double_array_NPE()1584     public void test_java_util_Arrays_sort_double_array_NPE() {
1585         double[] double_array_null = null;
1586         try {
1587             java.util.Arrays.sort(double_array_null);
1588             fail("Should throw java.lang.NullPointerException");
1589         } catch (NullPointerException e) {
1590             // Expected
1591         }
1592         try {
1593             // Regression for HARMONY-378
1594             java.util.Arrays.sort(double_array_null, (int) -1, (int) 1);
1595             fail("Should throw java.lang.NullPointerException");
1596         } catch (NullPointerException e) {
1597             // Expected
1598         }
1599     }
1600 
1601     /**
1602      * java.util.Arrays#sort(float[], int, int)
1603      */
test_java_util_Arrays_sort_float_array_NPE()1604     public void test_java_util_Arrays_sort_float_array_NPE() {
1605         float[] float_array_null = null;
1606         try {
1607             java.util.Arrays.sort(float_array_null);
1608             fail("Should throw java.lang.NullPointerException");
1609         } catch (NullPointerException e) {
1610             // Expected
1611         }
1612         try {
1613             // Regression for HARMONY-378
1614             java.util.Arrays.sort(float_array_null, (int) -1, (int) 1);
1615             fail("Should throw java.lang.NullPointerException");
1616         } catch (NullPointerException e) {
1617             // Expected
1618         }
1619     }
1620 
1621     /**
1622      * java.util.Arrays#sort(int[], int, int)
1623      */
test_java_util_Arrays_sort_int_array_NPE()1624     public void test_java_util_Arrays_sort_int_array_NPE() {
1625         int[] int_array_null = null;
1626         try {
1627             java.util.Arrays.sort(int_array_null);
1628             fail("Should throw java.lang.NullPointerException");
1629         } catch (NullPointerException e) {
1630             // Expected
1631         }
1632         try {
1633             // Regression for HARMONY-378
1634             java.util.Arrays.sort(int_array_null, (int) -1, (int) 1);
1635             fail("Should throw java.lang.NullPointerException");
1636         } catch (NullPointerException e) {
1637             // Expected
1638         }
1639     }
1640 
1641     /**
1642      * java.util.Arrays#sort(Object[], int, int)
1643      */
test_java_util_Arrays_sort_object_array_NPE()1644     public void test_java_util_Arrays_sort_object_array_NPE() {
1645         Object[] object_array_null = null;
1646         try {
1647             java.util.Arrays.sort(object_array_null);
1648             fail("Should throw java.lang.NullPointerException");
1649         } catch (NullPointerException e) {
1650             // Expected
1651         }
1652         try {
1653             // Regression for HARMONY-378
1654             java.util.Arrays.sort(object_array_null, (int) -1, (int) 1);
1655             fail("Should throw java.lang.NullPointerException");
1656         } catch (NullPointerException e) {
1657             // Expected
1658         }
1659         try {
1660             // Regression for HARMONY-378
1661             java.util.Arrays.sort(object_array_null, (int) -1, (int) 1, null);
1662             fail("Should throw java.lang.NullPointerException");
1663         } catch (NullPointerException e) {
1664             // Expected
1665         }
1666     }
1667 
1668     /**
1669      * java.util.Arrays#sort(long[], int, int)
1670      */
test_java_util_Arrays_sort_long_array_NPE()1671     public void test_java_util_Arrays_sort_long_array_NPE() {
1672         long[] long_array_null = null;
1673         try {
1674             java.util.Arrays.sort(long_array_null);
1675             fail("Should throw java.lang.NullPointerException");
1676         } catch (NullPointerException e) {
1677             // Expected
1678         }
1679         try {
1680             // Regression for HARMONY-378
1681             java.util.Arrays.sort(long_array_null, (int) -1, (int) 1);
1682             fail("Should throw java.lang.NullPointerException");
1683         } catch (NullPointerException e) {
1684             // Expected
1685         }
1686     }
1687 
1688     /**
1689      * java.util.Arrays#sort(short[], int, int)
1690      */
test_java_util_Arrays_sort_short_array_NPE()1691     public void test_java_util_Arrays_sort_short_array_NPE() {
1692         short[] short_array_null = null;
1693         try {
1694             java.util.Arrays.sort(short_array_null);
1695             fail("Should throw java.lang.NullPointerException");
1696         } catch (NullPointerException e) {
1697             // Expected
1698         }
1699         try {
1700             // Regression for HARMONY-378
1701             java.util.Arrays.sort(short_array_null, (int) -1, (int) 1);
1702             fail("Should throw java.lang.NullPointerException");
1703         } catch (NullPointerException e) {
1704             // Expected
1705         }
1706     }
1707 
1708     // Lenghts of arrays to test in test_sort;
1709     private static final int[] LENGTHS = { 0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 100, 1000, 10000 };
1710 
1711     /**
1712      * java.util.Arrays#sort()
1713      */
test_sort()1714     public void test_sort() {
1715         for (int len : LENGTHS) {
1716             PrimitiveTypeArrayBuilder.reset();
1717             int[] golden = new int[len];
1718             for (int m = 1; m < 2 * len; m *= 2) {
1719                 for (PrimitiveTypeArrayBuilder builder : PrimitiveTypeArrayBuilder.values()) {
1720                     builder.build(golden, m);
1721                     int[] test = golden.clone();
1722 
1723                     for (PrimitiveTypeConverter converter : PrimitiveTypeConverter.values()) {
1724                         Object convertedGolden = converter.convert(golden);
1725                         Object convertedTest = converter.convert(test);
1726                         sort(convertedTest);
1727                         checkSorted(convertedTest);
1728                         assertEquals(checkSum(convertedGolden), checkSum(convertedTest));
1729                     }
1730                 }
1731             }
1732         }
1733     }
1734 
sort(Object array)1735     private void sort(Object array) {
1736         if (array instanceof int[]) {
1737             Arrays.sort((int[]) array);
1738         }
1739         else if (array instanceof long[]) {
1740             Arrays.sort((long[]) array);
1741         } else if (array instanceof short[]) {
1742             Arrays.sort((short[]) array);
1743         } else if (array instanceof byte[]) {
1744             Arrays.sort((byte[]) array);
1745         } else if (array instanceof char[]) {
1746             Arrays.sort((char[]) array);
1747         } else if (array instanceof float[]) {
1748             Arrays.sort((float[]) array);
1749         } else if (array instanceof double[]) {
1750             Arrays.sort((double[]) array);
1751         } else {
1752             fail("Unknow type of array: " + array.getClass());
1753         }
1754     }
1755 
checkSorted(Object array)1756     private void checkSorted(Object array) {
1757         if (array instanceof int[]) {
1758             checkSorted((int[]) array);
1759         } else if (array instanceof long[]) {
1760             checkSorted((long[]) array);
1761         } else if (array instanceof short[]) {
1762             checkSorted((short[]) array);
1763         } else if (array instanceof byte[]) {
1764             checkSorted((byte[]) array);
1765         } else if (array instanceof char[]) {
1766             checkSorted((char[]) array);
1767         } else if (array instanceof float[]) {
1768             checkSorted((float[]) array);
1769         } else if (array instanceof double[]) {
1770             checkSorted((double[]) array);
1771         } else {
1772             fail("Unknow type of array: " + array.getClass());
1773         }
1774     }
1775 
checkSorted(int[] a)1776     private void checkSorted(int[] a) {
1777         for (int i = 0; i < a.length - 1; i++) {
1778             if (a[i] > a[i + 1]) {
1779                 orderFail(i, "" + a[i], "" + a[i + 1]);
1780             }
1781         }
1782     }
1783 
checkSorted(long[] a)1784     private void checkSorted(long[] a) {
1785         for (int i = 0; i < a.length - 1; i++) {
1786             if (a[i] > a[i + 1]) {
1787                 orderFail(i, "" + a[i], "" + a[i + 1]);
1788             }
1789         }
1790     }
1791 
checkSorted(short[] a)1792     private void checkSorted(short[] a) {
1793         for (int i = 0; i < a.length - 1; i++) {
1794             if (a[i] > a[i + 1]) {
1795                 orderFail(i, "" + a[i], "" + a[i + 1]);
1796             }
1797         }
1798     }
1799 
checkSorted(byte[] a)1800     private void checkSorted(byte[] a) {
1801         for (int i = 0; i < a.length - 1; i++) {
1802             if (a[i] > a[i + 1]) {
1803                 orderFail(i, "" + a[i], "" + a[i + 1]);
1804             }
1805         }
1806     }
1807 
checkSorted(char[] a)1808     private void checkSorted(char[] a) {
1809         for (int i = 0; i < a.length - 1; i++) {
1810             if (a[i] > a[i + 1]) {
1811                 orderFail(i, "" + a[i], "" + a[i + 1]);
1812             }
1813         }
1814     }
1815 
checkSorted(float[] a)1816     private void checkSorted(float[] a) {
1817         for (int i = 0; i < a.length - 1; i++) {
1818             if (a[i] > a[i + 1]) {
1819                 orderFail(i, "" + a[i], "" + a[i + 1]);
1820             }
1821         }
1822     }
1823 
checkSorted(double[] a)1824     private void checkSorted(double[] a) {
1825         for (int i = 0; i < a.length - 1; i++) {
1826             if (a[i] > a[i + 1]) {
1827                 orderFail(i, "" + a[i], "" + a[i + 1]);
1828             }
1829         }
1830     }
1831 
1832 
orderFail(int index, String value1, String value2)1833     private void orderFail(int index, String value1, String value2) {
1834         fail("Array is not sorted at " + index + "-th position: " + value1 + " and " + value2);
1835     }
1836 
checkSum(Object array)1837     private int checkSum(Object array) {
1838         if (array instanceof int[]) {
1839             return checkSum((int[]) array);
1840         } else if (array instanceof long[]) {
1841             return checkSum((long[]) array);
1842         } else if (array instanceof short[]) {
1843             return checkSum((short[]) array);
1844         } else if (array instanceof byte[]) {
1845             return checkSum((byte[]) array);
1846         } else if (array instanceof char[]) {
1847             return checkSum((char[]) array);
1848         } else if (array instanceof float[]) {
1849             return checkSum((float[]) array);
1850         } else if (array instanceof double[]) {
1851             return checkSum((double[]) array);
1852         } else {
1853             fail("Unknow type of array: " + array.getClass());
1854         }
1855         throw new AssertionError(); // Needed to shut up compiler
1856     }
1857 
checkSum(int[] a)1858     private int checkSum(int[] a) {
1859         int checkSum = 0;
1860 
1861         for (int e : a) {
1862             checkSum ^= e; // xor
1863         }
1864         return checkSum;
1865     }
1866 
checkSum(long[] a)1867     private int checkSum(long[] a) {
1868         long checkSum = 0;
1869 
1870         for (long e : a) {
1871             checkSum ^= e; // xor
1872         }
1873         return (int) checkSum;
1874     }
1875 
checkSum(short[] a)1876     private int checkSum(short[] a) {
1877         short checkSum = 0;
1878 
1879         for (short e : a) {
1880             checkSum ^= e; // xor
1881         }
1882         return (int) checkSum;
1883     }
1884 
checkSum(byte[] a)1885     private int checkSum(byte[] a) {
1886         byte checkSum = 0;
1887 
1888         for (byte e : a) {
1889             checkSum ^= e; // xor
1890         }
1891         return (int) checkSum;
1892     }
1893 
checkSum(char[] a)1894     private int checkSum(char[] a) {
1895         char checkSum = 0;
1896 
1897         for (char e : a) {
1898             checkSum ^= e; // xor
1899         }
1900         return (int) checkSum;
1901     }
1902 
checkSum(float[] a)1903     private int checkSum(float[] a) {
1904         int checkSum = 0;
1905 
1906         for (float e : a) {
1907             checkSum ^= (int) e; // xor
1908         }
1909         return checkSum;
1910     }
1911 
checkSum(double[] a)1912     private int checkSum(double[] a) {
1913         int checkSum = 0;
1914 
1915         for (double e : a) {
1916             checkSum ^= (int) e; // xor
1917         }
1918         return checkSum;
1919     }
1920 
1921     private enum PrimitiveTypeArrayBuilder {
1922 
1923         RANDOM {
build(int[] a, int m)1924             void build(int[] a, int m) {
1925                 for (int i = 0; i < a.length; i++) {
1926                     a[i] = ourRandom.nextInt();
1927                 }
1928             }
1929         },
1930 
1931         ASCENDING {
build(int[] a, int m)1932             void build(int[] a, int m) {
1933                 for (int i = 0; i < a.length; i++) {
1934                     a[i] = m + i;
1935                 }
1936             }
1937         },
1938 
1939         DESCENDING {
build(int[] a, int m)1940             void build(int[] a, int m) {
1941                 for (int i = 0; i < a.length; i++) {
1942                     a[i] = a.length - m - i;
1943                 }
1944             }
1945         },
1946 
1947         ALL_EQUAL {
build(int[] a, int m)1948             void build(int[] a, int m) {
1949                 for (int i = 0; i < a.length; i++) {
1950                     a[i] = m;
1951                 }
1952             }
1953         },
1954 
1955         SAW {
build(int[] a, int m)1956             void build(int[] a, int m) {
1957                 int incCount = 1;
1958                 int decCount = a.length;
1959                 int i = 0;
1960                 int period = m;
1961                 m--;
1962 
1963                 while (true) {
1964                     for (int k = 1; k <= period; k++) {
1965                         if (i >= a.length) {
1966                             return;
1967                         }
1968                         a[i++] = incCount++;
1969                     }
1970                     period += m;
1971 
1972                     for (int k = 1; k <= period; k++) {
1973                         if (i >= a.length) {
1974                             return;
1975                         }
1976                         a[i++] = decCount--;
1977                     }
1978                     period += m;
1979                 }
1980             }
1981         },
1982 
1983         REPEATED {
build(int[] a, int m)1984             void build(int[] a, int m) {
1985                 for (int i = 0; i < a.length; i++) {
1986                     a[i] = i % m;
1987                 }
1988             }
1989         },
1990 
1991         DUPLICATED {
build(int[] a, int m)1992             void build(int[] a, int m) {
1993                 for (int i = 0; i < a.length; i++) {
1994                     a[i] = ourRandom.nextInt(m);
1995                 }
1996             }
1997         },
1998 
1999         ORGAN_PIPES {
build(int[] a, int m)2000             void build(int[] a, int m) {
2001                 int middle = a.length / (m + 1);
2002 
2003                 for (int i = 0; i < middle; i++) {
2004                     a[i] = i;
2005                 }
2006                 for (int i = middle; i < a.length ; i++) {
2007                     a[i] = a.length - i - 1;
2008                 }
2009             }
2010         },
2011 
2012         STAGGER {
build(int[] a, int m)2013             void build(int[] a, int m) {
2014                 for (int i = 0; i < a.length; i++) {
2015                     a[i] = (i * m + i) % a.length;
2016                 }
2017             }
2018         },
2019 
2020         PLATEAU {
build(int[] a, int m)2021             void build(int[] a, int m) {
2022                 for (int i = 0; i < a.length; i++) {
2023                     a[i] =  Math.min(i, m);
2024                 }
2025             }
2026         },
2027 
2028         SHUFFLE {
build(int[] a, int m)2029             void build(int[] a, int m) {
2030                 for (int i = 0; i < a.length; i++) {
2031                     a[i] = ourRandom.nextBoolean() ? (ourFirst += 2) : (ourSecond += 2);
2032                 }
2033             }
2034         };
2035 
build(int[] a, int m)2036         abstract void build(int[] a, int m);
2037 
reset()2038         static void reset() {
2039             ourRandom = new Random(666);
2040             ourFirst = 0;
2041             ourSecond = 0;
2042         }
2043 
2044         @Override
toString()2045         public String toString() {
2046             String name = name();
2047 
2048             for (int i = name.length(); i < 12; i++) {
2049                 name += " " ;
2050             }
2051             return name;
2052         }
2053 
2054         private static int ourFirst;
2055         private static int ourSecond;
2056         private static Random ourRandom = new Random(666);
2057     }
2058 
2059     private enum PrimitiveTypeConverter {
2060 
2061         INT {
convert(int[] a)2062             Object convert(int[] a) {
2063                 return a;
2064             }
2065         },
2066 
2067         LONG {
convert(int[] a)2068             Object convert(int[] a) {
2069                 long[] b = new long[a.length];
2070 
2071                 for (int i = 0; i < a.length; i++) {
2072                     b[i] = (int) a[i];
2073                 }
2074                 return b;
2075             }
2076         },
2077 
2078         BYTE {
convert(int[] a)2079             Object convert(int[] a) {
2080                 byte[] b = new byte[a.length];
2081 
2082                 for (int i = 0; i < a.length; i++) {
2083                     b[i] = (byte) a[i];
2084                 }
2085                 return b;
2086             }
2087         },
2088 
2089         SHORT {
convert(int[] a)2090             Object convert(int[] a) {
2091                 short[] b = new short[a.length];
2092 
2093                 for (int i = 0; i < a.length; i++) {
2094                     b[i] = (short) a[i];
2095                 }
2096                 return b;
2097             }
2098         },
2099 
2100         CHAR {
convert(int[] a)2101             Object convert(int[] a) {
2102                 char[] b = new char[a.length];
2103 
2104                 for (int i = 0; i < a.length; i++) {
2105                     b[i] = (char) a[i];
2106                 }
2107                 return b;
2108             }
2109         },
2110 
2111         FLOAT {
convert(int[] a)2112             Object convert(int[] a) {
2113                 float[] b = new float[a.length];
2114 
2115                 for (int i = 0; i < a.length; i++) {
2116                     b[i] = (float) a[i];
2117                 }
2118                 return b;
2119             }
2120         },
2121 
2122         DOUBLE {
convert(int[] a)2123             Object convert(int[] a) {
2124                 double[] b = new double[a.length];
2125 
2126                 for (int i = 0; i < a.length; i++) {
2127                     b[i] = (double) a[i];
2128                 }
2129                 return b;
2130             }
2131         };
2132 
convert(int[] a)2133         abstract Object convert(int[] a);
2134 
toString()2135         public String toString() {
2136             String name = name();
2137 
2138             for (int i = name.length(); i < 9; i++) {
2139                 name += " " ;
2140             }
2141             return name;
2142         }
2143     }
2144 
2145 
2146     /**
2147      * java.util.Arrays#deepEquals(Object[], Object[])
2148      */
test_deepEquals$Ljava_lang_ObjectLjava_lang_Object()2149     public void test_deepEquals$Ljava_lang_ObjectLjava_lang_Object() {
2150        int [] a1 = {1, 2, 3};
2151        short [] a2 = {0, 1};
2152        Object [] a3 = {new Integer(1), a2};
2153        int [] a4 = {6, 5, 4};
2154 
2155        int [] b1 = {1, 2, 3};
2156        short [] b2 = {0, 1};
2157        Object [] b3 = {new Integer(1), b2};
2158 
2159        Object a [] = {a1, a2, a3};
2160        Object b [] = {b1, b2, b3};
2161 
2162        assertFalse(Arrays.equals(a, b));
2163        assertTrue(Arrays.deepEquals(a,b));
2164 
2165        a[2] = a4;
2166 
2167        assertFalse(Arrays.deepEquals(a, b));
2168     }
2169 
2170     /**
2171      * java.util.Arrays#deepHashCode(Object[])
2172      */
test_deepHashCode$Ljava_lang_Object()2173     public void test_deepHashCode$Ljava_lang_Object() {
2174         int [] a1 = {1, 2, 3};
2175         short [] a2 = {0, 1};
2176         Object [] a3 = {new Integer(1), a2};
2177 
2178         int [] b1 = {1, 2, 3};
2179         short [] b2 = {0, 1};
2180         Object [] b3 = {new Integer(1), b2};
2181 
2182         Object a [] = {a1, a2, a3};
2183         Object b [] = {b1, b2, b3};
2184 
2185         int deep_hash_a = Arrays.deepHashCode(a);
2186         int deep_hash_b = Arrays.deepHashCode(b);
2187 
2188         assertEquals(deep_hash_a, deep_hash_b);
2189      }
2190 
2191     /**
2192      * java.util.Arrays#hashCode(boolean[] a)
2193      */
test_hashCode$LZ()2194     public void test_hashCode$LZ() {
2195         int listHashCode;
2196         int arrayHashCode;
2197 
2198         boolean [] boolArr = {true, false, false, true, false};
2199         List listOfBoolean = new LinkedList();
2200         for (int i = 0; i < boolArr.length; i++) {
2201             listOfBoolean.add(new Boolean(boolArr[i]));
2202         }
2203         listHashCode = listOfBoolean.hashCode();
2204         arrayHashCode = Arrays.hashCode(boolArr);
2205         assertEquals(listHashCode, arrayHashCode);
2206     }
2207 
2208     /**
2209      * java.util.Arrays#hashCode(int[] a)
2210      */
test_hashCode$LI()2211     public void test_hashCode$LI() {
2212         int listHashCode;
2213         int arrayHashCode;
2214 
2215         int [] intArr = {10, 5, 134, 7, 19};
2216         List listOfInteger = new LinkedList();
2217 
2218         for (int i = 0; i < intArr.length; i++) {
2219             listOfInteger.add(new Integer(intArr[i]));
2220         }
2221         listHashCode = listOfInteger.hashCode();
2222         arrayHashCode = Arrays.hashCode(intArr);
2223         assertEquals(listHashCode, arrayHashCode);
2224 
2225         int [] intArr2 = {10, 5, 134, 7, 19};
2226         assertEquals(Arrays.hashCode(intArr2), Arrays.hashCode(intArr));
2227     }
2228 
2229     /**
2230      * java.util.Arrays#hashCode(char[] a)
2231      */
test_hashCode$LC()2232     public void test_hashCode$LC() {
2233         int listHashCode;
2234         int arrayHashCode;
2235 
2236         char [] charArr = {'a', 'g', 'x', 'c', 'm'};
2237         List listOfCharacter = new LinkedList();
2238         for (int i = 0; i < charArr.length; i++) {
2239             listOfCharacter.add(new Character(charArr[i]));
2240         }
2241         listHashCode = listOfCharacter.hashCode();
2242         arrayHashCode = Arrays.hashCode(charArr);
2243         assertEquals(listHashCode, arrayHashCode);
2244     }
2245 
2246     /**
2247      * java.util.Arrays#hashCode(byte[] a)
2248      */
test_hashCode$LB()2249     public void test_hashCode$LB() {
2250         int listHashCode;
2251         int arrayHashCode;
2252 
2253         byte [] byteArr = {5, 9, 7, 6, 17};
2254         List listOfByte = new LinkedList();
2255         for (int i = 0; i < byteArr.length; i++) {
2256             listOfByte.add(new Byte(byteArr[i]));
2257         }
2258         listHashCode = listOfByte.hashCode();
2259         arrayHashCode = Arrays.hashCode(byteArr);
2260         assertEquals(listHashCode, arrayHashCode);
2261     }
2262 
2263     /**
2264      * java.util.Arrays#hashCode(long[] a)
2265      */
test_hashCode$LJ()2266     public void test_hashCode$LJ() {
2267         int listHashCode;
2268         int arrayHashCode;
2269 
2270         long [] longArr = {67890234512l, 97587236923425l, 257421912912l,
2271                 6754268100l, 5};
2272         List listOfLong = new LinkedList();
2273         for (int i = 0; i < longArr.length; i++) {
2274             listOfLong.add(new Long(longArr[i]));
2275         }
2276         listHashCode = listOfLong.hashCode();
2277         arrayHashCode = Arrays.hashCode(longArr);
2278         assertEquals(listHashCode, arrayHashCode);
2279     }
2280 
2281     /**
2282      * java.util.Arrays#hashCode(float[] a)
2283      */
test_hashCode$LF()2284     public void test_hashCode$LF() {
2285         int listHashCode;
2286         int arrayHashCode;
2287 
2288         float [] floatArr = {0.13497f, 0.268934f, 12e-5f, -3e+2f, 10e-4f};
2289         List listOfFloat = new LinkedList();
2290         for (int i = 0; i < floatArr.length; i++) {
2291             listOfFloat.add(new Float(floatArr[i]));
2292         }
2293         listHashCode = listOfFloat.hashCode();
2294         arrayHashCode = Arrays.hashCode(floatArr);
2295         assertEquals(listHashCode, arrayHashCode);
2296 
2297         float [] floatArr2 = {0.13497f, 0.268934f, 12e-5f, -3e+2f, 10e-4f};
2298         assertEquals(Arrays.hashCode(floatArr2), Arrays.hashCode(floatArr));
2299     }
2300 
2301     /**
2302      * java.util.Arrays#hashCode(double[] a)
2303      */
test_hashCode$LD()2304     public void test_hashCode$LD() {
2305         int listHashCode;
2306         int arrayHashCode;
2307 
2308         double [] doubleArr = {0.134945657, 0.0038754, 11e-150, -30e-300, 10e-4};
2309         List listOfDouble = new LinkedList();
2310         for (int i = 0; i < doubleArr.length; i++) {
2311             listOfDouble.add(new Double(doubleArr[i]));
2312         }
2313         listHashCode = listOfDouble.hashCode();
2314         arrayHashCode = Arrays.hashCode(doubleArr);
2315         assertEquals(listHashCode, arrayHashCode);
2316     }
2317 
2318     /**
2319      * java.util.Arrays#hashCode(short[] a)
2320      */
test_hashCode$LS()2321     public void test_hashCode$LS() {
2322         int listHashCode;
2323         int arrayHashCode;
2324 
2325         short [] shortArr = {35, 13, 45, 2, 91};
2326         List listOfShort = new LinkedList();
2327         for (int i = 0; i < shortArr.length; i++) {
2328             listOfShort.add(new Short(shortArr[i]));
2329         }
2330         listHashCode = listOfShort.hashCode();
2331         arrayHashCode = Arrays.hashCode(shortArr);
2332         assertEquals(listHashCode, arrayHashCode);
2333     }
2334 
2335     /**
2336      * java.util.Arrays#hashCode(Object[] a)
2337      */
test_hashCode$Ljava_lang_Object()2338     public void test_hashCode$Ljava_lang_Object() {
2339         int listHashCode;
2340         int arrayHashCode;
2341 
2342         Object[] objectArr = {new Integer(1), new Float(10e-12f), null};
2343         List listOfObject= new LinkedList();
2344         for (int i = 0; i < objectArr.length; i++) {
2345             listOfObject.add(objectArr[i]);
2346         }
2347         listHashCode = listOfObject.hashCode();
2348         arrayHashCode = Arrays.hashCode(objectArr);
2349         assertEquals(listHashCode, arrayHashCode);
2350     }
2351 
2352     /**
2353      * Sets up the fixture, for example, open a network connection. This method
2354      * is called before a test is executed.
2355      */
setUp()2356     protected void setUp() {
2357         objArray = new Object[arraySize];
2358         for (int i = 0; i < objArray.length; i++)
2359             objArray[i] = new Integer(i);
2360 
2361         booleanArray = new boolean[arraySize];
2362         byteArray = new byte[arraySize];
2363         charArray = new char[arraySize];
2364         doubleArray = new double[arraySize];
2365         floatArray = new float[arraySize];
2366         intArray = new int[arraySize];
2367         longArray = new long[arraySize];
2368         objectArray = new Object[arraySize];
2369         shortArray = new short[arraySize];
2370 
2371         for (int counter = 0; counter < arraySize; counter++) {
2372             byteArray[counter] = (byte) counter;
2373             charArray[counter] = (char) (counter + 1);
2374             doubleArray[counter] = counter;
2375             floatArray[counter] = counter;
2376             intArray[counter] = counter;
2377             longArray[counter] = counter;
2378             objectArray[counter] = objArray[counter];
2379             shortArray[counter] = (short) counter;
2380         }
2381         for (int counter = 0; counter < arraySize; counter += 2) {
2382             booleanArray[counter] = false;
2383             booleanArray[counter + 1] = true;
2384         }
2385     }
2386 
2387     /**
2388      * Tears down the fixture, for example, close a network connection. This
2389      * method is called after a test is executed.
2390      */
tearDown()2391     protected void tearDown() {
2392         objArray = null;
2393         booleanArray = null;
2394         byteArray = null;
2395         charArray = null;
2396         doubleArray = null;
2397         floatArray = null;
2398         intArray = null;
2399         longArray = null;
2400         objectArray = null;
2401         shortArray = null;
2402     }
2403 }
2404