Skip to content

Commit

Permalink
updated tests to resolve #59"
Browse files Browse the repository at this point in the history
  • Loading branch information
mailund committed Dec 20, 2018
1 parent 7c26c34 commit 5b179ce
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
36 changes: 35 additions & 1 deletion tests/testthat/test-case_func.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ test_that("We can create match on function constructors", {

})

test_that("We can formula as well as assignment syntax", {
test_that("We can use formula as well as assignment syntax", {
type := ONE | TWO | THREE
f <- case_func(ONE ~ 1, TWO ~ 2, THREE ~ 3)
expect_equal(f(ONE), 1)
Expand Down Expand Up @@ -198,5 +198,39 @@ test_that("We get the additional arguments in the right order in case_func", {
})


test_that("We handle recursion", {
llist := NIL | CONS(car, cdr : llist)

lst <- CONS(1, CONS(2, CONS(3, NIL)))

llength <- case_func(
NIL -> 0,
CONS(., cdr) -> 1 + llength(cdr)
)

lsum <- case_func(
NIL -> 0,
CONS(car, cdr) -> car + lsum(cdr)
)
expect_equal(llength(lst), 3)
expect_equal(lsum(lst), 6)

# tail recursive
llength <- case_func(
acc = 0,
NIL -> acc,
CONS(., cdr) -> llength(cdr, acc + 1)
)

lsum <- case_func(
acc = 0,
NIL -> acc,
CONS(car, cdr) -> lsum(cdr, acc + car)
)
expect_equal(llength(lst), 3)
expect_equal(lsum(lst), 6)
})




35 changes: 35 additions & 0 deletions tests/testthat/test-case_trfunc.R
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,38 @@ test_that("We get the additional arguments in the right order in case_func", {



test_that("We handle recursion", {
llist := NIL | CONS(car, cdr : llist)

lst <- CONS(1, CONS(2, CONS(3, NIL)))

# tail recursive
llength <- case_trfunc(
acc = 0,
NIL -> acc,
CONS(., cdr) -> Recall(cdr, acc + 1)
)

lsum <- case_trfunc(
acc = 0,
NIL -> acc,
CONS(car, cdr) -> Recall(cdr, acc + car)
)
expect_equal(llength(lst), 3)
expect_equal(lsum(lst), 6)

# not tail recursive -- these still work, they just do not have
# the optimisation
llength <- case_trfunc(
NIL -> 0,
CONS(., cdr) -> 1 + Recall(cdr)
)

lsum <- case_trfunc(
NIL -> 0,
CONS(car, cdr) -> car + Recall(cdr)
)
expect_equal(llength(lst), 3)
expect_equal(lsum(lst), 6)

})

0 comments on commit 5b179ce

Please sign in to comment.