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