This file is indexed.

/usr/lib/R/site-library/igraph/tests/test_indexing2.R is in r-cran-igraph 0.7.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
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 <- graph.empty(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 <- graph.empty(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 <- graph.empty(10) ; A <- matrix(0, 10, 10)
  g <- set.edge.attribute(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 <- graph.empty(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 <- graph.empty(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 <- graph.empty(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)))
})