Array.java revision 5d1ac920fdaef5d4ec8f66bb734488cd9660b024
1// Copyright 2008 The Android Open Source Project
2
3
4/**
5 * Exercise arrays.
6 */
7public class Array {
8
9    /*
10     * Verify array contents.
11     */
12    static void checkBytes(byte[] bytes) {
13        assert(bytes[0] == 0);
14        assert(bytes[1] == -1);
15        assert(bytes[2] == -2);
16        assert(bytes[3] == -3);
17        assert(bytes[4] == -4);
18    }
19    static void checkShorts(short[] shorts) {
20        assert(shorts[0] == 20);
21        assert(shorts[1] == 10);
22        assert(shorts[2] == 0);
23        assert(shorts[3] == -10);
24        assert(shorts[4] == -20);
25    }
26    static void checkChars(char[] chars) {
27        assert(chars[0] == 40000);
28        assert(chars[1] == 40001);
29        assert(chars[2] == 40002);
30        assert(chars[3] == 40003);
31        assert(chars[4] == 40004);
32    }
33    static void checkInts(int[] ints) {
34        assert(ints[0] == 70000);
35        assert(ints[1] == 70001);
36        assert(ints[2] == 70002);
37        assert(ints[3] == 70003);
38        assert(ints[4] == 70004);
39    }
40    static void checkBooleans(boolean[] booleans) {
41        assert(booleans[0]);
42        assert(booleans[1]);
43        assert(!booleans[2]);
44        assert(booleans[3]);
45        assert(!booleans[4]);
46    }
47    static void checkFloats(float[] floats) {
48        assert(floats[0] == -1.5);
49        assert(floats[1] == -0.5);
50        assert(floats[2] == 0.0);
51        assert(floats[3] == 0.5);
52        assert(floats[4] == 1.5);
53    }
54    static void checkLongs(long[] longs) {
55        assert(longs[0] == 0x1122334455667788L);
56        assert(longs[1] == 0x8877665544332211L);
57        assert(longs[2] == 0L);
58        assert(longs[3] == 1L);
59        assert(longs[4] == -1L);
60    }
61    static void checkStrings(String[] strings) {
62        assert(strings[0].equals("zero"));
63        assert(strings[1].equals("one"));
64        assert(strings[2].equals("two"));
65        assert(strings[3].equals("three"));
66        assert(strings[4].equals("four"));
67    }
68
69    /*
70     * Try bad range values, 32 bit get/put.
71     */
72    static void checkRange32(int[] ints, int[] empty, int negVal1, int negVal2){
73        System.out.println("Array.checkRange32");
74        int i = 0;
75
76        assert(ints.length == 5);
77
78        try {
79            i = ints[5];            // exact bound
80            assert(false);
81        } catch (ArrayIndexOutOfBoundsException aioobe) {
82            // good
83        }
84        try {
85            ints[5] = i;            // exact bound
86            assert(false);
87        } catch (ArrayIndexOutOfBoundsException aioobe) {
88            // good
89        }
90        try {
91            i = ints[6];            // one past
92            assert(false);
93        } catch (ArrayIndexOutOfBoundsException aioobe) {
94            // good
95        }
96        try {
97            i = ints[negVal1];      // -1
98            assert(false);
99        } catch (ArrayIndexOutOfBoundsException aioobe) {
100            // good
101        }
102        try {
103            ints[negVal1] = i;      // -1
104            assert(false);
105        } catch (ArrayIndexOutOfBoundsException aioobe) {
106            // good
107        }
108        try {
109            i = ints[negVal2];      // min int
110            assert(false);
111        } catch (ArrayIndexOutOfBoundsException aioobe) {
112            // good
113        }
114
115
116        try {
117            i = empty[1];
118            assert(false);
119        } catch (ArrayIndexOutOfBoundsException aioobe) {
120            // good
121        }
122    }
123
124    /*
125     * Try bad range values, 64 bit get/put.
126     */
127    static void checkRange64(long[] longs, int negVal1, int negVal2) {
128        System.out.println("Array.checkRange64");
129        long l = 0L;
130
131        assert(longs.length == 5);
132
133        try {
134            l = longs[5];            // exact bound
135            assert(false);
136        } catch (ArrayIndexOutOfBoundsException aioobe) {
137            // good
138        }
139        try {
140            longs[5] = l;            // exact bound
141            assert(false);
142        } catch (ArrayIndexOutOfBoundsException aioobe) {
143            // good
144        }
145        try {
146            l = longs[6];            // one past
147            assert(false);
148        } catch (ArrayIndexOutOfBoundsException aioobe) {
149            // good
150        }
151        try {
152            l = longs[negVal1];      // -1
153            assert(false);
154        } catch (ArrayIndexOutOfBoundsException aioobe) {
155            // good
156        }
157        try {
158            longs[negVal1] = l;      // -1
159            assert(false);
160        } catch (ArrayIndexOutOfBoundsException aioobe) {
161            // good
162        }
163        try {
164            l = longs[negVal2];      // min int
165            assert(false);
166        } catch (ArrayIndexOutOfBoundsException aioobe) {
167            // good
168        }
169    }
170
171    /*
172     * Test negative allocations of object and primitive arrays.
173     */
174    static void checkNegAlloc(int count) {
175        System.out.println("Array.checkNegAlloc");
176        String[] strings;
177        int[] ints;
178
179        try {
180            ints = new int[count];
181            assert(false);
182        } catch (NegativeArraySizeException nase) {
183            // good
184        }
185
186        try {
187            strings = new String[count];
188            assert(false);
189        } catch (NegativeArraySizeException nase) {
190            // good
191        }
192    }
193
194    public static void run() {
195        System.out.println("Array check...");
196
197        byte[] xBytes = new byte[] { 0, -1, -2, -3, -4 };
198        short[] xShorts = new short[] { 20, 10, 0, -10, -20 };
199        char[] xChars = new char[] { 40000, 40001, 40002, 40003, 40004 };
200        int[] xInts = new int[] { 70000, 70001, 70002, 70003, 70004 };
201        boolean[] xBooleans = new boolean[] { true, true, false, true, false };
202        float[] xFloats = new float[] { -1.5f, -0.5f, 0.0f, 0.5f, 1.5f };
203        long[] xLongs = new long[] {
204            0x1122334455667788L, 0x8877665544332211L, 0L, 1L, -1l };
205        String[] xStrings = new String[] {
206            "zero", "one", "two", "three", "four" };
207
208        int[] xEmpty = new int[0];
209
210        checkBytes(xBytes);
211        checkShorts(xShorts);
212        checkChars(xChars);
213        checkInts(xInts);
214        checkBooleans(xBooleans);
215        checkFloats(xFloats);
216        checkLongs(xLongs);
217        checkStrings(xStrings);
218
219        checkRange32(xInts, xEmpty, -1, (int) 0x80000000);
220        checkRange64(xLongs, -1, (int) 0x80000000);
221
222        checkNegAlloc(-1);
223    }
224}
225