From b8af977117ec17011ba086eeca7920a9e4dc5c05 Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Mon, 16 Dec 2019 16:37:25 +0100 Subject: [PATCH] Added unit tests for lean_simple_partition --- DESCRIPTION | 2 ++ R/admix1-learn_simple_partition.R | 3 ++- tests/testthat.R | 4 ++++ tests/testthat/test-admix1.R | 20 ++++++++++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/testthat.R create mode 100644 tests/testthat/test-admix1.R diff --git a/DESCRIPTION b/DESCRIPTION index 95d291a..1cf976f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -12,3 +12,5 @@ License: GPL-3 Encoding: UTF-8 LazyData: true RoxygenNote: 7.0.2 +Suggests: + testthat (>= 2.1.0) diff --git a/R/admix1-learn_simple_partition.R b/R/admix1-learn_simple_partition.R index 5b82520..d2999e6 100644 --- a/R/admix1-learn_simple_partition.R +++ b/R/admix1-learn_simple_partition.R @@ -55,6 +55,7 @@ learn_simple_partition <- function(ordered_points, fii) { part[1:best_i] <- 1 part[(best_i + 1):best_j] <- 2 part[(best_j + 1):length(part)] <- 3 - }) + } + ) return(part) } \ No newline at end of file diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..34b4ef0 --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(rBAPS) + +test_check("rBAPS") diff --git a/tests/testthat/test-admix1.R b/tests/testthat/test-admix1.R new file mode 100644 index 0000000..df3d1e3 --- /dev/null +++ b/tests/testthat/test-admix1.R @@ -0,0 +1,20 @@ +context("Admixture analysis") + + +test_that("learn_simple_partition behaves like Matlab", { + p1 <- c(0, .5, 1, 1.5) + p2 <- c(seq(0, .5, .1), 1, 1, 1, 2) + p3 <- c(.1, .1, .1, .5, .5, .5, 1, 1, 1) + expect_equal( + object = learn_simple_partition(p1, 2), + expected = matrix(c(1, 1, 2, 2)) + ) + expect_equal( + object = learn_simple_partition(p2, 2), + expected = matrix(c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2)) + ) + expect_equal( + object = learn_simple_partition(p3, .5), + expected = matrix(c(1, 1, 1, 2, 2, 2, 3, 3, 3)) + ) +})