comparison truffle/com.oracle.truffle.tools.test/src/com/oracle/truffle/tools/test/NodeExecCounterTest.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.tools.test/src/com/oracle/truffle/tools/test/NodeExecCounterTest.java@3b8bbf51d320
children dc83cc1f94f2 3aad794eec0e
comparison
equal deleted inserted replaced
21950:2a5011c7e641 21951:9c8c0937da41
1 /*
2 * Copyright (c) 2014, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package com.oracle.truffle.tools.test;
26
27 import static com.oracle.truffle.tools.test.TestNodes.*;
28 import static org.junit.Assert.*;
29
30 import org.junit.*;
31
32 import com.oracle.truffle.api.*;
33 import com.oracle.truffle.api.instrument.*;
34 import com.oracle.truffle.api.nodes.*;
35 import com.oracle.truffle.tools.*;
36 import com.oracle.truffle.tools.NodeExecCounter.NodeExecutionCount;
37 import com.oracle.truffle.tools.test.TestNodes.TestAddNode;
38 import com.oracle.truffle.tools.test.TestNodes.TestValueNode;
39
40 public class NodeExecCounterTest {
41
42 @Test
43 public void testNoExecution() {
44 final NodeExecCounter tool = new NodeExecCounter();
45 assertEquals(tool.getCounts().length, 0);
46 tool.install();
47 assertEquals(tool.getCounts().length, 0);
48 tool.setEnabled(false);
49 assertEquals(tool.getCounts().length, 0);
50 tool.setEnabled(true);
51 assertEquals(tool.getCounts().length, 0);
52 tool.reset();
53 assertEquals(tool.getCounts().length, 0);
54 tool.dispose();
55 assertEquals(tool.getCounts().length, 0);
56 }
57
58 @Test
59 public void testToolCreatedTooLate() {
60 final CallTarget expr13callTarget = createExpr13TestCallTarget();
61 final NodeExecCounter tool = new NodeExecCounter();
62 tool.install();
63 assertEquals(13, expr13callTarget.call());
64 assertEquals(tool.getCounts().length, 0);
65 tool.dispose();
66 }
67
68 @Test
69 public void testToolInstalledcTooLate() {
70 final NodeExecCounter tool = new NodeExecCounter();
71 final CallTarget expr13callTarget = createExpr13TestCallTarget();
72 tool.install();
73 assertEquals(13, expr13callTarget.call());
74 assertEquals(tool.getCounts().length, 0);
75 tool.dispose();
76 }
77
78 @Test
79 public void testCountingAll() {
80 final NodeExecCounter tool = new NodeExecCounter();
81 tool.install();
82 final CallTarget expr13callTarget = createExpr13TestCallTarget();
83
84 // execute once
85 assertEquals(13, expr13callTarget.call());
86 final NodeExecutionCount[] count1 = tool.getCounts();
87 assertNotNull(count1);
88 assertEquals(count1.length, 2);
89 for (NodeExecutionCount count : count1) {
90 final Class<?> class1 = count.nodeClass();
91 final long executionCount = count.executionCount();
92 if (class1 == TestAddNode.class) {
93 assertEquals(executionCount, 1);
94 } else if (class1 == TestValueNode.class) {
95 assertEquals(executionCount, 2);
96 } else {
97 fail();
98 }
99 }
100
101 // Execute 99 more times
102 for (int i = 0; i < 99; i++) {
103 assertEquals(13, expr13callTarget.call());
104 }
105 final NodeExecutionCount[] counts100 = tool.getCounts();
106 assertNotNull(counts100);
107 assertEquals(counts100.length, 2);
108 for (NodeExecutionCount count : counts100) {
109 final Class<?> class1 = count.nodeClass();
110 final long executionCount = count.executionCount();
111 if (class1 == TestAddNode.class) {
112 assertEquals(executionCount, 100);
113 } else if (class1 == TestValueNode.class) {
114 assertEquals(executionCount, 200);
115 } else {
116 fail();
117 }
118 }
119
120 tool.dispose();
121 }
122
123 @Test
124 public void testCountingTagged() {
125 final NodeExecCounter tool = new NodeExecCounter(StandardSyntaxTag.STATEMENT);
126 tool.install();
127 final RootNode expr13rootNode = createExpr13TestRootNode();
128
129 // Not probed yet.
130 assertEquals(13, expr13rootNode.execute(null));
131 assertEquals(tool.getCounts().length, 0);
132
133 final Node addNode = expr13rootNode.getChildren().iterator().next();
134 final Probe probe = addNode.probe();
135
136 // Probed but not tagged yet.
137 assertEquals(13, expr13rootNode.execute(null));
138 assertEquals(tool.getCounts().length, 0);
139
140 probe.tagAs(StandardSyntaxTag.STATEMENT, "fake statement for testing");
141
142 // Counting now; execute once
143 assertEquals(13, expr13rootNode.execute(null));
144 final NodeExecutionCount[] counts1 = tool.getCounts();
145 assertNotNull(counts1);
146 assertEquals(counts1.length, 1);
147 final NodeExecutionCount count1 = counts1[0];
148 assertNotNull(count1);
149 assertEquals(count1.nodeClass(), addNode.getClass());
150 assertEquals(count1.executionCount(), 1);
151
152 // Execute 99 more times
153 for (int i = 0; i < 99; i++) {
154 assertEquals(13, expr13rootNode.execute(null));
155 }
156
157 final NodeExecutionCount[] counts100 = tool.getCounts();
158 assertNotNull(counts100);
159 assertEquals(counts100.length, 1);
160 final NodeExecutionCount count100 = counts100[0];
161 assertNotNull(count100);
162 assertEquals(count100.nodeClass(), addNode.getClass());
163 assertEquals(count100.executionCount(), 100);
164
165 tool.dispose();
166 }
167 }