comparison graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/GuardsTest.java @ 8596:6ef9fc7375c7

Updated codegen tests for guards and builtins.
author Christian Humer <christian.humer@gmail.com>
date Mon, 01 Apr 2013 21:43:39 +0200
parents
children 5f7f0d3e3638
comparison
equal deleted inserted replaced
8595:8a1115c92271 8596:6ef9fc7375c7
1 /*
2 * Copyright (c) 2012, 2012, 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.codegen.test;
24
25 import static com.oracle.truffle.api.codegen.test.TestHelper.*;
26 import static junit.framework.Assert.*;
27
28 import org.junit.*;
29
30 import com.oracle.truffle.api.codegen.*;
31 import com.oracle.truffle.api.codegen.test.GuardsTestFactory.GlobalFlagGuardFactory;
32 import com.oracle.truffle.api.codegen.test.GuardsTestFactory.InvocationGuardFactory;
33 import com.oracle.truffle.api.codegen.test.TypeSystemTest.ChildrenNode;
34 import com.oracle.truffle.api.codegen.test.TypeSystemTest.TestRootNode;
35 import com.oracle.truffle.api.codegen.test.TypeSystemTest.ValueNode;
36
37 @SuppressWarnings("unused")
38 public class GuardsTest {
39
40 private static final Object NULL = new Object();
41
42 @Test
43 public void testGuardInvocations() {
44 TestRootNode<InvocationGuard> root = create(InvocationGuardFactory.getInstance());
45
46 assertEquals(Integer.MAX_VALUE, executeWith(root, Integer.MAX_VALUE - 1, 1));
47 assertEquals(1, InvocationGuard.specializedInvocations);
48 assertEquals(0, InvocationGuard.genericInvocations);
49
50 assertEquals(42, executeWith(root, Integer.MAX_VALUE, 1));
51 assertEquals(1, InvocationGuard.specializedInvocations);
52 assertEquals(1, InvocationGuard.genericInvocations);
53 }
54
55 public abstract static class InvocationGuard extends ChildrenNode {
56
57 static int specializedInvocations = 0;
58 static int genericInvocations = 0;
59
60 public InvocationGuard(ValueNode... children) {
61 super(children);
62 }
63
64 public InvocationGuard(InvocationGuard node) {
65 super(node);
66 }
67
68 boolean guard(int value0, int value1) {
69 return value0 != Integer.MAX_VALUE;
70 }
71
72 @Specialization(guards = "guard")
73 int doSpecialized(int value0, int value1) {
74 specializedInvocations++;
75 return value0 + value1;
76 }
77
78 @Generic
79 int doGeneric(Object value0, Object value1) {
80 genericInvocations++;
81 return 42; // the generic answer to all questions
82 }
83 }
84
85 @Test
86 public void testGuardGlobal() {
87 TestRootNode<GlobalFlagGuard> root = create(GlobalFlagGuardFactory.getInstance());
88
89 assertEquals(42, executeWith(root, NULL));
90
91 GlobalFlagGuard.globalFlag = true;
92 assertEquals(41, executeWith(root, NULL));
93
94 GlobalFlagGuard.globalFlag = false;
95 assertEquals(42, executeWith(root, NULL));
96 }
97
98 public abstract static class GlobalFlagGuard extends ChildrenNode {
99
100 static boolean globalFlag = false;
101
102 public GlobalFlagGuard(ValueNode... children) {
103 super(children);
104 }
105
106 public GlobalFlagGuard(GlobalFlagGuard node) {
107 super(node);
108 }
109
110 static boolean globalFlagGuard() {
111 return globalFlag;
112 }
113
114 @Specialization(guards = "globalFlagGuard")
115 int doSpecialized(Object value0) {
116 return 41;
117 }
118
119 @Generic
120 int doGeneric(Object value0) {
121 return 42; // the generic answer to all questions
122 }
123 }
124
125 }