comparison graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/tools/NodeExecCounterTest.java @ 18988:cca166aa28c0

Truffle/Tools: unit tests for new framework and tool instances
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Tue, 27 Jan 2015 20:27:25 -0800
parents
children
comparison
equal deleted inserted replaced
18987:ac114ad31cdd 18988:cca166aa28c0
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.tools;
24
25 import static com.oracle.truffle.api.test.tools.TestNodes.*;
26 import static org.junit.Assert.*;
27
28 import org.junit.*;
29
30 import com.oracle.truffle.api.*;
31 import com.oracle.truffle.api.instrument.*;
32 import com.oracle.truffle.api.nodes.*;
33 import com.oracle.truffle.api.test.tools.TestNodes.TestAddNode;
34 import com.oracle.truffle.api.test.tools.TestNodes.TestValueNode;
35 import com.oracle.truffle.api.tools.*;
36 import com.oracle.truffle.api.tools.NodeExecCounter.NodeExecutionCount;
37
38 public class NodeExecCounterTest {
39
40 @Test
41 public void testNoExecution() {
42 final NodeExecCounter tool = new NodeExecCounter();
43 assertEquals(tool.getCounts().length, 0);
44 tool.install();
45 assertEquals(tool.getCounts().length, 0);
46 tool.setEnabled(false);
47 assertEquals(tool.getCounts().length, 0);
48 tool.setEnabled(true);
49 assertEquals(tool.getCounts().length, 0);
50 tool.reset();
51 assertEquals(tool.getCounts().length, 0);
52 tool.dispose();
53 assertEquals(tool.getCounts().length, 0);
54 }
55
56 @Test
57 public void testToolCreatedTooLate() {
58 final CallTarget expr13callTarget = createExpr13TestCallTarget();
59 final NodeExecCounter tool = new NodeExecCounter();
60 tool.install();
61 assertEquals(13, expr13callTarget.call());
62 assertEquals(tool.getCounts().length, 0);
63 tool.dispose();
64 }
65
66 @Test
67 public void testToolInstalledcTooLate() {
68 final NodeExecCounter tool = new NodeExecCounter();
69 final CallTarget expr13callTarget = createExpr13TestCallTarget();
70 tool.install();
71 assertEquals(13, expr13callTarget.call());
72 assertEquals(tool.getCounts().length, 0);
73 tool.dispose();
74 }
75
76 @Test
77 public void testCountingAll() {
78 final NodeExecCounter tool = new NodeExecCounter();
79 tool.install();
80 final CallTarget expr13callTarget = createExpr13TestCallTarget();
81
82 // execute once
83 assertEquals(13, expr13callTarget.call());
84 final NodeExecutionCount[] count1 = tool.getCounts();
85 assertNotNull(count1);
86 assertEquals(count1.length, 2);
87 for (NodeExecutionCount count : count1) {
88 final Class<?> class1 = count.nodeClass();
89 final long executionCount = count.executionCount();
90 if (class1 == TestAddNode.class) {
91 assertEquals(executionCount, 1);
92 } else if (class1 == TestValueNode.class) {
93 assertEquals(executionCount, 2);
94 } else {
95 fail();
96 }
97 }
98
99 // Execute 99 more times
100 for (int i = 0; i < 99; i++) {
101 assertEquals(13, expr13callTarget.call());
102 }
103 final NodeExecutionCount[] counts100 = tool.getCounts();
104 assertNotNull(counts100);
105 assertEquals(counts100.length, 2);
106 for (NodeExecutionCount count : counts100) {
107 final Class<?> class1 = count.nodeClass();
108 final long executionCount = count.executionCount();
109 if (class1 == TestAddNode.class) {
110 assertEquals(executionCount, 100);
111 } else if (class1 == TestValueNode.class) {
112 assertEquals(executionCount, 200);
113 } else {
114 fail();
115 }
116 }
117
118 tool.dispose();
119 }
120
121 @Test
122 public void testCountingTagged() {
123 final NodeExecCounter tool = new NodeExecCounter(StandardSyntaxTag.STATEMENT);
124 tool.install();
125 final RootNode expr13rootNode = createExpr13TestRootNode();
126
127 // Not probed yet.
128 assertEquals(13, expr13rootNode.execute(null));
129 assertEquals(tool.getCounts().length, 0);
130
131 final Node addNode = expr13rootNode.getChildren().iterator().next();
132 final Probe probe = addNode.probe();
133
134 // Probed but not tagged yet.
135 assertEquals(13, expr13rootNode.execute(null));
136 assertEquals(tool.getCounts().length, 0);
137
138 probe.tagAs(StandardSyntaxTag.STATEMENT, "fake statement for testing");
139
140 // Counting now; execute once
141 assertEquals(13, expr13rootNode.execute(null));
142 final NodeExecutionCount[] counts1 = tool.getCounts();
143 assertNotNull(counts1);
144 assertEquals(counts1.length, 1);
145 final NodeExecutionCount count1 = counts1[0];
146 assertNotNull(count1);
147 assertEquals(count1.nodeClass(), addNode.getClass());
148 assertEquals(count1.executionCount(), 1);
149
150 // Execute 99 more times
151 for (int i = 0; i < 99; i++) {
152 assertEquals(13, expr13rootNode.execute(null));
153 }
154
155 final NodeExecutionCount[] counts100 = tool.getCounts();
156 assertNotNull(counts100);
157 assertEquals(counts100.length, 1);
158 final NodeExecutionCount count100 = counts100[0];
159 assertNotNull(count100);
160 assertEquals(count100.nodeClass(), addNode.getClass());
161 assertEquals(count100.executionCount(), 100);
162
163 tool.dispose();
164 }
165 }