summaryrefslogtreecommitdiffstats
path: root/src/cmd/compile/internal/gc/pgen_test.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 13:14:23 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 13:14:23 +0000
commit73df946d56c74384511a194dd01dbe099584fd1a (patch)
treefd0bcea490dd81327ddfbb31e215439672c9a068 /src/cmd/compile/internal/gc/pgen_test.go
parentInitial commit. (diff)
downloadgolang-1.16-upstream.tar.xz
golang-1.16-upstream.zip
Adding upstream version 1.16.10.upstream/1.16.10upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/cmd/compile/internal/gc/pgen_test.go')
-rw-r--r--src/cmd/compile/internal/gc/pgen_test.go196
1 files changed, 196 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/gc/pgen_test.go b/src/cmd/compile/internal/gc/pgen_test.go
new file mode 100644
index 0000000..b1db298
--- /dev/null
+++ b/src/cmd/compile/internal/gc/pgen_test.go
@@ -0,0 +1,196 @@
+// Copyright 2015 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.
+
+package gc
+
+import (
+ "cmd/compile/internal/types"
+ "reflect"
+ "sort"
+ "testing"
+)
+
+func typeWithoutPointers() *types.Type {
+ t := types.New(TSTRUCT)
+ f := &types.Field{Type: types.New(TINT)}
+ t.SetFields([]*types.Field{f})
+ return t
+}
+
+func typeWithPointers() *types.Type {
+ t := types.New(TSTRUCT)
+ f := &types.Field{Type: types.NewPtr(types.New(TINT))}
+ t.SetFields([]*types.Field{f})
+ return t
+}
+
+func markUsed(n *Node) *Node {
+ n.Name.SetUsed(true)
+ return n
+}
+
+func markNeedZero(n *Node) *Node {
+ n.Name.SetNeedzero(true)
+ 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) {
+ testdata := []struct {
+ a, b *Node
+ lt bool
+ }{
+ {
+ nodeWithClass(Node{}, PAUTO),
+ nodeWithClass(Node{}, PFUNC),
+ false,
+ },
+ {
+ nodeWithClass(Node{}, PFUNC),
+ nodeWithClass(Node{}, PAUTO),
+ true,
+ },
+ {
+ nodeWithClass(Node{Xoffset: 0}, PFUNC),
+ nodeWithClass(Node{Xoffset: 10}, PFUNC),
+ true,
+ },
+ {
+ nodeWithClass(Node{Xoffset: 20}, PFUNC),
+ nodeWithClass(Node{Xoffset: 10}, PFUNC),
+ false,
+ },
+ {
+ nodeWithClass(Node{Xoffset: 10}, PFUNC),
+ nodeWithClass(Node{Xoffset: 10}, PFUNC),
+ false,
+ },
+ {
+ nodeWithClass(Node{Xoffset: 10}, PPARAM),
+ nodeWithClass(Node{Xoffset: 20}, PPARAMOUT),
+ true,
+ },
+ {
+ nodeWithClass(Node{Xoffset: 10}, PPARAMOUT),
+ nodeWithClass(Node{Xoffset: 20}, PPARAM),
+ true,
+ },
+ {
+ markUsed(nodeWithClass(Node{}, PAUTO)),
+ nodeWithClass(Node{}, PAUTO),
+ true,
+ },
+ {
+ nodeWithClass(Node{}, PAUTO),
+ markUsed(nodeWithClass(Node{}, PAUTO)),
+ false,
+ },
+ {
+ nodeWithClass(Node{Type: typeWithoutPointers()}, PAUTO),
+ nodeWithClass(Node{Type: typeWithPointers()}, PAUTO),
+ false,
+ },
+ {
+ nodeWithClass(Node{Type: typeWithPointers()}, PAUTO),
+ nodeWithClass(Node{Type: typeWithoutPointers()}, PAUTO),
+ true,
+ },
+ {
+ markNeedZero(nodeWithClass(Node{Type: &types.Type{}}, PAUTO)),
+ nodeWithClass(Node{Type: &types.Type{}, Name: &Name{}}, PAUTO),
+ true,
+ },
+ {
+ nodeWithClass(Node{Type: &types.Type{}, Name: &Name{}}, PAUTO),
+ markNeedZero(nodeWithClass(Node{Type: &types.Type{}}, PAUTO)),
+ false,
+ },
+ {
+ nodeWithClass(Node{Type: &types.Type{Width: 1}, Name: &Name{}}, PAUTO),
+ nodeWithClass(Node{Type: &types.Type{Width: 2}, Name: &Name{}}, PAUTO),
+ false,
+ },
+ {
+ nodeWithClass(Node{Type: &types.Type{Width: 2}, Name: &Name{}}, PAUTO),
+ nodeWithClass(Node{Type: &types.Type{Width: 1}, Name: &Name{}}, PAUTO),
+ true,
+ },
+ {
+ nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{Name: "abc"}}, PAUTO),
+ nodeWithClass(Node{Type: &types.Type{}, Sym: &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),
+ false,
+ },
+ {
+ nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{Name: "xyz"}}, PAUTO),
+ nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{Name: "abc"}}, PAUTO),
+ false,
+ },
+ }
+ for _, d := range testdata {
+ got := cmpstackvarlt(d.a, d.b)
+ if got != d.lt {
+ t.Errorf("want %#v < %#v", d.a, d.b)
+ }
+ // If we expect a < b to be true, check that b < a is false.
+ if d.lt && cmpstackvarlt(d.b, d.a) {
+ t.Errorf("unexpected %#v < %#v", d.b, d.a)
+ }
+ }
+}
+
+func TestStackvarSort(t *testing.T) {
+ 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),
+ }
+ 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),
+ }
+ sort.Sort(byStackVar(inp))
+ if !reflect.DeepEqual(want, inp) {
+ t.Error("sort failed")
+ for i := range inp {
+ g := inp[i]
+ w := want[i]
+ eq := reflect.DeepEqual(w, g)
+ if !eq {
+ t.Log(i, w, g)
+ }
+ }
+ }
+}