summaryrefslogtreecommitdiffstats
path: root/test/fixedbugs/bug449.go
blob: a9650f4c6f654f6d0e869104d7eecf6c68495edb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// runoutput

// Copyright 2012 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.

// Issue 3866
// runtime.equal failed to take padding between arguments and
// return values into account, so in certain cases gc-generated
// code will read a random bool from the stack as the result of
// the comparison.
// This program generates a lot of equality tests and hopes to
// catch this.
// NOTE: this program assumes comparing instance of T and T's
// underlying []byte will make gc emit calls to runtime.equal,
// and if gc optimizes this case, then the test will no longer
// be correct (in the sense that it no longer tests runtime.equal).

package main

import (
	"bytes"
	"fmt"
	"strconv"
	"strings"
)

const ntest = 1024

func main() {
	var decls, calls bytes.Buffer

	for i := 1; i <= ntest; i++ {
		s := strconv.Itoa(i)
		decls.WriteString(strings.Replace(decl, "$", s, -1))
		calls.WriteString(strings.Replace("call(test$)\n\t", "$", s, -1))
	}

	program = strings.Replace(program, "$DECLS", decls.String(), 1)
	program = strings.Replace(program, "$CALLS", calls.String(), 1)
	fmt.Print(program)
}

var program = `package main

var count int

func call(f func() bool) {
	if f() {
		count++
	}
}

$DECLS

func main() {
	$CALLS
	if count != 0 {
		println("failed", count, "case(s)")
	}
}
`

const decl = `
type T$ [$]uint8
func test$() bool {
	v := T${1}
	return v == [$]uint8{2} || v != [$]uint8{1}
}`