tests/testthat/test_adj_to_dyadlist.R

context("test adj_to_dyadlist function")

test_that("function works for integer inputs", {
    
    x = matrix(sample.int(100, 25), nrow = 5)
    x_sym = symmetrize(x)
    y = adj_to_dyadlist(x)
    
    for (i in nrow(y)) {
        w = y[i, 1:2]
        expect_equal(x[w[1], w[2]], y[i, 3], check.attributes = F)
        expect_equal(x[w[2], w[1]], y[i, 4], check.attributes = F)
    }

    expect_equal(adj_to_dyadlist(x_sym), y[, 1:3], 
                 check.attributes = F)
    
})

test_that("function works for numeric input", {
    
    x = matrix(rnorm(25), nrow = 5)
    x_sym = symmetrize(x)
    y = adj_to_dyadlist(x)
    
    for (i in nrow(y)) {
        w = y[i, 1:2]
        expect_equal(x[w[1], w[2]], y[i, 3], check.attributes = F)
        expect_equal(x[w[2], w[1]], y[i, 4], check.attributes = F)
    }
    
    expect_equal(adj_to_dyadlist(x_sym), y[, 1:3], 
                 check.attributes = F)

})

test_that("function works for character input", {
    
    x = matrix(letters[1:25], nrow = 5)
    x_sym = symmetrize(x)
    y = adj_to_dyadlist(x)
    
    for (i in nrow(y)) {
        w = as.integer(y[i, 1:2])
        expect_equal(x[w[1], w[2]], y[i, 3], check.attributes = F)
        expect_equal(x[w[2], w[1]], y[i, 4], check.attributes = F)
    }
    
    expect_equal(adj_to_dyadlist(x_sym), y[, 1:3], 
                 check.attributes = F)
    
})
    
test_that("function works for logical input", {
    
    x = matrix(sample(c(T,F), 25, replace = T), nrow = 5)
    x_sym = symmetrize(x)
    y = adj_to_dyadlist(x)
    
    for (i in nrow(y)) {
        w = as.integer(y[i, 1:2])
        expect_equal(x[w[1], w[2]], as.logical(y[i, 3]), 
                     check.attributes = F)
        expect_equal(x[w[2], w[1]], as.logical(y[i, 4]), 
                     check.attributes = F)
    }
    
    expect_equal(adj_to_dyadlist(x_sym), y[, 1:3], 
                 check.attributes = F)

})

test_that("function throws appropriate errors", {
    
    expect_error(adj_to_dyadlist(sample.int(100, 25)))
    expect_error(adj_to_dyadlist(matrix(sample.int(100, 20), nrow = 5)))

})
baruuum/btoolbox documentation built on Aug. 17, 2020, 1:29 a.m.