mirror of
https://github.com/golang/go
synced 2024-11-02 11:50:30 +00:00
e3c684777a
The general policy for the current state of js/wasm is that it only has to support tests that are also supported by nacl. The test nilptr3.go makes assumptions about which nil checks can be removed. Since WebAssembly does not signal on reading a null pointer, all nil checks have to be explicit. Updates #18892 Change-Id: I06a687860b8d22ae26b1c391499c0f5183e4c485 Reviewed-on: https://go-review.googlesource.com/110096 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
131 lines
2.2 KiB
Go
131 lines
2.2 KiB
Go
// run
|
|
|
|
// 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.
|
|
|
|
// Test that heavy recursion works. Simple torture test for
|
|
// segmented stacks: do math in unary by recursion.
|
|
|
|
package main
|
|
|
|
import "runtime"
|
|
|
|
type Number *Number
|
|
|
|
// -------------------------------------
|
|
// Peano primitives
|
|
|
|
func zero() *Number {
|
|
return nil
|
|
}
|
|
|
|
func is_zero(x *Number) bool {
|
|
return x == nil
|
|
}
|
|
|
|
func add1(x *Number) *Number {
|
|
e := new(Number)
|
|
*e = x
|
|
return e
|
|
}
|
|
|
|
func sub1(x *Number) *Number {
|
|
return *x
|
|
}
|
|
|
|
func add(x, y *Number) *Number {
|
|
if is_zero(y) {
|
|
return x
|
|
}
|
|
|
|
return add(add1(x), sub1(y))
|
|
}
|
|
|
|
func mul(x, y *Number) *Number {
|
|
if is_zero(x) || is_zero(y) {
|
|
return zero()
|
|
}
|
|
|
|
return add(mul(x, sub1(y)), x)
|
|
}
|
|
|
|
func fact(n *Number) *Number {
|
|
if is_zero(n) {
|
|
return add1(zero())
|
|
}
|
|
|
|
return mul(fact(sub1(n)), n)
|
|
}
|
|
|
|
// -------------------------------------
|
|
// Helpers to generate/count Peano integers
|
|
|
|
func gen(n int) *Number {
|
|
if n > 0 {
|
|
return add1(gen(n - 1))
|
|
}
|
|
|
|
return zero()
|
|
}
|
|
|
|
func count(x *Number) int {
|
|
if is_zero(x) {
|
|
return 0
|
|
}
|
|
|
|
return count(sub1(x)) + 1
|
|
}
|
|
|
|
func check(x *Number, expected int) {
|
|
var c = count(x)
|
|
if c != expected {
|
|
print("error: found ", c, "; expected ", expected, "\n")
|
|
panic("fail")
|
|
}
|
|
}
|
|
|
|
// -------------------------------------
|
|
// Test basic functionality
|
|
|
|
func init() {
|
|
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)
|
|
}
|
|
|
|
// -------------------------------------
|
|
// Factorial
|
|
|
|
var results = [...]int{
|
|
1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800,
|
|
39916800, 479001600,
|
|
}
|
|
|
|
func main() {
|
|
max := 9
|
|
if runtime.GOARCH == "wasm" {
|
|
max = 7 // stack size is limited
|
|
}
|
|
for i := 0; i <= max; i++ {
|
|
if f := count(fact(gen(i))); f != results[i] {
|
|
println("FAIL:", i, "!:", f, "!=", results[i])
|
|
panic(0)
|
|
}
|
|
}
|
|
}
|