context("Permutations with replacement") test_that("Permutations with replacement - npermutations", { expect_equal(npermutations(5, 3, replace = TRUE), 125) expect_equal(npermutations(x = LETTERS[1:5], k = 3, replace = TRUE), 125) expect_error(npermutations(10, 10, replace = TRUE), "integer overflow") expect_error(npermutations(x = LETTERS[1:10], k = 10, replace = TRUE), "integer overflow") expect_equal(npermutations(10, 10, replace = TRUE, bigz = TRUE), gmp::as.bigz("10000000000")) expect_equal(npermutations(10, 0, replace = TRUE), 1) expect_equal(npermutations(2, 3, replace = TRUE), 8) expect_error(npermutations(6, -1, replace = TRUE), "expect integer") expect_error(npermutations(6, 1.5, replace = TRUE), "expect integer") expect_equal(npermutations(0, 0, replace = TRUE), 1) expect_equal(npermutations(0, 1, replace = TRUE), 0) }) test_that("Permutations with replacement - permutations", { perm <- permutations(5, 3, replace= TRUE) expect_equal(nrow(perm), 125) expect_equal(ncol(perm), 3) expect_equal(perm[1, ], rep(1, 3)) expect_equal(perm[125, ], rep(5, 3)) perm <- permutations(5, 3, replace= TRUE, layout = "row") expect_equal(nrow(perm), 125) expect_equal(ncol(perm), 3) expect_equal(perm[1, ], rep(1, 3)) expect_equal(perm[125, ], rep(5, 3)) perm <- permutations(5, 3, replace= TRUE, layout = "column") expect_equal(ncol(perm), 125) expect_equal(nrow(perm), 3) expect_equal(perm[, 1], rep(1, 3)) expect_equal(perm[, 125], rep(5, 3)) perm <- permutations(5, 3, replace= TRUE, layout = "list") expect_equal(length(perm), 125) expect_equal(perm[[1]], rep(1, 3)) expect_equal(perm[[125]], rep(5, 3)) perm <- permutations(x = LETTERS[1:5], k = 3, replace= TRUE) expect_equal(nrow(perm), 125) expect_equal(ncol(perm), 3) expect_equal(perm[1, ], LETTERS[rep(1, 3)]) expect_equal(perm[125, ], LETTERS[rep(5, 3)]) expect_error(permutations(10, 10, replace = TRUE), "too many results") expect_error(permutations(5, -1, replace= TRUE), "expect integer") expect_error(permutations(5, 1.5, replace= TRUE), "expect integer") expect_equal(dim(permutations(5, 0, replace= TRUE)), c(1, 0)) expect_equal(dim(permutations(2, 3, replace= TRUE)), c(8, 3)) expect_equal(dim(permutations(0, 0, replace= TRUE)), c(1, 0)) expect_equal(dim(permutations(0, 1, replace= TRUE)), c(0, 1)) }) test_that("Permutations with replacement - ipermutations", { iperm <- ipermutations(5, 3, replace= TRUE) perm <- permutations(5, 3, replace= TRUE) expect_equal(iperm$collect(), perm) expect_equal(iperm$getnext(), rep(1, 3)) expect_equal(iperm$getnext(), c(1, 1, 2)) iperm$getnext(120) expect_equal(nrow(iperm$getnext(10)), 3) expect_equal(iperm$getnext(), NULL) perm <- permutations(5, 3, replace= TRUE, layout = "row") expect_equal(iperm$collect(layout = "row"), perm) expect_equal(iperm$getnext(layout = "row"), t(rep(1, 3))) expect_equal(iperm$getnext(layout = "row"), t(c(1, 1, 2))) iperm$getnext(120, layout = "row") expect_equal(nrow(iperm$getnext(10, layout = "row")), 3) expect_equal(iperm$getnext(layout = "row"), NULL) perm <- permutations(5, 3, replace= TRUE, layout = "column") expect_equal(iperm$collect(layout = "column"), perm) expect_equal(iperm$getnext(layout = "column"), t(t(rep(1, 3)))) expect_equal(iperm$getnext(layout = "column"), t(t(c(1, 1, 2)))) iperm$getnext(120, layout = "column") expect_equal(ncol(iperm$getnext(10, layout = "column")), 3) expect_equal(iperm$getnext(layout = "column"), NULL) perm <- permutations(5, 3, replace= TRUE, layout = "list") expect_equal(iperm$collect(layout = "list"), perm) expect_equal(iperm$getnext(layout = "list"), list(rep(1, 3))) expect_equal(iperm$getnext(layout = "list"), list(c(1, 1, 2))) iperm$getnext(120, layout = "list") expect_equal(length(iperm$getnext(10, layout = "list")), 3) expect_equal(iperm$getnext(layout = "list"), NULL) iperm <- ipermutations(5, 0, replace= TRUE) expect_equal(dim(iperm$collect()), c(1, 0)) expect_equal(iperm$getnext(), integer(0)) iperm <- ipermutations(2, 3, replace= TRUE) expect_equal(nrow(iperm$collect()), 8) expect_error(ipermutations(5, -1, replace= TRUE), "expect integer") expect_error(ipermutations(5, 1.5, replace= TRUE), "expect integer") }) test_that("Permutations with replacement - index", { comb <- permutations(5, 3, replace = TRUE) expect_equal(permutations(5, 3, replace = TRUE, index = 1:125), comb) expect_equal(permutations(5, 3, replace = TRUE, index = as.numeric(1:125)), comb) expect_equal(permutations(5, 3, replace = TRUE, index = as.character(1:125)), comb) expect_equal(permutations(5, 3, replace = TRUE, index = gmp::as.bigz(1:125)), comb) expect_equal(permutations(5, 3, replace = TRUE, index = 2), c(1, 1, 2)) expect_equal(permutations(5, 3, replace = TRUE, index = 125), rep(5, 3)) expect_equal(permutations(40, 10, replace = TRUE, index = 2), c(rep(1, 9), 2)) expect_equal(permutations(40, 10, replace = TRUE, index = "10485760000000000"), rep(40, 10)) expect_equal(permutations(5, 0, replace = TRUE, index = 1), integer(0)) expect_equal(permutations(0, 0, replace = TRUE, index = 1), integer(0)) expect_error(permutations(0, 1, replace = TRUE, index = 1), "invalid index") expect_equal(permutations(5, 0, replace = TRUE, index = gmp::as.bigz(1)), integer(0)) expect_equal(permutations(0, 0, replace = TRUE, index = gmp::as.bigz(1)), integer(0)) expect_error(permutations(0, 1, replace = TRUE, index = gmp::as.bigz(1)), "invalid index") }) test_that("Permutations with replacement - skip", { expect_equal(permutations(5, 3, replace = TRUE, skip = 125), permutations(5, 3, replace = TRUE)) expect_equal(permutations(5, 3, replace = TRUE, skip = 3), permutations(5, 3, replace = TRUE)[4:125, ]) expect_equal(permutations(5, 3, replace = TRUE, skip = 3, nitem = 4), permutations(5, 3, replace = TRUE)[4:7, ]) expect_equal(permutations(5, 3, replace = TRUE, skip = gmp::as.bigz(3), nitem = 4), permutations(5, 3, replace = TRUE)[4:7, ]) })