001/*
002 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.replacements.test;
024
025import org.junit.*;
026
027import com.oracle.graal.compiler.test.*;
028
029/**
030 * Tests the implementation of {@code [A]NEWARRAY}.
031 */
032public class NewArrayTest extends GraalCompilerTest {
033
034    @Override
035    protected void assertDeepEquals(Object expected, Object actual) {
036        Assert.assertTrue(expected != null);
037        Assert.assertTrue(actual != null);
038        super.assertDeepEquals(expected.getClass(), actual.getClass());
039        if (expected instanceof int[]) {
040            Assert.assertArrayEquals((int[]) expected, (int[]) actual);
041        } else if (expected instanceof byte[]) {
042            Assert.assertArrayEquals((byte[]) expected, (byte[]) actual);
043        } else if (expected instanceof char[]) {
044            Assert.assertArrayEquals((char[]) expected, (char[]) actual);
045        } else if (expected instanceof short[]) {
046            Assert.assertArrayEquals((short[]) expected, (short[]) actual);
047        } else if (expected instanceof float[]) {
048            Assert.assertArrayEquals((float[]) expected, (float[]) actual, 0.0f);
049        } else if (expected instanceof long[]) {
050            Assert.assertArrayEquals((long[]) expected, (long[]) actual);
051        } else if (expected instanceof double[]) {
052            Assert.assertArrayEquals((double[]) expected, (double[]) actual, 0.0d);
053        } else if (expected instanceof Object[]) {
054            Assert.assertArrayEquals((Object[]) expected, (Object[]) actual);
055        } else {
056            Assert.fail("non-array value encountered: " + expected);
057        }
058    }
059
060    @Test
061    public void test1() {
062        for (String type : new String[]{"Byte", "Char", "Short", "Int", "Float", "Long", "Double", "String"}) {
063            test("new" + type + "Array7");
064            test("new" + type + "ArrayMinus7");
065            test("new" + type + "Array", 7);
066            test("new" + type + "Array", -7);
067            test("new" + type + "Array", Integer.MAX_VALUE);
068            test("new" + type + "Array", Integer.MIN_VALUE);
069        }
070    }
071
072    public static Object newCharArray7() {
073        return new char[7];
074    }
075
076    public static Object newCharArrayMinus7() {
077        return new char[-7];
078    }
079
080    public static Object newCharArray(int length) {
081        return new char[length];
082    }
083
084    public static Object newShortArray7() {
085        return new short[7];
086    }
087
088    public static Object newShortArrayMinus7() {
089        return new short[-7];
090    }
091
092    public static Object newShortArray(int length) {
093        return new short[length];
094    }
095
096    public static Object newFloatArray7() {
097        return new float[7];
098    }
099
100    public static Object newFloatArrayMinus7() {
101        return new float[-7];
102    }
103
104    public static Object newFloatArray(int length) {
105        return new float[length];
106    }
107
108    public static Object newLongArray7() {
109        return new long[7];
110    }
111
112    public static Object newLongArrayMinus7() {
113        return new long[-7];
114    }
115
116    public static Object newLongArray(int length) {
117        return new long[length];
118    }
119
120    public static Object newDoubleArray7() {
121        return new double[7];
122    }
123
124    public static Object newDoubleArrayMinus7() {
125        return new double[-7];
126    }
127
128    public static Object newDoubleArray(int length) {
129        return new double[length];
130    }
131
132    public static Object newIntArray7() {
133        return new int[7];
134    }
135
136    public static Object newIntArrayMinus7() {
137        return new int[-7];
138    }
139
140    public static Object newIntArray(int length) {
141        return new int[length];
142    }
143
144    public static Object newByteArray7() {
145        return new byte[7];
146    }
147
148    public static Object newByteArrayMinus7() {
149        return new byte[-7];
150    }
151
152    public static Object newByteArray(int length) {
153        return new byte[length];
154    }
155
156    public static Object newStringArray7() {
157        return new String[7];
158    }
159
160    public static Object newStringArrayMinus7() {
161        return new String[-7];
162    }
163
164    public static Object newStringArray(int length) {
165        return new String[length];
166    }
167}