Skip to content

Commit

Permalink
adding unit tests for internal functions
Browse files Browse the repository at this point in the history
  • Loading branch information
AnestisTouloumis committed Jul 23, 2019
1 parent a7567b6 commit 7971908
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 13 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: SimCorMultRes
Type: Package
Title: Simulates Correlated Multinomial Responses
Description: Simulates correlated multinomial responses conditional on a marginal model specification.
Version: 1.6.7
Version: 1.6.8
Depends: R(>= 2.15.0)
Imports:
evd,
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# SimCorMultRes: Simulates Correlated Multinomial Responses

[![Github
version](https://img.shields.io/badge/GitHub%20-1.6.7-orange.svg)](%22commits/master%22)
version](https://img.shields.io/badge/GitHub%20-1.6.8-orange.svg)](%22commits/master%22)
[![Travis-CI Build
Status](https://travis-ci.org/AnestisTouloumis/SimCorMultRes.svg?branch=master)](https://travis-ci.org/AnestisTouloumis/SimCorMultRes)
[![Project Status: Active The project has reached a stable, usable state
Expand Down
4 changes: 3 additions & 1 deletion inst/NEWS
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Version 1.6.8 [2019-07-23]
* Added unit tests for internal functions.

Version 1.6.7 [2019-07-22]
* Improved R code readability.
* Added unit tests for internal functions.

Version 1.6.6 [2019-07-09]
* Minor fixed to comply with lintr.
Expand Down
74 changes: 64 additions & 10 deletions tests/testthat/test_internal_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@ check_cluster_size

test_that("checking cluster size", {
expect_silent(check_cluster_size(2))
expect_error(check_cluster_size(1))
expect_error(check_cluster_size(2.5))
expect_error(check_cluster_size(1),
"'clsize' must be a positive integer greater than or equal to two") # nolintr
expect_error(check_cluster_size(2.5),
"'clsize' must be a positive integer greater than or equal to two") # nolintr
})

test_that("checking categories", {
expect_silent(check_ncategories(3))
expect_error(check_ncategories(2))
expect_error(check_ncategories(2.5))
expect_error(check_ncategories(2),
"'ncategories' must be numeric greater or equal to three")
expect_error(check_ncategories(5.5),
"'ncategories' must be a positive integer")
})


test_that("creating marginal distributions", {
expect_equal(create_distribution("probit"), "qnorm")
expect_equal(create_distribution("logit"), "qlogis" )
expect_equal(create_distribution("logit"), "qlogis")
expect_equal(create_distribution("cloglog"), "qgumbel")
expect_equal(create_distribution("cauchit"), "qcauchy")
})



test_that("checking correlation matrix", {
cluster_size <- 5
categories_no <- 3
Expand All @@ -31,13 +34,23 @@ test_that("checking correlation matrix", {
correlation_matrix_3 <- matrix(1, cluster_size * (categories_no - 1),
cluster_size * (categories_no - 1))
expect_error(check_correlation_matrix(correlation_matrix_1, cluster_size,
rfctn = "rbin"))
rfctn = "rbin"),
"'cor_matrix' must be positive definite")
expect_error(check_correlation_matrix(correlation_matrix_1, cluster_size,
rfctn = "rmult.clm"))
rfctn = "rmult.clm"),
"'cor_matrix' must be positive definite")
expect_error(check_correlation_matrix(correlation_matrix_2, cluster_size,
rfctn = "rmult.bcl", categories_no))
rfctn = "rmult.bcl", categories_no),
"'cor_matrix' must be positive definite")
expect_error(check_correlation_matrix(correlation_matrix_1, cluster_size,
rfctn = "rmult.bcl", categories_no),
"'cor_matrix' must be a 15x15 matrix")
expect_error(check_correlation_matrix(correlation_matrix_3, cluster_size,
rfctn = "rmult.crm", categories_no))
rfctn = "rmult.crm", categories_no),
"'cor_matrix' must be positive definite")
expect_error(check_correlation_matrix(correlation_matrix_2, cluster_size,
rfctn = "rmult.crm", categories_no),
"'cor_matrix' must be a 10x10 matrix")
correlation_matrix_4 <- diag(1, cluster_size)
correlation_matrix_5 <- diag(1, cluster_size * categories_no)
correlation_matrix_6 <- diag(1, cluster_size * (categories_no - 1))
Expand All @@ -54,3 +67,44 @@ test_that("checking correlation matrix", {
rfctn = "rmult.crm", categories_no),
correlation_matrix_6)
})

