tests/testthat/test_reciprocated.R

context("test reciprocated")

test_that("test imput", {
    
    for (ii in 1:20) {
    
        n = 45
        x_ = sample(1:10, n * 2, TRUE)
        x = matrix(x_, nrow = n)
        y = unique(x)
        
        res = reciprocated(y)
        
        # duplicates
        if (nrow(x) != nrow(y))
            expect_error(reciprocated(x))
        
        # no of columns
        expect_error(
            reciprocated(matrix(x_, ncol = 3))
        )
        expect_error(
            reciprocated(matrix(x_, ncol = 5))
        )
        
        # input type: character matrix
        z = matrix(letters[y], nrow = nrow(y))
        expect_error(reciprocated(z))
        
        # input type: data.frame
        expect_identical(
            res,
            reciprocated(as.data.frame(y))
        )
    
    }    
        
})

test_that("test value", {
    
    for (ii in 1:20) {
    
        n = 45
        x_ = sample(1:10, n * 2, TRUE)
        x = unique(matrix(x_, nrow = n))
        res = reciprocated(x)
        
        # check that self-loops are not reciprocated
        expect_true(all(!res[x[, 1] == x[, 2]]))
        
        # check results
        
        x_char = apply(x, 1L, paste, collapse = ",")
        y1 = apply(
            x[res == 1L, c(2, 1)], 
            1L, 
            paste, 
            collapse = ","
        )
        y2 = apply(
            x[res == 0L & x[, 1] != x[, 2], c(2, 1)], 
            1L, 
            paste, 
            collapse = ","
        )
        
        expect_true(all(y1 %in% x_char))
        expect_true(all(!(y2 %in% x_char)))

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