]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.regabi] cmd/compile: clean up tests to know less about Node
authorRuss Cox <rsc@golang.org>
Wed, 18 Nov 2020 20:14:24 +0000 (15:14 -0500)
committerRuss Cox <rsc@golang.org>
Tue, 24 Nov 2020 20:05:54 +0000 (20:05 +0000)
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>

src/cmd/compile/internal/gc/pgen_test.go

index b1db29825c2c1efa1bc84834cbfb5eb65af5e5f5..932ab47d02635996d4c940971dac7f6386160b66 100644 (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) {