Skip to content

Commit

Permalink
More tests, including improving error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico committed Oct 15, 2024
1 parent 5ea67fb commit a321309
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 42 deletions.
81 changes: 39 additions & 42 deletions R/highlevel64.R
Original file line number Diff line number Diff line change
Expand Up @@ -1426,7 +1426,7 @@ match.integer64 <- function(x, table, nomatch = NA_integer_, nunique=NULL, metho
}
p <- orderpos(table, o, x, nomatch=nomatch)
}
, stop("unknown method")
, stop("unknown method ", method)
)
p
}
Expand All @@ -1436,42 +1436,39 @@ match.integer64 <- function(x, table, nomatch = NA_integer_, nunique=NULL, metho
`%in%.integer64` <- function(x, table, ...){
stopifnot(is.integer64(x))
table <- as.integer64(table)
nunique <- NULL
method <- NULL
nunique <- NULL
cache_env <- cache(table)
if (is.null(method)){
if (is.null(cache_env)){
nx <- length(x)
if (is.null(nunique))
nunique <- length(table)
btable <- as.integer(ceiling(log2(nunique*1.5)))
bx <- as.integer(ceiling(log2(nx*1.5)))
if (bx<=17L && btable>=16L){
method <- "hashrin"
}else{
method <- "hashfin"
}
} else if (exists("hashmap", envir=cache_env, inherits=FALSE)) {
method <- "hashfin"
} else if (exists("sort", envir=cache_env, inherits=FALSE) && (length(table)>length(x) || length(x)<4096L)) {
method <- "sortfin"
} else if (exists("order", envir=cache_env, inherits=FALSE) && (length(table)>length(x) || length(x)<4096L)) {
method <- "orderfin"
if (is.null(cache_env)) {
nx <- length(x)
if (is.null(nunique))
nunique <- length(table)
btable <- as.integer(ceiling(log2(nunique*1.5)))
bx <- as.integer(ceiling(log2(nx*1.5)))
if (bx<=17L && btable>=16L) {
method <- "hashrin"
} else {
nx <- length(x)
if (is.null(nunique)){
if (exists("nunique", envir=cache_env, inherits=FALSE))
nunique <- cache_env$nunique
else
nunique <- length(table)
}
btable <- as.integer(ceiling(log2(nunique*1.5)))
bx <- as.integer(ceiling(log2(nx*1.5)))
if (bx<=17L && btable>=16L) {
method <- "hashrin"
} else {
method <- "hashfin"
}
method <- "hashfin"
}
} else if (exists("hashmap", envir=cache_env, inherits=FALSE)) {
method <- "hashfin"
} else if (exists("sort", envir=cache_env, inherits=FALSE) && (length(table)>length(x) || length(x)<4096L)) {
method <- "sortfin"
} else if (exists("order", envir=cache_env, inherits=FALSE) && (length(table)>length(x) || length(x)<4096L)) {
method <- "orderfin"

Check warning on line 1457 in R/highlevel64.R

View check run for this annotation

Codecov / codecov/patch

R/highlevel64.R#L1452-L1457

Added lines #L1452 - L1457 were not covered by tests
} else {
nx <- length(x)
if (is.null(nunique)){
if (exists("nunique", envir=cache_env, inherits=FALSE))
nunique <- cache_env$nunique

Check warning on line 1462 in R/highlevel64.R

View check run for this annotation

Codecov / codecov/patch

R/highlevel64.R#L1459-L1462

Added lines #L1459 - L1462 were not covered by tests
else
nunique <- length(table)

Check warning on line 1464 in R/highlevel64.R

View check run for this annotation

Codecov / codecov/patch

R/highlevel64.R#L1464

Added line #L1464 was not covered by tests
}
btable <- as.integer(ceiling(log2(nunique*1.5)))
bx <- as.integer(ceiling(log2(nx*1.5)))
if (bx<=17L && btable>=16L) {
method <- "hashrin"

Check warning on line 1469 in R/highlevel64.R

View check run for this annotation

Codecov / codecov/patch

R/highlevel64.R#L1466-L1469

Added lines #L1466 - L1469 were not covered by tests
} else {
method <- "hashfin"

Check warning on line 1471 in R/highlevel64.R

View check run for this annotation

Codecov / codecov/patch

R/highlevel64.R#L1471

Added line #L1471 was not covered by tests
}
}
switch(method
Expand Down Expand Up @@ -1520,7 +1517,7 @@ match.integer64 <- function(x, table, nomatch = NA_integer_, nunique=NULL, metho
}
p <- orderfin(table, o, x)
}
, stop("unknown method")
, stop("unknown method ", method)

Check warning on line 1520 in R/highlevel64.R

View check run for this annotation

Codecov / codecov/patch

R/highlevel64.R#L1520

Added line #L1520 was not covered by tests
)
p
}
Expand Down Expand Up @@ -1571,7 +1568,7 @@ duplicated.integer64 <- function(x
if (is.null(method)){
if (is.null(cache_env)){
if (length(x)>50000000L)
method <- "sortorderdup"
method <- "sortorderdup" # nocov. Too large for practical unit tests.
else
method <- "hashdup"
} else if (exists("sort", envir=cache_env, inherits=FALSE) && exists("order", envir=cache_env, inherits=FALSE))
Expand Down Expand Up @@ -1613,7 +1610,7 @@ duplicated.integer64 <- function(x
}
p <- orderdup(x, o)
}
, stop("unknown method", method)
, stop("unknown method ", method)
)
p
}
Expand Down Expand Up @@ -1774,7 +1771,7 @@ unique.integer64 <- function(x
nunique <- ordernut(x, o)[1L]
p <- orderuni(x, o, nunique, keep.order=keep.order)
}
, stop("unknown method", method)
, stop("unknown method ", method)
)
p
}
Expand Down Expand Up @@ -1926,7 +1923,7 @@ unipos.integer64 <- function(x
nunique <- ordernut(x, o)[1L]
p <- orderupo(x, o, nunique, keep.order=keep.order)
}
, stop("unknown method", method)
, stop("unknown method ", method)
)
p
}
Expand Down Expand Up @@ -2326,7 +2323,7 @@ keypos.integer64 <- function(x
}
p <- orderkey(x, o)
}
, stop("unknown method", method)
, stop("unknown method ", method)
)
p
}
Expand Down Expand Up @@ -2409,7 +2406,7 @@ tiepos.integer64 <- function(x
nties <- ordernut(x, o)[2L]
p <- ordertie(x, o, nties)
}
, stop("unknown method", method)
, stop("unknown method ", method)
)
p
}
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-highlevel64.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ test_that("Different method= for match() and %in% work", {
expect_identical(match(x, y, method="hashpos"), expected)
expect_identical(match(x, y, method="hashrev"), expected)
expect_identical(match(x, y, method="sortorderpos"), expected)
expect_error(match(x, y, method="_unknown_"), "unknown method _unknown_", fixed=TRUE)
# TODO(#58): Fix this, currently fails.
# expect_identical(match(x, y, method="orderpos"), expected)

Expand Down Expand Up @@ -63,6 +64,9 @@ test_that("duplicated, unique, table methods work", {
expect_identical(duplicated(x), c(FALSE, TRUE, FALSE))
expect_identical(unique(x), x[c(1L, 3L)])
expect_identical(table.integer64(x), table(x = c(1L, 1L, 2L)))

expect_error(duplicated(x, method="_unknown_"), "unknown method _unknown_", fixed=TRUE)
expect_error(unique(x, method="_unknown_"), "unknown method _unknown_", fixed=TRUE)
})

test_that("different method= for duplicated, unique work", {
Expand Down Expand Up @@ -117,6 +121,7 @@ test_that("sorting methods work", {
x_tiepos = c(1L, 2L, 3L, 5L)
expect_identical(tiepos(x), x_tiepos)
expect_identical(tiepos(x, method="ordertie"), x_tiepos)
expect_error(tiepos(x, method="_unknown_"), "unknown method _unknown_", fixed=TRUE)
})

# These tests were previously kept as tests under \examples{\dontshow{...}}.
Expand Down Expand Up @@ -164,13 +169,15 @@ test_that("unipos() works as intended", {
expect_identical(unipos(x, method="hashupo"), x_unipos)
expect_identical(unipos(x, method="sortorderupo"), x_unipos)
expect_identical(unipos(x, method="orderupo"), x_unipos)
expect_error(unipos(x, method="_unknown_"), "unknown method _unknown_", fixed=TRUE)
})

test_that("keypos() works as intended", {
x = as.integer64(c(5L, 2L, 5L, 3L, 2L, 4L))
x_keypos = c(4L, 1L, 4L, 2L, 1L, 3L)
expect_identical(keypos(x), x_keypos)
expect_identical(keypos(x, method="orderkey"), x_keypos)
expect_error(keypos(x, method="_unknown_"), "unknown method _unknown_", fixed=TRUE)
})

test_that("summary() works as intended", {
Expand Down

0 comments on commit a321309

Please sign in to comment.