1
0
mirror of https://github.com/golang/go synced 2024-11-11 23:10:23 -07:00

[dev.regabi] cmd/compile: clean up tests to know less about Node

We want to refactor a bit, and these tests know too much about
the layout of Nodes. Use standard constructors instead.

Change-Id: I91f0325c89ea60086655414468c53419ebeacea4
Reviewed-on: https://go-review.googlesource.com/c/go/+/272626
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
Russ Cox 2020-11-18 15:14:24 -05:00
parent 742c05e3bc
commit 8e2106327c

View File

@ -35,106 +35,110 @@ func markNeedZero(n *Node) *Node {
return n
}
func nodeWithClass(n Node, c Class) *Node {
n.SetClass(c)
n.Name = new(Name)
return &n
}
// Test all code paths for cmpstackvarlt.
func TestCmpstackvar(t *testing.T) {
nod := func(xoffset int64, t *types.Type, s *types.Sym, cl Class) *Node {
if s == nil {
s = &types.Sym{Name: "."}
}
n := newname(s)
n.Type = t
n.Xoffset = xoffset
n.SetClass(cl)
return n
}
testdata := []struct {
a, b *Node
lt bool
}{
{
nodeWithClass(Node{}, PAUTO),
nodeWithClass(Node{}, PFUNC),
nod(0, nil, nil, PAUTO),
nod(0, nil, nil, PFUNC),
false,
},
{
nodeWithClass(Node{}, PFUNC),
nodeWithClass(Node{}, PAUTO),
nod(0, nil, nil, PFUNC),
nod(0, nil, nil, PAUTO),
true,
},
{
nodeWithClass(Node{Xoffset: 0}, PFUNC),
nodeWithClass(Node{Xoffset: 10}, PFUNC),
nod(0, nil, nil, PFUNC),
nod(10, nil, nil, PFUNC),
true,
},
{
nodeWithClass(Node{Xoffset: 20}, PFUNC),
nodeWithClass(Node{Xoffset: 10}, PFUNC),
nod(20, nil, nil, PFUNC),
nod(10, nil, nil, PFUNC),
false,
},
{
nodeWithClass(Node{Xoffset: 10}, PFUNC),
nodeWithClass(Node{Xoffset: 10}, PFUNC),
nod(10, nil, nil, PFUNC),
nod(10, nil, nil, PFUNC),
false,
},
{
nodeWithClass(Node{Xoffset: 10}, PPARAM),
nodeWithClass(Node{Xoffset: 20}, PPARAMOUT),
nod(10, nil, nil, PPARAM),
nod(20, nil, nil, PPARAMOUT),
true,
},
{
nodeWithClass(Node{Xoffset: 10}, PPARAMOUT),
nodeWithClass(Node{Xoffset: 20}, PPARAM),
nod(10, nil, nil, PPARAMOUT),
nod(20, nil, nil, PPARAM),
true,
},
{
markUsed(nodeWithClass(Node{}, PAUTO)),
nodeWithClass(Node{}, PAUTO),
markUsed(nod(0, nil, nil, PAUTO)),
nod(0, nil, nil, PAUTO),
true,
},
{
nodeWithClass(Node{}, PAUTO),
markUsed(nodeWithClass(Node{}, PAUTO)),
nod(0, nil, nil, PAUTO),
markUsed(nod(0, nil, nil, PAUTO)),
false,
},
{
nodeWithClass(Node{Type: typeWithoutPointers()}, PAUTO),
nodeWithClass(Node{Type: typeWithPointers()}, PAUTO),
nod(0, typeWithoutPointers(), nil, PAUTO),
nod(0, typeWithPointers(), nil, PAUTO),
false,
},
{
nodeWithClass(Node{Type: typeWithPointers()}, PAUTO),
nodeWithClass(Node{Type: typeWithoutPointers()}, PAUTO),
nod(0, typeWithPointers(), nil, PAUTO),
nod(0, typeWithoutPointers(), nil, PAUTO),
true,
},
{
markNeedZero(nodeWithClass(Node{Type: &types.Type{}}, PAUTO)),
nodeWithClass(Node{Type: &types.Type{}, Name: &Name{}}, PAUTO),
markNeedZero(nod(0, &types.Type{}, nil, PAUTO)),
nod(0, &types.Type{}, nil, PAUTO),
true,
},
{
nodeWithClass(Node{Type: &types.Type{}, Name: &Name{}}, PAUTO),
markNeedZero(nodeWithClass(Node{Type: &types.Type{}}, PAUTO)),
nod(0, &types.Type{}, nil, PAUTO),
markNeedZero(nod(0, &types.Type{}, nil, PAUTO)),
false,
},
{
nodeWithClass(Node{Type: &types.Type{Width: 1}, Name: &Name{}}, PAUTO),
nodeWithClass(Node{Type: &types.Type{Width: 2}, Name: &Name{}}, PAUTO),
nod(0, &types.Type{Width: 1}, nil, PAUTO),
nod(0, &types.Type{Width: 2}, nil, PAUTO),
false,
},
{
nodeWithClass(Node{Type: &types.Type{Width: 2}, Name: &Name{}}, PAUTO),
nodeWithClass(Node{Type: &types.Type{Width: 1}, Name: &Name{}}, PAUTO),
nod(0, &types.Type{Width: 2}, nil, PAUTO),
nod(0, &types.Type{Width: 1}, nil, PAUTO),
true,
},
{
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{Name: "abc"}}, PAUTO),
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{Name: "xyz"}}, PAUTO),
nod(0, &types.Type{}, &types.Sym{Name: "abc"}, PAUTO),
nod(0, &types.Type{}, &types.Sym{Name: "xyz"}, PAUTO),
true,
},
{
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{Name: "abc"}}, PAUTO),
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{Name: "abc"}}, PAUTO),
nod(0, &types.Type{}, &types.Sym{Name: "abc"}, PAUTO),
nod(0, &types.Type{}, &types.Sym{Name: "abc"}, PAUTO),
false,
},
{
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{Name: "xyz"}}, PAUTO),
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{Name: "abc"}}, PAUTO),
nod(0, &types.Type{}, &types.Sym{Name: "xyz"}, PAUTO),
nod(0, &types.Type{}, &types.Sym{Name: "abc"}, PAUTO),
false,
},
}
@ -151,35 +155,42 @@ func TestCmpstackvar(t *testing.T) {
}
func TestStackvarSort(t *testing.T) {
nod := func(xoffset int64, t *types.Type, s *types.Sym, cl Class) *Node {
n := newname(s)
n.Type = t
n.Xoffset = xoffset
n.SetClass(cl)
return n
}
inp := []*Node{
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{}}, PFUNC),
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{}}, PAUTO),
nodeWithClass(Node{Xoffset: 0, Type: &types.Type{}, Sym: &types.Sym{}}, PFUNC),
nodeWithClass(Node{Xoffset: 10, Type: &types.Type{}, Sym: &types.Sym{}}, PFUNC),
nodeWithClass(Node{Xoffset: 20, Type: &types.Type{}, Sym: &types.Sym{}}, PFUNC),
markUsed(nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{}}, PAUTO)),
nodeWithClass(Node{Type: typeWithoutPointers(), Sym: &types.Sym{}}, PAUTO),
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{}}, PAUTO),
markNeedZero(nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{}}, PAUTO)),
nodeWithClass(Node{Type: &types.Type{Width: 1}, Sym: &types.Sym{}}, PAUTO),
nodeWithClass(Node{Type: &types.Type{Width: 2}, Sym: &types.Sym{}}, PAUTO),
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{Name: "abc"}}, PAUTO),
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{Name: "xyz"}}, PAUTO),
nod(0, &types.Type{}, &types.Sym{}, PFUNC),
nod(0, &types.Type{}, &types.Sym{}, PAUTO),
nod(0, &types.Type{}, &types.Sym{}, PFUNC),
nod(10, &types.Type{}, &types.Sym{}, PFUNC),
nod(20, &types.Type{}, &types.Sym{}, PFUNC),
markUsed(nod(0, &types.Type{}, &types.Sym{}, PAUTO)),
nod(0, typeWithoutPointers(), &types.Sym{}, PAUTO),
nod(0, &types.Type{}, &types.Sym{}, PAUTO),
markNeedZero(nod(0, &types.Type{}, &types.Sym{}, PAUTO)),
nod(0, &types.Type{Width: 1}, &types.Sym{}, PAUTO),
nod(0, &types.Type{Width: 2}, &types.Sym{}, PAUTO),
nod(0, &types.Type{}, &types.Sym{Name: "abc"}, PAUTO),
nod(0, &types.Type{}, &types.Sym{Name: "xyz"}, PAUTO),
}
want := []*Node{
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{}}, PFUNC),
nodeWithClass(Node{Xoffset: 0, Type: &types.Type{}, Sym: &types.Sym{}}, PFUNC),
nodeWithClass(Node{Xoffset: 10, Type: &types.Type{}, Sym: &types.Sym{}}, PFUNC),
nodeWithClass(Node{Xoffset: 20, Type: &types.Type{}, Sym: &types.Sym{}}, PFUNC),
markUsed(nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{}}, PAUTO)),
markNeedZero(nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{}}, PAUTO)),
nodeWithClass(Node{Type: &types.Type{Width: 2}, Sym: &types.Sym{}}, PAUTO),
nodeWithClass(Node{Type: &types.Type{Width: 1}, Sym: &types.Sym{}}, PAUTO),
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{}}, PAUTO),
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{}}, PAUTO),
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{Name: "abc"}}, PAUTO),
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{Name: "xyz"}}, PAUTO),
nodeWithClass(Node{Type: typeWithoutPointers(), Sym: &types.Sym{}}, PAUTO),
nod(0, &types.Type{}, &types.Sym{}, PFUNC),
nod(0, &types.Type{}, &types.Sym{}, PFUNC),
nod(10, &types.Type{}, &types.Sym{}, PFUNC),
nod(20, &types.Type{}, &types.Sym{}, PFUNC),
markUsed(nod(0, &types.Type{}, &types.Sym{}, PAUTO)),
markNeedZero(nod(0, &types.Type{}, &types.Sym{}, PAUTO)),
nod(0, &types.Type{Width: 2}, &types.Sym{}, PAUTO),
nod(0, &types.Type{Width: 1}, &types.Sym{}, PAUTO),
nod(0, &types.Type{}, &types.Sym{}, PAUTO),
nod(0, &types.Type{}, &types.Sym{}, PAUTO),
nod(0, &types.Type{}, &types.Sym{Name: "abc"}, PAUTO),
nod(0, &types.Type{}, &types.Sym{Name: "xyz"}, PAUTO),
nod(0, typeWithoutPointers(), &types.Sym{}, PAUTO),
}
sort.Sort(byStackVar(inp))
if !reflect.DeepEqual(want, inp) {