# HG changeset patch # User Michael Van De Vanter # Date 1422419299 28800 # Node ID acd822f17ef56bcf9596b3797425e68b0acec9d8 # Parent cca166aa28c0445b43a20e8601b1a1d6f1bb4ac1 Truffle/Instrumentation: clean up and repair some old unit tests diff -r cca166aa28c0 -r acd822f17ef5 graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/InstrumentationTest.java --- a/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/InstrumentationTest.java Tue Jan 27 20:27:25 2015 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,752 +0,0 @@ -/* - * Copyright (c) 2014, 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.test; - -import java.lang.reflect.*; -import java.util.*; - -import org.junit.*; - -import com.oracle.truffle.api.*; -import com.oracle.truffle.api.frame.*; -import com.oracle.truffle.api.instrument.*; -import com.oracle.truffle.api.instrument.Probe.ProbeListener; -import com.oracle.truffle.api.instrument.ProbeNode.Instrumentable; -import com.oracle.truffle.api.instrument.ProbeNode.WrapperNode; -import com.oracle.truffle.api.instrument.impl.*; -import com.oracle.truffle.api.nodes.*; -import com.oracle.truffle.api.source.*; - -/** - *

AST Instrumentation

- * - * Instrumentation allows the insertion into Truffle ASTs language-specific instances of - * {@link WrapperNode} that propagate execution events through a {@link Probe} to any instances of - * {@link Instrument} that might be attached to the particular probe by tools. - *
    - *
  1. Creates a simple add AST
  2. - *
  3. Verifies its structure
  4. - *
  5. "Probes" the add node by adding a {@link WrapperNode} and associated {@link Probe}
  6. - *
  7. Attaches a simple {@link Instrument} to the node via the Probe's {@link ProbeNode}
  8. - *
  9. Verifies the structure of the probed AST
  10. - *
  11. Verifies the execution of the probed AST
  12. - *
  13. Verifies the results observed by the instrument.
  14. - *
