2012-02-16 21:51:04 -07:00
|
|
|
// run
|
2008-07-12 14:20:21 -06:00
|
|
|
|
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2012-02-23 16:30:39 -07:00
|
|
|
// Test that heavy recursion works. Simple torture test for
|
|
|
|
// segmented stacks: do math in unary by recursion.
|
|
|
|
|
2008-07-12 14:20:21 -06:00
|
|
|
package main
|
|
|
|
|
2018-03-04 04:15:37 -07:00
|
|
|
import "runtime"
|
|
|
|
|
2010-08-06 16:07:54 -06:00
|
|
|
type Number *Number
|
2008-07-12 14:20:21 -06:00
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Peano primitives
|
|
|
|
|
|
|
|
func zero() *Number {
|
2010-03-30 11:34:57 -06:00
|
|
|
return nil
|
2008-07-12 14:20:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func is_zero(x *Number) bool {
|
2010-03-30 11:34:57 -06:00
|
|
|
return x == nil
|
2008-07-12 14:20:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func add1(x *Number) *Number {
|
2010-03-30 11:34:57 -06:00
|
|
|
e := new(Number)
|
2010-08-06 16:07:54 -06:00
|
|
|
*e = x
|
2010-03-30 11:34:57 -06:00
|
|
|
return e
|
2008-07-12 14:20:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func sub1(x *Number) *Number {
|
2010-08-06 16:07:54 -06:00
|
|
|
return *x
|
2008-07-12 14:20:21 -06:00
|
|
|
}
|
|
|
|
|
2010-03-30 11:34:57 -06:00
|
|
|
func add(x, y *Number) *Number {
|
2008-07-12 14:20:21 -06:00
|
|
|
if is_zero(y) {
|
2010-03-30 11:34:57 -06:00
|
|
|
return x
|
2008-07-12 14:20:21 -06:00
|
|
|
}
|
|
|
|
|
2010-03-30 11:34:57 -06:00
|
|
|
return add(add1(x), sub1(y))
|
2008-07-12 14:20:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func mul(x, y *Number) *Number {
|
2010-03-30 11:34:57 -06:00
|
|
|
if is_zero(x) || is_zero(y) {
|
|
|
|
return zero()
|
2008-07-12 14:20:21 -06:00
|
|
|
}
|
|
|
|
|
2010-03-30 11:34:57 -06:00
|
|
|
return add(mul(x, sub1(y)), x)
|
2008-07-12 14:20:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func fact(n *Number) *Number {
|
|
|
|
if is_zero(n) {
|
2010-03-30 11:34:57 -06:00
|
|
|
return add1(zero())
|
2008-07-12 14:20:21 -06:00
|
|
|
}
|
|
|
|
|
2010-03-30 11:34:57 -06:00
|
|
|
return mul(fact(sub1(n)), n)
|
2008-07-12 14:20:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Helpers to generate/count Peano integers
|
|
|
|
|
|
|
|
func gen(n int) *Number {
|
|
|
|
if n > 0 {
|
2010-03-30 11:34:57 -06:00
|
|
|
return add1(gen(n - 1))
|
2008-07-12 14:20:21 -06:00
|
|
|
}
|
|
|
|
|
2010-03-30 11:34:57 -06:00
|
|
|
return zero()
|
2008-07-12 14:20:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func count(x *Number) int {
|
|
|
|
if is_zero(x) {
|
2010-03-30 11:34:57 -06:00
|
|
|
return 0
|
2008-07-12 14:20:21 -06:00
|
|
|
}
|
|
|
|
|
2010-03-30 11:34:57 -06:00
|
|
|
return count(sub1(x)) + 1
|
2008-07-12 14:20:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func check(x *Number, expected int) {
|
2010-03-30 11:34:57 -06:00
|
|
|
var c = count(x)
|
2008-07-12 14:20:21 -06:00
|
|
|
if c != expected {
|
2010-03-30 11:34:57 -06:00
|
|
|
print("error: found ", c, "; expected ", expected, "\n")
|
|
|
|
panic("fail")
|
2008-07-12 14:20:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Test basic functionality
|
|
|
|
|
2010-08-06 16:07:54 -06:00
|
|
|
func init() {
|
2010-03-30 11:34:57 -06:00
|
|
|
check(zero(), 0)
|
|
|
|
check(add1(zero()), 1)
|
|
|
|
check(gen(10), 10)
|
|
|
|
|
|
|
|
check(add(gen(3), zero()), 3)
|
|
|
|
check(add(zero(), gen(4)), 4)
|
|
|
|
check(add(gen(3), gen(4)), 7)
|
|
|
|
|
|
|
|
check(mul(zero(), zero()), 0)
|
|
|
|
check(mul(gen(3), zero()), 0)
|
|
|
|
check(mul(zero(), gen(4)), 0)
|
|
|
|
check(mul(gen(3), add1(zero())), 3)
|
|
|
|
check(mul(add1(zero()), gen(4)), 4)
|
|
|
|
check(mul(gen(3), gen(4)), 12)
|
|
|
|
|
|
|
|
check(fact(zero()), 1)
|
|
|
|
check(fact(add1(zero())), 1)
|
|
|
|
check(fact(gen(5)), 120)
|
2008-07-12 14:20:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Factorial
|
|
|
|
|
2012-01-18 15:31:31 -07:00
|
|
|
var results = [...]int{
|
|
|
|
1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800,
|
|
|
|
39916800, 479001600,
|
|
|
|
}
|
|
|
|
|
2008-07-12 14:20:21 -06:00
|
|
|
func main() {
|
2018-03-04 04:15:37 -07:00
|
|
|
max := 9
|
|
|
|
if runtime.GOARCH == "wasm" {
|
|
|
|
max = 7 // stack size is limited
|
|
|
|
}
|
|
|
|
for i := 0; i <= max; i++ {
|
2012-01-18 15:31:31 -07:00
|
|
|
if f := count(fact(gen(i))); f != results[i] {
|
|
|
|
println("FAIL:", i, "!:", f, "!=", results[i])
|
|
|
|
panic(0)
|
|
|
|
}
|
2008-07-12 14:20:21 -06:00
|
|
|
}
|
|
|
|
}
|