Mercurial > hg > truffle
comparison graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/ArrayTest.java @ 18605:58eb9bbb60c4
Truffle-DSL: fixed several bugs when using arrays as type. added arrays test.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Wed, 03 Dec 2014 21:02:27 +0100 |
parents | |
children | 59953a46c56f |
comparison
equal
deleted
inserted
replaced
18604:39441c10d314 | 18605:58eb9bbb60c4 |
---|---|
1 /* | |
2 * Copyright (c) 2014, 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 org.junit.*; | |
26 | |
27 import com.oracle.truffle.api.*; | |
28 import com.oracle.truffle.api.dsl.*; | |
29 import com.oracle.truffle.api.dsl.test.ArrayTestFactory.TestNode1Factory; | |
30 import com.oracle.truffle.api.frame.*; | |
31 import com.oracle.truffle.api.nodes.*; | |
32 | |
33 public class ArrayTest { | |
34 | |
35 @Test | |
36 public void testNode1() { | |
37 final TestNode1 node = TestNode1Factory.create(null); | |
38 RootNode root = new RootNode() { | |
39 @Child TestNode1 test = node; | |
40 | |
41 @Override | |
42 public Object execute(VirtualFrame frame) { | |
43 return test.executeWith(frame, frame.getArguments()[0]); | |
44 } | |
45 }; | |
46 CallTarget target = Truffle.getRuntime().createCallTarget(root); | |
47 | |
48 Assert.assertEquals(1, (int) target.call(1)); | |
49 Assert.assertArrayEquals(new double[0], (double[]) target.call(new int[0]), 0.0d); | |
50 Assert.assertArrayEquals(new double[0], (double[]) target.call(new double[0]), 0.0d); | |
51 Assert.assertArrayEquals(new String[0], (String[]) target.call((Object) new String[0])); | |
52 } | |
53 | |
54 @TypeSystemReference(ArrayTypeSystem.class) | |
55 abstract static class BaseNode extends Node { | |
56 | |
57 abstract Object execute(VirtualFrame frame); | |
58 | |
59 int executeInt(VirtualFrame frame) throws UnexpectedResultException { | |
60 return ArrayTypeSystemGen.ARRAYTYPESYSTEM.expectInteger(execute(frame)); | |
61 } | |
62 | |
63 int[] executeIntArray(VirtualFrame frame) throws UnexpectedResultException { | |
64 return ArrayTypeSystemGen.ARRAYTYPESYSTEM.expectIntArray(execute(frame)); | |
65 } | |
66 | |
67 String[] executeStringArray(VirtualFrame frame) throws UnexpectedResultException { | |
68 return ArrayTypeSystemGen.ARRAYTYPESYSTEM.expectStringArray(execute(frame)); | |
69 } | |
70 | |
71 double[] executeDoubleArray(VirtualFrame frame) throws UnexpectedResultException { | |
72 return ArrayTypeSystemGen.ARRAYTYPESYSTEM.expectDoubleArray(execute(frame)); | |
73 } | |
74 } | |
75 | |
76 @NodeChild | |
77 abstract static class TestNode1 extends BaseNode { | |
78 | |
79 abstract Object executeWith(VirtualFrame frame, Object operand); | |
80 | |
81 @Specialization | |
82 int doInt(int value) { | |
83 return value; | |
84 } | |
85 | |
86 @Specialization | |
87 double[] doDoubleArray(double[] value) { | |
88 return value; | |
89 } | |
90 | |
91 @Specialization | |
92 String[] doStringArray(String[] value) { | |
93 return value; | |
94 } | |
95 | |
96 } | |
97 | |
98 @TypeSystem({int.class, int[].class, double[].class, String[].class, Object[].class}) | |
99 public static class ArrayTypeSystem { | |
100 | |
101 @ImplicitCast | |
102 public double[] castFromInt(int[] array) { | |
103 double[] newArray = new double[array.length]; | |
104 for (int i = 0; i < array.length; i++) { | |
105 newArray[i] = array[i]; | |
106 } | |
107 return newArray; | |
108 } | |
109 | |
110 @TypeCheck | |
111 public boolean isIntArray(Object array) { | |
112 return array instanceof int[]; | |
113 } | |
114 | |
115 @TypeCast | |
116 public int[] asIntArray(Object array) { | |
117 return (int[]) array; | |
118 } | |
119 | |
120 } | |
121 | |
122 } |