/usr/share/doc/r-cran-tibble/tests/testthat/test-matrix.R is in r-cran-tibble 1.4.1-1ubuntu1.
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 105 | context("matrix")
test_that("correct rows and cols", {
x <- matrix(1:6, nrow = 2)
out <- as_tibble(x)
expect_equal(dim(out), c(2, 3))
})
test_that("preserves col names", {
x <- matrix(1:4, nrow = 2)
colnames(x) <- c("a", "b")
out <- as_tibble(x)
expect_equal(names(out), c("a", "b"))
})
test_that("creates col names", {
x <- matrix(1:4, nrow = 2)
out <- as_tibble(x)
expect_equal(names(out), c("V1", "V2"))
})
test_that("preserves attributes except dim and names", {
date <- Sys.Date() + 0:3
dim(date) <- c(2, 2)
colnames(date) <- c("a", "b")
attr(date, "special") <- 42
out <- as_tibble.matrix(date)
expect_null(attributes(out[[1]])$names)
expect_equal(attributes(out[[1]])$class, "Date")
expect_equal(attributes(out[[2]])$special, 42)
})
test_that("properly handles poly class (#110)", {
p <- poly(1:6, 3)
p_df <- as_tibble(p)
expect_equal(names(p_df), colnames(p))
expect_equal(class(p_df[[1L]]), class(p[, 1]))
})
test_that("handles atomic vectors", {
x <- matrix(TRUE, nrow = 2)
out <- as_tibble(x)
expect_equal(out[[1]], c(TRUE, TRUE))
x <- matrix(1L, nrow = 2)
out <- as_tibble(x)
expect_equal(out[[1]], c(1L, 1L))
x <- matrix(1.5, nrow = 2)
out <- as_tibble(x)
expect_equal(out[[1]], c(1.5, 1.5))
x <- matrix("a", nrow = 2)
out <- as_tibble(x)
expect_equal(out[[1]], c("a", "a"))
x <- matrix(complex(real = 1, imag = 2), nrow = 2)
out <- as_tibble(x)
expect_equal(out[[1]], as.vector(x))
})
test_that("auto-assigning names", {
expect_identical(
as_tibble(diag(3L)),
as_tibble(as.data.frame(diag(3L)))
)
})
test_that("forwarding to as.data.frame() for ts objects (#184)", {
mts <- cbind(
A = ts(c(1, 1, 2, 2), start = 2016, freq = 4),
B = ts(c(11, 11, 12, 13), start = 2016, freq = 4)
)
expect_identical(as_tibble(mts), as_tibble(as.data.frame(mts)))
})
test_that("converting from matrix removes row names by default", {
x <- matrix(1:30, 6, 5, dimnames = list(letters[1:6], LETTERS[1:5]))
df <- data.frame(A = 1:6, B = 7:12, C = 13:18, D = 19:24, E = 25:30)
out <- as_tibble(x)
expect_false(has_rownames(out))
expect_identical(out, as_tibble(df))
})
test_that("converting from matrix keeps row names if argument has them, with rownames = NA", {
x <- matrix(1:30, 6, 5, dimnames = list(letters[1:6], LETTERS[1:5]))
df <- data.frame(A = 1:6, B = 7:12, C = 13:18, D = 19:24, E = 25:30,
row.names = letters[1:6])
out <- as_tibble(x, rownames = NA)
expect_identical(rownames(out), rownames(x))
expect_identical(out, as_tibble(df))
})
test_that("converting from matrix supports storing row names in a column", {
x <- matrix(1:30, 6, 5, dimnames = list(letters[1:6], LETTERS[1:5]))
df <- tibble(id = letters[1:6], A = 1:6, B = 7:12, C = 13:18, D = 19:24, E = 25:30)
out <- as_tibble(x, rownames = "id")
expect_identical(out, df)
})
|