comparison truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.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.tck/src/com/oracle/truffle/tck/TruffleTCK.java@0a6e10379b9b
children a88981c5ce8b
comparison
equal deleted inserted replaced
21950:2a5011c7e641 21951:9c8c0937da41
1 /*
2 * Copyright (c) 2015, 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.tck;
26
27 import com.oracle.truffle.api.vm.TruffleVM;
28 import java.io.IOException;
29 import java.util.Random;
30 import static org.junit.Assert.*;
31 import org.junit.Test;
32
33 /**
34 * A collection of tests that can certify language implementation to be compliant with most recent
35 * requirements of the Truffle infrastructure and tooling. Subclass, implement abstract methods and
36 * include in your test suite.
37 */
38 public abstract class TruffleTCK {
39 private TruffleVM tckVM;
40
41 protected TruffleTCK() {
42 }
43
44 /**
45 * This methods is called before first test is executed. It's purpose is to set a TruffleVM with
46 * your language up, so it is ready for testing.
47 * {@link TruffleVM#eval(java.lang.String, java.lang.String) Execute} any scripts you need, and
48 * prepare global symbols with proper names. The symbols will then be looked up by the
49 * infrastructure (using the names provided by you from methods like {@link #plusInt()}) and
50 * used for internal testing.
51 *
52 * @return initialized Truffle virtual machine
53 * @throws java.lang.Exception thrown when the VM preparation fails
54 */
55 protected abstract TruffleVM prepareVM() throws Exception;
56
57 /**
58 * Mimetype associated with your language. The mimetype will be passed to
59 * {@link TruffleVM#eval(java.lang.String, java.lang.String)} method of the {@link #prepareVM()
60 * created TruffleVM}.
61 *
62 * @return mime type of the tested language
63 */
64 protected abstract String mimeType();
65
66 /**
67 * Name of function which will return value 42 as a number. The return value of the method
68 * should be instance of {@link Number} and its {@link Number#intValue()} should return
69 * <code>42</code>.
70 *
71 * @return name of globally exported symbol
72 */
73 protected abstract String fourtyTwo();
74
75 /**
76 * Name of a function that returns <code>null</code>. Truffle languages are encouraged to have
77 * their own type representing <code>null</code>, but when such value is returned from
78 * {@link TruffleVM#eval}, it needs to be converted to real Java <code>null</code> by sending a
79 * foreign access <em>isNull</em> message. There is a test to verify it is really true.
80 *
81 * @return name of globally exported symbol
82 */
83 protected abstract String returnsNull();
84
85 /**
86 * Name of function to add two integer values together. The symbol will be invoked with two
87 * parameters of type {@link Integer} and expects result of type {@link Number} which's
88 * {@link Number#intValue()} is equivalent of <code>param1 + param2</code>.
89 *
90 * @return name of globally exported symbol
91 */
92 protected abstract String plusInt();
93
94 /**
95 * Return a code snippet that is invalid in your language. Its
96 * {@link TruffleVM#eval(java.lang.String, java.lang.String) evaluation} should fail and yield
97 * an exception.
98 *
99 * @return code snippet invalid in the tested language
100 */
101 protected abstract String invalidCode();
102
103 private TruffleVM vm() throws Exception {
104 if (tckVM == null) {
105 tckVM = prepareVM();
106 }
107 return tckVM;
108 }
109
110 //
111 // The tests
112 //
113
114 @Test
115 public void testFortyTwo() throws Exception {
116 TruffleVM.Symbol fourtyTwo = findGlobalSymbol(fourtyTwo());
117
118 Object res = fourtyTwo.invoke(null);
119
120 assert res instanceof Number : "should yield a number, but was: " + res;
121
122 Number n = (Number) res;
123
124 assert 42 == n.intValue() : "The value is 42 = " + n.intValue();
125 }
126
127 @Test
128 public void testNull() throws Exception {
129 if (getClass() == TruffleTCK.class) {
130 return;
131 }
132 TruffleVM.Symbol retNull = findGlobalSymbol(returnsNull());
133
134 Object res = retNull.invoke(null);
135
136 assertNull("Should yield real Java null", res);
137 }
138
139 @Test
140 public void testPlusWithInts() throws Exception {
141 Random r = new Random();
142 int a = r.nextInt(100);
143 int b = r.nextInt(100);
144
145 TruffleVM.Symbol plus = findGlobalSymbol(plusInt());
146
147 Object res = plus.invoke(null, a, b);
148
149 assert res instanceof Number : "+ on two ints should yield a number, but was: " + res;
150
151 Number n = (Number) res;
152
153 assert a + b == n.intValue() : "The value is correct: (" + a + " + " + b + ") = " + n.intValue();
154 }
155
156 @Test(expected = IOException.class)
157 public void testInvalidTestMethod() throws Exception {
158 String mime = mimeType();
159 String code = invalidCode();
160 Object ret = vm().eval(mime, code);
161 fail("Should yield IOException, but returned " + ret);
162 }
163
164 private TruffleVM.Symbol findGlobalSymbol(String name) throws Exception {
165 TruffleVM.Symbol s = vm().findGlobalSymbol(name);
166 assert s != null : "Symbol " + name + " is not found!";
167 return s;
168 }
169 }