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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
test_that("Basic operations", {
s <- faststack()
expect_identical(s$size(), 0L)
s$push(5)
s$push(6)
s$push(NULL)
s$push(list(a=1, b=2))
s$mpush(.list=list(NULL))
s$mpush(.list=list(NULL,7))
s$mpush(8, .list=list(10), 9)
# as_list() returns in the order that they were inserted
expect_identical(
s$as_list(),
list(
5,
6,
NULL,
list(a=1, b=2),
NULL,
NULL,
7,
8,
9,
10
)
)
expect_identical(s$pop(), 10)
expect_identical(s$pop(), 9)
expect_identical(s$pop(), 8)
expect_identical(s$pop(), 7)
expect_identical(s$pop(), NULL)
expect_identical(s$pop(), NULL)
expect_identical(s$pop(), list(a=1, b=2))
expect_identical(s$peek(), NULL)
expect_identical(s$pop(), NULL)
expect_identical(s$size(), 2L)
expect_identical(s$as_list(), list(5, 6))
s$reset()
expect_identical(s$size(), 0L)
expect_identical(s$as_list(), list())
})
test_that("Pushing multiple", {
s <- faststack()
s$mpush(1,2,3)
s$mpush(4,5, .list = list(6, list(7,8)))
s$mpush(9,10)
expect_identical(s$as_list(), list(1,2,3,4,5,6,list(7,8),9,10))
expect_identical(s$pop(), 10)
expect_identical(s$pop(), 9)
expect_identical(s$pop(), list(7,8))
})
test_that("Popping from empty stack", {
s <- faststack()
expect_null(s$pop())
expect_null(s$pop())
expect_null(s$peek())
expect_identical(s$size(), 0L)
s$push(5)
s$push(6)
expect_identical(s$as_list(), list(5, 6))
})
test_that("Different values when popping from an empty stack", {
s <- faststack()
expect_identical(s$pop(missing = "foo"), "foo")
expect_identical(s$peek(missing = "foo"), "foo")
s <- faststack(missing_default = key_missing())
expect_identical(s$pop(), key_missing())
expect_identical(s$pop(), key_missing())
expect_identical(s$peek(), key_missing())
expect_identical(s$size(), 0L)
s$push(5)
s$push(6)
expect_identical(s$pop(), 6)
expect_identical(s$pop(), 5)
expect_identical(s$pop(missing = "foo"), "foo")
expect_identical(s$pop(), key_missing())
})
test_that("Error expressions prevent any from being added", {
s <- faststack()
expect_error(s$push(1, stop("2"), 3))
expect_identical(s$size(), 0L)
expect_null(s$peek())
expect_identical(s$as_list(), list())
expect_error(s$push(1, .list = list(2, stop("3")), 4))
expect_identical(s$size(), 0L)
expect_null(s$peek())
expect_identical(s$as_list(), list())
})
test_that("mpop()", {
s <- faststack(2L)
s$mpush(1,2,3,4,5,6,7,8,9,10,11,12,13)
expect_identical(s$mpop(6), list(13,12,11,10,9,8))
expect_identical(s$as_list(), list(1,2,3,4,5,6,7))
expect_identical(s$size(), 7L)
# Check that we did NOT resize the underlying list since we haven't gone under
# the 1/2 threshold.
expect_identical(env(s)$s, c(list(1,2,3,4,5,6,7), rep(list(NULL), 6)))
expect_identical(s$mpop(1), list(7))
expect_identical(s$as_list(), list(1,2,3,4,5,6))
expect_identical(s$size(), 6L)
# Now we should have resized.
expect_identical(env(s)$s, list(1,2,3,4,5,6))
expect_identical(s$mpop(9), list(6,5,4,3,2,1,NULL,NULL,NULL))
expect_identical(env(s)$s, list(NULL,NULL))
expect_identical(s$size(), 0L)
# Different `missing`
s <- faststack(2, missing_default = NA)
s$mpush(1,2,3,4,5,6,7,8,9,10,11,12,13)
expect_identical(s$mpop(6), list(13,12,11,10,9,8))
expect_identical(s$mpop(9), list(7,6,5,4,3,2,1,NA,NA))
expect_identical(s$mpop(3, missing = "x"), list("x","x","x"))
})
|