Commit dca709da authored by Keith Randall's avatar Keith Randall Committed by Keith Randall

cmd/compile: move last compile tests to new test infrastructure

R=go1.12

Fixes #26469

Change-Id: Idbba88ef60f15a0ec9a83c78541a4d4fb63e534a
Reviewed-on: https://go-review.googlesource.com/127116Reviewed-by: 's avatarDavid Chase <drchase@google.com>
parent 25ea4e57
...@@ -20,39 +20,6 @@ import ( ...@@ -20,39 +20,6 @@ import (
"testing" "testing"
) )
// TODO: move all these tests elsewhere?
// Perhaps teach test/run.go how to run them with a new action verb.
func runTest(t *testing.T, filename string, flags ...string) {
t.Parallel()
doTest(t, filename, "run", flags...)
}
func doTest(t *testing.T, filename string, kind string, flags ...string) {
testenv.MustHaveGoBuild(t)
gotool := testenv.GoToolPath(t)
var stdout, stderr bytes.Buffer
args := []string{kind}
if len(flags) == 0 {
args = append(args, "-gcflags=-d=ssa/check/on")
} else {
args = append(args, flags...)
}
args = append(args, filepath.Join("testdata", filename))
cmd := exec.Command(gotool, args...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
t.Fatalf("Failed: %v:\nOut: %s\nStderr: %s\n", err, &stdout, &stderr)
}
if s := stdout.String(); s != "" {
t.Errorf("Stdout = %s\nWant empty", s)
}
if s := stderr.String(); strings.Contains(s, "SSA unimplemented") {
t.Errorf("Unimplemented message found in stderr:\n%s", s)
}
}
// runGenTest runs a test-generator, then runs the generated test. // runGenTest runs a test-generator, then runs the generated test.
// Generated test can either fail in compilation or execution. // Generated test can either fail in compilation or execution.
// The environment variable parameter(s) is passed to the run // The environment variable parameter(s) is passed to the run
...@@ -222,24 +189,3 @@ func TestCode(t *testing.T) { ...@@ -222,24 +189,3 @@ func TestCode(t *testing.T) {
} }
} }
} }
// TestClosure tests closure related behavior.
func TestClosure(t *testing.T) { runTest(t, "closure.go") }
func TestArray(t *testing.T) { runTest(t, "array.go") }
func TestAppend(t *testing.T) { runTest(t, "append.go") }
func TestAddressed(t *testing.T) { runTest(t, "addressed.go") }
func TestUnsafe(t *testing.T) { runTest(t, "unsafe.go") }
func TestPhi(t *testing.T) { runTest(t, "phi.go") }
func TestSlice(t *testing.T) { runTest(t, "slice.go") }
func TestNamedReturn(t *testing.T) { runTest(t, "namedReturn.go") }
func TestDuplicateLoad(t *testing.T) { runTest(t, "dupLoad.go") }
func TestSqrt(t *testing.T) { runTest(t, "sqrt_const.go") }
...@@ -4,48 +4,51 @@ ...@@ -4,48 +4,51 @@
package main package main
import "fmt" import (
"fmt"
"testing"
)
var output string var output string
func mypanic(s string) { func mypanic(t *testing.T, s string) {
fmt.Printf(output) t.Fatalf(s + "\n" + output)
panic(s)
} }
func assertEqual(x, y int) { func assertEqual(t *testing.T, x, y int) {
if x != y { if x != y {
mypanic("assertEqual failed") mypanic(t, fmt.Sprintf("assertEqual failed got %d, want %d", x, y))
} }
} }
func main() { func TestAddressed(t *testing.T) {
x := f1_ssa(2, 3) x := f1_ssa(2, 3)
output += fmt.Sprintln("*x is", *x) output += fmt.Sprintln("*x is", *x)
output += fmt.Sprintln("Gratuitously use some stack") output += fmt.Sprintln("Gratuitously use some stack")
output += fmt.Sprintln("*x is", *x) output += fmt.Sprintln("*x is", *x)
assertEqual(*x, 9) assertEqual(t, *x, 9)
w := f3a_ssa(6) w := f3a_ssa(6)
output += fmt.Sprintln("*w is", *w) output += fmt.Sprintln("*w is", *w)
output += fmt.Sprintln("Gratuitously use some stack") output += fmt.Sprintln("Gratuitously use some stack")
output += fmt.Sprintln("*w is", *w) output += fmt.Sprintln("*w is", *w)
assertEqual(*w, 6) assertEqual(t, *w, 6)
y := f3b_ssa(12) y := f3b_ssa(12)
output += fmt.Sprintln("*y.(*int) is", *y.(*int)) output += fmt.Sprintln("*y.(*int) is", *y.(*int))
output += fmt.Sprintln("Gratuitously use some stack") output += fmt.Sprintln("Gratuitously use some stack")
output += fmt.Sprintln("*y.(*int) is", *y.(*int)) output += fmt.Sprintln("*y.(*int) is", *y.(*int))
assertEqual(*y.(*int), 12) assertEqual(t, *y.(*int), 12)
z := f3c_ssa(8) z := f3c_ssa(8)
output += fmt.Sprintln("*z.(*int) is", *z.(*int)) output += fmt.Sprintln("*z.(*int) is", *z.(*int))
output += fmt.Sprintln("Gratuitously use some stack") output += fmt.Sprintln("Gratuitously use some stack")
output += fmt.Sprintln("*z.(*int) is", *z.(*int)) output += fmt.Sprintln("*z.(*int) is", *z.(*int))
assertEqual(*z.(*int), 8) assertEqual(t, *z.(*int), 8)
args() args(t)
test_autos() test_autos(t)
} }
//go:noinline //go:noinline
...@@ -75,13 +78,13 @@ type V struct { ...@@ -75,13 +78,13 @@ type V struct {
w, x int64 w, x int64
} }
func args() { func args(t *testing.T) {
v := V{p: nil, w: 1, x: 1} v := V{p: nil, w: 1, x: 1}
a := V{p: &v, w: 2, x: 2} a := V{p: &v, w: 2, x: 2}
b := V{p: &v, w: 0, x: 0} b := V{p: &v, w: 0, x: 0}
i := v.args_ssa(a, b) i := v.args_ssa(a, b)
output += fmt.Sprintln("i=", i) output += fmt.Sprintln("i=", i)
assertEqual(int(i), 2) assertEqual(t, int(i), 2)
} }
//go:noinline //go:noinline
...@@ -100,32 +103,32 @@ func (v V) args_ssa(a, b V) int64 { ...@@ -100,32 +103,32 @@ func (v V) args_ssa(a, b V) int64 {
return -1 return -1
} }
func test_autos() { func test_autos(t *testing.T) {
test(11) test(t, 11)
test(12) test(t, 12)
test(13) test(t, 13)
test(21) test(t, 21)
test(22) test(t, 22)
test(23) test(t, 23)
test(31) test(t, 31)
test(32) test(t, 32)
} }
func test(which int64) { func test(t *testing.T, which int64) {
output += fmt.Sprintln("test", which) output += fmt.Sprintln("test", which)
v1 := V{w: 30, x: 3, p: nil} v1 := V{w: 30, x: 3, p: nil}
v2, v3 := v1.autos_ssa(which, 10, 1, 20, 2) v2, v3 := v1.autos_ssa(which, 10, 1, 20, 2)
if which != v2.val() { if which != v2.val() {
output += fmt.Sprintln("Expected which=", which, "got v2.val()=", v2.val()) output += fmt.Sprintln("Expected which=", which, "got v2.val()=", v2.val())
mypanic("Failure of expected V value") mypanic(t, "Failure of expected V value")
} }
if v2.p.val() != v3.val() { if v2.p.val() != v3.val() {
output += fmt.Sprintln("Expected v2.p.val()=", v2.p.val(), "got v3.val()=", v3.val()) output += fmt.Sprintln("Expected v2.p.val()=", v2.p.val(), "got v3.val()=", v3.val())
mypanic("Failure of expected V.p value") mypanic(t, "Failure of expected V.p value")
} }
if which != v3.p.p.p.p.p.p.p.val() { if which != v3.p.p.p.p.p.p.p.val() {
output += fmt.Sprintln("Expected which=", which, "got v3.p.p.p.p.p.p.p.val()=", v3.p.p.p.p.p.p.p.val()) output += fmt.Sprintln("Expected which=", which, "got v3.p.p.p.p.p.p.p.val()=", v3.p.p.p.p.p.p.p.val())
mypanic("Failure of expected V.p value") mypanic(t, "Failure of expected V.p value")
} }
} }
......
...@@ -5,9 +5,7 @@ ...@@ -5,9 +5,7 @@
// append_ssa.go tests append operations. // append_ssa.go tests append operations.
package main package main
import "fmt" import "testing"
var failed = false
//go:noinline //go:noinline
func appendOne_ssa(a []int, x int) []int { func appendOne_ssa(a []int, x int) []int {
...@@ -19,7 +17,7 @@ func appendThree_ssa(a []int, x, y, z int) []int { ...@@ -19,7 +17,7 @@ func appendThree_ssa(a []int, x, y, z int) []int {
return append(a, x, y, z) return append(a, x, y, z)
} }
func eq(a, b []int) bool { func eqBytes(a, b []int) bool {
if len(a) != len(b) { if len(a) != len(b) {
return false return false
} }
...@@ -31,40 +29,33 @@ func eq(a, b []int) bool { ...@@ -31,40 +29,33 @@ func eq(a, b []int) bool {
return true return true
} }
func expect(got, want []int) { func expect(t *testing.T, got, want []int) {
if eq(got, want) { if eqBytes(got, want) {
return return
} }
fmt.Printf("expected %v, got %v\n", want, got) t.Errorf("expected %v, got %v\n", want, got)
failed = true
} }
func testAppend() { func testAppend(t *testing.T) {
var store [7]int var store [7]int
a := store[:0] a := store[:0]
a = appendOne_ssa(a, 1) a = appendOne_ssa(a, 1)
expect(a, []int{1}) expect(t, a, []int{1})
a = appendThree_ssa(a, 2, 3, 4) a = appendThree_ssa(a, 2, 3, 4)
expect(a, []int{1, 2, 3, 4}) expect(t, a, []int{1, 2, 3, 4})
a = appendThree_ssa(a, 5, 6, 7) a = appendThree_ssa(a, 5, 6, 7)
expect(a, []int{1, 2, 3, 4, 5, 6, 7}) expect(t, a, []int{1, 2, 3, 4, 5, 6, 7})
if &a[0] != &store[0] { if &a[0] != &store[0] {
fmt.Println("unnecessary grow") t.Errorf("unnecessary grow")
failed = true
} }
a = appendOne_ssa(a, 8) a = appendOne_ssa(a, 8)
expect(a, []int{1, 2, 3, 4, 5, 6, 7, 8}) expect(t, a, []int{1, 2, 3, 4, 5, 6, 7, 8})
if &a[0] == &store[0] { if &a[0] == &store[0] {
fmt.Println("didn't grow") t.Errorf("didn't grow")
failed = true
} }
} }
func main() { func TestAppend(t *testing.T) {
testAppend() testAppend(t)
if failed {
panic("failed")
}
} }
package main package main
var failed = false import "testing"
//go:noinline //go:noinline
func testSliceLenCap12_ssa(a [10]int, i, j int) (int, int) { func testSliceLenCap12_ssa(a [10]int, i, j int) (int, int) {
...@@ -20,7 +20,7 @@ func testSliceLenCap2_ssa(a [10]int, i, j int) (int, int) { ...@@ -20,7 +20,7 @@ func testSliceLenCap2_ssa(a [10]int, i, j int) (int, int) {
return len(b), cap(b) return len(b), cap(b)
} }
func testSliceLenCap() { func testSliceLenCap(t *testing.T) {
a := [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} a := [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
tests := [...]struct { tests := [...]struct {
fn func(a [10]int, i, j int) (int, int) fn func(a [10]int, i, j int) (int, int)
...@@ -43,11 +43,9 @@ func testSliceLenCap() { ...@@ -43,11 +43,9 @@ func testSliceLenCap() {
{testSliceLenCap2_ssa, -1, 10, 10, 10}, {testSliceLenCap2_ssa, -1, 10, 10, 10},
} }
for i, t := range tests { for i, test := range tests {
if l, c := t.fn(a, t.i, t.j); l != t.l && c != t.c { if l, c := test.fn(a, test.i, test.j); l != test.l && c != test.c {
println("#", i, " len(a[", t.i, ":", t.j, "]), cap(a[", t.i, ":", t.j, "]) =", l, c, t.Errorf("#%d len(a[%d:%d]), cap(a[%d:%d]) = %d %d, want %d %d", i, test.i, test.j, test.i, test.j, l, c, test.l, test.c)
", want", t.l, t.c)
failed = true
} }
} }
} }
...@@ -57,7 +55,7 @@ func testSliceGetElement_ssa(a [10]int, i, j, p int) int { ...@@ -57,7 +55,7 @@ func testSliceGetElement_ssa(a [10]int, i, j, p int) int {
return a[i:j][p] return a[i:j][p]
} }
func testSliceGetElement() { func testSliceGetElement(t *testing.T) {
a := [10]int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90} a := [10]int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90}
tests := [...]struct { tests := [...]struct {
i, j, p int i, j, p int
...@@ -69,10 +67,9 @@ func testSliceGetElement() { ...@@ -69,10 +67,9 @@ func testSliceGetElement() {
{1, 9, 7, 80}, {1, 9, 7, 80},
} }
for i, t := range tests { for i, test := range tests {
if got := testSliceGetElement_ssa(a, t.i, t.j, t.p); got != t.want { if got := testSliceGetElement_ssa(a, test.i, test.j, test.p); got != test.want {
println("#", i, " a[", t.i, ":", t.j, "][", t.p, "] = ", got, " wanted ", t.want) t.Errorf("#%d a[%d:%d][%d] = %d, wanted %d", i, test.i, test.j, test.p, got, test.want)
failed = true
} }
} }
} }
...@@ -82,7 +79,7 @@ func testSliceSetElement_ssa(a *[10]int, i, j, p, x int) { ...@@ -82,7 +79,7 @@ func testSliceSetElement_ssa(a *[10]int, i, j, p, x int) {
(*a)[i:j][p] = x (*a)[i:j][p] = x
} }
func testSliceSetElement() { func testSliceSetElement(t *testing.T) {
a := [10]int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90} a := [10]int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90}
tests := [...]struct { tests := [...]struct {
i, j, p int i, j, p int
...@@ -94,49 +91,42 @@ func testSliceSetElement() { ...@@ -94,49 +91,42 @@ func testSliceSetElement() {
{1, 9, 7, 99}, {1, 9, 7, 99},
} }
for i, t := range tests { for i, test := range tests {
testSliceSetElement_ssa(&a, t.i, t.j, t.p, t.want) testSliceSetElement_ssa(&a, test.i, test.j, test.p, test.want)
if got := a[t.i+t.p]; got != t.want { if got := a[test.i+test.p]; got != test.want {
println("#", i, " a[", t.i, ":", t.j, "][", t.p, "] = ", got, " wanted ", t.want) t.Errorf("#%d a[%d:%d][%d] = %d, wanted %d", i, test.i, test.j, test.p, got, test.want)
failed = true
} }
} }
} }
func testSlicePanic1() { func testSlicePanic1(t *testing.T) {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
println("panicked as expected") //println("panicked as expected")
} }
}() }()
a := [10]int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90} a := [10]int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90}
testSliceLenCap12_ssa(a, 3, 12) testSliceLenCap12_ssa(a, 3, 12)
println("expected to panic, but didn't") t.Errorf("expected to panic, but didn't")
failed = true
} }
func testSlicePanic2() { func testSlicePanic2(t *testing.T) {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
println("panicked as expected") //println("panicked as expected")
} }
}() }()
a := [10]int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90} a := [10]int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90}
testSliceGetElement_ssa(a, 3, 7, 4) testSliceGetElement_ssa(a, 3, 7, 4)
println("expected to panic, but didn't") t.Errorf("expected to panic, but didn't")
failed = true
} }
func main() { func TestArray(t *testing.T) {
testSliceLenCap() testSliceLenCap(t)
testSliceGetElement() testSliceGetElement(t)
testSliceSetElement() testSliceSetElement(t)
testSlicePanic1() testSlicePanic1(t)
testSlicePanic2() testSlicePanic2(t)
if failed {
panic("failed")
}
} }
...@@ -2,12 +2,10 @@ ...@@ -2,12 +2,10 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// map_ssa.go tests map operations. // closure.go tests closure operations.
package main package main
import "fmt" import "testing"
var failed = false
//go:noinline //go:noinline
func testCFunc_ssa() int { func testCFunc_ssa() int {
...@@ -22,17 +20,13 @@ func testCFunc_ssa() int { ...@@ -22,17 +20,13 @@ func testCFunc_ssa() int {
return a return a
} }
func testCFunc() { func testCFunc(t *testing.T) {
if want, got := 2, testCFunc_ssa(); got != want { if want, got := 2, testCFunc_ssa(); got != want {
fmt.Printf("expected %d, got %d", want, got) t.Errorf("expected %d, got %d", want, got)
failed = true
} }
} }
func main() { // TestClosure tests closure related behavior.
testCFunc() func TestClosure(t *testing.T) {
testCFunc(t)
if failed {
panic("failed")
}
} }
// run
// Copyright 2016 The Go Authors. All rights reserved. // Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
...@@ -9,7 +7,7 @@ ...@@ -9,7 +7,7 @@
package main package main
import "fmt" import "testing"
//go:noinline //go:noinline
func read1(b []byte) (uint16, uint16) { func read1(b []byte) (uint16, uint16) {
...@@ -19,9 +17,8 @@ func read1(b []byte) (uint16, uint16) { ...@@ -19,9 +17,8 @@ func read1(b []byte) (uint16, uint16) {
return uint16(v), uint16(v) | uint16(b[1])<<8 return uint16(v), uint16(v) | uint16(b[1])<<8
} }
const N = 100000 func main1(t *testing.T) {
const N = 100000
func main1() {
done := make(chan struct{}) done := make(chan struct{})
b := make([]byte, 2) b := make([]byte, 2)
go func() { go func() {
...@@ -35,8 +32,7 @@ func main1() { ...@@ -35,8 +32,7 @@ func main1() {
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
x, y := read1(b) x, y := read1(b)
if byte(x) != byte(y) { if byte(x) != byte(y) {
fmt.Printf("x=%x y=%x\n", x, y) t.Fatalf("x=%x y=%x\n", x, y)
panic("bad")
} }
} }
done <- struct{}{} done <- struct{}{}
...@@ -53,7 +49,8 @@ func read2(b []byte) (uint16, uint16) { ...@@ -53,7 +49,8 @@ func read2(b []byte) (uint16, uint16) {
return v, uint16(b[0]) | v return v, uint16(b[0]) | v
} }
func main2() { func main2(t *testing.T) {
const N = 100000
done := make(chan struct{}) done := make(chan struct{})
b := make([]byte, 2) b := make([]byte, 2)
go func() { go func() {
...@@ -67,8 +64,7 @@ func main2() { ...@@ -67,8 +64,7 @@ func main2() {
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
x, y := read2(b) x, y := read2(b)
if x&0xff00 != y&0xff00 { if x&0xff00 != y&0xff00 {
fmt.Printf("x=%x y=%x\n", x, y) t.Fatalf("x=%x y=%x\n", x, y)
panic("bad")
} }
} }
done <- struct{}{} done <- struct{}{}
...@@ -77,7 +73,7 @@ func main2() { ...@@ -77,7 +73,7 @@ func main2() {
<-done <-done
} }
func main() { func TestDupLoad(t *testing.T) {
main1() main1(t)
main2() main2(t)
} }
// run
// Copyright 2016 The Go Authors. All rights reserved. // Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
...@@ -11,8 +9,8 @@ ...@@ -11,8 +9,8 @@
package main package main
import ( import (
"fmt"
"runtime" "runtime"
"testing"
) )
// Our heap-allocated object that will be GC'd incorrectly. // Our heap-allocated object that will be GC'd incorrectly.
...@@ -21,44 +19,44 @@ import ( ...@@ -21,44 +19,44 @@ import (
type B [4]int type B [4]int
// small (SSAable) array // small (SSAable) array
type T1 [3]*B type A1 [3]*B
//go:noinline //go:noinline
func f1() (t T1) { func f1() (t A1) {
t[0] = &B{91, 92, 93, 94} t[0] = &B{91, 92, 93, 94}
runtime.GC() runtime.GC()
return t return t
} }
// large (non-SSAable) array // large (non-SSAable) array
type T2 [8]*B type A2 [8]*B
//go:noinline //go:noinline
func f2() (t T2) { func f2() (t A2) {
t[0] = &B{91, 92, 93, 94} t[0] = &B{91, 92, 93, 94}
runtime.GC() runtime.GC()
return t return t
} }
// small (SSAable) struct // small (SSAable) struct
type T3 struct { type A3 struct {
a, b, c *B a, b, c *B
} }
//go:noinline //go:noinline
func f3() (t T3) { func f3() (t A3) {
t.a = &B{91, 92, 93, 94} t.a = &B{91, 92, 93, 94}
runtime.GC() runtime.GC()
return t return t
} }
// large (non-SSAable) struct // large (non-SSAable) struct
type T4 struct { type A4 struct {
a, b, c, d, e, f *B a, b, c, d, e, f *B
} }
//go:noinline //go:noinline
func f4() (t T4) { func f4() (t A4) {
t.a = &B{91, 92, 93, 94} t.a = &B{91, 92, 93, 94}
runtime.GC() runtime.GC()
return t return t
...@@ -68,7 +66,7 @@ var sink *B ...@@ -68,7 +66,7 @@ var sink *B
func f5() int { func f5() int {
b := &B{91, 92, 93, 94} b := &B{91, 92, 93, 94}
t := T4{b, nil, nil, nil, nil, nil} t := A4{b, nil, nil, nil, nil, nil}
sink = b // make sure b is heap allocated ... sink = b // make sure b is heap allocated ...
sink = nil // ... but not live sink = nil // ... but not live
runtime.GC() runtime.GC()
...@@ -76,30 +74,20 @@ func f5() int { ...@@ -76,30 +74,20 @@ func f5() int {
return t.a[1] return t.a[1]
} }
func main() { func TestNamedReturn(t *testing.T) {
failed := false
if v := f1()[0][1]; v != 92 { if v := f1()[0][1]; v != 92 {
fmt.Printf("f1()[0][1]=%d, want 92\n", v) t.Errorf("f1()[0][1]=%d, want 92\n", v)
failed = true
} }
if v := f2()[0][1]; v != 92 { if v := f2()[0][1]; v != 92 {
fmt.Printf("f2()[0][1]=%d, want 92\n", v) t.Errorf("f2()[0][1]=%d, want 92\n", v)
failed = true
} }
if v := f3().a[1]; v != 92 { if v := f3().a[1]; v != 92 {
fmt.Printf("f3().a[1]=%d, want 92\n", v) t.Errorf("f3().a[1]=%d, want 92\n", v)
failed = true
} }
if v := f4().a[1]; v != 92 { if v := f4().a[1]; v != 92 {
fmt.Printf("f4().a[1]=%d, want 92\n", v) t.Errorf("f4().a[1]=%d, want 92\n", v)
failed = true
} }
if v := f5(); v != 92 { if v := f5(); v != 92 {
fmt.Printf("f5()=%d, want 92\n", v) t.Errorf("f5()=%d, want 92\n", v)
failed = true
}
if failed {
panic("bad")
} }
} }
...@@ -9,13 +9,10 @@ package main ...@@ -9,13 +9,10 @@ package main
// of the post-shortened size. // of the post-shortened size.
import ( import (
"fmt"
"runtime" "runtime"
"testing"
) )
// unfoldable true
var true_ = true
var data1 [26]int32 var data1 [26]int32
var data2 [26]int64 var data2 [26]int64
...@@ -29,7 +26,7 @@ func init() { ...@@ -29,7 +26,7 @@ func init() {
func foo() int32 { func foo() int32 {
var a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z int32 var a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z int32
if true_ { if always {
a = data1[0] a = data1[0]
b = data1[1] b = data1[1]
c = data1[2] c = data1[2]
...@@ -93,11 +90,10 @@ func foo() int32 { ...@@ -93,11 +90,10 @@ func foo() int32 {
return a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p + q + r + s + t + u + v + w + x + y + z return a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p + q + r + s + t + u + v + w + x + y + z
} }
func main() { func TestPhi(t *testing.T) {
want := int32(0) want := int32(0)
got := foo() got := foo()
if got != want { if got != want {
fmt.Printf("want %d, got %d\n", want, got) t.Fatalf("want %d, got %d\n", want, got)
panic("bad")
} }
} }
// run // Copyright 2018 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.
// This test makes sure that t.s = t.s[0:x] doesn't write // This test makes sure that t.s = t.s[0:x] doesn't write
// either the slice pointer or the capacity. // either the slice pointer or the capacity.
...@@ -6,45 +8,39 @@ ...@@ -6,45 +8,39 @@
package main package main
import "fmt" import "testing"
const N = 1000000 const N = 1000000
type T struct { type X struct {
s []int s []int
} }
func main() { func TestSlice(t *testing.T) {
done := make(chan struct{}) done := make(chan struct{})
a := make([]int, N+10) a := make([]int, N+10)
t := &T{a} x := &X{a}
go func() { go func() {
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
t.s = t.s[1:9] x.s = x.s[1:9]
} }
done <- struct{}{} done <- struct{}{}
}() }()
go func() { go func() {
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
t.s = t.s[0:8] // should only write len x.s = x.s[0:8] // should only write len
} }
done <- struct{}{} done <- struct{}{}
}() }()
<-done <-done
<-done <-done
ok := true if cap(x.s) != cap(a)-N {
if cap(t.s) != cap(a)-N { t.Errorf("wanted cap=%d, got %d\n", cap(a)-N, cap(x.s))
fmt.Printf("wanted cap=%d, got %d\n", cap(a)-N, cap(t.s))
ok = false
} }
if &t.s[0] != &a[N] { if &x.s[0] != &a[N] {
fmt.Printf("wanted ptr=%p, got %p\n", &a[N], &t.s[0]) t.Errorf("wanted ptr=%p, got %p\n", &a[N], &x.s[0])
ok = false
}
if !ok {
panic("bad")
} }
} }
...@@ -5,8 +5,8 @@ ...@@ -5,8 +5,8 @@
package main package main
import ( import (
"fmt"
"math" "math"
"testing"
) )
var tests = [...]struct { var tests = [...]struct {
...@@ -33,27 +33,18 @@ var nanTests = [...]struct { ...@@ -33,27 +33,18 @@ var nanTests = [...]struct {
{"sqrtNegInf", math.Inf(-1), math.Sqrt(math.Inf(-1))}, {"sqrtNegInf", math.Inf(-1), math.Sqrt(math.Inf(-1))},
} }
var failed = false func TestSqrtConst(t *testing.T) {
func main() {
for _, test := range tests { for _, test := range tests {
if test.got != test.want { if test.got != test.want {
fmt.Printf("%s: math.Sqrt(%f): got %f, want %f\n", test.name, test.in, test.got, test.want) t.Errorf("%s: math.Sqrt(%f): got %f, want %f\n", test.name, test.in, test.got, test.want)
failed = true
} }
} }
for _, test := range nanTests { for _, test := range nanTests {
if math.IsNaN(test.got) != true { if math.IsNaN(test.got) != true {
fmt.Printf("%s: math.Sqrt(%f): got %f, want NaN\n", test.name, test.in, test.got) t.Errorf("%s: math.Sqrt(%f): got %f, want NaN\n", test.name, test.in, test.got)
failed = true
} }
} }
if got := math.Sqrt(math.Inf(1)); !math.IsInf(got, 1) { if got := math.Sqrt(math.Inf(1)); !math.IsInf(got, 1) {
fmt.Printf("math.Sqrt(+Inf), got %f, want +Inf\n", got) t.Errorf("math.Sqrt(+Inf), got %f, want +Inf\n", got)
failed = true
}
if failed {
panic("failed")
} }
} }
...@@ -5,8 +5,8 @@ ...@@ -5,8 +5,8 @@
package main package main
import ( import (
"fmt"
"runtime" "runtime"
"testing"
"unsafe" "unsafe"
) )
...@@ -14,7 +14,7 @@ import ( ...@@ -14,7 +14,7 @@ import (
var a *[8]uint var a *[8]uint
// unfoldable true // unfoldable true
var b = true var always = true
// Test to make sure that a pointer value which is alive // Test to make sure that a pointer value which is alive
// across a call is retained, even when there are matching // across a call is retained, even when there are matching
...@@ -25,7 +25,7 @@ var b = true ...@@ -25,7 +25,7 @@ var b = true
func f_ssa() *[8]uint { func f_ssa() *[8]uint {
// Make x a uintptr pointing to where a points. // Make x a uintptr pointing to where a points.
var x uintptr var x uintptr
if b { if always {
x = uintptr(unsafe.Pointer(a)) x = uintptr(unsafe.Pointer(a))
} else { } else {
x = 0 x = 0
...@@ -48,7 +48,7 @@ func f_ssa() *[8]uint { ...@@ -48,7 +48,7 @@ func f_ssa() *[8]uint {
// to unsafe.Pointer can't be combined with the // to unsafe.Pointer can't be combined with the
// uintptr cast above. // uintptr cast above.
var z uintptr var z uintptr
if b { if always {
z = y z = y
} else { } else {
z = 0 z = 0
...@@ -61,7 +61,7 @@ func f_ssa() *[8]uint { ...@@ -61,7 +61,7 @@ func f_ssa() *[8]uint {
func g_ssa() *[7]uint { func g_ssa() *[7]uint {
// Make x a uintptr pointing to where a points. // Make x a uintptr pointing to where a points.
var x uintptr var x uintptr
if b { if always {
x = uintptr(unsafe.Pointer(a)) x = uintptr(unsafe.Pointer(a))
} else { } else {
x = 0 x = 0
...@@ -87,7 +87,7 @@ func g_ssa() *[7]uint { ...@@ -87,7 +87,7 @@ func g_ssa() *[7]uint {
// to unsafe.Pointer can't be combined with the // to unsafe.Pointer can't be combined with the
// uintptr cast above. // uintptr cast above.
var z uintptr var z uintptr
if b { if always {
z = y z = y
} else { } else {
z = 0 z = 0
...@@ -95,7 +95,7 @@ func g_ssa() *[7]uint { ...@@ -95,7 +95,7 @@ func g_ssa() *[7]uint {
return (*[7]uint)(unsafe.Pointer(z)) return (*[7]uint)(unsafe.Pointer(z))
} }
func testf() { func testf(t *testing.T) {
a = new([8]uint) a = new([8]uint)
for i := 0; i < 8; i++ { for i := 0; i < 8; i++ {
a[i] = 0xabcd a[i] = 0xabcd
...@@ -103,13 +103,12 @@ func testf() { ...@@ -103,13 +103,12 @@ func testf() {
c := f_ssa() c := f_ssa()
for i := 0; i < 8; i++ { for i := 0; i < 8; i++ {
if c[i] != 0xabcd { if c[i] != 0xabcd {
fmt.Printf("%d:%x\n", i, c[i]) t.Fatalf("%d:%x\n", i, c[i])
panic("bad c")
} }
} }
} }
func testg() { func testg(t *testing.T) {
a = new([8]uint) a = new([8]uint)
for i := 0; i < 8; i++ { for i := 0; i < 8; i++ {
a[i] = 0xabcd a[i] = 0xabcd
...@@ -117,8 +116,7 @@ func testg() { ...@@ -117,8 +116,7 @@ func testg() {
c := g_ssa() c := g_ssa()
for i := 0; i < 7; i++ { for i := 0; i < 7; i++ {
if c[i] != 0xabcd { if c[i] != 0xabcd {
fmt.Printf("%d:%x\n", i, c[i]) t.Fatalf("%d:%x\n", i, c[i])
panic("bad c")
} }
} }
} }
...@@ -130,19 +128,18 @@ func alias_ssa(ui64 *uint64, ui32 *uint32) uint32 { ...@@ -130,19 +128,18 @@ func alias_ssa(ui64 *uint64, ui32 *uint32) uint32 {
*ui64 = 0xffffffffffffffff // store *ui64 = 0xffffffffffffffff // store
return ret return ret
} }
func testdse() { func testdse(t *testing.T) {
x := int64(-1) x := int64(-1)
// construct two pointers that alias one another // construct two pointers that alias one another
ui64 := (*uint64)(unsafe.Pointer(&x)) ui64 := (*uint64)(unsafe.Pointer(&x))
ui32 := (*uint32)(unsafe.Pointer(&x)) ui32 := (*uint32)(unsafe.Pointer(&x))
if want, got := uint32(0), alias_ssa(ui64, ui32); got != want { if want, got := uint32(0), alias_ssa(ui64, ui32); got != want {
fmt.Printf("alias_ssa: wanted %d, got %d\n", want, got) t.Fatalf("alias_ssa: wanted %d, got %d\n", want, got)
panic("alias_ssa")
} }
} }
func main() { func TestUnsafe(t *testing.T) {
testf() testf(t)
testg() testg(t)
testdse() testdse(t)
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment