mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-11-14 15:05:29 +00:00
47 lines
598 B
Plaintext
47 lines
598 B
Plaintext
|
|
test_sum = fn () {
|
|
mut i = 1
|
|
mut ret = 0
|
|
while i <= 10 {
|
|
ret = ret + i
|
|
i = i + 1
|
|
}
|
|
|
|
assert(ret == 55)
|
|
}
|
|
|
|
test_fib = fn () {
|
|
fib = fn (x) {
|
|
if x < 2 {
|
|
x
|
|
} else {
|
|
self(x - 2) + self(x -1)
|
|
}
|
|
}
|
|
|
|
ret = fib(15)
|
|
|
|
assert(ret == 610)
|
|
}
|
|
|
|
test_closure = fn () {
|
|
make_func = fn (mut x) {
|
|
mut n = 100
|
|
fn () {
|
|
n = n + 1
|
|
x = x + 1 + n
|
|
}
|
|
}
|
|
|
|
f = make_func(10)
|
|
f()
|
|
f()
|
|
ret = f()
|
|
|
|
assert(ret == 319)
|
|
}
|
|
|
|
test_sum()
|
|
test_fib()
|
|
test_closure()
|