@@ -31,13 +31,13 @@ test_that("bracketing works with a function (not changing attribute of similar g
31
31
g <- set_edge_attr(g , name = " weight" , value = 1 : ecount(g ))
32
32
g <- set_graph_attr(g , name = " name" , " foo" )
33
33
34
- run.test <- function (g ) {
34
+ copy_test <- function (g ) {
35
35
graph2 <- set_vertex_attr(g , name = " weight" , value = rep(1 , vcount(g )))
36
36
graph2 <- set_edge_attr(g , name = " weight" , value = rep(1 , ecount(g )))
37
37
graph2 <- set_graph_attr(g , name = " name" , " foobar" )
38
38
}
39
39
40
- g2 <- run.test (g )
40
+ g2 <- copy_test (g )
41
41
expect_equal(vertex_attr(g , name = " weight" ), 1 : 4 )
42
42
expect_equal(edge_attr(g , name = " weight" ), 1 : 3 )
43
43
expect_equal(graph_attr(g , name = " name" ), " foo" )
@@ -50,13 +50,13 @@ test_that("bracketing works with shortcuts (not changing attribute of similar gr
50
50
g <- set_edge_attr(g , name = " weight" , value = 1 : ecount(g ))
51
51
g <- set_graph_attr(g , name = " name" , " foo" )
52
52
53
- run.test <- function (graph ) {
53
+ copy_test <- function (graph ) {
54
54
V(graph )$ weight <- rep(1 , vcount(graph ))
55
55
E(graph )$ weight <- rep(1 , ecount(graph ))
56
56
graph $ name <- " foobar"
57
57
}
58
58
59
- g2 <- run.test (g )
59
+ g_copy <- copy_test (g )
60
60
expect_equal(vertex_attr(g , name = " weight" ), 1 : 4 )
61
61
expect_equal(edge_attr(g , name = " weight" ), 1 : 3 )
62
62
expect_equal(graph_attr(g , name = " name" ), " foo" )
@@ -188,25 +188,31 @@ test_that("attribute combinations handle errors correctly", {
188
188
expect_error(as_undirected(g , edge.attr.comb = list (weight = sum )), " invalid 'type'" )
189
189
})
190
190
191
- test_that(" can change type of attributes (#466)" , {
191
+ test_that(" can change type of attributes" , {
192
+ # https://github.com/igraph/rigraph/issues/466
192
193
g <- make_ring(10 )
193
194
194
195
V(g )$ foo <- 1
195
196
expect_equal(V(g )$ foo , rep(1 , 10 ))
197
+
196
198
V(g )$ foo <- " a"
197
199
expect_equal(V(g )$ foo , rep(" a" , 10 ))
200
+
198
201
V(g )$ foo <- 2
199
202
expect_equal(V(g )$ foo , rep(2 , 10 ))
200
203
201
204
E(g )$ foo <- 1
202
205
expect_equal(E(g )$ foo , rep(1 , 10 ))
206
+
203
207
E(g )$ foo <- " a"
204
208
expect_equal(E(g )$ foo , rep(" a" , 10 ))
209
+
205
210
E(g )$ foo <- 2
206
211
expect_equal(E(g )$ foo , rep(2 , 10 ))
207
212
})
208
213
209
- test_that(" setting attributes strips names (#466)" , {
214
+ test_that(" setting attributes strips names" , {
215
+ # https://github.com/igraph/rigraph/issues/466
210
216
g <- make_ring(10 )
211
217
212
218
V(g )$ foo <- stats :: setNames(1 : 10 , letters [1 : 10 ])
@@ -222,7 +228,8 @@ test_that("setting attributes strips names (#466)", {
222
228
expect_identical(E(g )$ bar , rep(1 , 10 ))
223
229
})
224
230
225
- test_that(" setting NULL attributes works and doesn't change the input (#466)" , {
231
+ test_that(" setting NULL attributes works and doesn't change the input" , {
232
+ # https://github.com/igraph/rigraph/issues/466
226
233
g <- make_ring(10 )
227
234
228
235
expect_identical(set_vertex_attr(g , " foo" , value = NULL ), g )
@@ -231,96 +238,6 @@ test_that("setting NULL attributes works and doesn't change the input (#466)", {
231
238
expect_identical(set_edge_attr(g , " foo" , 1 : 3 , value = NULL ), g )
232
239
})
233
240
234
- test_that(" GRAPH attributes are destroyed when the graph is destroyed" , {
235
- finalized <- FALSE
236
- finalizer <- function (e ) {
237
- finalized <<- TRUE
238
- }
239
-
240
- env <- new.env(parent = emptyenv())
241
- reg.finalizer(env , finalizer )
242
-
243
- g <- make_ring(1 )
244
- g $ a <- list (env )
245
- rm(env )
246
- gc()
247
- expect_false(finalized )
248
-
249
- rm(g )
250
- gc()
251
- expect_true(finalized )
252
- })
253
-
254
- test_that(" vertex attributes are destroyed when the graph is destroyed" , {
255
- finalized <- FALSE
256
- finalizer <- function (e ) {
257
- finalized <<- TRUE
258
- }
259
-
260
- env <- new.env(parent = emptyenv())
261
- reg.finalizer(env , finalizer )
262
-
263
- g <- make_ring(1 )
264
- V(g )$ a <- list (env )
265
- rm(env )
266
- gc()
267
- expect_false(finalized )
268
-
269
- g <- add_vertices(g , 1 )
270
- gc()
271
- expect_false(finalized )
272
-
273
- g <- delete_vertices(g , 2 )
274
- gc()
275
- expect_false(finalized )
276
-
277
- # Called for the side effect of clearing the protect list
278
- make_empty_graph()
279
- expect_false(finalized )
280
-
281
- rm(g )
282
-
283
- gc()
284
- expect_true(finalized )
285
- })
286
-
287
- test_that(" edge attributes are destroyed when the graph is destroyed" , {
288
- finalized <- FALSE
289
- finalizer <- function (e ) {
290
- finalized <<- TRUE
291
- }
292
-
293
- env <- new.env(parent = emptyenv())
294
- reg.finalizer(env , finalizer )
295
-
296
- g <- make_ring(2 )
297
- E(g )$ a <- list (env )
298
- rm(env )
299
- gc()
300
- expect_false(finalized )
301
-
302
- g <- add_vertices(g , 1 )
303
- gc()
304
- expect_false(finalized )
305
-
306
- g <- add_edges(g , c(2 , 3 ))
307
- gc()
308
- expect_false(finalized )
309
-
310
- g <- delete_edges(g , 2 )
311
- gc()
312
- expect_false(finalized )
313
-
314
- # Called for the side effect of clearing the protect list
315
- make_empty_graph()
316
- expect_false(finalized )
317
-
318
- rm(g )
319
-
320
- gc()
321
- expect_true(finalized )
322
- })
323
-
324
241
test_that(" assert_named_list() works" , {
325
242
not_list <- 1 : 10
326
243
expect_error(assert_named_list(not_list ), " named list" )
@@ -338,81 +255,81 @@ test_that("assert_named_list() works", {
338
255
})
339
256
340
257
test_that(" is_bipartite works" , {
341
- I <- matrix (sample(0 : 1 , 35 , replace = TRUE , prob = c(3 , 1 )), ncol = 5 )
342
- g <- graph_from_biadjacency_matrix(I )
343
- expect_true(bipartite_mapping(g )$ res )
258
+ biadj_mat1 <- matrix (sample(0 : 1 , 35 , replace = TRUE , prob = c(3 , 1 )), ncol = 5 )
259
+ g1 <- graph_from_biadjacency_matrix(biadj_mat1 )
260
+ expect_true(bipartite_mapping(g1 )$ res )
344
261
345
262
withr :: local_seed(42 )
346
- I <- matrix (sample(0 : 1 , 35 , replace = TRUE , prob = c(3 , 1 )), ncol = 5 )
347
- g <- graph_from_biadjacency_matrix(I )
263
+ biadj_mat2 <- matrix (sample(0 : 1 , 35 , replace = TRUE , prob = c(3 , 1 )), ncol = 5 )
264
+ g2 <- graph_from_biadjacency_matrix(biadj_mat2 )
348
265
expect_equal(
349
- bipartite_mapping(g ),
266
+ bipartite_mapping(g2 ),
350
267
list (res = TRUE , type = c(rep(FALSE , 7 ), rep(TRUE , 5 )))
351
268
)
352
269
})
353
270
354
271
test_that(" without_attr" , {
355
272
withr :: local_seed(42 )
356
- g <- sample_gnp(10 , 2 / 10 ) %> %
273
+ g_stripped <- sample_gnp(10 , 2 / 10 ) %> %
357
274
delete_graph_attr(" name" ) %> %
358
275
delete_graph_attr(" type" ) %> %
359
276
delete_graph_attr(" loops" ) %> %
360
277
delete_graph_attr(" p" )
361
278
362
279
withr :: local_seed(42 )
363
- g2 <- sample_(gnp(10 , 2 / 10 ), without_attr())
280
+ g_no_attr <- sample_(gnp(10 , 2 / 10 ), without_attr())
364
281
365
- expect_identical_graphs(g , g2 )
366
- expect_equal(graph_attr_names(g2 ), character ())
367
- expect_equal(vertex_attr_names(g2 ), character ())
368
- expect_equal(edge_attr_names(g2 ), character ())
282
+ expect_identical_graphs(g_stripped , g_no_attr )
283
+ expect_equal(graph_attr_names(g_no_attr ), character ())
284
+ expect_equal(vertex_attr_names(g_no_attr ), character ())
285
+ expect_equal(edge_attr_names(g_no_attr ), character ())
369
286
})
370
287
371
288
372
289
test_that(" without_loops" , {
373
- g <- make_graph(~ A - A : B : C , B - A : B : C , simplify = FALSE ) %> %
290
+ g1 <- make_graph(~ A - A : B : C , B - A : B : C , simplify = FALSE ) %> %
374
291
simplify(remove.multiple = FALSE )
375
292
376
293
g2 <- make_(
377
294
from_literal(A - A : B : C , B - A : B : C , simplify = FALSE ),
378
295
without_loops()
379
296
)
380
297
381
- expect_identical_graphs(g , g2 )
298
+ expect_identical_graphs(g1 , g2 )
382
299
expect_true(all(! which_loop(g2 )))
383
300
})
384
301
385
302
386
303
test_that(" without_multiple" , {
387
- g <- make_graph(~ A - A : B : C , B - A : B : C , simplify = FALSE ) %> %
304
+ g1 <- make_graph(~ A - A : B : C , B - A : B : C , simplify = FALSE ) %> %
388
305
simplify(remove.loops = FALSE )
389
306
390
307
g2 <- make_(
391
308
from_literal(A - A : B : C , B - A : B : C , simplify = FALSE ),
392
309
without_multiples()
393
310
)
394
311
395
- expect_identical_graphs(g , g2 )
312
+ expect_identical_graphs(g1 , g2 )
396
313
expect_true(all(! which_multiple(g2 )))
397
314
})
398
315
399
316
400
317
test_that(" simplified" , {
401
- g <- make_graph(~ A - A : B : C , B - A : B : C )
318
+ g1 <- make_graph(~ A - A : B : C , B - A : B : C )
402
319
403
320
g2 <- make_(
404
321
from_literal(A - A : B : C , B - A : B : C , simplify = FALSE ),
405
322
simplified()
406
323
)
407
324
408
- expect_identical_graphs(g , g2 )
325
+ expect_identical_graphs(g1 , g2 )
409
326
expect_true(all(! which_multiple(g2 )))
410
327
expect_true(all(! which_loop(g2 )))
411
328
})
412
329
413
330
414
331
test_that(" with_vertex_" , {
415
- g <- make_graph(~ A - A : B : C , B - A : B : C ) %> %
332
+ g1 <- make_graph(~ A - A : B : C , B - A : B : C ) %> %
416
333
set_vertex_attr(" color" , value = " red" ) %> %
417
334
set_vertex_attr(" foo" , value = paste0(" xx" , 1 : 3 ))
418
335
@@ -424,14 +341,14 @@ test_that("with_vertex_", {
424
341
)
425
342
)
426
343
427
- expect_identical_graphs(g , g2 )
344
+ expect_identical_graphs(g1 , g2 )
428
345
expect_equal(V(g2 )$ color , rep(" red" , gorder(g2 )))
429
346
expect_equal(V(g2 )$ foo , paste0(" xx" , 1 : 3 ))
430
347
})
431
348
432
349
433
350
test_that(" with_edge_" , {
434
- g <- make_graph(~ A - A : B : C , B - A : B : C ) %> %
351
+ g1 <- make_graph(~ A - A : B : C , B - A : B : C ) %> %
435
352
set_edge_attr(" color" , value = " red" ) %> %
436
353
set_edge_attr(" foo" , value = seq_len(3 ))
437
354
@@ -443,14 +360,14 @@ test_that("with_edge_", {
443
360
)
444
361
)
445
362
446
- expect_identical_graphs(g , g2 )
447
- expect_equal(E(g )$ color , E(g2 )$ color )
448
- expect_equal(E(g )$ foo , E(g2 )$ foo )
363
+ expect_identical_graphs(g1 , g2 )
364
+ expect_equal(E(g1 )$ color , E(g2 )$ color )
365
+ expect_equal(E(g1 )$ foo , E(g2 )$ foo )
449
366
})
450
367
451
368
452
369
test_that(" with_graph_" , {
453
- g <- make_graph(~ A - A : B : C , B - A : B : C ) %> %
370
+ g1 <- make_graph(~ A - A : B : C , B - A : B : C ) %> %
454
371
set_graph_attr(" color" , value = " red" ) %> %
455
372
set_graph_attr(" foo" , value = 1 : 5 )
456
373
@@ -462,24 +379,24 @@ test_that("with_graph_", {
462
379
)
463
380
)
464
381
465
- expect_identical_graphs(g , g2 )
466
- expect_equal(g $ color , g2 $ color )
467
- expect_equal(g $ foo , g2 $ foo )
382
+ expect_identical_graphs(g1 , g2 )
383
+ expect_equal(g1 $ color , g2 $ color )
384
+ expect_equal(g1 $ foo , g2 $ foo )
468
385
})
469
386
470
387
471
388
test_that(" adding and removing attributes" , {
472
- g <- make_empty_graph()
389
+ g1 <- make_empty_graph()
473
390
g2 <- make_empty_graph()
474
391
475
- g $ foo <- " bar"
476
- g <- delete_graph_attr(g , " foo" )
477
- E(g )$ foo <- " bar"
478
- g <- delete_edge_attr(g , " foo" )
479
- V(g )$ foo <- " bar"
480
- g <- delete_vertex_attr(g , " foo" )
392
+ g1 $ foo <- " bar"
393
+ g1 <- delete_graph_attr(g1 , " foo" )
394
+ E(g1 )$ foo <- " bar"
395
+ g1 <- delete_edge_attr(g1 , " foo" )
396
+ V(g1 )$ foo <- " bar"
397
+ g1 <- delete_vertex_attr(g1 , " foo" )
481
398
482
- expect_identical_graphs(g , g2 )
399
+ expect_identical_graphs(g1 , g2 )
483
400
})
484
401
485
402
test_that(" error messages work" , {
@@ -499,3 +416,10 @@ test_that("empty returns work", {
499
416
expect_length(vertex_attr_names(g ), 0 )
500
417
expect_length(edge_attr_names(g ), 0 )
501
418
})
419
+
420
+ test_that(" assign data.frame attributes works" , {
421
+ # https://github.com/igraph/rigraph/issues/1669
422
+ g <- make_tree(10 , 3 )
423
+ edge.attributes(g ) <- head(mtcars , ecount(g ))
424
+ expect_no_error(E(g )[c(1 , 2 )])
425
+ })
0 commit comments