view truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/MethodGuardsWithArgumentsTest.java @ 22241:14e6dfb1ef05

Truffle/Testing: massive rework of tests to accommodate recent instrumentation change with respect to "applyInstrumentation" - Caused by lack of a "vm" known to the Accessor during most testing, making the Instrumenter unavailable, which is now required to create a CallTarget - Modified the Instrumenter/Accessor with a static field that can be reflectively "poked" with a vm value for testing - Create a pair of methods to globally enable/disable testing mode to modify that static field and restore it when tests complete.
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Tue, 22 Sep 2015 20:31:19 -0700
parents dc83cc1f94f2
children 0d36601f233e
line wrap: on
line source

/*
 * Copyright (c) 2012, 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.truffle.api.dsl.test;

import static com.oracle.truffle.api.dsl.test.TestHelper.createRoot;
import static com.oracle.truffle.api.dsl.test.TestHelper.executeWith;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.NodeChildren;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.dsl.test.MethodGuardsWithArgumentsTestFactory.MArguments0Factory;
import com.oracle.truffle.api.dsl.test.MethodGuardsWithArgumentsTestFactory.MArguments1Factory;
import com.oracle.truffle.api.dsl.test.MethodGuardsWithArgumentsTestFactory.MArgumentsDouble0Factory;
import com.oracle.truffle.api.dsl.test.MethodGuardsWithArgumentsTestFactory.MArgumentsDouble1Factory;
import com.oracle.truffle.api.dsl.test.MethodGuardsWithArgumentsTestFactory.MArgumentsDouble2Factory;
import com.oracle.truffle.api.dsl.test.MethodGuardsWithArgumentsTestFactory.MArgumentsDouble3Factory;
import com.oracle.truffle.api.dsl.test.MethodGuardsWithArgumentsTestFactory.MArgumentsSingle2Factory;
import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode;
import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode;
import com.oracle.truffle.api.dsl.test.utilities.InstrumentationTestMode;

public class MethodGuardsWithArgumentsTest {

    @Before
    public void before() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
        InstrumentationTestMode.set(true);
    }

    @After
    public void after() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
        InstrumentationTestMode.set(false);
    }

    @Test
    public void testMArguments0() {
        TestRootNode<MArguments0> root = createRoot(MArguments0Factory.getInstance());
        Assert.assertEquals(42, executeWith(root));
    }

    abstract static class MArguments0 extends ValueNode {

        static boolean guard() {
            return true;
        }

        @Specialization(guards = "guard()")
        int do1() {
            return 42;
        }
    }

    @Test
    public void testMArguments1() {
        TestRootNode<MArguments1> root = createRoot(MArguments1Factory.getInstance());
        Assert.assertEquals(42, executeWith(root));
    }

    abstract static class MArguments1 extends ValueNode {

        static boolean guard() {
            return true;
        }

        @Specialization(guards = "guard()")
        int do1() {
            return 42;
        }
    }

    @Test
    public void testMArgumentsSingle0() {
        TestRootNode<MArguments1> root = createRoot(MArguments1Factory.getInstance());
        Assert.assertEquals(42, executeWith(root, 42));
    }

    @NodeChild("a")
    abstract static class MArgumentsSingle0 extends ValueNode {

        static boolean guard() {
            return true;
        }

        @Specialization(guards = "guard()")
        int do1(int a) {
            return a;
        }
    }

    @Test
    public void testMArgumentsSingle1() {
        TestRootNode<MArguments1> root = createRoot(MArguments1Factory.getInstance());
        Assert.assertEquals(42, executeWith(root, 42));
    }

    @NodeChild("a")
    abstract static class MArgumentsSingle1 extends ValueNode {

        static boolean guard(int a) {
            return a == 42;
        }

        @Specialization(guards = "guard(a)")
        int do1(int a) {
            return a;
        }
    }

    @Test
    public void testMArgumentsSingle2() {
        TestRootNode<MArgumentsSingle2> root = createRoot(MArgumentsSingle2Factory.getInstance());
        Assert.assertEquals(42, executeWith(root, 42));
    }

    @NodeChild("a")
    abstract static class MArgumentsSingle2 extends ValueNode {

        static boolean guard(int a1, int a2) {
            return a1 == 42 && a2 == 42;
        }

        @Specialization(guards = "guard(a,a)")
        int do1(int a) {
            return a;
        }
    }

    @Test
    public void testMArgumentsDouble0() {
        TestRootNode<MArgumentsDouble0> root = createRoot(MArgumentsDouble0Factory.getInstance());
        Assert.assertEquals(42, executeWith(root, 42, 0));
    }

    @NodeChild("a")
    abstract static class MArgumentsDouble0 extends ValueNode {

        static boolean guard(int a1, Object a2) {
            return a1 == 42 && a2.equals(new Integer(42));
        }

        @Specialization(guards = "guard(a,a)")
        int do1(int a) {
            return a;
        }
    }

    @Test
    public void testMArgumentsDouble1() {
        TestRootNode<MArgumentsDouble1> root = createRoot(MArgumentsDouble1Factory.getInstance());
        Assert.assertEquals(42, executeWith(root, 42, 41));
    }

    @NodeChildren({@NodeChild("a"), @NodeChild("b")})
    abstract static class MArgumentsDouble1 extends ValueNode {

        static boolean guard(int a1, double a2) {
            return a1 == 42 && a2 == 41;
        }

        @Specialization(guards = "guard(a,b)")
        int do1(int a, @SuppressWarnings("unused") double b) {
            return a;
        }
    }

    @Test
    public void testMArgumentsDouble2() {
        TestRootNode<MArgumentsDouble2> root = createRoot(MArgumentsDouble2Factory.getInstance());
        Assert.assertEquals(42, executeWith(root, 42, 41.0));
    }

    @NodeChildren({@NodeChild("a"), @NodeChild("b")})
    abstract static class MArgumentsDouble2 extends ValueNode {

        static boolean guard(double a1, int a2) {
            return a1 == 41 && a2 == 42;
        }

        @Specialization(guards = "guard(b,a)")
        int do1(int a, @SuppressWarnings("unused") double b) {
            return a;
        }
    }

    @Test
    public void testMArgumentsDouble3() {
        TestRootNode<MArgumentsDouble3> root = createRoot(MArgumentsDouble3Factory.getInstance());
        Assert.assertEquals(42, executeWith(root, 42, 41.0));
    }

    @NodeChildren({@NodeChild("a"), @NodeChild("b")})
    abstract static class MArgumentsDouble3 extends ValueNode {

        static boolean guard(Object a1, double a2) {
            return new Double(41.0).equals(a1) && a2 == 41;
        }

        @Specialization(guards = "guard(b,b)")
        int do1(int a, @SuppressWarnings("unused") double b) {
            return a;
        }
    }

    abstract static class MArgumentsError0 extends ValueNode {

        static boolean guard() {
            return true;
        }

        @ExpectError("Error parsing expression 'guard(': -- line 1 col 7: \")\" expected%")
        @Specialization(guards = "guard(")
        int do1() {
            return 42;
        }
    }

    abstract static class MArgumentsError1 extends ValueNode {

        static boolean guard() {
            return true;
        }

        @ExpectError("Error parsing expression 'guard)': -- line 1 col 6: EOF expected%")
        @Specialization(guards = "guard)")
        int do1() {
            return 42;
        }

    }

    abstract static class MArgumentsError2 extends ValueNode {

        static boolean guard() {
            return true;
        }

        @ExpectError("Error parsing expression 'guard(a)': a cannot be resolved.")
        @Specialization(guards = "guard(a)")
        int do1() {
            return 42;
        }
    }

    @NodeChild("b")
    abstract static class MArgumentsError3 extends ValueNode {

        static boolean guard() {
            return true;
        }

        @ExpectError("Error parsing expression 'guard(a)': a cannot be resolved.")
        @Specialization(guards = "guard(a)")
        int do1(int b) {
            return b;
        }
    }

}