summaryrefslogtreecommitdiffstats
path: root/test/003-omnibus-opcodes/src/Array.java
blob: 1daf3b9fd580725b0aa5da2710eb03c385b67923 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright 2008 The Android Open Source Project


/**
 * Exercise arrays.
 */
public class Array {

    /*
     * Verify array contents.
     */
    static void checkBytes(byte[] bytes) {
        Main.assertTrue(bytes[0] == 0);
        Main.assertTrue(bytes[1] == -1);
        Main.assertTrue(bytes[2] == -2);
        Main.assertTrue(bytes[3] == -3);
        Main.assertTrue(bytes[4] == -4);
    }
    static void checkShorts(short[] shorts) {
        Main.assertTrue(shorts[0] == 20);
        Main.assertTrue(shorts[1] == 10);
        Main.assertTrue(shorts[2] == 0);
        Main.assertTrue(shorts[3] == -10);
        Main.assertTrue(shorts[4] == -20);
    }
    static void checkChars(char[] chars) {
        Main.assertTrue(chars[0] == 40000);
        Main.assertTrue(chars[1] == 40001);
        Main.assertTrue(chars[2] == 40002);
        Main.assertTrue(chars[3] == 40003);
        Main.assertTrue(chars[4] == 40004);
    }
    static void checkInts(int[] ints) {
        Main.assertTrue(ints[0] == 70000);
        Main.assertTrue(ints[1] == 70001);
        Main.assertTrue(ints[2] == 70002);
        Main.assertTrue(ints[3] == 70003);
        Main.assertTrue(ints[4] == 70004);
    }
    static void checkBooleans(boolean[] booleans) {
        Main.assertTrue(booleans[0]);
        Main.assertTrue(booleans[1]);
        Main.assertTrue(!booleans[2]);
        Main.assertTrue(booleans[3]);
        Main.assertTrue(!booleans[4]);
    }
    static void checkFloats(float[] floats) {
        Main.assertTrue(floats[0] == -1.5);
        Main.assertTrue(floats[1] == -0.5);
        Main.assertTrue(floats[2] == 0.0);
        Main.assertTrue(floats[3] == 0.5);
        Main.assertTrue(floats[4] == 1.5);
    }
    static void checkLongs(long[] longs) {
        Main.assertTrue(longs[0] == 0x1122334455667788L);
        Main.assertTrue(longs[1] == 0x8877665544332211L);
        Main.assertTrue(longs[2] == 0L);
        Main.assertTrue(longs[3] == 1L);
        Main.assertTrue(longs[4] == -1L);
    }
    static void checkStrings(String[] strings) {
        Main.assertTrue(strings[0].equals("zero"));
        Main.assertTrue(strings[1].equals("one"));
        Main.assertTrue(strings[2].equals("two"));
        Main.assertTrue(strings[3].equals("three"));
        Main.assertTrue(strings[4].equals("four"));
    }

    /*
     * Try bad range values, 32 bit get/put.
     */
    static void checkRange32(int[] ints, int[] empty, int negVal1, int negVal2){
        System.out.println("Array.checkRange32");
        int i = 0;

        Main.assertTrue(ints.length == 5);

        try {
            i = ints[5];            // exact bound
            Main.assertTrue(false);
        } catch (ArrayIndexOutOfBoundsException aioobe) {
            // good
        }
        try {
            ints[5] = i;            // exact bound
            Main.assertTrue(false);
        } catch (ArrayIndexOutOfBoundsException aioobe) {
            // good
        }
        try {
            i = ints[6];            // one past
            Main.assertTrue(false);
        } catch (ArrayIndexOutOfBoundsException aioobe) {
            // good
        }
        try {
            i = ints[negVal1];      // -1
            Main.assertTrue(false);
        } catch (ArrayIndexOutOfBoundsException aioobe) {
            // good
        }
        try {
            ints[negVal1] = i;      // -1
            Main.assertTrue(false);
        } catch (ArrayIndexOutOfBoundsException aioobe) {
            // good
        }
        try {
            i = ints[negVal2];      // min int
            Main.assertTrue(false);
        } catch (ArrayIndexOutOfBoundsException aioobe) {
            // good
        }


        try {
            i = empty[1];
            Main.assertTrue(false);
        } catch (ArrayIndexOutOfBoundsException aioobe) {
            // good
        }
    }

    /*
     * Try bad range values, 64 bit get/put.
     */
    static void checkRange64(long[] longs, int negVal1, int negVal2) {
        System.out.println("Array.checkRange64");
        long l = 0L;

        Main.assertTrue(longs.length == 5);

        try {
            l = longs[5];            // exact bound
            Main.assertTrue(false);
        } catch (ArrayIndexOutOfBoundsException aioobe) {
            // good
        }
        try {
            longs[5] = l;            // exact bound
            Main.assertTrue(false);
        } catch (ArrayIndexOutOfBoundsException aioobe) {
            // good
        }
        try {
            l = longs[6];            // one past
            Main.assertTrue(false);
        } catch (ArrayIndexOutOfBoundsException aioobe) {
            // good
        }
        try {
            l = longs[negVal1];      // -1
            Main.assertTrue(false);
        } catch (ArrayIndexOutOfBoundsException aioobe) {
            // good
        }
        try {
            longs[negVal1] = l;      // -1
            Main.assertTrue(false);
        } catch (ArrayIndexOutOfBoundsException aioobe) {
            // good
        }
        try {
            l = longs[negVal2];      // min int
            Main.assertTrue(false);
        } catch (ArrayIndexOutOfBoundsException aioobe) {
            // good
        }
    }

    /*
     * Test negative allocations of object and primitive arrays.
     */
    static void checkNegAlloc(int count) {
        System.out.println("Array.checkNegAlloc");
        String[] strings;
        int[] ints;

        try {
            ints = new int[count];
            Main.assertTrue(false);
        } catch (NegativeArraySizeException nase) {
            // good
        }

        try {
            strings = new String[count];
            Main.assertTrue(false);
        } catch (NegativeArraySizeException nase) {
            // good
        }
    }

    public static void run() {
        System.out.println("Array check...");

        byte[] xBytes = new byte[] { 0, -1, -2, -3, -4 };
        short[] xShorts = new short[] { 20, 10, 0, -10, -20 };
        char[] xChars = new char[] { 40000, 40001, 40002, 40003, 40004 };
        int[] xInts = new int[] { 70000, 70001, 70002, 70003, 70004 };
        boolean[] xBooleans = new boolean[] { true, true, false, true, false };
        float[] xFloats = new float[] { -1.5f, -0.5f, 0.0f, 0.5f, 1.5f };
        long[] xLongs = new long[] {
            0x1122334455667788L, 0x8877665544332211L, 0L, 1L, -1l };
        String[] xStrings = new String[] {
            "zero", "one", "two", "three", "four" };

        int[] xEmpty = new int[0];

        checkBytes(xBytes);
        checkShorts(xShorts);
        checkChars(xChars);
        checkInts(xInts);
        checkBooleans(xBooleans);
        checkFloats(xFloats);
        checkLongs(xLongs);
        checkStrings(xStrings);

        checkRange32(xInts, xEmpty, -1, (int) 0x80000000);
        checkRange64(xLongs, -1, (int) 0x80000000);

        checkNegAlloc(-1);
    }
}