-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest-float-to-int.R
More file actions
39 lines (30 loc) · 910 Bytes
/
Copy pathtest-float-to-int.R
File metadata and controls
39 lines (30 loc) · 910 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Float-to-int conversion semantics (truncation vs floor/ceiling).
test_that("as.integer(double) truncates toward zero", {
fn <- function(x) {
declare(type(x = double(NA)))
out <- as.integer(x)
out
}
expect_translation_snapshots(fn)
x <- c(-2.9, -2.1, -1.9, -1.1, -0.9, -0.1, 0, 0.1, 0.9, 1.1, 1.9, 2.1)
expect_quick_identical(fn, x)
})
test_that("trunc() returns double and truncates toward zero", {
fn_d <- function(x) {
declare(type(x = double(NA)))
out <- trunc(x)
out
}
expect_translation_snapshots(fn_d)
x <- c(-2.9, -2.1, -1.9, -1.1, -0.9, -0.1, 0, 0.1, 0.9, 1.1, 1.9, 2.1)
expect_quick_equal(fn_d, x)
fn_i <- function(x) {
declare(type(x = integer(NA)))
out <- trunc(x)
out
}
expect_translation_snapshots(fn_i)
xi <- as.integer(c(-3, -1, 0, 2, 5))
expect_quick_equal(fn_i, xi)
expect_identical(typeof(fn_i(xi)), "double")
})