Mercurial > hg > graal-compiler
annotate graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/ImplicitCastTest.java @ 11659:ec7aad9dfc47
made 'mx build' refresh IDE configs automatically
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Mon, 16 Sep 2013 13:09:02 +0200 |
parents | 2fb276f5e3e9 |
children | 7aa2a8c69ba3 |
rev | line source |
---|---|
11545
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
1 /* |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
4 * |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
7 * published by the Free Software Foundation. |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
8 * |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
13 * accompanied this code). |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
14 * |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
15 * You should have received a copy of the GNU General Public License version |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
18 * |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
21 * questions. |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
22 */ |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
23 package com.oracle.truffle.api.dsl.test; |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
24 |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
25 import org.junit.*; |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
26 |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
27 import com.oracle.truffle.api.dsl.*; |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
28 import com.oracle.truffle.api.dsl.test.ImplicitCastTestFactory.ImplicitCast0NodeFactory; |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
29 import com.oracle.truffle.api.dsl.test.NodeContainerTest.Str; |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
30 import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode; |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
31 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode; |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
32 import com.oracle.truffle.api.frame.*; |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
33 |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
34 public class ImplicitCastTest { |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
35 |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
36 @TypeSystem({int.class, boolean.class, String.class, Str.class}) |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
37 static class ImplicitCast0Types { |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
38 |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
39 @ImplicitCast |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
40 boolean castInt(int intvalue) { |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
41 return intvalue == 1 ? true : false; |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
42 } |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
43 |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
44 @ImplicitCast |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
45 boolean castString(String strvalue) { |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
46 return strvalue.equals("1"); |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
47 } |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
48 |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
49 } |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
50 |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
51 @TypeSystemReference(ImplicitCast0Types.class) |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
52 @NodeChild(value = "operand", type = ImplicitCast0Node.class) |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
53 abstract static class ImplicitCast0Node extends ValueNode { |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
54 |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
55 public abstract Object executeEvaluated(VirtualFrame frame, Object value2); |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
56 |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
57 @Specialization(order = 1) |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
58 public String op1(String value) throws RuntimeException { |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
59 return value; |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
60 } |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
61 |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
62 @Specialization(order = 2) |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
63 public boolean op1(boolean value) throws RuntimeException { |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
64 return value; |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
65 } |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
66 |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
67 } |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
68 |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
69 @Test |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
70 public void testImplicitCast0() { |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
71 ImplicitCast0Node node = ImplicitCast0NodeFactory.create(null); |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
72 TestRootNode<ImplicitCast0Node> root = new TestRootNode<>(node); |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
73 Assert.assertEquals("2", root.getNode().executeEvaluated(null, "2")); |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
74 Assert.assertEquals(true, root.getNode().executeEvaluated(null, 1)); |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
75 Assert.assertEquals("1", root.getNode().executeEvaluated(null, "1")); |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
76 Assert.assertEquals(true, root.getNode().executeEvaluated(null, 1)); |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
77 Assert.assertEquals(true, root.getNode().executeEvaluated(null, true)); |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
78 } |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
79 |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
80 // TODO assert implicit casts only in one direction |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
81 |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
82 // test example that covers the most cases |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
83 |
2fb276f5e3e9
Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
84 } |