# HG changeset patch # User Jaroslav Tulach # Date 1452082475 -3600 # Node ID f8fb609939a73597b5167b1fab82398cc4914959 # Parent 2643b968c0c63613f58adbce02c0087c05072d12 Give implementors of the TCK control over comparing doubles. diff -r 2643b968c0c6 -r f8fb609939a7 truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java --- a/truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java Wed Jan 06 10:59:58 2016 +0100 +++ b/truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java Wed Jan 06 13:14:35 2016 +0100 @@ -47,10 +47,10 @@ import org.junit.After; -import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.Test; +import static org.junit.Assert.assertTrue; /** * This is the way to verify your language implementation is compatible. @@ -213,67 +213,20 @@ return "eval"; } - // - // Ignore tests working on floats and double - // - @Override - public void testPlusWithDouble() throws Exception { - } - - @Override - public void testPlusWithFloat() throws Exception { - } - - @Override - public void testPrimitiveReturnTypeDouble() throws Exception { - } - - @Override - public void testPrimitiveReturnTypeFloat() throws Exception { - } - - @Override - public void testPrimitiveidentityDouble() throws Exception { - } - - @Override - public void testPrimitiveidentityFloat() throws Exception { + protected String complexCopy() { + // skip these tests; SL doesn't have arrays + return null; } @Override - public void testSumRealOfComplexNumbersA() throws Exception { - } - - @Override - public void testSumRealOfComplexNumbersB() throws Exception { - } - - @Override - public void testSumRealOfComplexNumbersAsStructuredDataColumnBased() throws Exception { - } - - @Override - public void testSumRealOfComplexNumbersAsStructuredDataRowBased() throws Exception { + protected String complexSumReal() { + // skip these tests; SL doesn't have arrays + return null; } @Override - public void testCopyComplexNumbersA() throws Exception { - } - - @Override - public void testCopyComplexNumbersB() throws Exception { - } - - @Override - public void testCopyStructuredComplexToComplexNumbersA() throws Exception { - } - - @Override - public void readWriteDoubleValue() throws Exception { - } - - @Override - public void readWriteFloatValue() throws Exception { + protected void assertDouble(String msg, double expectedValue, double actualValue) { + // don't compare doubles, SL had to convert them to longs } } diff -r 2643b968c0c6 -r f8fb609939a7 truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java --- a/truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java Wed Jan 06 10:59:58 2016 +0100 +++ b/truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java Wed Jan 06 13:14:35 2016 +0100 @@ -26,13 +26,6 @@ import com.oracle.truffle.tck.impl.LongBinaryOperation; import com.oracle.truffle.tck.impl.ObjectBinaryOperation; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.io.IOException; import java.lang.reflect.Field; @@ -54,6 +47,13 @@ import com.oracle.truffle.api.vm.PolyglotEngine; import com.oracle.truffle.api.vm.PolyglotEngine.Language; import com.oracle.truffle.tck.Schema.Type; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * Test compatibility kit (the TCK) is a collection of tests to certify your @@ -225,7 +225,7 @@ * argument and provides the sum of all real parts. The argument is an array/buffer of complex * numbers. * - * @return name of globally exported symbol + * @return name of globally exported symbol, null if the test should be skipped */ protected String complexSumReal() { throw new UnsupportedOperationException("complexSumReal() method not implemented"); @@ -237,7 +237,7 @@ * called real and imaginary. The first argument is the destination, the second argument is the * source. * - * @return name of globally exported symbol + * @return name of globally exported symbol, null if the test should be skipped */ protected String complexCopy() { throw new UnsupportedOperationException("complexCopy() method not implemented"); @@ -358,6 +358,25 @@ throw new UnsupportedOperationException("valuesObject() method not implemented"); } + /** Assert two double values are the same. Various languages may have different + * semantics with respect to double numbers. Some of the language may not + * support double or float values at all. Those languages + * may override this method and compare the values with as much precision + * as they like. + *

