Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yutannihilation committed Oct 21, 2024
1 parent 362211a commit cd7c152
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 5 additions & 2 deletions R-package/tests/testthat/test-numeric.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ test_that("NumericSexp works for usize conversions", {
# f64 to usize
expect_equal(
# 2147483647 = .Machine$integer.max
usize_to_string(c(0.0, 10.0, 2147483648.0)),
c("0", "10", "2147483648")
usize_to_string(c(0.0, 10.0, 2147483648.0, 9007199254740991.0)),
c("0", "10", "2147483648", "9007199254740991")
)

# error cases
Expand All @@ -50,6 +50,7 @@ test_that("NumericSexp works for usize conversions", {
expect_error(usize_to_string(NaN))
expect_error(usize_to_string(-1L))
expect_error(usize_to_string(-1.0))
expect_error(usize_to_string_scalar(9007199254740992.0))
})


Expand Down Expand Up @@ -81,6 +82,7 @@ test_that("NumericScalar works for usize conversions", {
expect_equal(usize_to_string_scalar(10.0), "10")
# 2147483647 = .Machine$integer.max
expect_equal(usize_to_string_scalar(2147483648.0), "2147483648")
expect_equal(usize_to_string_scalar(9007199254740991.0), "9007199254740991")

# error cases
expect_error(usize_to_string_scalar(NA_integer_))
Expand All @@ -89,4 +91,5 @@ test_that("NumericScalar works for usize conversions", {
expect_error(usize_to_string_scalar(NaN))
expect_error(usize_to_string_scalar(-1L))
expect_error(usize_to_string_scalar(-1.0))
expect_error(usize_to_string_scalar(9007199254740992.0))
})
4 changes: 3 additions & 1 deletion src/sexp/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const I32MAX: f64 = i32::MAX as f64;
const I32MIN: f64 = i32::MIN as f64;

// f64 can represent 2^53
// cf. https://en.wikipedia.org/wiki/Double-precision_floating-point_format
//
// cf. https://en.wikipedia.org/wiki/Double-precision_floating-point_format,
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
const F64_MAX_SIGFIG: f64 = (2_u64.pow(53) - 1) as f64;

const TOLERANCE: f64 = 0.01; // This is super-tolerant than vctrs, but this should be sufficient.
Expand Down

0 comments on commit cd7c152

Please sign in to comment.