1/*
2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.graalvm.compiler.replacements.test;
24
25import org.junit.Assert;
26import org.junit.Test;
27
28import org.graalvm.compiler.core.test.GraalCompilerTest;
29
30/**
31 * Tests the implementation of {@code [A]NEWARRAY}.
32 */
33public class NewArrayTest extends GraalCompilerTest {
34
35    @Override
36    protected void assertDeepEquals(Object expected, Object actual) {
37        Assert.assertTrue(expected != null);
38        Assert.assertTrue(actual != null);
39        super.assertDeepEquals(expected.getClass(), actual.getClass());
40        if (expected instanceof int[]) {
41            Assert.assertArrayEquals((int[]) expected, (int[]) actual);
42        } else if (expected instanceof byte[]) {
43            Assert.assertArrayEquals((byte[]) expected, (byte[]) actual);
44        } else if (expected instanceof char[]) {
45            Assert.assertArrayEquals((char[]) expected, (char[]) actual);
46        } else if (expected instanceof short[]) {
47            Assert.assertArrayEquals((short[]) expected, (short[]) actual);
48        } else if (expected instanceof float[]) {
49            Assert.assertArrayEquals((float[]) expected, (float[]) actual, 0.0f);
50        } else if (expected instanceof long[]) {
51            Assert.assertArrayEquals((long[]) expected, (long[]) actual);
52        } else if (expected instanceof double[]) {
53            Assert.assertArrayEquals((double[]) expected, (double[]) actual, 0.0d);
54        } else if (expected instanceof Object[]) {
55            Assert.assertArrayEquals((Object[]) expected, (Object[]) actual);
56        } else {
57            Assert.fail("non-array value encountered: " + expected);
58        }
59    }
60
61    @Test
62    public void test1() {
63        for (String type : new String[]{"Byte", "Char", "Short", "Int", "Float", "Long", "Double", "String"}) {
64            test("new" + type + "Array7");
65            test("new" + type + "ArrayMinus7");
66            test("new" + type + "Array", 7);
67            test("new" + type + "Array", -7);
68            test("new" + type + "Array", Integer.MAX_VALUE);
69            test("new" + type + "Array", Integer.MIN_VALUE);
70        }
71    }
72
73    public static Object newCharArray7() {
74        return new char[7];
75    }
76
77    public static Object newCharArrayMinus7() {
78        return new char[-7];
79    }
80
81    public static Object newCharArray(int length) {
82        return new char[length];
83    }
84
85    public static Object newShortArray7() {
86        return new short[7];
87    }
88
89    public static Object newShortArrayMinus7() {
90        return new short[-7];
91    }
92
93    public static Object newShortArray(int length) {
94        return new short[length];
95    }
96
97    public static Object newFloatArray7() {
98        return new float[7];
99    }
100
101    public static Object newFloatArrayMinus7() {
102        return new float[-7];
103    }
104
105    public static Object newFloatArray(int length) {
106        return new float[length];
107    }
108
109    public static Object newLongArray7() {
110        return new long[7];
111    }
112
113    public static Object newLongArrayMinus7() {
114        return new long[-7];
115    }
116
117    public static Object newLongArray(int length) {
118        return new long[length];
119    }
120
121    public static Object newDoubleArray7() {
122        return new double[7];
123    }
124
125    public static Object newDoubleArrayMinus7() {
126        return new double[-7];
127    }
128
129    public static Object newDoubleArray(int length) {
130        return new double[length];
131    }
132
133    public static Object newIntArray7() {
134        return new int[7];
135    }
136
137    public static Object newIntArrayMinus7() {
138        return new int[-7];
139    }
140
141    public static Object newIntArray(int length) {
142        return new int[length];
143    }
144
145    public static Object newByteArray7() {
146        return new byte[7];
147    }
148
149    public static Object newByteArrayMinus7() {
150        return new byte[-7];
151    }
152
153    public static Object newByteArray(int length) {
154        return new byte[length];
155    }
156
157    public static Object newStringArray7() {
158        return new String[7];
159    }
160
161    public static Object newStringArrayMinus7() {
162        return new String[-7];
163    }
164
165    public static Object newStringArray(int length) {
166        return new String[length];
167    }
168}
169