001/*
002 * Copyright (c) 2013, 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.truffle.test;
024
025import org.junit.*;
026
027import com.oracle.graal.truffle.*;
028import com.oracle.graal.truffle.test.nodes.*;
029import com.oracle.truffle.api.*;
030import com.oracle.truffle.api.frame.*;
031import com.oracle.truffle.api.nodes.*;
032
033public class AssumptionPartialEvaluationTest extends PartialEvaluationTest {
034    public static Object constant42() {
035        return 42;
036    }
037
038    @Test
039    public void constantValue() {
040        Assumption assumption = Truffle.getRuntime().createAssumption();
041        AbstractTestNode result = new ConstantWithAssumptionTestNode(assumption, 42);
042        RootTestNode rootNode = new RootTestNode(new FrameDescriptor(), "constantValue", result);
043        OptimizedCallTarget callTarget = assertPartialEvalEquals("constant42", rootNode);
044        Assert.assertTrue(callTarget.isValid());
045        assertDeepEquals(42, callTarget.call());
046        Assert.assertTrue(callTarget.isValid());
047        try {
048            assumption.check();
049        } catch (InvalidAssumptionException e) {
050            Assert.fail("Assumption must not have been invalidated.");
051        }
052        assumption.invalidate();
053        try {
054            assumption.check();
055            Assert.fail("Assumption must have been invalidated.");
056        } catch (InvalidAssumptionException e) {
057        }
058        Assert.assertFalse(callTarget.isValid());
059        assertDeepEquals(43, callTarget.call());
060    }
061
062    /**
063     * This tests whether a valid Assumption does successfully cut of the branch that is not
064     * executed.
065     */
066    @Test
067    public void assumptionBranchCutoff() {
068        Assumption assumption = Truffle.getRuntime().createAssumption();
069        AssumptionCutsBranchTestNode result = new AssumptionCutsBranchTestNode(assumption);
070        RootTestNode rootNode = new RootTestNode(new FrameDescriptor(), "cutoffBranch", result);
071        OptimizedCallTarget compilable = compileHelper("cutoffBranch", rootNode, new Object[0]);
072
073        for (int i = 0; i < 100000; i++) {
074            Assert.assertEquals(0, compilable.call(new Object[0]));
075        }
076        Assert.assertNull(result.getChildNode());
077    }
078}