Skip to content

Commit 8383e4e

Browse files
another iteration
1 parent 2d38f0a commit 8383e4e

1 file changed

Lines changed: 37 additions & 6 deletions

File tree

tests/testthat/test-sortuse64.R

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,58 @@
1-
with_parameters_test_that("orderfin and orderpos work with partial order", method=1:3, {
1+
with_parameters_test_that("sortfin works", method=1:3, {
2+
x = as.integer64(1:10)
3+
r = sample(x)
4+
expect_identical(sortfin(integer64(), 1:10, method=method), rep(FALSE, 10L))
5+
expect_identical(sortfin(x, integer64(), method=method), logical())
6+
7+
expect_true(all(sortfin(x, r, method=method)))
8+
9+
expect_true(sortfin(x, 1L, method=method))
10+
expect_true(all(sortfin(x, c(1.0, 4.0), method=method)))
11+
expect_identical(sortfin(x, as.integer64(0:1), method=method), c(FALSE, TRUE))
12+
# only 'x' need be sorted
13+
expect_identical(sortfin(x, as.integer64(1:0), method=method), c(TRUE, FALSE))
14+
})
15+
16+
with_parameters_test_that("orderfin and orderpos work", method=1:3, {
17+
x = as.integer64(1:10)
18+
idx = seq_along(x)
19+
20+
expect_identical(orderfin(x, idx, 0:1, method=method), c(FALSE, TRUE))
21+
expect_identical(orderpos(x, idx, 0:1, method=method), c(NA_integer_, 1L))
22+
223
# These were segfaulting due to length(order) < length(table)
324
table = as.integer64(c(10, 20, 30, 5, 15, 25))
425
order = c(4, 1, 5, 2, 6, 3) # order of table is 5, 10, 15, 20, 25, 30
526

627
partial_order = c(4, 1, 5, 2) # order of subset of table is 5, 10, 15, 20
728

8-
x = as.integer64(c(5, 10, 15, 20, 25, 30, 99))
29+
x_search = as.integer64(c(5, 10, 15, 20, 25, 30, 99))
930

1031
# reference with full order
1132
full_fin = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE)
1233
full_pos = c(4L, 1L, 5L, 2L, 6L, 3L, NA_integer_)
13-
expect_identical(orderfin(table, order, x, method=method), full_fin)
14-
expect_identical(orderpos(table, order, x, method=method), full_pos)
34+
expect_identical(orderfin(table, order, x_search, method=method), full_fin)
35+
expect_identical(orderpos(table, order, x_search, method=method), full_pos)
1536

1637
# with partial order, we search in a subset
1738
partial_fin = c(TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE)
1839
partial_pos = c(4L, 1L, 5L, 2L, NA_integer_, NA_integer_, NA_integer_)
19-
expect_identical(orderfin(table, partial_order, x, method=method), partial_fin)
20-
expect_identical(orderpos(table, partial_order, x, method=method), partial_pos)
40+
expect_identical(orderfin(table, partial_order, x_search, method=method), partial_fin)
41+
expect_identical(orderpos(table, partial_order, x_search, method=method), partial_pos)
2142

2243
# user's example, where `table[order]` is not sorted ascending.
2344
# The C functions expect ascending order, so they will fail to find the values.
2445
# The test confirms the functions no longer segfault and return a consistent result.
2546
expect_identical(orderfin(as.integer64(10:1), 1:3, 8:11, method=method), rep(FALSE, 4L))
2647
expect_identical(orderpos(as.integer64(10:1), 1:3, 8:11, method=method), rep(NA_integer_, 4L))
2748
})
49+
50+
test_that("ordertab and orderdup work", {
51+
x = as.integer64(1:10)
52+
x = c(x, x[1:8], x[1:6])
53+
idx = order(x)
54+
55+
expect_identical(ordertab(x, idx, 10L), rep(3:1, c(6L, 2L, 2L)))
56+
expect_identical(orderdup(x, idx), rep(c(FALSE, TRUE), c(10L, 14L)))
57+
expect_identical(orderdup(x, idx, method=2L), rep(c(FALSE, TRUE), c(10L, 14L)))
58+
})

0 commit comments

Comments
 (0)