NegateNodeCanonicalizationTest.java revision 12657:6ef01bd40ce2
1/*
2 * Copyright (c) 2013, 2014, 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.nodes.test;
24
25import static org.graalvm.compiler.core.common.CompilationIdentifier.INVALID_COMPILATION_ID;
26import static org.junit.Assert.assertEquals;
27import jdk.vm.ci.meta.JavaConstant;
28
29import org.junit.Before;
30import org.junit.Test;
31
32import org.graalvm.compiler.core.common.type.ArithmeticOpTable;
33import org.graalvm.compiler.nodes.ConstantNode;
34import org.graalvm.compiler.nodes.StructuredGraph;
35import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
36
37/**
38 * This class tests that the canonicalization for constant negate nodes cover all cases.
39 */
40public class NegateNodeCanonicalizationTest {
41
42    private StructuredGraph graph;
43
44    @Before
45    public void before() {
46        graph = new StructuredGraph(AllowAssumptions.YES, INVALID_COMPILATION_ID);
47    }
48
49    @Test
50    public void testByte() {
51        byte[] a = new byte[]{Byte.MIN_VALUE, Byte.MIN_VALUE + 1, -1, 0, 1, Byte.MAX_VALUE - 1, Byte.MAX_VALUE};
52        for (byte i : a) {
53            ConstantNode node = ConstantNode.forByte(i, graph);
54            JavaConstant expected = JavaConstant.forInt(-i);
55            assertEquals(expected, ArithmeticOpTable.forStamp(node.stamp()).getNeg().foldConstant(node.asConstant()));
56        }
57    }
58
59    @Test
60    public void testChar() {
61        char[] a = new char[]{Character.MIN_VALUE, Character.MIN_VALUE + 1, 0, 1, Character.MAX_VALUE - 1, Character.MAX_VALUE};
62        for (char i : a) {
63            ConstantNode node = ConstantNode.forChar(i, graph);
64            JavaConstant expected = JavaConstant.forInt(-i);
65            assertEquals(expected, ArithmeticOpTable.forStamp(node.stamp()).getNeg().foldConstant(node.asConstant()));
66        }
67    }
68
69    @Test
70    public void testShort() {
71        short[] a = new short[]{Short.MIN_VALUE, Short.MIN_VALUE + 1, -1, 0, 1, Short.MAX_VALUE - 1, Short.MAX_VALUE};
72        for (short i : a) {
73            ConstantNode node = ConstantNode.forShort(i, graph);
74            JavaConstant expected = JavaConstant.forInt(-i);
75            assertEquals(expected, ArithmeticOpTable.forStamp(node.stamp()).getNeg().foldConstant(node.asConstant()));
76        }
77    }
78
79    @Test
80    public void testInt() {
81        int[] a = new int[]{Integer.MIN_VALUE, Integer.MIN_VALUE + 1, -1, 0, 1, Integer.MAX_VALUE - 1, Integer.MAX_VALUE};
82        for (int i : a) {
83            ConstantNode node = ConstantNode.forInt(i, graph);
84            JavaConstant expected = JavaConstant.forInt(-i);
85            assertEquals(expected, ArithmeticOpTable.forStamp(node.stamp()).getNeg().foldConstant(node.asConstant()));
86        }
87    }
88
89    @Test
90    public void testLong() {
91        long[] a = new long[]{Long.MIN_VALUE, Long.MIN_VALUE + 1, -1, 0, 1, Long.MAX_VALUE - 1, Long.MAX_VALUE};
92        for (long i : a) {
93            ConstantNode node = ConstantNode.forLong(i, graph);
94            JavaConstant expected = JavaConstant.forLong(-i);
95            assertEquals(expected, ArithmeticOpTable.forStamp(node.stamp()).getNeg().foldConstant(node.asConstant()));
96        }
97    }
98
99    @Test
100    public void testFloat() {
101        float[] a = new float[]{Float.MIN_VALUE, Float.MIN_VALUE + 1, -1, 0, 1, Float.MAX_VALUE - 1, Float.MAX_VALUE};
102        for (float i : a) {
103            ConstantNode node = ConstantNode.forFloat(i, graph);
104            JavaConstant expected = JavaConstant.forFloat(-i);
105            assertEquals(expected, ArithmeticOpTable.forStamp(node.stamp()).getNeg().foldConstant(node.asConstant()));
106        }
107    }
108
109    @Test
110    public void testDouble() {
111        double[] a = new double[]{Double.MIN_VALUE, Double.MIN_VALUE + 1, -1, 0, 1, Double.MAX_VALUE - 1, Double.MAX_VALUE};
112        for (double i : a) {
113            ConstantNode node = ConstantNode.forDouble(i, graph);
114            JavaConstant expected = JavaConstant.forDouble(-i);
115            assertEquals(expected, ArithmeticOpTable.forStamp(node.stamp()).getNeg().foldConstant(node.asConstant()));
116        }
117    }
118
119}
120