Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reproduce Float-passing problem #9201

Merged
merged 17 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@

/** 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)
Expand All @@ -21,15 +16,6 @@ public HostAccess allWithTypeMapping() {
.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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
65 changes: 65 additions & 0 deletions test/Base_Tests/src/Data/Numbers_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions test/Base_Tests/src/Semantic/Java_Interop_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading