File: test_indexing2.R

package info (click to toggle)
r-cran-igraph 1.0.1-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 18,232 kB
  • sloc: ansic: 173,538; cpp: 19,365; fortran: 4,550; yacc: 1,164; tcl: 931; lex: 484; makefile: 149; sh: 9
file content (105 lines) | stat: -rw-r--r-- 2,995 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

context("Assignments via indexing")

library(igraph)

am <- function(x) {
  x <- as.matrix(x)
  dimnames(x) <- NULL
  x
}

test_that("[ can add and delete edges", {

  g <- make_empty_graph(10) ;  A <- matrix(0, 10, 10)

  A[1,2] <- g[1,2] <- TRUE
  expect_that(am(g[]), equals(A))
  
  A[2,1] <- g[2,1] <- TRUE
  expect_that(am(g[]), equals(A))
  
  g[2,1] <- NULL ; A[2,1] <- 0
  expect_that(am(g[]), equals(A))
  
  A[1,2] <- g[1,2] <- FALSE
  expect_that(am(g[]), equals(A))

  g <- make_empty_graph(10) ; A <- matrix(0, 10, 10)
  A[-1,1] <- g[-1,1] <- 1
  expect_that(am(g[]), equals(A))
})

test_that("[ can set weights and delete weighted edges", {

  g <- make_empty_graph(10) ; A <- matrix(0, 10, 10)
  g <- set_edge_attr(g, "weight", c(), 1)
  A[1,2] <- g[1,2] <- 1
  expect_that(am(g[]), equals(A))
  
  A[2,1] <- g[2,1] <- 2
  expect_that(am(g[]), equals(A))
  
  A[1,2] <- g[1,2] <- 3
  expect_that(am(g[]), equals(A))
  
  A[1:2,2:3] <- g[1:2,2:3] <- -1
  expect_that(am(g[]), equals(A))

  g[1,2] <- NULL ; A[1,2] <- 0
  expect_that(am(g[]), equals(A))
})

test_that("[ can add edges and ste weights via vertex names", {

  g <- make_empty_graph(10) ; A <- matrix(0, 10, 10)
  V(g)$name <- letters[1:vcount(g)]
  rownames(A) <- colnames(A) <- letters[1:vcount(g)]

  A['a', 'b'] <- g['a','b'] <- TRUE
  A['b', 'c'] <- g['b','c'] <- TRUE
  expect_that(am(g[]), equals(am(A)))
  
  A[c('a','f'), c('f','a')] <- g[c('a','f'),c('f','a')] <- TRUE
  expect_that(am(g[]), equals(am(A)))

  A[A==1] <- NA
  A[c('a','c','h'), c('a', 'b', 'c')] <-
    g[c('a','c','h'), c('a','b','c'), attr="weight"] <- 3
  expect_that(am(g[]), equals(am(A)))
})

test_that("[ and the from-to notation", {

  g <- make_empty_graph(10) ; A <- matrix(0, 10, 10)
  V(g)$name <- letters[1:vcount(g)]
  rownames(A) <- colnames(A) <- letters[1:vcount(g)]

  g[from=c('a','c','h'), to=c('a','b','c')] <- 1
  A['a','a'] <- A['c','b'] <- A['h','c'] <- 1
  expect_that(g[from=c('a','c','h','d'), to=c('a','b','c','e')],
              equals(c(1,1,1,0)))
  expect_that(am(g[]), equals(am(A)))

  g[from=c('a','c','h','a'), to=c('a','a','a','e'), attr="weight"] <- 3
  A[A!=0] <- NA ; A['a','a'] <- A['c','a'] <- A['h','a'] <- A['a','e'] <- 3
  expect_that(g[from=c('a','c','h','a','c','c'),
                to=c('a','a','a','e','f','b')], equals(c(3,3,3,3,0,NA)))
  expect_that(am(g[]), equals(am(A)))
})

test_that("[ and from-to with multiple values", {

  g <- make_empty_graph(10) ; A <- matrix(0, 10, 10)
  V(g)$name <- letters[1:vcount(g)]
  rownames(A) <- colnames(A) <- letters[1:vcount(g)]

  g[from=c('a','c','h'), to=c('a','b','c')] <- 1
  A['a','a'] <- A['c','b'] <- A['h','c'] <- 1
  g[from=c('a','c','h','a'), to=c('a','a','a','e'), attr="weight"] <- 5:8
  A[A!=0] <- NA ; A['a','a'] <- 5 ; A['c','a'] <- 6 ; A['h','a'] <- 7
  A['a','e'] <- 8
  expect_that(g[from=c('a','c','h','a','c','c'),
                to=c('a','a','a','e','f','b')], equals(c(5:8,0,NA)))
  expect_that(am(g[]), equals(am(A)))
})