view graal/com.oracle.graal.truffle.test/src/com/oracle/graal/truffle/test/ExactMathTest.java @ 12488:1a4dc163cd0a

abstracted HotSpotGraalRuntime as a RuntimeProvider which removes the recent selector addition to the Graal capabilities API and also makes Truffle independent of the graal.hotspot project (GRAAL-363)
author Doug Simon <doug.simon@oracle.com>
date Fri, 18 Oct 2013 18:23:14 +0200
parents 0097fb11c16f
children 6a13c422fca4
line wrap: on
line source

/*
 * 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.truffle.test;

import org.junit.*;

import com.oracle.graal.api.runtime.*;
import com.oracle.graal.compiler.test.*;
import com.oracle.graal.nodes.spi.*;
import com.oracle.graal.runtime.*;
import com.oracle.graal.truffle.substitutions.*;
import com.oracle.truffle.api.*;

public class ExactMathTest extends GraalCompilerTest {

    private static boolean substitutionsInstalled;

    public ExactMathTest() {
        if (!substitutionsInstalled) {
            Replacements replacements = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend().getProviders().getReplacements();
            replacements.registerSubstitutions(ExactMathSubstitutions.class);
            substitutionsInstalled = true;
        }
    }

    @Test
    public void testAdd() {
        test("add", 1, 2);
        test("add", Integer.MAX_VALUE, 2);
    }

    @Test
    public void testMul() {
        test("mul", 1, 2);
        test("mul", Integer.MAX_VALUE, 2);
    }

    @Test
    public void testSub() {
        test("sub", 1, 2);
        test("sub", Integer.MIN_VALUE, 2);
    }

    @Test
    public void testLongAdd() {
        test("longAdd", (long) Integer.MAX_VALUE, 2L);
        test("longAdd", Long.MAX_VALUE, 2L);
    }

    @Test
    public void testLongMul() {
        test("longMul", (long) Integer.MAX_VALUE, 2L);
        test("longMul", (long) Integer.MIN_VALUE, 2L);
        test("longMul", Long.MAX_VALUE, 2L);
    }

    @Test
    public void testLongSub() {
        test("longSub", (long) Integer.MIN_VALUE, 2L);
        test("longSub", Long.MIN_VALUE, 2L);
    }

    public static int add(int a, int b) {
        return ExactMath.addExact(a, b);
    }

    public static int mul(int a, int b) {
        return ExactMath.multiplyExact(a, b);
    }

    public static int sub(int a, int b) {
        return ExactMath.subtractExact(a, b);
    }

    public static long longAdd(long a, long b) {
        return ExactMath.addExact(a, b);
    }

    public static long longMul(long a, long b) {
        return ExactMath.multiplyExact(a, b);
    }

    public static long longSub(long a, long b) {
        return ExactMath.subtractExact(a, b);
    }
}