|
1 | | -with_parameters_test_that("sortfin works", method=1:3, { |
2 | | - x = as.integer64(1:10) |
3 | | - r = sample(x) # NB: default method assumes 'sorted' is, well, sorted, so this requires method!=1 |
4 | | - expect_identical(sortfin(integer64(), 1:10, method=method), rep(FALSE, 10L)) |
5 | | - expect_identical(sortfin(x, integer64(), method=method), logical()) |
| 1 | +test_that("orderfin and orderpos work with partial order", { |
| 2 | + # These were segfaulting due to length(order) < length(table) |
| 3 | + table = as.integer64(c(10, 20, 30, 5, 15, 25)) |
| 4 | + order = c(4, 1, 5, 2, 6, 3) # order of table is 5, 10, 15, 20, 25, 30 |
6 | 5 |
|
7 | | - expect_true(all(sortfin(x, r))) |
| 6 | + partial_order = c(4, 1, 5, 2) # order of subset of table is 5, 10, 15, 20 |
8 | 7 |
|
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 | | -test_that("orderfin and orderpos work", { |
17 | | - x = as.integer64(1:10) |
18 | | - idx = seq_along(x) |
| 8 | + x = as.integer64(c(5, 10, 15, 20, 25, 30, 99)) |
19 | 9 |
|
20 | | - expect_identical(orderfin(x, idx, 0:1), c(FALSE, TRUE)) |
21 | | - expect_identical(orderfin(x, idx, as.integer64(0:1), method=2L), c(FALSE, TRUE)) |
22 | | - expect_identical(orderfin(x, idx, as.integer64(0:1), method=3L), c(FALSE, TRUE)) |
23 | | - |
24 | | - expect_identical(orderpos(x, idx, 0:1), c(NA_integer_, 1L)) |
25 | | - expect_identical(orderpos(x, idx, as.integer64(0:1), method=2L), c(NA_integer_, 1L)) |
26 | | - expect_identical(orderpos(x, idx, as.integer64(0:1), method=3L), c(NA_integer_, 1L)) |
27 | | -}) |
| 10 | + # reference with full order |
| 11 | + full_fin = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE) |
| 12 | + full_pos = c(4L, 1L, 5L, 2L, 6L, 3L, NA_integer_) |
| 13 | + expect_identical(orderfin(table, order, x), full_fin) |
| 14 | + expect_identical(orderpos(table, order, x), full_pos) |
28 | 15 |
|
29 | | -test_that("ordertab and orderdup work", { |
30 | | - x = as.integer64(1:10) |
31 | | - x = c(x, x[1:8], x[1:6]) |
32 | | - idx = order(x) |
| 16 | + # with partial order, we search in a subset |
| 17 | + partial_fin = c(TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE) |
| 18 | + partial_pos = c(4L, 1L, 5L, 2L, NA_integer_, NA_integer_, NA_integer_) |
| 19 | + expect_identical(orderfin(table, partial_order, x), partial_fin) |
| 20 | + expect_identical(orderpos(table, partial_order, x), partial_pos) |
33 | 21 |
|
34 | | - expect_identical(ordertab(x, idx, 10L), rep(3:1, c(6L, 2L, 2L))) |
35 | | - expect_identical(orderdup(x, idx), rep(c(FALSE, TRUE), c(10L, 14L))) |
36 | | - expect_identical(orderdup(x, idx, method=2L), rep(c(FALSE, TRUE), c(10L, 14L))) |
| 22 | + # user's example, table is not sorted ascending with order |
| 23 | + # just check for no error |
| 24 | + expect_identical(orderfin(as.integer64(10:1), 1:3, 8:11, method=1L), c(TRUE, TRUE, TRUE, FALSE)) |
| 25 | + expect_identical(orderpos(as.integer64(10:1), 1:3, 8:11, method=1L), c(1L, 2L, 3L, NA_integer_)) |
37 | 26 | }) |
0 commit comments