Switch to unified view

a b/tests/testthat/test-utils.R
1
context("utils")
2
3
# # from timeOmics
4
# check_matrix <- function(X){
5
#     # add rownames and colnames if absent, cast into matrix
6
#     if(!(is.matrix(X) || is.data.frame(X))) return(FALSE)
7
#     
8
#     if(is.data.frame(X)){
9
#         X <- as.matrix(X)
10
#     }
11
#     if(is.null(rownames(X))){
12
#         rownames(X) <- 1:nrow(X)
13
#     }
14
#     if(is.null(colnames(X))){
15
#         colnames(X) <- paste0("V", 1:ncol(X))
16
#     }
17
#     return(X)
18
# }
19
20
test_that("check_matrix", {
21
    expect_false(check_matrix(c(1,2,3)))
22
    X = data.frame("a" = c(1,2,3), 'b'= c(2,3,4))
23
    expect_is(check_matrix(X), 'matrix')
24
    X = matrix(c(1,2,3,2,3,4), nrow = 2)
25
    expect_is(check_matrix(X), 'matrix')
26
    rownames(X) <-  NULL
27
    expect_is(check_matrix(X), 'matrix')
28
    colnames(X) <-  NULL
29
   expect_is(check_matrix(X), 'matrix')
30
})
31
    
32
# validate_matrix_X <- function(X){
33
#     # X should be a numeric matrix
34
#     X <- check_matrix(X)
35
#     if(!is.numeric(X)){
36
#         stop("'X' must be a numeric matrix/data.frame")
37
#     }
38
#     # if(any(!X)) stop("'X' must be a numeric matrix/data.frame")
39
#     return(X)
40
# }
41
42
test_that("validate_matrix_X", {
43
    X = data.frame("a" = c("A","B", "C"), 'b'= c(NA))
44
    expect_error(validate_matrix_X(X), "'X' must be a numeric matrix/data.frame", fixed = TRUE)
45
})
46
47
# validate_list_matrix_X <- function(X){
48
#     if(!is.list(X)){
49
#         stop("'X' must be a list of matrix/data.frame")
50
#     }
51
#     X <- lapply(X, validate_matrix_X)
52
#     return(X)
53
# }
54
55
test_that("validate_list_matrix_X", {
56
    expect_error(validate_list_matrix_X(X = 2), "'X' must be a list of matrix/data.frame", fixed=TRUE)
57
    X <- list(data.frame("a" = c(1,2,3), 'b'= c(2,3,4)), data.frame("a" = c(1,2,3), 'b'= c(2,3,4)))
58
    expect_is(validate_list_matrix_X(X), "list")
59
})
60
    
61
62
63
64
65
66