Reproduce Float-passing problem (#9201)

This commit is contained in:
GregoryTravis 2024-03-14 15:35:18 -04:00 committed by GitHub
parent cd94437f54
commit dc84317ff1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 73 additions and 20 deletions

View File

@ -4,11 +4,6 @@ import org.graalvm.polyglot.HostAccess;
/** Utility class for creating HostAccess object. */
public class HostAccessFactory {
// Copied from com.oracle.truffle.api.interop.NumberUtils
// since the latter is inaccessible
private static final long LONG_MAX_SAFE_DOUBLE = 9007199254740991L; // 2 ** 53 - 1
public HostAccess allWithTypeMapping() {
return HostAccess.newBuilder()
.allowPublicAccess(true)
@ -21,15 +16,6 @@ public class HostAccessFactory {
.allowIteratorAccess(true)
.allowMapAccess(true)
.allowAccessInheritance(true)
.targetTypeMapping(
Long.class,
Double.class,
(v) -> v != null && !longInSafeDoubleRange(v, LONG_MAX_SAFE_DOUBLE),
(v) -> Double.longBitsToDouble(v))
.build();
}
private boolean longInSafeDoubleRange(Long v, Long max) {
return v >= -max && v <= max;
}
}

View File

@ -0,0 +1,8 @@
package org.enso.base_test_helpers;
public class Number_Utils {
// For testing only.
public static Double floatId(Double d) {
return d;
}
}

View File

@ -12,6 +12,7 @@ import project.Data.Round_Spec
polyglot java import java.math.BigInteger
polyglot java import java.math.BigDecimal
polyglot java import org.enso.base_test_helpers.Number_Utils
Integer.is_even self = self % 2 == 0
@ -39,6 +40,10 @@ type Complex_Comparator
Comparable.from (_:Complex) = Complex_Comparator
Comparable.from (_:Number) = Complex_Comparator
float_id : Float -> Float
float_id f =
Number_Utils.floatId f
add_specs suite_builder =
eps = 0.000001
@ -701,6 +706,66 @@ add_specs suite_builder =
[x, y, z, w].each b->
a+b . should_equal 20
suite_builder.group "arithmetic" group_builder->
group_builder.specify "should be able to pass a Float to Java" <|
vs = []
+ [92233720368547758080.0]
+ [9223372036854777000.0]
+ [9223372036854776001.0]
+ [9223372036854776000.0]
+ [9223372036854775808.0]
+ [9223372036854775000.0]
+ [9223372036854774000.0]
+ [9223372036854700000.0]
+ [9223372036854000000.0]
+ [9223372000000000000.0]
+ [922337203685470000.0]
+ [92233720368547000.0]
+ [9223372036854700.0]
+ [922337203685470.0]
+ [92233720368547.0]
+ [9223372036854.0]
+ [922337203685.0]
+ [92233720368.0]
+ [9223372036.0]
+ [922337203.0]
+ [92233720.0]
+ [9223372.0]
+ [922337.0]
+ [92233.0]
+ [9223.0]
+ [922.0]
+ [-92233720368547758080.0]
+ [-9223372036854777000.0]
+ [-9223372036854776001.0]
+ [-9223372036854776000.0]
+ [-9223372036854775808.0]
+ [-9223372036854775000.0]
+ [-9223372036854774000.0]
+ [-9223372036854700000.0]
+ [-9223372036854000000.0]
+ [-9223372000000000000.0]
+ [-922337203685470000.0]
+ [-92233720368547000.0]
+ [-9223372036854700.0]
+ [-922337203685470.0]
+ [-92233720368547.0]
+ [-9223372036854.0]
+ [-922337203685.0]
+ [-92233720368.0]
+ [-9223372036.0]
+ [-922337203.0]
+ [-92233720.0]
+ [-9223372.0]
+ [-922337.0]
+ [-92233.0]
+ [-9223.0]
+ [-922.0]
vs.map v->
r = float_id v
r . should_equal v
main filter=Nothing =
suite = Test.build suite_builder->
add_specs suite_builder

View File

@ -46,12 +46,6 @@ add_specs suite_builder =
x = Java_Integer.valueOf 1
x.test_me x . should_equal False
suite_builder.group "Numeric values" group_builder->
group_builder.specify "can be passed in host calls without lossy coercion exception" <|
large_long = 6907338656278321365
moments = Moments.new 1
moments.add large_long
suite_builder.group "Java/Enso Date" group_builder->
group_builder.specify "Java date has Enso properties" <|
april1st = LocalDate.of 2022 04 01