comparison truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/LimitTest.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.dsl.test/src/com/oracle/truffle/api/dsl/test/LimitTest.java@62c43fcf5be2
children dc83cc1f94f2
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.dsl.test;
24
25 import static com.oracle.truffle.api.dsl.test.TestHelper.*;
26 import static org.junit.Assert.*;
27
28 import org.junit.*;
29
30 import com.oracle.truffle.api.*;
31 import com.oracle.truffle.api.dsl.*;
32 import com.oracle.truffle.api.dsl.test.LimitTestFactory.ConstantLimitTestFactory;
33 import com.oracle.truffle.api.dsl.test.LimitTestFactory.DefaultLimit3TestFactory;
34 import com.oracle.truffle.api.dsl.test.LimitTestFactory.LocalLimitTestFactory;
35 import com.oracle.truffle.api.dsl.test.LimitTestFactory.MethodLimitTestFactory;
36 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode;
37
38 @SuppressWarnings("unused")
39 public class LimitTest {
40
41 @Test
42 public void testDefaultLimit3() {
43 CallTarget root = createCallTarget(DefaultLimit3TestFactory.getInstance());
44 assertEquals(42, root.call(42));
45 assertEquals(43, root.call(43));
46 assertEquals(44, root.call(44));
47 try {
48 root.call(45);
49 fail();
50 } catch (UnsupportedSpecializationException e) {
51 }
52 }
53
54 @NodeChild
55 static class DefaultLimit3Test extends ValueNode {
56 @Specialization(guards = "value == cachedValue")
57 static int do1(int value, @Cached("value") int cachedValue) {
58 return cachedValue;
59 }
60 }
61
62 @Test
63 public void testConstantLimit() {
64 CallTarget root = createCallTarget(ConstantLimitTestFactory.getInstance());
65 assertEquals(42, root.call(42));
66 assertEquals(43, root.call(43));
67 try {
68 root.call(44);
69 fail();
70 } catch (UnsupportedSpecializationException e) {
71 }
72 }
73
74 @NodeChild
75 static class ConstantLimitTest extends ValueNode {
76
77 public static final int LIMIT = 2;
78
79 @Specialization(limit = "LIMIT", guards = "value == cachedValue")
80 static int do1(int value, @Cached("value") int cachedValue) {
81 return cachedValue;
82 }
83 }
84
85 @Test
86 public void testLocalLimit() {
87 CallTarget root = createCallTarget(LocalLimitTestFactory.getInstance());
88 assertEquals(42, root.call(42));
89 assertEquals(43, root.call(43));
90 try {
91 root.call(44);
92 fail();
93 } catch (UnsupportedSpecializationException e) {
94 }
95 }
96
97 @NodeChild
98 static class LocalLimitTest extends ValueNode {
99
100 protected int localLimit = 2;
101
102 @Specialization(limit = "localLimit", guards = "value == cachedValue")
103 static int do1(int value, @Cached("value") int cachedValue) {
104 return cachedValue;
105 }
106 }
107
108 @Test
109 public void testMethodLimit() {
110 MethodLimitTest.invocations = 0;
111 CallTarget root = createCallTarget(MethodLimitTestFactory.getInstance());
112 assertEquals(42, root.call(42));
113 assertEquals(43, root.call(43));
114 try {
115 root.call(44);
116 fail();
117 } catch (UnsupportedSpecializationException e) {
118 }
119 assertEquals(3, MethodLimitTest.invocations);
120 }
121
122 @NodeChild
123 static class MethodLimitTest extends ValueNode {
124
125 static int invocations = 0;
126
127 @Specialization(limit = "calculateLimitFor(cachedValue, invocations)", guards = "value == cachedValue")
128 static int do1(int value, @Cached("value") int cachedValue) {
129 return cachedValue;
130 }
131
132 int calculateLimitFor(int cachedValue, int boundField) {
133 invocations = boundField + 1;
134 return 2;
135 }
136
137 }
138
139 @NodeChild
140 static class LimitErrorTest1 extends ValueNode {
141 @ExpectError("The limit expression has no effect.%")
142 @Specialization(limit = "4")
143 static int do1(int value) {
144 return value;
145 }
146 }
147
148 @NodeChild
149 static class LimitErrorTest2 extends ValueNode {
150 public static final Object CONSTANT = new Object();
151
152 @ExpectError("Incompatible return type Object. Limit expressions must return int.")
153 @Specialization(limit = "CONSTANT", guards = "value == cachedValue")
154 static int do1(int value, @Cached("value") int cachedValue) {
155 return value;
156 }
157 }
158
159 @NodeChild
160 static class LimitErrorTest3 extends ValueNode {
161
162 public static final Object CONSTANT = new Object();
163
164 @ExpectError("Limit expressions must not bind dynamic parameter values.")
165 @Specialization(limit = "value", guards = "value == cachedValue")
166 static int do1(int value, @Cached("value") int cachedValue) {
167 return value;
168 }
169 }
170
171 }