comparison truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/instrument/AdvancedInstrumentTest.java @ 21951:9c8c0937da41

Moving all sources into truffle subdirectory
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 17 Jun 2015 10:58:08 +0200
parents graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/instrument/AdvancedInstrumentTest.java@47bebae7454f
children dc83cc1f94f2 3aad794eec0e
comparison
equal deleted inserted replaced
21950:2a5011c7e641 21951:9c8c0937da41
1 /*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.oracle.truffle.api.test.instrument;
24
25 import static org.junit.Assert.*;
26
27 import org.junit.*;
28
29 import com.oracle.truffle.api.*;
30 import com.oracle.truffle.api.instrument.*;
31 import com.oracle.truffle.api.nodes.*;
32 import com.oracle.truffle.api.test.instrument.InstrumentationTestNodes.TestAdditionNode;
33 import com.oracle.truffle.api.test.instrument.InstrumentationTestNodes.TestAdvancedInstrumentCounterRoot;
34 import com.oracle.truffle.api.test.instrument.InstrumentationTestNodes.TestRootNode;
35 import com.oracle.truffle.api.test.instrument.InstrumentationTestNodes.TestValueNode;
36
37 /**
38 * Tests the kind of instrumentation where a client can provide an AST fragment to be
39 * <em>spliced</em> directly into the AST.
40 */
41 public class AdvancedInstrumentTest {
42
43 @Test
44 public void testAdvancedInstrumentListener() {
45 // Create a simple addition AST
46 final TruffleRuntime runtime = Truffle.getRuntime();
47 final TestValueNode leftValueNode = new TestValueNode(6);
48 final TestValueNode rightValueNode = new TestValueNode(7);
49 final TestAdditionNode addNode = new TestAdditionNode(leftValueNode, rightValueNode);
50 final TestRootNode rootNode = new TestRootNode(addNode);
51 final CallTarget callTarget1 = runtime.createCallTarget(rootNode);
52
53 // Ensure it executes correctly
54 assertEquals(13, callTarget1.call());
55
56 // Probe the addition node
57 final Probe probe = addNode.probe();
58
59 assertEquals(13, callTarget1.call());
60
61 // Attach a null factory; it never actually attaches a node.
62 final Instrument instrument = Instrument.create(null, new AdvancedInstrumentRootFactory() {
63
64 public AdvancedInstrumentRoot createInstrumentRoot(Probe p, Node n) {
65 return null;
66 }
67 }, null, "test AdvancedInstrument");
68 probe.attach(instrument);
69
70 assertEquals(13, callTarget1.call());
71
72 final TestAdvancedInstrumentCounterRoot counter = new TestAdvancedInstrumentCounterRoot();
73
74 // Attach a factory that splices an execution counter into the AST.
75 probe.attach(Instrument.create(null, new AdvancedInstrumentRootFactory() {
76
77 public AdvancedInstrumentRoot createInstrumentRoot(Probe p, Node n) {
78 return counter;
79 }
80 }, null, "test AdvancedInstrument"));
81 assertEquals(0, counter.getCount());
82
83 assertEquals(13, callTarget1.call());
84
85 assertEquals(1, counter.getCount());
86 }
87 }