# HG changeset patch # User twisti # Date 1381978155 25200 # Node ID 99ca997f4eae2cf449339064b185459677ad276b # Parent bb5a7574ae8185e42f00a61f9bc1709c44a7ca0c added NegateNodeCanonicalizationTest diff -r bb5a7574ae81 -r 99ca997f4eae graal/com.oracle.graal.nodes.test/src/com/oracle/graal/nodes/test/NegateNodeCanonicalizationTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.nodes.test/src/com/oracle/graal/nodes/test/NegateNodeCanonicalizationTest.java Wed Oct 16 19:49:15 2013 -0700 @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.graal.nodes.test; + +import static org.junit.Assert.*; + +import org.junit.*; + +import com.oracle.graal.api.meta.*; +import com.oracle.graal.nodes.*; +import com.oracle.graal.nodes.calc.*; + +/** + * This class tests that the canonicalization for constant negate nodes cover all cases. + */ +public class NegateNodeCanonicalizationTest { + + private StructuredGraph graph; + + @Before + public void before() { + graph = new StructuredGraph(); + } + + @Test + public void testByte() { + byte[] a = new byte[]{Byte.MIN_VALUE, Byte.MIN_VALUE + 1, -1, 0, 1, Byte.MAX_VALUE - 1, Byte.MAX_VALUE}; + for (byte i : a) { + ConstantNode node = ConstantNode.forByte(i, graph); + Constant expected = Constant.forInt(-i); + assertEquals(expected, new NegateNode(node).evalConst(node.asConstant())); + } + } + + @Test + public void testChar() { + char[] a = new char[]{Character.MIN_VALUE, Character.MIN_VALUE + 1, 0, 1, Character.MAX_VALUE - 1, Character.MAX_VALUE}; + for (char i : a) { + ConstantNode node = ConstantNode.forChar(i, graph); + Constant expected = Constant.forInt(-i); + assertEquals(expected, new NegateNode(node).evalConst(node.asConstant())); + } + } + + @Test + public void testShort() { + short[] a = new short[]{Short.MIN_VALUE, Short.MIN_VALUE + 1, -1, 0, 1, Short.MAX_VALUE - 1, Short.MAX_VALUE}; + for (short i : a) { + ConstantNode node = ConstantNode.forShort(i, graph); + Constant expected = Constant.forInt(-i); + assertEquals(expected, new NegateNode(node).evalConst(node.asConstant())); + } + } + + @Test + public void testInt() { + int[] a = new int[]{Integer.MIN_VALUE, Integer.MIN_VALUE + 1, -1, 0, 1, Integer.MAX_VALUE - 1, Integer.MAX_VALUE}; + for (int i : a) { + ConstantNode node = ConstantNode.forInt(i, graph); + Constant expected = Constant.forInt(-i); + assertEquals(expected, new NegateNode(node).evalConst(node.asConstant())); + } + } + + @Test + public void testLong() { + long[] a = new long[]{Long.MIN_VALUE, Long.MIN_VALUE + 1, -1, 0, 1, Long.MAX_VALUE - 1, Long.MAX_VALUE}; + for (long i : a) { + ConstantNode node = ConstantNode.forLong(i, graph); + Constant expected = Constant.forLong(-i); + assertEquals(expected, new NegateNode(node).evalConst(node.asConstant())); + } + } + + @Test + public void testFloat() { + float[] a = new float[]{Float.MIN_VALUE, Float.MIN_VALUE + 1, -1, 0, 1, Float.MAX_VALUE - 1, Float.MAX_VALUE}; + for (float i : a) { + ConstantNode node = ConstantNode.forFloat(i, graph); + Constant expected = Constant.forFloat(-i); + assertEquals(expected, new NegateNode(node).evalConst(node.asConstant())); + } + } + + @Test + public void testDouble() { + double[] a = new double[]{Double.MIN_VALUE, Double.MIN_VALUE + 1, -1, 0, 1, Double.MAX_VALUE - 1, Double.MAX_VALUE}; + for (double i : a) { + ConstantNode node = ConstantNode.forDouble(i, graph); + Constant expected = Constant.forDouble(-i); + assertEquals(expected, new NegateNode(node).evalConst(node.asConstant())); + } + } + +} diff -r bb5a7574ae81 -r 99ca997f4eae graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/NegateNode.java --- a/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/NegateNode.java Thu Oct 17 01:28:01 2013 +0200 +++ b/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/NegateNode.java Wed Oct 16 19:49:15 2013 -0700 @@ -57,17 +57,18 @@ public Constant evalConst(Constant... inputs) { assert inputs.length == 1; - switch (inputs[0].getKind()) { + Constant constant = inputs[0]; + switch (constant.getKind()) { case Int: - return Constant.forInt(-inputs[0].asInt()); + return Constant.forInt(-(constant.asInt())); case Long: - return Constant.forLong(-inputs[0].asLong()); + return Constant.forLong(-(constant.asLong())); case Float: - return Constant.forFloat(-inputs[0].asFloat()); + return Constant.forFloat(-(constant.asFloat())); case Double: - return Constant.forDouble(-inputs[0].asDouble()); + return Constant.forDouble(-(constant.asDouble())); default: - throw GraalInternalError.shouldNotReachHere(); + throw GraalInternalError.shouldNotReachHere("unknown kind " + constant.getKind()); } }