[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
1 changed files with 79 additions and 68 deletions

View File

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