From 6b600df04bec1fe70c272604f274ec5309840e65 Mon Sep 17 00:00:00 2001 From: Leo Sendelbach Date: Tue, 9 Jan 2024 16:26:07 +0100 Subject: [PATCH] Add tests for convert.adjacency.matrix.list.to.array Signed-off by: Leo Sendelbach --- tests/test-util-networks-misc.R | 91 +++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/tests/test-util-networks-misc.R b/tests/test-util-networks-misc.R index c2df6ed0..93c34327 100644 --- a/tests/test-util-networks-misc.R +++ b/tests/test-util-networks-misc.R @@ -736,4 +736,95 @@ test_that("getting cumulative sums of adjacency matrices generated from networks ## Assert compare.sparse.matrices(matrix.out.one, result[[1]]) compare.sparse.matrices(matrix.out.two, result[[2]]) +}) + +test_that("getting cumulative sums of adjacency matrices generated from networks, + two networks, then convert to array", { + + ## Arrange + vertices = data.frame( + name = c("Heinz", "Dieter", "Klaus"), + kind = TYPE.AUTHOR, + type = TYPE.AUTHOR + ) + edges = data.frame( + from = c("Heinz", "Dieter", "Dieter"), + to = c("Dieter", "Klaus", "Heinz") + ) + network.in.one = igraph::graph.data.frame(edges, directed = FALSE, vertices = vertices) + authors.in.one = sort(c("Heinz", "Dieter", "Klaus")) + + edges = data.frame( + from = c("Klaus"), + to = c("Dieter") + ) + network.in.two = igraph::graph.data.frame(edges, directed = FALSE, vertices = vertices) + + expected.array = array(data = 0, dim = c(3, 3, 2)) + rownames(expected.array) = authors.in.one + colnames(expected.array) = authors.in.one + + expected.array[1, 2, 1] = 1 + expected.array[1, 3, 1] = 1 + expected.array[2, 1, 1] = 1 + expected.array[3, 1, 1] = 1 + + expected.array[1, 2, 2] = 1 + expected.array[1, 3, 2] = 1 + expected.array[2, 1, 2] = 1 + expected.array[3, 1, 2] = 1 + + ## Act + result.adjacency = get.expanded.adjacency.cumulated(networks = list(network.in.one, network.in.two)) + result.array = convert.adjacency.matrix.list.to.array(result.adjacency) + + ## Assert + expect_equal(expected.array, result.array) +}) + +test_that("getting cumulative sums of adjacency matrices generated from networks, + two networks, weighted, then convert to array", { + + ## Arrange + vertices = data.frame( + name = c("Heinz", "Dieter", "Klaus"), + kind = TYPE.AUTHOR, + type = TYPE.AUTHOR + ) + edges = data.frame( + from = c("Heinz", "Dieter", "Dieter"), + to = c("Dieter", "Klaus", "Heinz"), + weight = c(1, 2, 1) + ) + network.in.one = igraph::graph.data.frame(edges, directed = FALSE, vertices = vertices) + authors.in.one = sort(c("Heinz", "Dieter", "Klaus")) + + edges = data.frame( + from = c("Klaus"), + to = c("Dieter"), + weight = c(1) + ) + network.in.two = igraph::graph.data.frame(edges, directed = FALSE, vertices = vertices) + authors.in.two = sort(c("Heinz", "Dieter", "Klaus")) + + expected.array = array(data = 0, dim = c(3, 3, 2)) + rownames(expected.array) = authors.in.one + colnames(expected.array) = authors.in.one + + expected.array[1, 2, 1] = 2 + expected.array[1, 3, 1] = 2 + expected.array[2, 1, 1] = 2 + expected.array[3, 1, 1] = 2 + + expected.array[1, 2, 2] = 2 + expected.array[1, 3, 2] = 3 + expected.array[2, 1, 2] = 2 + expected.array[3, 1, 2] = 3 + + ## Act + result.adjacency = get.expanded.adjacency.cumulated(networks = list(network.in.one, network.in.two), weighted = TRUE) + result.array = convert.adjacency.matrix.list.to.array(result.adjacency) + + ## Assert + expect_equal(expected.array, result.array) }) \ No newline at end of file