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