- * To do these tests, several required classes have been implemented in their most basic form, only - * implementing the methods necessary for the tests to pass, with stubs elsewhere. - */ -public class InstrumentationTest { - - private static final SyntaxTag ADD_TAG = new SyntaxTag() { - - public String name() { - return "Addition"; - } - - public String getDescription() { - return "Test Language Addition Node"; - } - }; - - private static final SyntaxTag VALUE_TAG = new SyntaxTag() { - - public String name() { - return "Value"; - } - - public String getDescription() { - return "Test Language Value Node"; - } - }; - - @Test - public void testBasicInstrumentation() { - // Create a simple addition AST - final TruffleRuntime runtime = Truffle.getRuntime(); - final TestValueNode leftValueNode = new TestValueNode(6); - final TestValueNode rightValueNode = new TestValueNode(7); - final TestAdditionNode addNode = new TestAdditionNode(leftValueNode, rightValueNode); - final TestRootNode rootNode = new TestRootNode(addNode); - - // Creating a call target sets the parent pointers in this tree and is necessary prior to - // checking any parent/child relationships - final CallTarget callTarget1 = runtime.createCallTarget(rootNode); - - // Check the tree structure - Assert.assertEquals(addNode, leftValueNode.getParent()); - Assert.assertEquals(addNode, rightValueNode.getParent()); - Iterator iterator = addNode.getChildren().iterator(); - Assert.assertEquals(leftValueNode, iterator.next()); - Assert.assertEquals(rightValueNode, iterator.next()); - Assert.assertFalse(iterator.hasNext()); - Assert.assertEquals(rootNode, addNode.getParent()); - iterator = rootNode.getChildren().iterator(); - Assert.assertEquals(addNode, iterator.next()); - Assert.assertFalse(iterator.hasNext()); - - // Ensure it executes correctly - Assert.assertEquals(13, callTarget1.call()); - - // Probe the addition node - final Probe probe = addNode.probe(); - - // Check the modified tree structure - Assert.assertEquals(addNode, leftValueNode.getParent()); - Assert.assertEquals(addNode, rightValueNode.getParent()); - iterator = addNode.getChildren().iterator(); - Assert.assertEquals(leftValueNode, iterator.next()); - Assert.assertEquals(rightValueNode, iterator.next()); - Assert.assertFalse(iterator.hasNext()); - - // Ensure there's a WrapperNode correctly inserted into the AST - iterator = rootNode.getChildren().iterator(); - Node wrapperNode = iterator.next(); - Assert.assertTrue(wrapperNode instanceof TestLanguageWrapperNode); - Assert.assertFalse(iterator.hasNext()); - Assert.assertEquals(rootNode, wrapperNode.getParent()); - - // Check that the WrapperNode has both the probe and the wrapped node as children - iterator = wrapperNode.getChildren().iterator(); - Assert.assertEquals(addNode, iterator.next()); - ProbeNode probeNode = (ProbeNode) iterator.next(); - Assert.assertTrue(probeNode.getProbe() != null); - Assert.assertFalse(iterator.hasNext()); - - // Check that you can't probe the WrapperNodes - TestLanguageWrapperNode wrapper = (TestLanguageWrapperNode) wrapperNode; - try { - wrapper.probe(); - Assert.fail(); - } catch (IllegalStateException e) { - } - - // Check that the "probed" AST still executes correctly - Assert.assertEquals(13, callTarget1.call()); - - // Attach a counting instrument to the probe - final TestCounter counterA = new TestCounter(); - counterA.attach(probe); - - // Attach a second counting instrument to the probe - final TestCounter counterB = new TestCounter(); - counterB.attach(probe); - - // Run it again and check that the two instruments are working - Assert.assertEquals(13, callTarget1.call()); - Assert.assertEquals(counterA.enterCount, 1); - Assert.assertEquals(counterA.leaveCount, 1); - Assert.assertEquals(counterB.enterCount, 1); - Assert.assertEquals(counterB.leaveCount, 1); - - // Remove counterA and check the "instrument chain" - counterA.dispose(); - iterator = probeNode.getChildren().iterator(); - - // Run it again and check that instrument B is still working but not A - Assert.assertEquals(13, callTarget1.call()); - Assert.assertEquals(counterA.enterCount, 1); - Assert.assertEquals(counterA.leaveCount, 1); - Assert.assertEquals(counterB.enterCount, 2); - Assert.assertEquals(counterB.leaveCount, 2); - - // Simulate a split by cloning the AST - final CallTarget callTarget2 = runtime.createCallTarget((TestRootNode) rootNode.copy()); - // Run the clone and check that instrument B is still working but not A - Assert.assertEquals(13, callTarget2.call()); - Assert.assertEquals(counterA.enterCount, 1); - Assert.assertEquals(counterA.leaveCount, 1); - Assert.assertEquals(counterB.enterCount, 3); - Assert.assertEquals(counterB.leaveCount, 3); - - // Run the original and check that instrument B is still working but not A - Assert.assertEquals(13, callTarget2.call()); - Assert.assertEquals(counterA.enterCount, 1); - Assert.assertEquals(counterA.leaveCount, 1); - Assert.assertEquals(counterB.enterCount, 4); - Assert.assertEquals(counterB.leaveCount, 4); - - // Attach a second instrument to the probe - final TestCounter counterC = new TestCounter(); - counterC.attach(probe); - - // Run the original and check that instruments B,C working but not A - Assert.assertEquals(13, callTarget1.call()); - Assert.assertEquals(counterA.enterCount, 1); - Assert.assertEquals(counterA.leaveCount, 1); - Assert.assertEquals(counterB.enterCount, 5); - Assert.assertEquals(counterB.leaveCount, 5); - Assert.assertEquals(counterC.enterCount, 1); - Assert.assertEquals(counterC.leaveCount, 1); - - // Run the clone and check that instruments B,C working but not A - Assert.assertEquals(13, callTarget2.call()); - Assert.assertEquals(counterA.enterCount, 1); - Assert.assertEquals(counterA.leaveCount, 1); - Assert.assertEquals(counterB.enterCount, 6); - Assert.assertEquals(counterB.leaveCount, 6); - Assert.assertEquals(counterC.enterCount, 2); - Assert.assertEquals(counterC.leaveCount, 2); - - // Remove instrumentC - counterC.dispose(); - - // Run the original and check that instrument B working but not A,C - Assert.assertEquals(13, callTarget1.call()); - Assert.assertEquals(counterA.enterCount, 1); - Assert.assertEquals(counterA.leaveCount, 1); - Assert.assertEquals(counterB.enterCount, 7); - Assert.assertEquals(counterB.leaveCount, 7); - Assert.assertEquals(counterC.enterCount, 2); - Assert.assertEquals(counterC.leaveCount, 2); - - // Run the clone and check that instrument B working but not A,C - Assert.assertEquals(13, callTarget2.call()); - Assert.assertEquals(counterA.enterCount, 1); - Assert.assertEquals(counterA.leaveCount, 1); - Assert.assertEquals(counterB.enterCount, 8); - Assert.assertEquals(counterB.leaveCount, 8); - Assert.assertEquals(counterC.enterCount, 2); - Assert.assertEquals(counterC.leaveCount, 2); - - // Remove instrumentB - counterB.dispose(); - - // Run both the original and clone, check that no instruments working - Assert.assertEquals(13, callTarget1.call()); - Assert.assertEquals(13, callTarget2.call()); - Assert.assertEquals(counterA.enterCount, 1); - Assert.assertEquals(counterA.leaveCount, 1); - Assert.assertEquals(counterB.enterCount, 8); - Assert.assertEquals(counterB.leaveCount, 8); - Assert.assertEquals(counterC.enterCount, 2); - Assert.assertEquals(counterC.leaveCount, 2); - } - - @Test - public void testTagging() { - - // Applies appropriate tags - final TestASTProber astProber = new TestASTProber(); - Probe.registerASTProber(astProber); - - // Listens for probes and tags being added - final TestProbeListener probeListener = new TestProbeListener(); - Probe.addProbeListener(probeListener); - - // Counts all entries to all instances of addition nodes - final TestMultiCounter additionCounter = new TestMultiCounter(); - - // Counts all entries to all instances of value nodes - final TestMultiCounter valueCounter = new TestMultiCounter(); - - // Create a simple addition AST - final TruffleRuntime runtime = Truffle.getRuntime(); - final TestValueNode leftValueNode = new TestValueNode(6); - final TestValueNode rightValueNode = new TestValueNode(7); - final TestAdditionNode addNode = new TestAdditionNode(leftValueNode, rightValueNode); - - final TestRootNode rootNode = new TestRootNode(addNode); - - final CallTarget callTarget = runtime.createCallTarget(rootNode); - - // Check that the prober added probes to the tree - Assert.assertEquals(probeListener.probeCount, 3); - Assert.assertEquals(probeListener.tagCount, 3); - - Assert.assertEquals(Probe.findProbesTaggedAs(ADD_TAG).size(), 1); - Assert.assertEquals(Probe.findProbesTaggedAs(VALUE_TAG).size(), 2); - - // Check that it executes correctly - Assert.assertEquals(13, callTarget.call()); - - // Dynamically attach a counter for all executions of all Addition nodes - for (Probe probe : Probe.findProbesTaggedAs(ADD_TAG)) { - additionCounter.attachCounter(probe); - } - // Dynamically attach a counter for all executions of all Value nodes - for (Probe probe : Probe.findProbesTaggedAs(VALUE_TAG)) { - valueCounter.attachCounter(probe); - } - - // Counters initialized at 0 - Assert.assertEquals(additionCounter.count, 0); - Assert.assertEquals(valueCounter.count, 0); - - // Execute again - Assert.assertEquals(13, callTarget.call()); - - // There are two value nodes in the AST, but only one addition node - Assert.assertEquals(additionCounter.count, 1); - Assert.assertEquals(valueCounter.count, 2); - - Probe.unregisterASTProber(astProber); - - } - - @Test - public void testProbeLite() { - - // Use the "lite-probing" option, limited to a single pass of - // probing and a single Instrument at each probed node. This - // particular test uses a shared event receiver at every - // lite-probed node. - final TestEventReceiver receiver = new TestEventReceiver(); - - TestASTLiteProber astLiteProber = new TestASTLiteProber(receiver); - Probe.registerASTProber(astLiteProber); - - // Create a simple addition AST - final TruffleRuntime runtime = Truffle.getRuntime(); - final TestValueNode leftValueNode = new TestValueNode(6); - final TestValueNode rightValueNode = new TestValueNode(7); - final TestAdditionNode addNode = new TestAdditionNode(leftValueNode, rightValueNode); - final TestRootNode rootNode = new TestRootNode(addNode); - - // Creating a call target sets the parent pointers in this tree and is necessary prior to - // checking any parent/child relationships - final CallTarget callTarget = runtime.createCallTarget(rootNode); - - // Check that the instrument is working as expected. - Assert.assertEquals(0, receiver.counter); - callTarget.call(); - Assert.assertEquals(2, receiver.counter); - - // Check that you can't probe a node that's already received a probeLite() call - try { - leftValueNode.probe(); - Assert.fail(); - } catch (IllegalStateException e) { - } - - try { - rightValueNode.probe(); - Assert.fail(); - } catch (IllegalStateException e) { - } - - // Check tree structure - Assert.assertTrue(leftValueNode.getParent() instanceof TestLanguageWrapperNode); - Assert.assertTrue(rightValueNode.getParent() instanceof TestLanguageWrapperNode); - TestLanguageWrapperNode leftWrapper = (TestLanguageWrapperNode) leftValueNode.getParent(); - TestLanguageWrapperNode rightWrapper = (TestLanguageWrapperNode) rightValueNode.getParent(); - Assert.assertEquals(addNode, leftWrapper.getParent()); - Assert.assertEquals(addNode, rightWrapper.getParent()); - Iterator iterator = addNode.getChildren().iterator(); - Assert.assertEquals(leftWrapper, iterator.next()); - Assert.assertEquals(rightWrapper, iterator.next()); - Assert.assertFalse(iterator.hasNext()); - Assert.assertEquals(rootNode, addNode.getParent()); - iterator = rootNode.getChildren().iterator(); - Assert.assertEquals(addNode, iterator.next()); - Assert.assertFalse(iterator.hasNext()); - - // Check that you can't get a probe on the wrappers because they were "lite-probed" - try { - leftWrapper.getProbe(); - Assert.fail(); - } catch (IllegalStateException e) { - } - try { - rightWrapper.getProbe(); - Assert.fail(); - } catch (IllegalStateException e) { - } - - // Check that you can't probe the wrappers - try { - leftWrapper.probe(); - Assert.fail(); - } catch (IllegalStateException e) { - } - try { - rightWrapper.probe(); - Assert.fail(); - } catch (IllegalStateException e) { - } - try { - leftWrapper.probeLite(null); - Assert.fail(); - } catch (IllegalStateException e) { - } - try { - rightWrapper.probeLite(null); - Assert.fail(); - } catch (IllegalStateException e) { - } - - // Use reflection to check that each WrapperNode has a ProbeLiteNode with a - // SimpleEventReceiver - try { - Field probeNodeField = leftWrapper.getClass().getDeclaredField("probeNode"); - - // cheat: probeNode is private, so we change it's accessibility at runtime - probeNodeField.setAccessible(true); - ProbeNode probeNode = (ProbeNode) probeNodeField.get(leftWrapper); - - // hack: Since ProbeLiteNode is not visible, we do a string compare here - Assert.assertTrue(probeNode.getClass().toString().endsWith("ProbeLiteNode")); - - // Now we do the same to check the type of the eventReceiver in ProbeLiteNode - Field eventReceiverField = probeNode.getClass().getDeclaredField("eventReceiver"); - eventReceiverField.setAccessible(true); - TruffleEventReceiver eventReceiver = (TruffleEventReceiver) eventReceiverField.get(probeNode); - Assert.assertTrue(eventReceiver instanceof SimpleEventReceiver); - - // Reset accessibility - probeNodeField.setAccessible(false); - eventReceiverField.setAccessible(false); - - } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { - Assert.fail(); - } - - Probe.unregisterASTProber(astLiteProber); - - } - - /** - * A guest language usually has a single language-specific subclass of {@link Node} from which - * all other nodes in the guest language subclass. By making this node {@link Instrumentable}, - * we allow all nodes of the guest language to have {@link Probe}s attach to them. - * - */ - private abstract class TestLanguageNode extends Node implements Instrumentable { - public abstract Object execute(VirtualFrame frame); - - public Probe probe() { - Node parent = getParent(); - - if (parent == null) { - throw new IllegalStateException("Cannot call probe() on a node without a parent."); - } - - if (parent instanceof TestLanguageWrapperNode) { - return ((TestLanguageWrapperNode) parent).getProbe(); - } - - // Create a new wrapper/probe with this node as its child. - final TestLanguageWrapperNode wrapper = new TestLanguageWrapperNode(this); - - // Connect it to a Probe - final Probe probe = ProbeNode.insertProbe(wrapper); - - // Replace this node in the AST with the wrapper - this.replace(wrapper); - - return probe; - } - - public void probeLite(TruffleEventReceiver eventReceiver) { - Node parent = getParent(); - - if (parent == null) { - throw new IllegalStateException("Cannot call probeLite() on a node without a parent"); - } - - if (parent instanceof TestLanguageWrapperNode) { - throw new IllegalStateException("Cannot call probeLite() on a node that already has a wrapper."); - } - - final TestLanguageWrapperNode wrapper = new TestLanguageWrapperNode(this); - ProbeNode.insertProbeLite(wrapper, eventReceiver); - - this.replace(wrapper); - } - } - - /** - * The wrapper node class is usually language-specific and inherits from the language-specific - * subclass of {@link Node}, in this case, {@link TestLanguageNode}. - */ - @NodeInfo(cost = NodeCost.NONE) - private class TestLanguageWrapperNode extends TestLanguageNode implements WrapperNode { - @Child private TestLanguageNode child; - @Child private ProbeNode probeNode; - - public TestLanguageWrapperNode(TestLanguageNode child) { - assert !(child instanceof TestLanguageWrapperNode); - this.child = child; - } - - public String instrumentationInfo() { - return "Wrapper node for testing"; - } - - public void insertProbe(ProbeNode newProbeNode) { - this.probeNode = newProbeNode; - } - - public Probe getProbe() { - try { - return probeNode.getProbe(); - } catch (IllegalStateException e) { - throw new IllegalStateException("Cannot call getProbe() on a wrapper that has no probe"); - } - } - - @Override - public Node getChild() { - return child; - } - - @Override - public Object execute(VirtualFrame frame) { - probeNode.enter(child, frame); - Object result; - - try { - result = child.execute(frame); - probeNode.returnValue(child, frame, result); - } catch (KillException e) { - throw (e); - } catch (Exception e) { - probeNode.returnExceptional(child, frame, e); - throw (e); - } - - return result; - } - - @Override - public Probe probe() { - throw new IllegalStateException("Cannot call probe() on a wrapper."); - } - - @Override - public void probeLite(TruffleEventReceiver eventReceiver) { - throw new IllegalStateException("Cannot call probeLite() on a wrapper."); - } - } - - /** - * A simple node for our test language to store a value. - */ - private class TestValueNode extends TestLanguageNode { - private final int value; - - public TestValueNode(int value) { - this.value = value; - } - - @Override - public Object execute(VirtualFrame frame) { - return new Integer(this.value); - } - } - - /** - * A node for our test language that adds up two {@link TestValueNode}s. - */ - private class TestAdditionNode extends TestLanguageNode { - @Child private TestLanguageNode leftChild; - @Child private TestLanguageNode rightChild; - - public TestAdditionNode(TestValueNode leftChild, TestValueNode rightChild) { - this.leftChild = insert(leftChild); - this.rightChild = insert(rightChild); - } - - @Override - public Object execute(VirtualFrame frame) { - return new Integer(((Integer) leftChild.execute(frame)).intValue() + ((Integer) rightChild.execute(frame)).intValue()); - } - } - - /** - * Truffle requires that all guest languages to have a {@link RootNode} which sits atop any AST - * of the guest language. This is necessary since creating a {@link CallTarget} is how Truffle - * completes an AST. The root nodes serves as our entry point into a program. - */ - private class TestRootNode extends RootNode { - @Child private TestLanguageNode body; - - /** - * This constructor emulates the global machinery that applies registered probers to every - * newly created AST. Global registry is not used, since that would interfere with other - * tests run in the same environment. - */ - public TestRootNode(TestLanguageNode body) { - super(null); - this.body = body; - } - - @Override - public Object execute(VirtualFrame frame) { - return body.execute(frame); - } - - @Override - public boolean isCloningAllowed() { - return true; - } - - @Override - public void applyInstrumentation() { - Probe.applyASTProbers(body); - } - } - - /** - * A counter for the number of times execution enters and leaves a probed AST node. - */ - private class TestCounter { - - public int enterCount = 0; - public int leaveCount = 0; - public final Instrument instrument; - - public TestCounter() { - instrument = Instrument.create(new SimpleEventReceiver() { - - @Override - public void enter(Node node, VirtualFrame frame) { - enterCount++; - } - - @Override - public void returnAny(Node node, VirtualFrame frame) { - leaveCount++; - } - }, "Instrumentation Test Counter"); - } - - public void attach(Probe probe) { - probe.attach(instrument); - } - - public void dispose() { - instrument.dispose(); - } - - } - - /** - * Tags selected nodes on newly constructed ASTs. - */ - private static final class TestASTProber implements NodeVisitor, ASTProber { - - public boolean visit(Node node) { - if (node instanceof TestLanguageNode) { - - final TestLanguageNode testNode = (TestLanguageNode) node; - - if (node instanceof TestValueNode) { - testNode.probe().tagAs(VALUE_TAG, null); - - } else if (node instanceof TestAdditionNode) { - testNode.probe().tagAs(ADD_TAG, null); - - } - } - return true; - } - - public void probeAST(Node node) { - node.accept(this); - } - } - - /** - * "lite-probes" every value node with a shared event receiver. - */ - private static final class TestASTLiteProber implements NodeVisitor, ASTProber { - private final TruffleEventReceiver eventReceiver; - - public TestASTLiteProber(SimpleEventReceiver simpleEventReceiver) { - this.eventReceiver = simpleEventReceiver; - } - - public boolean visit(Node node) { - if (node instanceof TestValueNode) { - final TestLanguageNode testNode = (TestValueNode) node; - testNode.probeLite(eventReceiver); - } - return true; - } - - public void probeAST(Node node) { - node.accept(this); - } - } - - /** - * Counts the number of "enter" events at probed nodes. - * - */ - static final class TestEventReceiver extends SimpleEventReceiver { - - public int counter = 0; - - @Override - public void enter(Node node, VirtualFrame frame) { - counter++; - } - - } - - /** - * A counter that can count executions at multiple nodes; it attaches a separate instrument at - * each Probe, but keeps a total count. - */ - private static final class TestMultiCounter { - - public int count = 0; - - public void attachCounter(Probe probe) { - - // Attach a new instrument for every Probe - // where we want to count executions. - // it will get copied when ASTs cloned, so - // keep the count in this outer class. - probe.attach(Instrument.create(new SimpleEventReceiver() { - - @Override - public void enter(Node node, VirtualFrame frame) { - count++; - } - }, "Instrumentation Test MultiCounter")); - } - } - - private static final class TestProbeListener implements ProbeListener { - - public int probeCount = 0; - public int tagCount = 0; - - public void newProbeInserted(Probe probe) { - probeCount++; - } - - public void probeTaggedAs(Probe probe, SyntaxTag tag, Object tagValue) { - tagCount++; - } - - public void startASTProbing(Source source) { - } - - public void endASTProbing(Source source) { - } - - } - -} diff -r cca166aa28c0 -r acd822f17ef5 graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/instrument/InstrumentationTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/instrument/InstrumentationTest.java Tue Jan 27 20:28:19 2015 -0800 @@ -0,0 +1,721 @@ +/* + * Copyright (c) 2014, 2015, 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.test.instrument; + +import static org.junit.Assert.*; + +import java.util.*; + +import org.junit.*; + +import com.oracle.truffle.api.*; +import com.oracle.truffle.api.frame.*; +import com.oracle.truffle.api.instrument.*; +import com.oracle.truffle.api.instrument.ProbeFailure.Reason; +import com.oracle.truffle.api.instrument.ProbeNode.WrapperNode; +import com.oracle.truffle.api.instrument.impl.*; +import com.oracle.truffle.api.nodes.*; + +/** + *

AST Instrumentation

+ * + * Instrumentation allows the insertion into Truffle ASTs language-specific instances of + * {@link WrapperNode} that propagate execution events through a {@link Probe} to any instances of + * {@link Instrument} that might be attached to the particular probe by tools. + *
    + *
  1. Creates a simple add AST
  2. + *
  3. Verifies its structure
  4. + *
  5. "Probes" the add node by adding a {@link WrapperNode} and associated {@link Probe}
  6. + *
  7. Attaches a simple {@link Instrument} to the node via the Probe's {@link ProbeNode}
  8. + *
  9. Verifies the structure of the probed AST
  10. + *
  11. Verifies the execution of the probed AST
  12. + *
  13. Verifies the results observed by the instrument.
  14. + *
+ * To do these tests, several required classes have been implemented in their most basic form, only + * implementing the methods necessary for the tests to pass, with stubs elsewhere. + */ +public class InstrumentationTest { + + private static final SyntaxTag ADD_TAG = new SyntaxTag() { + + @Override + public String name() { + return "Addition"; + } + + @Override + public String getDescription() { + return "Test Language Addition Node"; + } + }; + + private static final SyntaxTag VALUE_TAG = new SyntaxTag() { + + @Override + public String name() { + return "Value"; + } + + @Override + public String getDescription() { + return "Test Language Value Node"; + } + }; + + @Test + public void testBasicInstrumentation() { + // Create a simple addition AST + final TruffleRuntime runtime = Truffle.getRuntime(); + final TestValueNode leftValueNode = new TestValueNode(6); + final TestValueNode rightValueNode = new TestValueNode(7); + final TestAdditionNode addNode = new TestAdditionNode(leftValueNode, rightValueNode); + + try { + addNode.probe(); + } catch (ProbeException e) { + assertEquals(e.getFailure().getReason(), Reason.NO_PARENT); + } + final TestRootNode rootNode = new TestRootNode(addNode); + + // Creating a call target sets the parent pointers in this tree and is necessary prior to + // checking any parent/child relationships + final CallTarget callTarget1 = runtime.createCallTarget(rootNode); + + // Check the tree structure + assertEquals(addNode, leftValueNode.getParent()); + assertEquals(addNode, rightValueNode.getParent()); + Iterator iterator = addNode.getChildren().iterator(); + assertEquals(leftValueNode, iterator.next()); + assertEquals(rightValueNode, iterator.next()); + assertFalse(iterator.hasNext()); + assertEquals(rootNode, addNode.getParent()); + iterator = rootNode.getChildren().iterator(); + assertEquals(addNode, iterator.next()); + assertFalse(iterator.hasNext()); + + // Ensure it executes correctly + assertEquals(13, callTarget1.call()); + + // Probe the addition node + final Probe probe = addNode.probe(); + + // Check the modified tree structure + assertEquals(addNode, leftValueNode.getParent()); + assertEquals(addNode, rightValueNode.getParent()); + iterator = addNode.getChildren().iterator(); + assertEquals(leftValueNode, iterator.next()); + assertEquals(rightValueNode, iterator.next()); + assertFalse(iterator.hasNext()); + + // Ensure there's a WrapperNode correctly inserted into the AST + iterator = rootNode.getChildren().iterator(); + Node wrapperNode = iterator.next(); + assertTrue(wrapperNode instanceof TestLanguageWrapperNode); + assertFalse(iterator.hasNext()); + assertEquals(rootNode, wrapperNode.getParent()); + + // Check that the WrapperNode has both the probe and the wrapped node as children + iterator = wrapperNode.getChildren().iterator(); + assertEquals(addNode, iterator.next()); + ProbeNode probeNode = (ProbeNode) iterator.next(); + assertTrue(probeNode.getProbe() != null); + assertFalse(iterator.hasNext()); + + // Check that you can't probe the WrapperNodes + TestLanguageWrapperNode wrapper = (TestLanguageWrapperNode) wrapperNode; + try { + wrapper.probe(); + fail(); + } catch (ProbeException e) { + assertEquals(e.getFailure().getReason(), Reason.WRAPPER_NODE); + } + + // Check that the "probed" AST still executes correctly + assertEquals(13, callTarget1.call()); + + // Attach a counting instrument to the probe + final TestCounter counterA = new TestCounter(); + counterA.attach(probe); + + // Attach a second counting instrument to the probe + final TestCounter counterB = new TestCounter(); + counterB.attach(probe); + + // Run it again and check that the two instruments are working + assertEquals(13, callTarget1.call()); + assertEquals(counterA.enterCount, 1); + assertEquals(counterA.leaveCount, 1); + assertEquals(counterB.enterCount, 1); + assertEquals(counterB.leaveCount, 1); + + // Remove counterA and check the "instrument chain" + counterA.dispose(); + iterator = probeNode.getChildren().iterator(); + + // Run it again and check that instrument B is still working but not A + assertEquals(13, callTarget1.call()); + assertEquals(counterA.enterCount, 1); + assertEquals(counterA.leaveCount, 1); + assertEquals(counterB.enterCount, 2); + assertEquals(counterB.leaveCount, 2); + + // Simulate a split by cloning the AST + final CallTarget callTarget2 = runtime.createCallTarget((TestRootNode) rootNode.copy()); + // Run the clone and check that instrument B is still working but not A + assertEquals(13, callTarget2.call()); + assertEquals(counterA.enterCount, 1); + assertEquals(counterA.leaveCount, 1); + assertEquals(counterB.enterCount, 3); + assertEquals(counterB.leaveCount, 3); + + // Run the original and check that instrument B is still working but not A + assertEquals(13, callTarget2.call()); + assertEquals(counterA.enterCount, 1); + assertEquals(counterA.leaveCount, 1); + assertEquals(counterB.enterCount, 4); + assertEquals(counterB.leaveCount, 4); + + // Attach a second instrument to the probe + final TestCounter counterC = new TestCounter(); + counterC.attach(probe); + + // Run the original and check that instruments B,C working but not A + assertEquals(13, callTarget1.call()); + assertEquals(counterA.enterCount, 1); + assertEquals(counterA.leaveCount, 1); + assertEquals(counterB.enterCount, 5); + assertEquals(counterB.leaveCount, 5); + assertEquals(counterC.enterCount, 1); + assertEquals(counterC.leaveCount, 1); + + // Run the clone and check that instruments B,C working but not A + assertEquals(13, callTarget2.call()); + assertEquals(counterA.enterCount, 1); + assertEquals(counterA.leaveCount, 1); + assertEquals(counterB.enterCount, 6); + assertEquals(counterB.leaveCount, 6); + assertEquals(counterC.enterCount, 2); + assertEquals(counterC.leaveCount, 2); + + // Remove instrumentC + counterC.dispose(); + + // Run the original and check that instrument B working but not A,C + assertEquals(13, callTarget1.call()); + assertEquals(counterA.enterCount, 1); + assertEquals(counterA.leaveCount, 1); + assertEquals(counterB.enterCount, 7); + assertEquals(counterB.leaveCount, 7); + assertEquals(counterC.enterCount, 2); + assertEquals(counterC.leaveCount, 2); + + // Run the clone and check that instrument B working but not A,C + assertEquals(13, callTarget2.call()); + assertEquals(counterA.enterCount, 1); + assertEquals(counterA.leaveCount, 1); + assertEquals(counterB.enterCount, 8); + assertEquals(counterB.leaveCount, 8); + assertEquals(counterC.enterCount, 2); + assertEquals(counterC.leaveCount, 2); + + // Remove instrumentB + counterB.dispose(); + + // Run both the original and clone, check that no instruments working + assertEquals(13, callTarget1.call()); + assertEquals(13, callTarget2.call()); + assertEquals(counterA.enterCount, 1); + assertEquals(counterA.leaveCount, 1); + assertEquals(counterB.enterCount, 8); + assertEquals(counterB.leaveCount, 8); + assertEquals(counterC.enterCount, 2); + assertEquals(counterC.leaveCount, 2); + } + + @Test + public void testTagging() { + + // Applies appropriate tags + final TestASTProber astProber = new TestASTProber(); + Probe.registerASTProber(astProber); + + // Listens for probes and tags being added + final TestProbeListener probeListener = new TestProbeListener(); + Probe.addProbeListener(probeListener); + + // Counts all entries to all instances of addition nodes + final TestMultiCounter additionCounter = new TestMultiCounter(); + + // Counts all entries to all instances of value nodes + final TestMultiCounter valueCounter = new TestMultiCounter(); + + // Create a simple addition AST + final TruffleRuntime runtime = Truffle.getRuntime(); + final TestValueNode leftValueNode = new TestValueNode(6); + final TestValueNode rightValueNode = new TestValueNode(7); + final TestAdditionNode addNode = new TestAdditionNode(leftValueNode, rightValueNode); + + final TestRootNode rootNode = new TestRootNode(addNode); + + final CallTarget callTarget = runtime.createCallTarget(rootNode); + + // Check that the prober added probes to the tree + assertEquals(probeListener.probeCount, 3); + assertEquals(probeListener.tagCount, 3); + + assertEquals(Probe.findProbesTaggedAs(ADD_TAG).size(), 1); + assertEquals(Probe.findProbesTaggedAs(VALUE_TAG).size(), 2); + + // Check that it executes correctly + assertEquals(13, callTarget.call()); + + // Dynamically attach a counter for all executions of all Addition nodes + for (Probe probe : Probe.findProbesTaggedAs(ADD_TAG)) { + additionCounter.attachCounter(probe); + } + // Dynamically attach a counter for all executions of all Value nodes + for (Probe probe : Probe.findProbesTaggedAs(VALUE_TAG)) { + valueCounter.attachCounter(probe); + } + + // Counters initialized at 0 + assertEquals(additionCounter.count, 0); + assertEquals(valueCounter.count, 0); + + // Execute again + assertEquals(13, callTarget.call()); + + // There are two value nodes in the AST, but only one addition node + assertEquals(additionCounter.count, 1); + assertEquals(valueCounter.count, 2); + + Probe.unregisterASTProber(astProber); + + } + + @Test + public void testProbeLite() { + + // Use the "lite-probing" option, limited to a single pass of + // probing and a single Instrument at each probed node. This + // particular test uses a shared event receiver at every + // lite-probed node. + final TestEventReceiver receiver = new TestEventReceiver(); + + TestASTLiteProber astLiteProber = new TestASTLiteProber(receiver); + Probe.registerASTProber(astLiteProber); + + // Create a simple addition AST + final TruffleRuntime runtime = Truffle.getRuntime(); + final TestValueNode leftValueNode = new TestValueNode(6); + final TestValueNode rightValueNode = new TestValueNode(7); + final TestAdditionNode addNode = new TestAdditionNode(leftValueNode, rightValueNode); + final TestRootNode rootNode = new TestRootNode(addNode); + + // Creating a call target sets the parent pointers in this tree and is necessary prior to + // checking any parent/child relationships + final CallTarget callTarget = runtime.createCallTarget(rootNode); + + // Check that the instrument is working as expected. + assertEquals(0, receiver.counter); + callTarget.call(); + assertEquals(2, receiver.counter); + + // Check that you can't probe a node that's already received a probeLite() call + try { + leftValueNode.probe(); + fail(); + } catch (IllegalStateException e) { + } + + try { + rightValueNode.probe(); + fail(); + } catch (IllegalStateException e) { + } + + // Check tree structure + assertTrue(leftValueNode.getParent() instanceof TestLanguageWrapperNode); + assertTrue(rightValueNode.getParent() instanceof TestLanguageWrapperNode); + TestLanguageWrapperNode leftWrapper = (TestLanguageWrapperNode) leftValueNode.getParent(); + TestLanguageWrapperNode rightWrapper = (TestLanguageWrapperNode) rightValueNode.getParent(); + assertEquals(addNode, leftWrapper.getParent()); + assertEquals(addNode, rightWrapper.getParent()); + Iterator iterator = addNode.getChildren().iterator(); + assertEquals(leftWrapper, iterator.next()); + assertEquals(rightWrapper, iterator.next()); + assertFalse(iterator.hasNext()); + assertEquals(rootNode, addNode.getParent()); + iterator = rootNode.getChildren().iterator(); + assertEquals(addNode, iterator.next()); + assertFalse(iterator.hasNext()); + + // Check that you can't get a probe on the wrappers because they were "lite-probed" + try { + leftWrapper.getProbe(); + fail(); + } catch (IllegalStateException e) { + } + try { + rightWrapper.getProbe(); + fail(); + } catch (IllegalStateException e) { + } + + // Check that you can't probe the wrappers + try { + leftWrapper.probe(); + fail(); + } catch (ProbeException e) { + assertEquals(e.getFailure().getReason(), ProbeFailure.Reason.WRAPPER_NODE); + } + try { + rightWrapper.probe(); + fail(); + } catch (ProbeException e) { + assertEquals(e.getFailure().getReason(), ProbeFailure.Reason.WRAPPER_NODE); + } + try { + leftWrapper.probeLite(null); + fail(); + } catch (ProbeException e) { + assertEquals(e.getFailure().getReason(), ProbeFailure.Reason.WRAPPER_NODE); + } + try { + rightWrapper.probeLite(null); + fail(); + } catch (ProbeException e) { + assertEquals(e.getFailure().getReason(), ProbeFailure.Reason.WRAPPER_NODE); + } + + // Use reflection to check that each WrapperNode has a ProbeLiteNode with a + // SimpleEventReceiver + try { + java.lang.reflect.Field probeNodeField = leftWrapper.getClass().getDeclaredField("probeNode"); + + // cheat: probeNode is private, so we change it's accessibility at runtime + probeNodeField.setAccessible(true); + ProbeNode probeNode = (ProbeNode) probeNodeField.get(leftWrapper); + + // hack: Since ProbeLiteNode is not visible, we do a string compare here + assertTrue(probeNode.getClass().toString().endsWith("ProbeLiteNode")); + + // Now we do the same to check the type of the eventReceiver in ProbeLiteNode + java.lang.reflect.Field eventReceiverField = probeNode.getClass().getDeclaredField("eventReceiver"); + eventReceiverField.setAccessible(true); + TruffleEventReceiver eventReceiver = (TruffleEventReceiver) eventReceiverField.get(probeNode); + assertTrue(eventReceiver instanceof SimpleEventReceiver); + + // Reset accessibility + probeNodeField.setAccessible(false); + eventReceiverField.setAccessible(false); + + } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { + fail(); + } + + Probe.unregisterASTProber(astLiteProber); + + } + + private abstract class TestLanguageNode extends Node { + public abstract Object execute(VirtualFrame frame); + + @Override + public boolean isInstrumentable() { + return true; + } + + @Override + public WrapperNode createWrapperNode() { + return new TestLanguageWrapperNode(this); + } + } + + @NodeInfo(cost = NodeCost.NONE) + private class TestLanguageWrapperNode extends TestLanguageNode implements WrapperNode { + @Child private TestLanguageNode child; + @Child private ProbeNode probeNode; + + public TestLanguageWrapperNode(TestLanguageNode child) { + assert !(child instanceof TestLanguageWrapperNode); + this.child = child; + } + + @Override + public String instrumentationInfo() { + return "Wrapper node for testing"; + } + + @Override + public boolean isInstrumentable() { + return false; + } + + @Override + public void insertProbe(ProbeNode newProbeNode) { + this.probeNode = newProbeNode; + } + + @Override + public Probe getProbe() { + try { + return probeNode.getProbe(); + } catch (IllegalStateException e) { + throw new IllegalStateException("Cannot call getProbe() on a wrapper that has no probe"); + } + } + + @Override + public Node getChild() { + return child; + } + + @Override + public Object execute(VirtualFrame frame) { + probeNode.enter(child, frame); + Object result; + + try { + result = child.execute(frame); + probeNode.returnValue(child, frame, result); + } catch (KillException e) { + throw (e); + } catch (Exception e) { + probeNode.returnExceptional(child, frame, e); + throw (e); + } + + return result; + } + } + + /** + * A simple node for our test language to store a value. + */ + private class TestValueNode extends TestLanguageNode { + private final int value; + + public TestValueNode(int value) { + this.value = value; + } + + @Override + public Object execute(VirtualFrame frame) { + return new Integer(this.value); + } + } + + /** + * A node for our test language that adds up two {@link TestValueNode}s. + */ + private class TestAdditionNode extends TestLanguageNode { + @Child private TestLanguageNode leftChild; + @Child private TestLanguageNode rightChild; + + public TestAdditionNode(TestValueNode leftChild, TestValueNode rightChild) { + this.leftChild = insert(leftChild); + this.rightChild = insert(rightChild); + } + + @Override + public Object execute(VirtualFrame frame) { + return new Integer(((Integer) leftChild.execute(frame)).intValue() + ((Integer) rightChild.execute(frame)).intValue()); + } + } + + /** + * Truffle requires that all guest languages to have a {@link RootNode} which sits atop any AST + * of the guest language. This is necessary since creating a {@link CallTarget} is how Truffle + * completes an AST. The root nodes serves as our entry point into a program. + */ + private class TestRootNode extends RootNode { + @Child private TestLanguageNode body; + + /** + * This constructor emulates the global machinery that applies registered probers to every + * newly created AST. Global registry is not used, since that would interfere with other + * tests run in the same environment. + */ + public TestRootNode(TestLanguageNode body) { + super(null); + this.body = body; + } + + @Override + public Object execute(VirtualFrame frame) { + return body.execute(frame); + } + + @Override + public boolean isCloningAllowed() { + return true; + } + + @Override + public void applyInstrumentation() { + Probe.applyASTProbers(body); + } + } + + /** + * A counter for the number of times execution enters and leaves a probed AST node. + */ + private class TestCounter { + + public int enterCount = 0; + public int leaveCount = 0; + public final Instrument instrument; + + public TestCounter() { + instrument = Instrument.create(new SimpleEventReceiver() { + + @Override + public void enter(Node node, VirtualFrame frame) { + enterCount++; + } + + @Override + public void returnAny(Node node, VirtualFrame frame) { + leaveCount++; + } + }, "Instrumentation Test Counter"); + } + + public void attach(Probe probe) { + probe.attach(instrument); + } + + public void dispose() { + instrument.dispose(); + } + + } + + /** + * Tags selected nodes on newly constructed ASTs. + */ + private static final class TestASTProber implements NodeVisitor, ASTProber { + + @Override + public boolean visit(Node node) { + if (node instanceof TestLanguageNode) { + + final TestLanguageNode testNode = (TestLanguageNode) node; + + if (node instanceof TestValueNode) { + testNode.probe().tagAs(VALUE_TAG, null); + + } else if (node instanceof TestAdditionNode) { + testNode.probe().tagAs(ADD_TAG, null); + + } + } + return true; + } + + @Override + public void probeAST(Node node) { + node.accept(this); + } + } + + /** + * "lite-probes" every value node with a shared event receiver. + */ + private static final class TestASTLiteProber implements NodeVisitor, ASTProber { + private final TruffleEventReceiver eventReceiver; + + public TestASTLiteProber(SimpleEventReceiver simpleEventReceiver) { + this.eventReceiver = simpleEventReceiver; + } + + public boolean visit(Node node) { + if (node instanceof TestValueNode) { + final TestLanguageNode testNode = (TestValueNode) node; + testNode.probeLite(eventReceiver); + } + return true; + } + + public void probeAST(Node node) { + node.accept(this); + } + } + + /** + * Counts the number of "enter" events at probed nodes. + * + */ + static final class TestEventReceiver extends SimpleEventReceiver { + + public int counter = 0; + + @Override + public void enter(Node node, VirtualFrame frame) { + counter++; + } + + } + + /** + * A counter that can count executions at multiple nodes; it attaches a separate instrument at + * each Probe, but keeps a total count. + */ + private static final class TestMultiCounter { + + public int count = 0; + + public void attachCounter(Probe probe) { + + // Attach a new instrument for every Probe + // where we want to count executions. + // it will get copied when ASTs cloned, so + // keep the count in this outer class. + probe.attach(Instrument.create(new SimpleEventReceiver() { + + @Override + public void enter(Node node, VirtualFrame frame) { + count++; + } + }, "Instrumentation Test MultiCounter")); + } + } + + private static final class TestProbeListener extends DefaultProbeListener { + + public int probeCount = 0; + public int tagCount = 0; + + @Override + public void newProbeInserted(Probe probe) { + probeCount++; + } + + @Override + public void probeTaggedAs(Probe probe, SyntaxTag tag, Object tagValue) { + tagCount++; + } + } + +} diff -r cca166aa28c0 -r acd822f17ef5 graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/package-info.java --- a/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/package-info.java Tue Jan 27 20:27:25 2015 -0800 +++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/package-info.java Tue Jan 27 20:28:19 2015 -0800 @@ -44,7 +44,7 @@ *
  • How to use frames and frame slots to store values local to an activation? {@link com.oracle.truffle.api.test.FrameTest}
  • *
  • How to use type specialization and speculation for frame slots? {@link com.oracle.truffle.api.test.FrameSlotTypeSpecializationTest}
  • *
  • How to use type specialization and speculation for node return values? {@link com.oracle.truffle.api.test.ReturnTypeSpecializationTest}
  • - *
  • How to "instrument" an AST with nodes that can provide access to runtime state from external tools {@link com.oracle.truffle.api.test.InstrumentationTest}
  • + *
  • How to "instrument" an AST with nodes that can provide access to runtime state from external tools {@link com.oracle.truffle.api.test.instrument.InstrumentationTest}
  • * * * diff -r cca166aa28c0 -r acd822f17ef5 graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/source/SourceTextTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/source/SourceTextTest.java Tue Jan 27 20:28:19 2015 -0800 @@ -0,0 +1,202 @@ +/* + * Copyright (c) 2013, 2015, 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.test.source; + +import static org.junit.Assert.*; + +import org.junit.*; + +import com.oracle.truffle.api.source.*; + +public class SourceTextTest { + + private final Source emptySource = Source.fromText("", null); + + private final Source emptyLineSource = Source.fromText("\n", null); + + private final Source shortSource = Source.fromText("01", null); + + private final Source longSource = Source.fromText("01234\n67\n9\n", null); + + @Test + public void emptyTextTest0() { + assertEquals(emptySource.getLineCount(), 0); + } + + @Test(expected = IllegalArgumentException.class) + public void emptyTextTest1() { + emptySource.getLineNumber(0); + } + + @Test(expected = IllegalArgumentException.class) + public void emptyTextTest2() { + emptySource.getColumnNumber(0); + } + + @Test(expected = IllegalArgumentException.class) + public void emptyTextTest3() { + emptySource.getLineNumber(-1); + } + + @Test(expected = IllegalArgumentException.class) + public void emptyTextTest4() { + emptySource.getLineStartOffset(0); + } + + @Test(expected = IllegalArgumentException.class) + public void emptyTextTest5() { + emptySource.getLineStartOffset(1); + } + + @Test(expected = IllegalArgumentException.class) + public void emptyTextTest6() { + emptySource.getLineLength(1); + } + + @Test + public void emptyLineTest0() { + assertEquals(emptyLineSource.getLineCount(), 1); + assertEquals(emptyLineSource.getLineNumber(0), 1); + assertEquals(emptyLineSource.getLineStartOffset(1), 0); + assertEquals(emptyLineSource.getColumnNumber(0), 1); + assertEquals(emptyLineSource.getLineLength(1), 0); + } + + @Test(expected = IllegalArgumentException.class) + public void emptyLineTest1() { + emptyLineSource.getLineNumber(1); + } + + @Test(expected = IllegalArgumentException.class) + public void emptyLineTest2() { + emptyLineSource.getLineStartOffset(2); + } + + @Test(expected = IllegalArgumentException.class) + public void emptyLineTest3() { + emptyLineSource.getColumnNumber(1); + } + + @Test(expected = IllegalArgumentException.class) + public void emptyLineTest4() { + emptyLineSource.getLineLength(2); + } + + @Test + public void shortTextTest0() { + + assertEquals(shortSource.getLineCount(), 1); + + assertEquals(shortSource.getLineNumber(0), 1); + assertEquals(shortSource.getLineStartOffset(1), 0); + assertEquals(shortSource.getColumnNumber(0), 1); + + assertEquals(shortSource.getLineNumber(1), 1); + assertEquals(shortSource.getColumnNumber(1), 2); + + assertEquals(shortSource.getLineLength(1), 2); + } + + @Test(expected = IllegalArgumentException.class) + public void shortTextTest1() { + shortSource.getLineNumber(-1); + } + + @Test(expected = IllegalArgumentException.class) + public void shortTextTest2() { + shortSource.getColumnNumber(-1); + } + + @Test(expected = IllegalArgumentException.class) + public void shortTextTest3() { + shortSource.getLineNumber(2); + } + + @Test(expected = IllegalArgumentException.class) + public void shortTextTest4() { + shortSource.getColumnNumber(2); + } + + @Test(expected = IllegalArgumentException.class) + public void shortTextTest5() { + shortSource.getLineLength(2); + } + + @Test(expected = IllegalArgumentException.class) + public void shortTextTest6() { + shortSource.getLineLength(2); + } + + @Test + public void longTextTest0() { + + assertEquals(longSource.getLineCount(), 3); + + assertEquals(longSource.getLineNumber(0), 1); + assertEquals(longSource.getLineStartOffset(1), 0); + assertEquals(longSource.getColumnNumber(0), 1); + + assertEquals(longSource.getLineNumber(4), 1); + assertEquals(longSource.getColumnNumber(4), 5); + + assertEquals(longSource.getLineNumber(5), 1); // newline + assertEquals(longSource.getColumnNumber(5), 6); // newline + assertEquals(longSource.getLineLength(1), 5); + + assertEquals(longSource.getLineNumber(6), 2); + assertEquals(longSource.getLineStartOffset(2), 6); + assertEquals(longSource.getColumnNumber(6), 1); + + assertEquals(longSource.getLineNumber(7), 2); + assertEquals(longSource.getColumnNumber(7), 2); + + assertEquals(longSource.getLineNumber(8), 2); // newline + assertEquals(longSource.getLineNumber(8), 2); // newline + assertEquals(longSource.getLineLength(2), 2); + + assertEquals(longSource.getLineNumber(9), 3); + assertEquals(longSource.getLineStartOffset(3), 9); + assertEquals(longSource.getColumnNumber(9), 1); + + assertEquals(longSource.getLineNumber(10), 3); // newline + assertEquals(longSource.getColumnNumber(10), 2); // newline + assertEquals(longSource.getLineLength(3), 1); + + } + + @Test(expected = IllegalArgumentException.class) + public void longTextTest1() { + longSource.getLineNumber(11); + } + + @Test(expected = IllegalArgumentException.class) + public void longTextTest2() { + longSource.getColumnNumber(11); + } + + @Test(expected = IllegalArgumentException.class) + public void longTextTest3() { + longSource.getLineStartOffset(4); + } + +} diff -r cca166aa28c0 -r acd822f17ef5 graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/SourceTextTest.java --- a/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/SourceTextTest.java Tue Jan 27 20:27:25 2015 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,202 +0,0 @@ -/* - * 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.truffle.api.test.utilities; - -import static org.junit.Assert.*; - -import org.junit.*; - -import com.oracle.truffle.api.source.*; - -public class SourceTextTest { - - private final Source emptySource = Source.fromText("", null); - - private final Source emptyLineSource = Source.fromText("\n", null); - - private final Source shortSource = Source.fromText("01", null); - - private final Source longSource = Source.fromText("01234\n67\n9\n", null); - - @Test - public void emptyTextTest0() { - assertEquals(emptySource.getLineCount(), 0); - } - - @Test(expected = IllegalArgumentException.class) - public void emptyTextTest1() { - emptySource.getLineNumber(0); - } - - @Test(expected = IllegalArgumentException.class) - public void emptyTextTest2() { - emptySource.getColumnNumber(0); - } - - @Test(expected = IllegalArgumentException.class) - public void emptyTextTest3() { - emptySource.getLineNumber(-1); - } - - @Test(expected = IllegalArgumentException.class) - public void emptyTextTest4() { - emptySource.getLineStartOffset(0); - } - - @Test(expected = IllegalArgumentException.class) - public void emptyTextTest5() { - emptySource.getLineStartOffset(1); - } - - @Test(expected = IllegalArgumentException.class) - public void emptyTextTest6() { - emptySource.getLineLength(1); - } - - @Test - public void emptyLineTest0() { - assertEquals(emptyLineSource.getLineCount(), 1); - assertEquals(emptyLineSource.getLineNumber(0), 1); - assertEquals(emptyLineSource.getLineStartOffset(1), 0); - assertEquals(emptyLineSource.getColumnNumber(0), 1); - assertEquals(emptyLineSource.getLineLength(1), 0); - } - - @Test(expected = IllegalArgumentException.class) - public void emptyLineTest1() { - emptyLineSource.getLineNumber(1); - } - - @Test(expected = IllegalArgumentException.class) - public void emptyLineTest2() { - emptyLineSource.getLineStartOffset(2); - } - - @Test(expected = IllegalArgumentException.class) - public void emptyLineTest3() { - emptyLineSource.getColumnNumber(1); - } - - @Test(expected = IllegalArgumentException.class) - public void emptyLineTest4() { - emptyLineSource.getLineLength(2); - } - - @Test - public void shortTextTest0() { - - assertEquals(shortSource.getLineCount(), 1); - - assertEquals(shortSource.getLineNumber(0), 1); - assertEquals(shortSource.getLineStartOffset(1), 0); - assertEquals(shortSource.getColumnNumber(0), 1); - - assertEquals(shortSource.getLineNumber(1), 1); - assertEquals(shortSource.getColumnNumber(1), 2); - - assertEquals(shortSource.getLineLength(1), 2); - } - - @Test(expected = IllegalArgumentException.class) - public void shortTextTest1() { - shortSource.getLineNumber(-1); - } - - @Test(expected = IllegalArgumentException.class) - public void shortTextTest2() { - shortSource.getColumnNumber(-1); - } - - @Test(expected = IllegalArgumentException.class) - public void shortTextTest3() { - shortSource.getLineNumber(2); - } - - @Test(expected = IllegalArgumentException.class) - public void shortTextTest4() { - shortSource.getColumnNumber(2); - } - - @Test(expected = IllegalArgumentException.class) - public void shortTextTest5() { - shortSource.getLineLength(2); - } - - @Test(expected = IllegalArgumentException.class) - public void shortTextTest6() { - shortSource.getLineLength(2); - } - - @Test - public void longTextTest0() { - - assertEquals(longSource.getLineCount(), 3); - - assertEquals(longSource.getLineNumber(0), 1); - assertEquals(longSource.getLineStartOffset(1), 0); - assertEquals(longSource.getColumnNumber(0), 1); - - assertEquals(longSource.getLineNumber(4), 1); - assertEquals(longSource.getColumnNumber(4), 5); - - assertEquals(longSource.getLineNumber(5), 1); // newline - assertEquals(longSource.getColumnNumber(5), 6); // newline - assertEquals(longSource.getLineLength(1), 5); - - assertEquals(longSource.getLineNumber(6), 2); - assertEquals(longSource.getLineStartOffset(2), 6); - assertEquals(longSource.getColumnNumber(6), 1); - - assertEquals(longSource.getLineNumber(7), 2); - assertEquals(longSource.getColumnNumber(7), 2); - - assertEquals(longSource.getLineNumber(8), 2); // newline - assertEquals(longSource.getLineNumber(8), 2); // newline - assertEquals(longSource.getLineLength(2), 2); - - assertEquals(longSource.getLineNumber(9), 3); - assertEquals(longSource.getLineStartOffset(3), 9); - assertEquals(longSource.getColumnNumber(9), 1); - - assertEquals(longSource.getLineNumber(10), 3); // newline - assertEquals(longSource.getColumnNumber(10), 2); // newline - assertEquals(longSource.getLineLength(3), 1); - - } - - @Test(expected = IllegalArgumentException.class) - public void longTextTest1() { - longSource.getLineNumber(11); - } - - @Test(expected = IllegalArgumentException.class) - public void longTextTest2() { - longSource.getColumnNumber(11); - } - - @Test(expected = IllegalArgumentException.class) - public void longTextTest3() { - longSource.getLineStartOffset(4); - } - -}