001/*
002 * Copyright (c) 2013, 2014, 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.nodes.test;
024
025import jdk.internal.jvmci.meta.*;
026import static org.junit.Assert.*;
027
028import org.junit.*;
029
030import com.oracle.graal.compiler.common.type.*;
031import com.oracle.graal.nodes.*;
032import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
033
034/**
035 * This class tests that the canonicalization for constant negate nodes cover all cases.
036 */
037public class NegateNodeCanonicalizationTest {
038
039    private StructuredGraph graph;
040
041    @Before
042    public void before() {
043        graph = new StructuredGraph(AllowAssumptions.YES);
044    }
045
046    @Test
047    public void testByte() {
048        byte[] a = new byte[]{Byte.MIN_VALUE, Byte.MIN_VALUE + 1, -1, 0, 1, Byte.MAX_VALUE - 1, Byte.MAX_VALUE};
049        for (byte i : a) {
050            ConstantNode node = ConstantNode.forByte(i, graph);
051            JavaConstant expected = JavaConstant.forInt(-i);
052            assertEquals(expected, ArithmeticOpTable.forStamp(node.stamp()).getNeg().foldConstant(node.asConstant()));
053        }
054    }
055
056    @Test
057    public void testChar() {
058        char[] a = new char[]{Character.MIN_VALUE, Character.MIN_VALUE + 1, 0, 1, Character.MAX_VALUE - 1, Character.MAX_VALUE};
059        for (char i : a) {
060            ConstantNode node = ConstantNode.forChar(i, graph);
061            JavaConstant expected = JavaConstant.forInt(-i);
062            assertEquals(expected, ArithmeticOpTable.forStamp(node.stamp()).getNeg().foldConstant(node.asConstant()));
063        }
064    }
065
066    @Test
067    public void testShort() {
068        short[] a = new short[]{Short.MIN_VALUE, Short.MIN_VALUE + 1, -1, 0, 1, Short.MAX_VALUE - 1, Short.MAX_VALUE};
069        for (short i : a) {
070            ConstantNode node = ConstantNode.forShort(i, graph);
071            JavaConstant expected = JavaConstant.forInt(-i);
072            assertEquals(expected, ArithmeticOpTable.forStamp(node.stamp()).getNeg().foldConstant(node.asConstant()));
073        }
074    }
075
076    @Test
077    public void testInt() {
078        int[] a = new int[]{Integer.MIN_VALUE, Integer.MIN_VALUE + 1, -1, 0, 1, Integer.MAX_VALUE - 1, Integer.MAX_VALUE};
079        for (int i : a) {
080            ConstantNode node = ConstantNode.forInt(i, graph);
081            JavaConstant expected = JavaConstant.forInt(-i);
082            assertEquals(expected, ArithmeticOpTable.forStamp(node.stamp()).getNeg().foldConstant(node.asConstant()));
083        }
084    }
085
086    @Test
087    public void testLong() {
088        long[] a = new long[]{Long.MIN_VALUE, Long.MIN_VALUE + 1, -1, 0, 1, Long.MAX_VALUE - 1, Long.MAX_VALUE};
089        for (long i : a) {
090            ConstantNode node = ConstantNode.forLong(i, graph);
091            JavaConstant expected = JavaConstant.forLong(-i);
092            assertEquals(expected, ArithmeticOpTable.forStamp(node.stamp()).getNeg().foldConstant(node.asConstant()));
093        }
094    }
095
096    @Test
097    public void testFloat() {
098        float[] a = new float[]{Float.MIN_VALUE, Float.MIN_VALUE + 1, -1, 0, 1, Float.MAX_VALUE - 1, Float.MAX_VALUE};
099        for (float i : a) {
100            ConstantNode node = ConstantNode.forFloat(i, graph);
101            JavaConstant expected = JavaConstant.forFloat(-i);
102            assertEquals(expected, ArithmeticOpTable.forStamp(node.stamp()).getNeg().foldConstant(node.asConstant()));
103        }
104    }
105
106    @Test
107    public void testDouble() {
108        double[] a = new double[]{Double.MIN_VALUE, Double.MIN_VALUE + 1, -1, 0, 1, Double.MAX_VALUE - 1, Double.MAX_VALUE};
109        for (double i : a) {
110            ConstantNode node = ConstantNode.forDouble(i, graph);
111            JavaConstant expected = JavaConstant.forDouble(-i);
112            assertEquals(expected, ArithmeticOpTable.forStamp(node.stamp()).getNeg().foldConstant(node.asConstant()));
113        }
114    }
115
116}