comparison truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java @ 22523:6ab540203853

Expanding TCK to cover reading and writing of object properties with various primitive types
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Tue, 22 Dec 2015 18:00:04 +0100
parents b3569a53c24c
children 579d21e36582
comparison
equal deleted inserted replaced
22522:cda3eebfa777 22523:6ab540203853
42 import org.junit.Test; 42 import org.junit.Test;
43 43
44 import com.oracle.truffle.api.TruffleLanguage; 44 import com.oracle.truffle.api.TruffleLanguage;
45 import com.oracle.truffle.api.interop.TruffleObject; 45 import com.oracle.truffle.api.interop.TruffleObject;
46 import com.oracle.truffle.api.interop.java.JavaInterop; 46 import com.oracle.truffle.api.interop.java.JavaInterop;
47 import com.oracle.truffle.api.interop.java.MethodMessage;
47 import com.oracle.truffle.api.source.Source; 48 import com.oracle.truffle.api.source.Source;
48 import com.oracle.truffle.api.vm.PolyglotEngine; 49 import com.oracle.truffle.api.vm.PolyglotEngine;
49 import com.oracle.truffle.api.vm.PolyglotEngine.Language; 50 import com.oracle.truffle.api.vm.PolyglotEngine.Language;
50 import com.oracle.truffle.tck.Schema.Type; 51 import com.oracle.truffle.tck.Schema.Type;
51 52
114 throw new UnsupportedOperationException("Override plus(Class,Class) method!"); 115 throw new UnsupportedOperationException("Override plus(Class,Class) method!");
115 } 116 }
116 117
117 /** 118 /**
118 * Name of function to add two numbers together. The symbol will be invoked with two parameters 119 * Name of function to add two numbers together. The symbol will be invoked with two parameters
119 * of <code>type1</code> and <code>type2</code> and expects result of type {@link Number} 120 * of <code>type1</code> and <code>type2</code> and expects result of type {@link Number}
120 * which's {@link Number#intValue()} is equivalent of <code>param1 + param2</code>. As some 121 * which's {@link Number#intValue()} is equivalent of <code>param1 + param2</code>. As some
121 * languages may have different operations for different types of numbers, the actual types are 122 * languages may have different operations for different types of numbers, the actual types are
122 * passed to the method and the implementation can decide to return different symbol based on 123 * passed to the method and the implementation can decide to return different symbol based on
123 * the parameters. 124 * the parameters.
124 * 125 *
267 * 268 *
268 * @return name of a function that returns such compound object 269 * @return name of a function that returns such compound object
269 */ 270 */
270 protected String compoundObject() { 271 protected String compoundObject() {
271 throw new UnsupportedOperationException("compoundObject() method not implemented"); 272 throw new UnsupportedOperationException("compoundObject() method not implemented");
273 }
274
275 /**
276 * Name of a function that returns a compound object with members representing certain primitive
277 * types. In the JavaScript the object should look like:
278 *
279 * <pre>
280 * <b>var</b> obj = {
281 * 'byteValue': 0,
282 * 'shortValue': 0,
283 * 'intValue': 0,
284 * 'longValue': 0,
285 * 'floatValue': 0.0,
286 * 'doubleValue': 0.0,
287 * 'charValue': '0',
288 * 'stringValue': '',
289 * 'booleanVlaue': false
290 * };
291 * <b>return</b> obj;
292 * </pre>
293 *
294 * The returned object shall have slots for these values that can be read and written to.
295 * Various test methods try to read and modify the values. Each invocation of the function
296 * should yield new object.
297 *
298 * @return name of a function that returns such values object
299 */
300 protected String valuesObject() {
301 throw new UnsupportedOperationException("valuesObject() method not implemented");
272 } 302 }
273 303
274 private PolyglotEngine vm() throws Exception { 304 private PolyglotEngine vm() throws Exception {
275 if (tckVM == null) { 305 if (tckVM == null) {
276 tckVM = prepareVM(); 306 tckVM = prepareVM();
940 apply.execute(a, b); 970 apply.execute(a, b);
941 971
942 Assert.assertArrayEquals(new double[]{41, 42, 43, 44, 45, 46}, a.getData(), 0.1); 972 Assert.assertArrayEquals(new double[]{41, 42, 43, 44, 45, 46}, a.getData(), 0.1);
943 } 973 }
944 974
975 @Test
976 public void readWriteByteValue() throws Exception {
977 String id = valuesObject();
978 ValuesObject values = findGlobalSymbol(id).execute().as(ValuesObject.class);
979 assertEquals("Zero", 0, values.byteValue());
980 final byte value = (byte) RANDOM.nextInt(128);
981 values.byteValue(value);
982 assertEquals("Correct value", value, values.byteValue());
983 }
984
985 @Test
986 public void readWriteShortValue() throws Exception {
987 String id = valuesObject();
988 ValuesObject values = findGlobalSymbol(id).execute().as(ValuesObject.class);
989 assertEquals("Zero", 0, values.shortValue());
990 final short value = (short) RANDOM.nextInt(32768);
991 values.shortValue(value);
992 assertEquals("Correct value", value, values.shortValue());
993 }
994
995 @Test
996 public void readWriteIntValue() throws Exception {
997 String id = valuesObject();
998 ValuesObject values = findGlobalSymbol(id).execute().as(ValuesObject.class);
999 assertEquals("Zero", 0, values.intValue());
1000 final int value = RANDOM.nextInt();
1001 values.intValue(value);
1002 assertEquals("Correct value", value, values.intValue());
1003 }
1004
1005 @Test
1006 public void readWriteFloatValue() throws Exception {
1007 String id = valuesObject();
1008 ValuesObject values = findGlobalSymbol(id).execute().as(ValuesObject.class);
1009 assertEquals("Zero", 0, values.floatValue(), 0.1);
1010 final float value = RANDOM.nextFloat() * 1000.0f;
1011 values.floatValue(value);
1012 assertEquals("Correct value", value, values.floatValue(), 0.1);
1013 }
1014
1015 @Test
1016 public void readWriteDoubleValue() throws Exception {
1017 String id = valuesObject();
1018 ValuesObject values = findGlobalSymbol(id).execute().as(ValuesObject.class);
1019 assertEquals("Zero", 0, values.doubleValue(), 0.1);
1020 final double value = RANDOM.nextDouble() * 1000.0;
1021 values.doubleValue(value);
1022 assertEquals("Correct value", value, values.doubleValue(), 0.1);
1023 }
1024
1025 @Test
1026 public void readWriteCharValue() throws Exception {
1027 String id = valuesObject();
1028 ValuesObject values = findGlobalSymbol(id).execute().as(ValuesObject.class);
1029 assertEquals("Zero", '0', values.charValue());
1030 String letters = "P\u0159\u00EDli\u0161 \u017Elu\u0165ou\u010Dk\u00FD k\u016F\u0148 \u00FAp\u011Bl \u010F\u00E1belsk\u00E9 \u00F3dy";
1031 final char value = letters.charAt(RANDOM.nextInt(letters.length()));
1032 values.charValue(value);
1033 assertEquals("Correct value", value, values.charValue());
1034 }
1035
1036 @Test
1037 public void readWriteBooleanValue() throws Exception {
1038 String id = valuesObject();
1039 ValuesObject values = findGlobalSymbol(id).execute().as(ValuesObject.class);
1040 assertEquals("False", false, values.booleanValue());
1041 values.booleanValue(true);
1042 assertEquals("Correct value", true, values.booleanValue());
1043 values.booleanValue(false);
1044 assertEquals("Correct value2", false, values.booleanValue());
1045 }
1046
945 private static void putDoubles(byte[] buffer, double[] values) { 1047 private static void putDoubles(byte[] buffer, double[] values) {
946 for (int index = 0; index < values.length; index++) { 1048 for (int index = 0; index < values.length; index++) {
947 int doubleSize = Double.SIZE / Byte.SIZE; 1049 int doubleSize = Double.SIZE / Byte.SIZE;
948 byte[] bytes = new byte[doubleSize]; 1050 byte[] bytes = new byte[doubleSize];
949 ByteBuffer.wrap(bytes).putDouble(values[index]); 1051 ByteBuffer.wrap(bytes).putDouble(values[index]);
1003 1105
1004 Object returnsNull(); 1106 Object returnsNull();
1005 1107
1006 CompoundObject returnsThis(); 1108 CompoundObject returnsThis();
1007 } 1109 }
1110
1111 interface ValuesObject {
1112 byte byteValue();
1113
1114 @MethodMessage(message = "WRITE")
1115 void byteValue(byte v);
1116
1117 short shortValue();
1118
1119 @MethodMessage(message = "WRITE")
1120 void shortValue(short v);
1121
1122 int intValue();
1123
1124 @MethodMessage(message = "WRITE")
1125 void intValue(int v);
1126
1127 long longValue();
1128
1129 @MethodMessage(message = "WRITE")
1130 void longValue(long v);
1131
1132 float floatValue();
1133
1134 @MethodMessage(message = "WRITE")
1135 void floatValue(float v);
1136
1137 double doubleValue();
1138
1139 @MethodMessage(message = "WRITE")
1140 void doubleValue(double v);
1141
1142 char charValue();
1143
1144 @MethodMessage(message = "WRITE")
1145 void charValue(char v);
1146
1147 boolean booleanValue();
1148
1149 @MethodMessage(message = "WRITE")
1150 void booleanValue(boolean v);
1151 }
1008 } 1152 }