Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/TruffleTCK.java @ 21468:99942eac9c6d
Introducing TruffleVM - a central place to invoke code in any registered TruffleLanguage.
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Fri, 22 May 2015 13:41:10 +0200 |
parents | |
children | 3286fb5fea4a b1530a6cce8c |
comparison
equal
deleted
inserted
replaced
21467:d4db9d812c8d | 21468:99942eac9c6d |
---|---|
1 /* | |
2 * Copyright (c) 2012, 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.vm; | |
24 | |
25 import com.oracle.truffle.api.vm.TruffleVM; | |
26 import java.util.Random; | |
27 | |
28 /** | |
29 * A collection of tests that can certify language implementaiton to be complient with most recent | |
30 * requirements of the Truffle infrastructure and tooling. Subclass, implement abstract methods and | |
31 * include in your test suite. | |
32 */ | |
33 public abstract class TruffleTCK { | |
34 private TruffleVM vm; | |
35 | |
36 protected TruffleTCK() { | |
37 } | |
38 | |
39 /** | |
40 * This methods is called before first test is executed. It's purpose is to set a TruffleVM with | |
41 * your language up, so it is ready for testing. | |
42 * {@link TruffleVM#eval(java.lang.String, java.lang.String) Execute} any scripts you need, and | |
43 * prepare global symbols with proper names. The symbols will then be looked up by the | |
44 * infastructure (using the names provided by you from methods like {@link #plusInt()}) and used | |
45 * for internal testing. | |
46 * | |
47 * @return initialized Truffle virtual machine | |
48 */ | |
49 protected abstract TruffleVM prepareVM() throws Exception; | |
50 | |
51 /** | |
52 * Name of function which will return value 42 as a number. The return value of the method | |
53 * should be instance of {@link Number} and its {@link Number#intValue()} should return | |
54 * <code>42</code>. | |
55 * | |
56 * @return name of globally exported symbol | |
57 */ | |
58 protected abstract String fourtyTwo(); | |
59 | |
60 /** | |
61 * Name of function to add two integer values together. The symbol will be invoked with two | |
62 * parameters of type {@link Integer} and expects result of type {@link Number} which's | |
63 * {@link Number#intValue()} is equivalent of <code>param1 + param2</code>. | |
64 * | |
65 * @return name of globally exported symbol | |
66 */ | |
67 protected abstract String plusInt(); | |
68 | |
69 private TruffleVM vm() throws Exception { | |
70 if (vm == null) { | |
71 vm = prepareVM(); | |
72 } | |
73 return vm; | |
74 } | |
75 | |
76 // | |
77 // The tests | |
78 // | |
79 | |
80 public void testFortyTwo() throws Exception { | |
81 TruffleVM.Symbol fourtyTwo = findGlobalSymbol(fourtyTwo()); | |
82 | |
83 Object res = fourtyTwo.invoke(null); | |
84 | |
85 assert res instanceof Number : "should yield a number, but was: " + res; | |
86 | |
87 Number n = (Number) res; | |
88 | |
89 assert 42 == n.intValue() : "The value is 42 = " + n.intValue(); | |
90 } | |
91 | |
92 public void testPlusWithInts() throws Exception { | |
93 Random r = new Random(); | |
94 int a = r.nextInt(100); | |
95 int b = r.nextInt(100); | |
96 | |
97 TruffleVM.Symbol plus = findGlobalSymbol(plusInt()); | |
98 | |
99 Object res = plus.invoke(null, a, b); | |
100 | |
101 assert res instanceof Number : "+ on two ints should yield a number, but was: " + res; | |
102 | |
103 Number n = (Number) res; | |
104 | |
105 assert a + b == n.intValue() : "The value is correct: (" + a + " + " + b + ") = " + n.intValue(); | |
106 } | |
107 | |
108 private TruffleVM.Symbol findGlobalSymbol(String name) throws Exception { | |
109 TruffleVM.Symbol s = vm().findGlobalSymbol(name); | |
110 assert s != null : "Symbol " + name + " is not found!"; | |
111 return s; | |
112 } | |
113 } |