From cda6d4cab82ba758ecd811ce6dcae995a31ac46e Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Sat, 1 Feb 2025 16:40:19 -0800 Subject: [PATCH 1/3] shellorder, shellsortorder tests, and tests of ties --- tests/testthat/test-sort64.R | 232 ++++++++++++++++++++++++----------- 1 file changed, 157 insertions(+), 75 deletions(-) diff --git a/tests/testthat/test-sort64.R b/tests/testthat/test-sort64.R index 0fcc5c3..c2ccc1e 100644 --- a/tests/testthat/test-sort64.R +++ b/tests/testthat/test-sort64.R @@ -36,22 +36,26 @@ test_that("ramsort method for integer64 works", { for (na.last in c(FALSE, TRUE)) { for (decreasing in c(FALSE, TRUE)) { - for (n_missing in 0:2) { - na_entries = rep(NA_integer64_, n_missing) - y = sample(c(x, na_entries)) - expect_identical(ramsort(y, decreasing=decreasing, na.last=na.last), n_missing) - # TODO(#154): Drop explicit 'else' branches - expected_value = c( - if (na.last) integer64() else na_entries, - if (decreasing) rev(x) else x, - if (na.last) na_entries else integer64() - ) - expect_identical(y, expected_value, - info=sprintf( - "(na.last, decreasing, n_missing)=(%s, %s, %d)", - na.last, decreasing, n_missing + for (duplicates in c(FALSE, TRUE)) { + for (n_missing in 0:2) { + na_entries = rep(NA_integer64_, n_missing) + y = sample(c(x, if (duplicates) x[1L], na_entries)) + expect_identical(ramsort(y, decreasing=decreasing, na.last=na.last), n_missing) + # TODO(#154): Drop explicit 'else' branches + expected_value = c( + if (na.last) integer64() else na_entries, + if (duplicates && !decreasing) x[1L], + if (decreasing) rev(x) else x, + if (duplicates && decreasing) x[1L], + if (na.last) na_entries else integer64() ) - ) + expect_identical(y, expected_value, + info=sprintf( + "(na.last, decreasing, duplicates, n_missing)=(%s, %s, %s, %d)", + na.last, decreasing, duplicates, n_missing + ) + ) + } } } } @@ -62,22 +66,26 @@ test_that("shellsort method for integer64 works", { for (na.last in c(FALSE, TRUE)) { for (decreasing in c(FALSE, TRUE)) { - for (n_missing in 0:2) { - na_entries = rep(NA_integer64_, n_missing) - y = sample(c(x, na_entries)) - expect_identical(shellsort(y, decreasing=decreasing, na.last=na.last), n_missing) - # TODO(#154): Drop explicit 'else' branches - expected_value = c( - if (na.last) integer64() else na_entries, - if (decreasing) rev(x) else x, - if (na.last) na_entries else integer64() - ) - expect_identical(y, expected_value, - info=sprintf( - "(na.last, decreasing, n_missing)=(%s, %s, %d)", - na.last, decreasing, n_missing + for (duplicates in c(FALSE, TRUE)) { + for (n_missing in 0:2) { + na_entries = rep(NA_integer64_, n_missing) + y = sample(c(x, if (duplicates) x[1L], na_entries)) + expect_identical(shell(y, decreasing=decreasing, na.last=na.last), n_missing) + # TODO(#154): Drop explicit 'else' branches + expected_value = c( + if (na.last) integer64() else na_entries, + if (duplicates && !decreasing) x[1L], + if (decreasing) rev(x) else x, + if (duplicates && decreasing) x[1L], + if (na.last) na_entries else integer64() + ) + expect_identical(y, expected_value, + info=sprintf( + "(na.last, decreasing, duplicates, n_missing)=(%s, %s, %s, %d)", + na.last, decreasing, duplicates, n_missing + ) ) - ) + } } } } @@ -88,22 +96,26 @@ test_that("mergesort method for integer64 works", { for (na.last in c(FALSE, TRUE)) { for (decreasing in c(FALSE, TRUE)) { - for (n_missing in 0:2) { - na_entries = rep(NA_integer64_, n_missing) - y = sample(c(x, na_entries)) - expect_identical(mergesort(y, decreasing=decreasing, na.last=na.last), n_missing) - # TODO(#154): Drop explicit 'else' branches - expected_value = c( - if (na.last) integer64() else na_entries, - if (decreasing) rev(x) else x, - if (na.last) na_entries else integer64() - ) - expect_identical(y, expected_value, - info=sprintf( - "(na.last, decreasing, n_missing)=(%s, %s, %d)", - na.last, decreasing, n_missing + for (duplicates in c(FALSE, TRUE)) { + for (n_missing in 0:2) { + na_entries = rep(NA_integer64_, n_missing) + y = sample(c(x, if (duplicates) x[1L], na_entries)) + expect_identical(mergesort(y, decreasing=decreasing, na.last=na.last), n_missing) + # TODO(#154): Drop explicit 'else' branches + expected_value = c( + if (na.last) integer64() else na_entries, + if (duplicates && !decreasing) x[1L], + if (decreasing) rev(x) else x, + if (duplicates && decreasing) x[1L], + if (na.last) na_entries else integer64() + ) + expect_identical(y, expected_value, + info=sprintf( + "(na.last, decreasing, duplicates, n_missing)=(%s, %s, %s, %d)", + na.last, decreasing, duplicates, n_missing + ) ) - ) + } } } } @@ -114,22 +126,26 @@ test_that("radixsort method for integer64 works", { for (na.last in c(FALSE, TRUE)) { for (decreasing in c(FALSE, TRUE)) { - for (n_missing in 0:2) { - na_entries = rep(NA_integer64_, n_missing) - y = sample(c(x, na_entries)) - expect_identical(radixsort(y, decreasing=decreasing, na.last=na.last), n_missing) - # TODO(#154): Drop explicit 'else' branches - expected_value = c( - if (na.last) integer64() else na_entries, - if (decreasing) rev(x) else x, - if (na.last) na_entries else integer64() - ) - expect_identical(y, expected_value, - info=sprintf( - "(na.last, decreasing, n_missing)=(%s, %s, %d)", - na.last, decreasing, n_missing + for (duplicates in c(FALSE, TRUE)) { + for (n_missing in 0:2) { + na_entries = rep(NA_integer64_, n_missing) + y = sample(c(x, if (duplicates) x[1L], na_entries)) + expect_identical(radixsort(y, decreasing=decreasing, na.last=na.last), n_missing) + # TODO(#154): Drop explicit 'else' branches + expected_value = c( + if (na.last) integer64() else na_entries, + if (duplicates && !decreasing) x[1L], + if (decreasing) rev(x) else x, + if (duplicates && decreasing) x[1L], + if (na.last) na_entries else integer64() + ) + expect_identical(y, expected_value, + info=sprintf( + "(na.last, decreasing, duplicates, n_missing)=(%s, %s, %s, %d)", + na.last, decreasing, duplicates, n_missing + ) ) - ) + } } } } @@ -140,22 +156,88 @@ test_that("quicksort method for integer64 works", { for (na.last in c(FALSE, TRUE)) { for (decreasing in c(FALSE, TRUE)) { - for (n_missing in 0:2) { - na_entries = rep(NA_integer64_, n_missing) - y = sample(c(x, na_entries)) - expect_identical(quicksort(y, decreasing=decreasing, na.last=na.last), n_missing) - # TODO(#154): Drop explicit 'else' branches - expected_value = c( - if (na.last) integer64() else na_entries, - if (decreasing) rev(x) else x, - if (na.last) na_entries else integer64() - ) - expect_identical(y, expected_value, - info=sprintf( - "(na.last, decreasing, n_missing)=(%s, %s, %d)", - na.last, decreasing, n_missing + for (duplicates in c(FALSE, TRUE)) { + for (n_missing in 0:2) { + na_entries = rep(NA_integer64_, n_missing) + y = sample(c(x, if (duplicates) x[1L], na_entries)) + expect_identical(quicksort(y, decreasing=decreasing, na.last=na.last), n_missing) + # TODO(#154): Drop explicit 'else' branches + expected_value = c( + if (na.last) integer64() else na_entries, + if (duplicates && !decreasing) x[1L], + if (decreasing) rev(x) else x, + if (duplicates && decreasing) x[1L], + if (na.last) na_entries else integer64() + ) + expect_identical(y, expected_value, + info=sprintf( + "(na.last, decreasing, duplicates, n_missing)=(%s, %s, %s, %d)", + na.last, decreasing, duplicates, n_missing + ) + ) + } + } + } + } +}) + +test_that("shellorder method for integer64 works", { + x = as.integer64(1:10) + + for (na.last in c(FALSE, TRUE)) { + for (decreasing in c(FALSE, TRUE)) { + for (duplicates in c(FALSE, TRUE)) { + for (n_missing in 0:2) { + na_entries = rep(NA_integer64_, n_missing) + y = sample(c(x, if (duplicates) x[1L], na_entries)) + i = seq_along(y) + expect_identical(shellorder(y, i, decreasing=decreasing, na.last=na.last), n_missing) + # TODO(#154): Drop explicit 'else' branches + expected_value = c( + if (na.last) integer64() else na_entries, + if (duplicates && !decreasing) x[1L], + if (decreasing) rev(x) else x, + if (duplicates && decreasing) x[1L], + if (na.last) na_entries else integer64() ) - ) + expect_identical(y[i], expected_value, + info=sprintf( + "(na.last, decreasing, duplicates, n_missing)=(%s, %s, %s, %d)", + na.last, decreasing, duplicates, n_missing + ) + ) + } + } + } + } +}) + +test_that("shellsortorder method for integer64 works", { + x = as.integer64(1:10) + + for (na.last in c(FALSE, TRUE)) { + for (decreasing in c(FALSE, TRUE)) { + for (duplicates in c(FALSE, TRUE)) { + for (n_missing in 0:2) { + na_entries = rep(NA_integer64_, n_missing) + y = sample(c(x, if (duplicates) x[1L], na_entries)) + i = seq_along(y) + expect_identical(shellsortorder(y, i, decreasing=decreasing, na.last=na.last), n_missing) + # TODO(#154): Drop explicit 'else' branches + expected_value = c( + if (na.last) integer64() else na_entries, + if (duplicates && !decreasing) x[1L], + if (decreasing) rev(x) else x, + if (duplicates && decreasing) x[1L], + if (na.last) na_entries else integer64() + ) + expect_identical(y, expected_value, + info=sprintf( + "(na.last, decreasing, duplicates, n_missing)=(%s, %s, %s, %d)", + na.last, decreasing, duplicates, n_missing + ) + ) + } } } } From 33743d9cba89519c6b0abeca606ea16cf8f0b9d7 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Sat, 1 Feb 2025 16:44:13 -0800 Subject: [PATCH 2/3] Add as TODO --- tests/testthat/test-sort64.R | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/testthat/test-sort64.R b/tests/testthat/test-sort64.R index c2ccc1e..ee2fe7d 100644 --- a/tests/testthat/test-sort64.R +++ b/tests/testthat/test-sort64.R @@ -231,6 +231,7 @@ test_that("shellsortorder method for integer64 works", { if (duplicates && decreasing) x[1L], if (na.last) na_entries else integer64() ) + # TODO(#159): Also add expectations for the update to i expect_identical(y, expected_value, info=sprintf( "(na.last, decreasing, duplicates, n_missing)=(%s, %s, %s, %d)", From 21e39dcb450ab9aa336ad60b7a5e8e57773b1c23 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Sat, 1 Feb 2025 16:44:41 -0800 Subject: [PATCH 3/3] typo --- tests/testthat/test-sort64.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-sort64.R b/tests/testthat/test-sort64.R index ee2fe7d..05ef7fb 100644 --- a/tests/testthat/test-sort64.R +++ b/tests/testthat/test-sort64.R @@ -70,7 +70,7 @@ test_that("shellsort method for integer64 works", { for (n_missing in 0:2) { na_entries = rep(NA_integer64_, n_missing) y = sample(c(x, if (duplicates) x[1L], na_entries)) - expect_identical(shell(y, decreasing=decreasing, na.last=na.last), n_missing) + expect_identical(shellsort(y, decreasing=decreasing, na.last=na.last), n_missing) # TODO(#154): Drop explicit 'else' branches expected_value = c( if (na.last) integer64() else na_entries,