comparison truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java @ 22495:aeba89e1d8da

Add ComplexNumber sequence tests
author Matthias Grimmer <grimmer@ssw.jku.at>
date Fri, 11 Dec 2015 15:20:27 +0100
parents fa7b15454c66
children b3569a53c24c
comparison
equal deleted inserted replaced
22489:28227895fa35 22495:aeba89e1d8da
22 * or visit www.oracle.com if you need additional information or have any 22 * or visit www.oracle.com if you need additional information or have any
23 * questions. 23 * questions.
24 */ 24 */
25 package com.oracle.truffle.tck; 25 package com.oracle.truffle.tck;
26 26
27 import com.oracle.truffle.api.TruffleLanguage;
28 import com.oracle.truffle.api.interop.TruffleObject;
29 import com.oracle.truffle.api.interop.java.JavaInterop;
30 import com.oracle.truffle.api.source.Source;
31 import com.oracle.truffle.api.vm.PolyglotEngine;
32 import com.oracle.truffle.api.vm.PolyglotEngine.Language;
33 import java.io.IOException;
34 import java.lang.reflect.Field;
35 import java.util.Random;
36 import org.junit.Test;
37 import static org.junit.Assert.assertEquals; 27 import static org.junit.Assert.assertEquals;
38 import static org.junit.Assert.assertNotNull; 28 import static org.junit.Assert.assertNotNull;
39 import static org.junit.Assert.assertNotSame; 29 import static org.junit.Assert.assertNotSame;
40 import static org.junit.Assert.assertNull; 30 import static org.junit.Assert.assertNull;
41 import static org.junit.Assert.assertSame; 31 import static org.junit.Assert.assertSame;
42 import static org.junit.Assert.assertTrue; 32 import static org.junit.Assert.assertTrue;
43 import static org.junit.Assert.fail; 33 import static org.junit.Assert.fail;
34
35 import java.io.IOException;
36 import java.lang.reflect.Field;
37 import java.nio.ByteBuffer;
38 import java.util.Arrays;
39 import java.util.Random;
40
41 import org.junit.Assert;
42 import org.junit.Test;
43
44 import com.oracle.truffle.api.TruffleLanguage;
45 import com.oracle.truffle.api.interop.TruffleObject;
46 import com.oracle.truffle.api.interop.java.JavaInterop;
47 import com.oracle.truffle.api.source.Source;
48 import com.oracle.truffle.api.vm.PolyglotEngine;
49 import com.oracle.truffle.api.vm.PolyglotEngine.Language;
50 import com.oracle.truffle.tck.Schema.Type;
44 51
45 /** 52 /**
46 * A collection of tests that can certify language implementation to be compliant with most recent 53 * A collection of tests that can certify language implementation to be compliant with most recent
47 * requirements of the Truffle infrastructure and tooling. Subclass, implement abstract methods and 54 * requirements of the Truffle infrastructure and tooling. Subclass, implement abstract methods and
48 * include in your test suite. 55 * include in your test suite.
151 * 158 *
152 * @return name of globally exported symbol 159 * @return name of globally exported symbol
153 */ 160 */
154 protected String complexAdd() { 161 protected String complexAdd() {
155 throw new UnsupportedOperationException("complexAdd() method not implemented"); 162 throw new UnsupportedOperationException("complexAdd() method not implemented");
163 }
164
165 /**
166 * Name of a function that adds up the real part of complex numbers. The function accepts one
167 * argument and provides the sum of all real parts. The argument is an array/buffer of complex
168 * numbers.
169 *
170 * @return name of globally exported symbol
171 */
172 protected String complexSumReal() {
173 throw new UnsupportedOperationException("complexSumReal() method not implemented");
174 }
175
176 /**
177 * Name of a function that copies a list of complex numbers. The function accepts two arguments
178 * and provides no return value. The arguments are two lists of complex numbers with members
179 * called real and imaginary. The first argument is the destination, the second argument is the
180 * source.
181 *
182 * @return name of globally exported symbol
183 */
184 protected String complexCopy() {
185 throw new UnsupportedOperationException("complexCopy() method not implemented");
156 } 186 }
157 187
158 /** 188 /**
159 * Name of a function to return global object. The function can be executed without providing 189 * Name of a function to return global object. The function can be executed without providing
160 * any arguments and should return global object of the language, if the language supports it. 190 * any arguments and should return global object of the language, if the language supports it.
794 824
795 assertEquals(42.0, a.get(ComplexNumber.REAL_IDENTIFIER), 0.1); 825 assertEquals(42.0, a.get(ComplexNumber.REAL_IDENTIFIER), 0.1);
796 assertEquals(42.0, a.get(ComplexNumber.IMAGINARY_IDENTIFIER), 0.1); 826 assertEquals(42.0, a.get(ComplexNumber.IMAGINARY_IDENTIFIER), 0.1);
797 } 827 }
798 828
829 @Test
830 public void testSumRealOfComplexNumbersA() throws Exception {
831 String id = complexSumReal();
832 if (id == null) {
833 return;
834 }
835 PolyglotEngine.Value apply = findGlobalSymbol(id);
836
837 ComplexNumbersA numbers = new ComplexNumbersA(new double[]{2, -1, 30, -1, 10, -1});
838
839 Number n = (Number) apply.execute(numbers).get();
840 assertEquals("The same value returned", 42.0, n.doubleValue(), 0.01);
841 }
842
843 @Test
844 public void testSumRealOfComplexNumbersB() throws Exception {
845 String id = complexSumReal();
846 if (id == null) {
847 return;
848 }
849 PolyglotEngine.Value apply = findGlobalSymbol(id);
850
851 ComplexNumbersB numbers = new ComplexNumbersB(new double[]{2, 30, 10}, new double[]{-1, -1, -1});
852
853 Number n = (Number) apply.execute(numbers).get();
854 assertEquals("The same value returned", 42.0, n.doubleValue(), 0.01);
855 }
856
857 @Test
858 public void testSumRealOfComplexNumbersAsStructuredDataRowBased() throws Exception {
859 String id = complexSumReal();
860 if (id == null) {
861 return;
862 }
863 PolyglotEngine.Value apply = findGlobalSymbol(id);
864
865 Schema schema = new Schema(3, true, Arrays.asList(ComplexNumber.REAL_IDENTIFIER, ComplexNumber.IMAGINARY_IDENTIFIER), Arrays.asList(Type.DOUBLE, Type.DOUBLE));
866 byte[] buffer = new byte[(6 * Double.SIZE / Byte.SIZE)];
867 putDoubles(buffer, new double[]{2, -1, 30, -1, 10, -1});
868 StructuredData numbers = new StructuredData(buffer, schema);
869
870 Number n = (Number) apply.execute(numbers).get();
871 assertEquals("The same value returned", 42.0, n.doubleValue(), 0.01);
872 }
873
874 @Test
875 public void testSumRealOfComplexNumbersAsStructuredDataColumnBased() throws Exception {
876 String id = complexSumReal();
877 if (id == null) {
878 return;
879 }
880 PolyglotEngine.Value apply = findGlobalSymbol(id);
881
882 Schema schema = new Schema(3, false, Arrays.asList(ComplexNumber.REAL_IDENTIFIER, ComplexNumber.IMAGINARY_IDENTIFIER), Arrays.asList(Type.DOUBLE, Type.DOUBLE));
883 byte[] buffer = new byte[6 * Double.SIZE / Byte.SIZE];
884 putDoubles(buffer, new double[]{2, 30, 10, -1, -1, -1});
885
886 StructuredData numbers = new StructuredData(buffer, schema);
887
888 Number n = (Number) apply.execute(numbers).get();
889 assertEquals("The same value returned", 42.0, n.doubleValue(), 0.01);
890 }
891
892 @Test
893 public void testCopyComplexNumbersA() throws Exception {
894 String id = complexCopy();
895 if (id == null) {
896 return;
897 }
898 PolyglotEngine.Value apply = findGlobalSymbol(id);
899
900 ComplexNumbersA a = new ComplexNumbersA(new double[]{-1, -1, -1, -1, -1, -1});
901 ComplexNumbersA b = new ComplexNumbersA(new double[]{41, 42, 43, 44, 45, 46});
902
903 apply.execute(a, b);
904
905 Assert.assertArrayEquals(new double[]{41, 42, 43, 44, 45, 46}, a.getData(), 0.1);
906 }
907
908 @Test
909 public void testCopyComplexNumbersB() throws Exception {
910 String id = complexCopy();
911 if (id == null) {
912 return;
913 }
914 PolyglotEngine.Value apply = findGlobalSymbol(id);
915
916 ComplexNumbersB a = new ComplexNumbersB(new double[]{-1, -1, -1}, new double[]{-1, -1, -1});
917 ComplexNumbersB b = new ComplexNumbersB(new double[]{41, 43, 45}, new double[]{42, 44, 46});
918
919 apply.execute(a, b);
920
921 Assert.assertArrayEquals(new double[]{41, 42, 43, 44, 45, 46}, a.getData(), 0.1);
922 }
923
924 @Test
925 public void testCopyStructuredComplexToComplexNumbersA() throws Exception {
926 String id = complexCopy();
927 if (id == null) {
928 return;
929 }
930 PolyglotEngine.Value apply = findGlobalSymbol(id);
931
932 ComplexNumbersA a = new ComplexNumbersA(new double[]{-1, -1, -1, -1, -1, -1});
933
934 Schema schema = new Schema(3, true, Arrays.asList(ComplexNumber.REAL_IDENTIFIER, ComplexNumber.IMAGINARY_IDENTIFIER), Arrays.asList(Type.DOUBLE, Type.DOUBLE));
935 byte[] buffer = new byte[6 * Double.SIZE / Byte.SIZE];
936 putDoubles(buffer, new double[]{41, 42, 43, 44, 45, 46});
937
938 StructuredData b = new StructuredData(buffer, schema);
939
940 apply.execute(a, b);
941
942 Assert.assertArrayEquals(new double[]{41, 42, 43, 44, 45, 46}, a.getData(), 0.1);
943 }
944
945 private static void putDoubles(byte[] buffer, double[] values) {
946 for (int index = 0; index < values.length; index++) {
947 int doubleSize = Double.SIZE / Byte.SIZE;
948 byte[] bytes = new byte[doubleSize];
949 ByteBuffer.wrap(bytes).putDouble(values[index]);
950 for (int i = 0; i < doubleSize; i++) {
951 buffer[index * doubleSize + i] = bytes[i];
952 }
953 }
954 }
955
799 private PolyglotEngine.Value findGlobalSymbol(String name) throws Exception { 956 private PolyglotEngine.Value findGlobalSymbol(String name) throws Exception {
800 PolyglotEngine.Value s = vm().findGlobalSymbol(name); 957 PolyglotEngine.Value s = vm().findGlobalSymbol(name);
801 assert s != null : "Symbol " + name + " is not found!"; 958 assert s != null : "Symbol " + name + " is not found!";
802 return s; 959 return s;
803 } 960 }