Skip to content

Commit

Permalink
Add mutual recursion test
Browse files Browse the repository at this point in the history
  • Loading branch information
alecdotninja committed May 31, 2024
1 parent d31e46b commit bd83c26
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tailcall/tests/thunk_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@ fn factorial_in_new_runtime() {
assert!(factorial(5) == 120);
}

#[test]
fn is_even_in_new_runtime() {
assert!(is_even(0));
assert!(!is_even(1));
assert!(is_even(2));
assert!(!is_even(3));
assert!(is_even(4));
assert!(!is_even(5));
}

#[test]
fn is_odd_in_new_runtime() {
assert!(!is_odd(0));
assert!(is_odd(1));
assert!(!is_odd(2));
assert!(is_odd(3));
assert!(!is_odd(4));
assert!(is_odd(5));
}

fn factorial(input: u64) -> u64 {
#[inline(always)]
fn call_factorial_inner<'slot>(
Expand All @@ -27,3 +47,35 @@ fn factorial(input: u64) -> u64 {

factorial_inner(1, input)
}

fn is_even(x: u128) -> bool {
trampoline::run(move |slot| build_is_even_action(slot, x))
}

#[doc(hidden)]
#[inline(always)]
fn build_is_even_action<'slot>(slot: &'slot mut slot::Slot, x: u128) -> trampoline::Action<'slot, bool> {
trampoline::call(slot, move |slot| {
if x > 0 {
build_is_odd_action(slot, x - 1)
} else {
trampoline::done(slot, true)
}
})
}

fn is_odd(x: u128) -> bool {
trampoline::run(move |slot| build_is_odd_action(slot, x))
}

#[doc(hidden)]
#[inline(always)]
fn build_is_odd_action<'slot>(slot: &'slot mut slot::Slot, x: u128) -> trampoline::Action<'slot, bool> {
trampoline::call(slot, move |slot| {
if x > 0 {
build_is_even_action(slot, x - 1)
} else {
trampoline::done(slot, false)
}
})
}

0 comments on commit bd83c26

Please sign in to comment.