Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.api.interop.java.test/src/com/oracle/truffle/api/interop/java/test/PrimitiveRawArrayInteropTest.java @ 22135:e70b20f4bb00
Implementing API for Java/Truffle interop. Based around JavaInterop.asJavaObject and JavaInterop.asTruffleObject methods. Connected to TruffleVM via Symbol.as(Class) wrapper. Verified by extended TCK.
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Mon, 07 Sep 2015 17:07:20 +0200 |
parents | |
children | dc83cc1f94f2 |
comparison
equal
deleted
inserted
replaced
22134:025869c88840 | 22135:e70b20f4bb00 |
---|---|
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. 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.api.interop.java.test; | |
26 | |
27 import com.oracle.truffle.api.interop.TruffleObject; | |
28 import com.oracle.truffle.api.interop.java.JavaInterop; | |
29 import java.util.List; | |
30 import org.junit.Before; | |
31 import org.junit.Test; | |
32 import static org.junit.Assert.*; | |
33 | |
34 public class PrimitiveRawArrayInteropTest { | |
35 private Object[] objArr; | |
36 private byte[] byteArr; | |
37 private short[] shortArr; | |
38 private int[] intArr; | |
39 private long[] longArr; | |
40 private float[] floatArr; | |
41 private double[] doubleArr; | |
42 private char[] charArr; | |
43 private boolean[] boolArr; | |
44 | |
45 public Object arr(int type) { | |
46 switch (type) { | |
47 case 0: | |
48 return objArr; | |
49 case 1: | |
50 return byteArr; | |
51 case 2: | |
52 return shortArr; | |
53 case 3: | |
54 return intArr; | |
55 case 4: | |
56 return longArr; | |
57 case 5: | |
58 return floatArr; | |
59 case 6: | |
60 return doubleArr; | |
61 case 7: | |
62 return charArr; | |
63 case 8: | |
64 return boolArr; | |
65 default: | |
66 throw new IllegalStateException("type: " + type); | |
67 } | |
68 } | |
69 | |
70 public interface RawInterop { | |
71 List<Object> arr(int type); | |
72 } | |
73 | |
74 private TruffleObject obj; | |
75 private RawInterop interop; | |
76 | |
77 @Before | |
78 public void initObjects() { | |
79 obj = JavaInterop.asTruffleObject(this); | |
80 interop = JavaInterop.asJavaObject(RawInterop.class, obj); | |
81 } | |
82 | |
83 @Test | |
84 public void everyThingIsNull() { | |
85 assertNull(interop.arr(0)); | |
86 assertNull(interop.arr(1)); | |
87 assertNull(interop.arr(2)); | |
88 assertNull(interop.arr(3)); | |
89 assertNull(interop.arr(4)); | |
90 assertNull(interop.arr(5)); | |
91 assertNull(interop.arr(6)); | |
92 assertNull(interop.arr(7)); | |
93 assertNull(interop.arr(8)); | |
94 } | |
95 | |
96 @Test | |
97 @SuppressWarnings({"rawtypes", "unchecked"}) | |
98 public void stringAsList() { | |
99 objArr = new Object[]{"Hello", "World", "!"}; | |
100 List<Object> list = interop.arr(0); | |
101 assertEquals("Three elements", 3, list.size()); | |
102 assertEquals("Hello", list.get(0)); | |
103 assertEquals("World", list.get(1)); | |
104 assertEquals("!", list.get(2)); | |
105 | |
106 list.set(1, "there"); | |
107 assertEquals("there", objArr[1]); | |
108 | |
109 list.set(0, null); | |
110 assertNull("set to null", objArr[0]); | |
111 | |
112 List rawList = list; | |
113 rawList.set(0, 42); | |
114 assertEquals("safelly changed", 42, objArr[0]); | |
115 } | |
116 | |
117 @Test | |
118 public void charOp() { | |
119 charArr = new char[]{'A', 'h', 'o', 'j'}; | |
120 assertEquals('j', (char) interop.arr(7).get(3)); | |
121 interop.arr(7).set(3, 'y'); | |
122 | |
123 String s = new String(charArr); | |
124 assertEquals("Ahoy", s); | |
125 } | |
126 | |
127 @Test | |
128 public void boolOp() { | |
129 boolArr = new boolean[]{true, false}; | |
130 | |
131 interop.arr(8).set(1, !(Boolean) interop.arr(8).get(1)); | |
132 | |
133 assertEquals(boolArr[0], boolArr[1]); | |
134 } | |
135 | |
136 @Test | |
137 public void byteSum() { | |
138 byteArr = new byte[]{(byte) 1, (byte) 2, (byte) 3}; | |
139 assertSum("Sum is OK", 6, interop.arr(1)); | |
140 } | |
141 | |
142 @Test | |
143 public void shortSum() { | |
144 shortArr = new short[]{(short) 1, (short) 2, (short) 3}; | |
145 assertSum("Sum is OK", 6, interop.arr(2)); | |
146 } | |
147 | |
148 @Test | |
149 public void intSum() { | |
150 intArr = new int[]{1, 2, 3}; | |
151 assertSum("Sum is OK", 6, interop.arr(3)); | |
152 } | |
153 | |
154 @Test | |
155 public void longSum() { | |
156 longArr = new long[]{1, 2, 3}; | |
157 assertSum("Sum is OK", 6, interop.arr(4)); | |
158 } | |
159 | |
160 @Test | |
161 public void floatSum() { | |
162 floatArr = new float[]{1, 2, 3}; | |
163 assertSum("Sum is OK", 6, interop.arr(5)); | |
164 } | |
165 | |
166 @Test | |
167 public void doubleSum() { | |
168 doubleArr = new double[]{1, 2, 3}; | |
169 assertSum("Sum is OK", 6, interop.arr(6)); | |
170 } | |
171 | |
172 @Test | |
173 public void writeSomebyteSum() { | |
174 byteArr = new byte[]{(byte) 10, (byte) 2, (byte) 3}; | |
175 interop.arr(1).set(0, (byte) 1); | |
176 assertSum("Sum is OK", 6, interop.arr(1)); | |
177 } | |
178 | |
179 @Test | |
180 public void writeSomeshortSum() { | |
181 shortArr = new short[]{(short) 10, (short) 2, (short) 3}; | |
182 interop.arr(2).set(0, (short) 1); | |
183 assertSum("Sum is OK", 6, interop.arr(2)); | |
184 } | |
185 | |
186 @Test | |
187 public void writeSomeintSum() { | |
188 intArr = new int[]{10, 2, 3}; | |
189 interop.arr(3).set(0, 1); | |
190 assertSum("Sum is OK", 6, interop.arr(3)); | |
191 } | |
192 | |
193 @Test | |
194 public void writeSomelongSum() { | |
195 longArr = new long[]{10, 2, 3}; | |
196 interop.arr(4).set(0, (long) 1); | |
197 assertSum("Sum is OK", 6, interop.arr(4)); | |
198 } | |
199 | |
200 @Test | |
201 public void writeSomefloatSum() { | |
202 floatArr = new float[]{10, 2, 3}; | |
203 interop.arr(5).set(0, (float) 1); | |
204 assertSum("Sum is OK", 6, interop.arr(5)); | |
205 } | |
206 | |
207 @Test | |
208 public void writeSomedoubleSum() { | |
209 doubleArr = new double[]{10, 2, 3}; | |
210 interop.arr(6).set(0, (double) 1); | |
211 assertSum("Sum is OK", 6, interop.arr(6)); | |
212 } | |
213 | |
214 private static void assertSum(String msg, double expected, List<? extends Object> numbers) { | |
215 double v = 0.0; | |
216 for (Object o : numbers) { | |
217 if (o instanceof Number) { | |
218 Number n = (Number) o; | |
219 v += n.doubleValue(); | |
220 } | |
221 } | |
222 assertEquals(msg, expected, v, 0.05); | |
223 } | |
224 } |