test_that("checking covariates formula", {
xformula <- ~ -1
expect_error(check_xformula(xformula),
"No covariates were found in 'formula'")
xformula <- ~ x1 + x2
expect_equal(check_xformula(xformula), as.formula(~ x1 + x2))
xformula <- y ~ x1 + x2
expect_equal(check_xformula(xformula), as.formula(y ~ x1 + x2))
xformula <- y ~ x1 + x2 - 1
expect_equal(check_xformula(xformula), as.formula(y ~ x1 + x2))
})

test_that("checking intercepts", {
cluster_size <- 5
rfctn <- "rbin"
intercepts <- c(-Inf, 3, Inf)
expect_error(check_intercepts(intercepts, cluster_size, rfctn),
"'intercepts' must not be Inf or -Inf")
intercepts <- c(2, 3)
expect_error(check_intercepts(intercepts, cluster_size, rfctn),
"'intercepts' must have either one or 5 elements")
intercepts <- 2
ans <- rep(intercepts, cluster_size)
ans <- cbind(-Inf, ans, Inf)
colnames(ans) <- c("","intercepts","")
expect_equal(check_intercepts(intercepts, cluster_size, rfctn), ans)
intercepts <- 1:cluster_size
ans <- cbind(-Inf, intercepts, Inf)
colnames(ans) <- c("","intercepts","")
expect_equal(check_intercepts(intercepts, cluster_size, rfctn), ans)
rfctn <- "rmult.clm"
intercepts <- c(-Inf, 3, Inf)
expect_error(check_intercepts(intercepts, cluster_size, rfctn),
"'intercepts' must not be Inf or -Inf")
intercepts <- c(2, 3)
ans <- matrix(intercepts, nrow = cluster_size, ncol = 2, byrow = TRUE)
ans <- cbind(-Inf, ans, Inf)
colnames(ans) <- NULL
expect_equal(check_intercepts(intercepts, cluster_size, rfctn), ans)
})

3 comments on commit 7971908

@lintr-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests/testthat/test_internal_functions.R:95:25: style: Commas should always have a space after.

​  colnames(ans) <- c("","intercepts","")
                        ^

tests/testthat/test_internal_functions.R:95:38: style: Commas should always have a space after.

​  colnames(ans) <- c("","intercepts","")
                                     ^

tests/testthat/test_internal_functions.R:99:25: style: Commas should always have a space after.

​  colnames(ans) <- c("","intercepts","")
                        ^

tests/testthat/test_internal_functions.R:99:38: style: Commas should always have a space after.

​  colnames(ans) <- c("","intercepts","")
                                     ^

@lintr-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests/testthat/test_internal_functions.R:95:25: style: Commas should always have a space after.

​  colnames(ans) <- c("","intercepts","")
                        ^

tests/testthat/test_internal_functions.R:95:38: style: Commas should always have a space after.

​  colnames(ans) <- c("","intercepts","")
                                     ^

tests/testthat/test_internal_functions.R:99:25: style: Commas should always have a space after.

​  colnames(ans) <- c("","intercepts","")
                        ^

tests/testthat/test_internal_functions.R:99:38: style: Commas should always have a space after.

​  colnames(ans) <- c("","intercepts","")
                                     ^

@lintr-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests/testthat/test_internal_functions.R:95:25: style: Commas should always have a space after.

​  colnames(ans) <- c("","intercepts","")
                        ^

tests/testthat/test_internal_functions.R:95:38: style: Commas should always have a space after.

​  colnames(ans) <- c("","intercepts","")
                                     ^

tests/testthat/test_internal_functions.R:99:25: style: Commas should always have a space after.

​  colnames(ans) <- c("","intercepts","")
                        ^

tests/testthat/test_internal_functions.R:99:38: style: Commas should always have a space after.

​  colnames(ans) <- c("","intercepts","")
                                     ^

Please sign in to comment.