1 /*
2  * Copyright (c) 2011, 2018, 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  */
23 
24 
25 package org.graalvm.compiler.replacements.test;
26 
27 import org.junit.Assert;
28 import org.junit.Test;
29 
30 import org.graalvm.compiler.core.test.GraalCompilerTest;
31 
32 /**
33  * Tests the implementation of {@code [A]NEWARRAY}.
34  */
35 public class NewArrayTest extends GraalCompilerTest {
36 
37     @Override
assertDeepEquals(Object expected, Object actual)38     protected void assertDeepEquals(Object expected, Object actual) {
39         Assert.assertTrue(expected != null);
40         Assert.assertTrue(actual != null);
41         super.assertDeepEquals(expected.getClass(), actual.getClass());
42         if (expected instanceof int[]) {
43             Assert.assertArrayEquals((int[]) expected, (int[]) actual);
44         } else if (expected instanceof byte[]) {
45             Assert.assertArrayEquals((byte[]) expected, (byte[]) actual);
46         } else if (expected instanceof char[]) {
47             Assert.assertArrayEquals((char[]) expected, (char[]) actual);
48         } else if (expected instanceof short[]) {
49             Assert.assertArrayEquals((short[]) expected, (short[]) actual);
50         } else if (expected instanceof float[]) {
51             Assert.assertArrayEquals((float[]) expected, (float[]) actual, 0.0f);
52         } else if (expected instanceof long[]) {
53             Assert.assertArrayEquals((long[]) expected, (long[]) actual);
54         } else if (expected instanceof double[]) {
55             Assert.assertArrayEquals((double[]) expected, (double[]) actual, 0.0d);
56         } else if (expected instanceof Object[]) {
57             Assert.assertArrayEquals((Object[]) expected, (Object[]) actual);
58         } else {
59             Assert.fail("non-array value encountered: " + expected);
60         }
61     }
62 
63     @Test
test1()64     public void test1() {
65         for (String type : new String[]{"Byte", "Char", "Short", "Int", "Float", "Long", "Double", "String"}) {
66             test("new" + type + "Array7");
67             test("new" + type + "ArrayMinus7");
68             test("new" + type + "Array", 7);
69             test("new" + type + "Array", -7);
70             test("new" + type + "Array", Integer.MAX_VALUE);
71             test("new" + type + "Array", Integer.MIN_VALUE);
72         }
73     }
74 
newCharArray7()75     public static Object newCharArray7() {
76         return new char[7];
77     }
78 
newCharArrayMinus7()79     public static Object newCharArrayMinus7() {
80         return new char[-7];
81     }
82 
newCharArray(int length)83     public static Object newCharArray(int length) {
84         return new char[length];
85     }
86 
newShortArray7()87     public static Object newShortArray7() {
88         return new short[7];
89     }
90 
newShortArrayMinus7()91     public static Object newShortArrayMinus7() {
92         return new short[-7];
93     }
94 
newShortArray(int length)95     public static Object newShortArray(int length) {
96         return new short[length];
97     }
98 
newFloatArray7()99     public static Object newFloatArray7() {
100         return new float[7];
101     }
102 
newFloatArrayMinus7()103     public static Object newFloatArrayMinus7() {
104         return new float[-7];
105     }
106 
newFloatArray(int length)107     public static Object newFloatArray(int length) {
108         return new float[length];
109     }
110 
newLongArray7()111     public static Object newLongArray7() {
112         return new long[7];
113     }
114 
newLongArrayMinus7()115     public static Object newLongArrayMinus7() {
116         return new long[-7];
117     }
118 
newLongArray(int length)119     public static Object newLongArray(int length) {
120         return new long[length];
121     }
122 
newDoubleArray7()123     public static Object newDoubleArray7() {
124         return new double[7];
125     }
126 
newDoubleArrayMinus7()127     public static Object newDoubleArrayMinus7() {
128         return new double[-7];
129     }
130 
newDoubleArray(int length)131     public static Object newDoubleArray(int length) {
132         return new double[length];
133     }
134 
newIntArray7()135     public static Object newIntArray7() {
136         return new int[7];
137     }
138 
newIntArrayMinus7()139     public static Object newIntArrayMinus7() {
140         return new int[-7];
141     }
142 
newIntArray(int length)143     public static Object newIntArray(int length) {
144         return new int[length];
145     }
146 
newByteArray7()147     public static Object newByteArray7() {
148         return new byte[7];
149     }
150 
newByteArrayMinus7()151     public static Object newByteArrayMinus7() {
152         return new byte[-7];
153     }
154 
newByteArray(int length)155     public static Object newByteArray(int length) {
156         return new byte[length];
157     }
158 
newStringArray7()159     public static Object newStringArray7() {
160         return new String[7];
161     }
162 
newStringArrayMinus7()163     public static Object newStringArrayMinus7() {
164         return new String[-7];
165     }
166 
newStringArray(int length)167     public static Object newStringArray(int length) {
168         return new String[length];
169     }
170 }
171