|
| 1 | +require(methods) |
| 2 | + |
| 3 | +if (exists("test.data.table", .GlobalEnv, inherits=FALSE)) { |
| 4 | + if ((tt<-compiler::enableJIT(-1))>0) |
| 5 | + cat("This is dev mode and JIT is enabled (level ", tt, ") so there will be a brief pause around the first test.\n", sep="") |
| 6 | +} else { |
| 7 | + require(data.table) |
| 8 | + test = data.table:::test |
| 9 | +} |
| 10 | + |
| 11 | +# cbindlist, setcbindlist |
| 12 | + |
| 13 | +local({ |
| 14 | + l = list( |
| 15 | + d1 = data.table(x=1:3, v1=1L), |
| 16 | + d2 = data.table(y=3:1, v2=2L), |
| 17 | + d3 = data.table(z=2:4, v3=3L) |
| 18 | + ) |
| 19 | + ans = cbindlist(l) |
| 20 | + expected = data.table(l$d1, l$d2, l$d3) |
| 21 | + test(11.01, ans, expected) |
| 22 | + test(11.02, intersect(vapply(ans, address, ""), unlist(lapply(l, vapply, address, ""))), character()) |
| 23 | + ans = setcbindlist(l) |
| 24 | + expected = setDT(c(l$d1, l$d2, l$d3)) |
| 25 | + test(11.03, ans, expected) |
| 26 | + test(11.04, length(intersect(vapply(ans, address, ""), unlist(lapply(l, vapply, address, "")))), ncol(expected)) |
| 27 | +}) |
| 28 | + |
| 29 | +test(11.05, cbindlist(list(data.table(a=1L), data.table(), data.table(d=2L), data.table(f=3L))), data.table(a=1L, d=2L, f=3L)) |
| 30 | +## codecov |
| 31 | +test(12.01, cbindlist(data.frame(a=1L)), error="must be a list") |
| 32 | +test(12.02, cbindlist(TRUE), error="must be a list") |
| 33 | +test(12.03, cbindlist(list(data.table(a=1L), 1L)), error="is not a data.table") |
| 34 | +test(12.04, options = c(datatable.verbose=TRUE), cbindlist(list(data.table(a=1:2), data.table(b=1:2))), data.table(a=1:2, b=1:2), output="cbindlist.*took") |
| 35 | +test(12.05, cbindlist(list(data.table(), data.table(a=1:2), data.table(b=1:2))), data.table(a=1:2, b=1:2)) |
| 36 | +test(12.06, cbindlist(list(data.table(), data.table(a=1:2), list(b=1:2))), data.table(a=1:2, b=1:2)) |
| 37 | +test(12.07, cbindlist(list(data.table(a=integer()), list(b=integer()))), data.table(a=integer(), b=integer())) |
| 38 | +## duplicated names |
| 39 | +test(12.08, cbindlist(list(data.table(a=1L, b=2L), data.table(b=3L, d=4L))), data.table(a=1L, b=2L, b=3L, d=4L)) |
| 40 | +local({ |
| 41 | + # also test that keys, indices are wiped |
| 42 | + ans = cbindlist(list(setindexv(data.table(a=2:1, b=1:2), "a"), data.table(a=1:2, b=2:1, key="a"), data.table(a=2:1, b=1:2))) |
| 43 | + test(12.09, ans, data.table(a=2:1, b=1:2, a=1:2, b=2:1, a=2:1, b=1:2)) |
| 44 | + test(12.10, indices(ans), NULL) |
| 45 | +}) |
| 46 | +## recycling, first ensure cbind recycling that we want to match to |
| 47 | +test(12.11, cbind(data.table(x=integer()), data.table(a=1:2)), data.table(x=c(NA_integer_, NA), a=1:2)) |
| 48 | +test(12.12, cbind(data.table(x=1L), data.table(a=1:2)), data.table(x=c(1L, 1L), a=1:2)) |
| 49 | +test(12.13, cbindlist(list(data.table(a=integer()), data.table(b=1:2))), error="Recycling.*not yet implemented") |
| 50 | +test(12.14, cbindlist(list(data.table(a=1L), data.table(b=1:2))), error="Recycling.*not yet implemented") |
| 51 | +test(12.15, setcbindlist(list(data.table(a=integer()), data.table(b=1:2))), error="have to have the same number of rows") |
| 52 | +test(12.16, setcbindlist(list(data.table(a=1L), data.table(b=1:2))), error="have to have the same number of rows") |
| 53 | + |
| 54 | +## retain indices |
| 55 | +local({ |
| 56 | + l = list( |
| 57 | + data.table(id1=1:5, id2=5:1, id3=1:5, v1=1:5), |
| 58 | + data.table(id4=5:1, id5=1:5, v2=1:5), |
| 59 | + data.table(id6=5:1, id7=1:5, v3=1:5), |
| 60 | + data.table(id8=5:1, id9=5:1, v4=1:5) |
| 61 | + ) |
| 62 | + setkeyv(l[[1L]], "id1"); setindexv(l[[1L]], list("id1", "id2", "id3", c("id1","id2","id3"))); setindexv(l[[3L]], list("id6", "id7")); setindexv(l[[4L]], "id9") |
| 63 | + ii = lapply(l, indices) |
| 64 | + ans = cbindlist(l) |
| 65 | + test(13.1, key(ans), "id1") |
| 66 | + test(13.2, indices(ans), c("id1", "id2", "id3", "id1__id2__id3", "id6", "id7", "id9")) |
| 67 | + test(13.3, ii, lapply(l, indices)) ## this tests that original indices have not been touched, shallow_duplicate in mergeIndexAttrib |
| 68 | +}) |
| 69 | +test(13.4, cbindlist(list(data.table(a=1:2), data.table(b=3:4, key="b"))), data.table(a=1:2, b=3:4, key="b")) |
| 70 | +# TODO(#7116): this could be supported |
| 71 | +# test(13.5, cbindlist(list(data.table(a=1:2, key="a"), data.table(b=3:4, key="b"))), data.table(a=1:2, b=3:4, key=c("a", "b"))) |
0 commit comments