+ * Default implementation of this method calls + * {@link Assert#assertEquals(java.lang.String, double, double, double)} + * with delta 0.1. + * + * @param msg assertion message to display in case of error + * @param expectedValue the value expected by the test + * @param actualValue the real value produced by the language + * @throws AssertionError if the values are different according to the language semantics + */ + protected void assertDouble(String msg, double expectedValue, double actualValue) { + assertEquals(msg, expectedValue, actualValue, 0.1); + } + private PolyglotEngine vm() throws Exception { if (tckVM == null) { tckVM = prepareVM(); @@ -466,24 +485,24 @@ @Test public void testPlusWithFloat() throws Exception { - float a = RANDOM.nextFloat(); - float b = RANDOM.nextFloat(); + float a = RANDOM.nextFloat() * 100.0f; + float b = RANDOM.nextFloat() * 100.0f; PolyglotEngine.Value plus = findGlobalSymbol(plus(float.class, float.class)); Number n = plus.execute(a, b).as(Number.class); - assertEquals("Correct value computed", a + b, n.floatValue(), 0.01f); + assertDouble("Correct value computed", a + b, n.floatValue()); } @Test public void testPlusWithDouble() throws Exception { - double a = RANDOM.nextDouble(); - double b = RANDOM.nextDouble(); + double a = RANDOM.nextDouble() * 100.0; + double b = RANDOM.nextDouble() * 100.0; PolyglotEngine.Value plus = findGlobalSymbol(plus(float.class, float.class)); Number n = plus.execute(a, b).as(Number.class); - assertEquals("Correct value computed", a + b, n.doubleValue(), 0.01); + assertDouble("Correct value computed", a + b, n.doubleValue()); } @Test @@ -592,7 +611,7 @@ TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value)); Number n = apply.execute(fn).as(Number.class); - assertEquals("The same value returned", value + 10, n.floatValue(), 0.01); + assertDouble("The same value returned", value + 10, n.floatValue()); } @Test @@ -603,7 +622,7 @@ TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value)); Number n = apply.execute(fn).as(Number.class); - assertEquals("The same value returned", value + 10, n.doubleValue(), 0.01); + assertDouble("The same value returned", value + 10, n.doubleValue()); } @Test @@ -732,7 +751,7 @@ float value = RANDOM.nextInt(1000) + RANDOM.nextFloat(); Number n = (Number) apply.execute(value).get(); - assertEquals("The same value returned", value, n.floatValue(), 0.01); + assertDouble("The same value returned", value, n.floatValue()); } @Test @@ -747,7 +766,7 @@ BoxedValue boxed = new BoxedValue(value); Number n = (Number) apply.execute(boxed).get(); - assertEquals("The same value returned", value, n.floatValue(), 0.01); + assertDouble("The same value returned", value, n.floatValue()); } @Test @@ -761,7 +780,7 @@ double value = RANDOM.nextInt(1000) + RANDOM.nextDouble(); Number n = (Number) apply.execute(value).get(); - assertEquals("The same value returned", value, n.doubleValue(), 0.01); + assertDouble("The same value returned", value, n.doubleValue()); } @Test @@ -776,7 +795,7 @@ BoxedValue boxed = new BoxedValue(value); Number n = (Number) apply.execute(boxed).get(); - assertEquals("The same value returned", value, n.doubleValue(), 0.01); + assertDouble("The same value returned", value, n.doubleValue()); } @Test @@ -880,7 +899,7 @@ Object parsed = function.execute("application/x-tck", "" + expect).get(); assertTrue("Expecting numeric result, was:" + expect, parsed instanceof Number); double value = ((Number) parsed).doubleValue(); - assertEquals("Gets the double", expect, value, 0.01); + assertDouble("Gets the double", expect, value); } @Test @@ -924,7 +943,7 @@ ComplexNumbersRowBased numbers = new ComplexNumbersRowBased(new double[]{2, -1, 30, -1, 10, -1}); Number n = (Number) apply.execute(numbers).get(); - assertEquals("The same value returned", 42.0, n.doubleValue(), 0.01); + assertDouble("The same value returned", 42.0, n.doubleValue()); } @Test @@ -938,7 +957,7 @@ ComplexNumbersColumnBased numbers = new ComplexNumbersColumnBased(new double[]{2, 30, 10}, new double[]{-1, -1, -1}); Number n = (Number) apply.execute(numbers).get(); - assertEquals("The same value returned", 42.0, n.doubleValue(), 0.01); + assertDouble("The same value returned", 42.0, n.doubleValue()); } @Test @@ -955,7 +974,7 @@ StructuredData numbers = new StructuredData(buffer, schema); Number n = (Number) apply.execute(numbers).get(); - assertEquals("The same value returned", 42.0, n.doubleValue(), 0.01); + assertDouble("The same value returned", 42.0, n.doubleValue()); } @Test @@ -973,7 +992,7 @@ StructuredData numbers = new StructuredData(buffer, schema); Number n = (Number) apply.execute(numbers).get(); - assertEquals("The same value returned", 42.0, n.doubleValue(), 0.01); + assertDouble("The same value returned", 42.0, n.doubleValue()); } @Test @@ -1063,20 +1082,20 @@ public void readWriteFloatValue() throws Exception { String id = valuesObject(); ValuesObject values = findGlobalSymbol(id).execute().as(ValuesObject.class); - assertEquals("Zero", 0, values.floatValue(), 0.1); + assertDouble("Zero", 0, values.floatValue()); final float value = RANDOM.nextFloat() * 1000.0f; values.floatValue(value); - assertEquals("Correct value", value, values.floatValue(), 0.1); + assertDouble("Correct value", value, values.floatValue()); } @Test public void readWriteDoubleValue() throws Exception { String id = valuesObject(); ValuesObject values = findGlobalSymbol(id).execute().as(ValuesObject.class); - assertEquals("Zero", 0, values.doubleValue(), 0.1); + assertDouble("Zero", 0, values.doubleValue()); final double value = RANDOM.nextDouble() * 1000.0; values.doubleValue(value); - assertEquals("Correct value", value, values.doubleValue(), 0.1); + assertDouble("Correct value", value, values.doubleValue()); } @Test