Commit f482a0f0 authored by Håvard Haugen's avatar Håvard Haugen Committed by Dave Cheney

cmd/compile/internal/gc: add unit test for cmpstackvar

A followup CL will rewrite listsort to use the new cmpstackvarlt and
change cmpstackvar to avoid stringsCompare.

Change-Id: Idf0857a3bd67f9e2243ba82aa0bff510612927c3
Reviewed-on: https://go-review.googlesource.com/14611Reviewed-by: 's avatarDave Cheney <dave@cheney.net>
parent 2dc63d15
......@@ -164,6 +164,11 @@ func emitptrargsmap() {
ggloblsym(sym, int32(off), obj.RODATA|obj.LOCAL)
}
// cmpstackvarlt reports whether the stack variable a sorts before b.
func cmpstackvarlt(a, b *Node) bool {
return cmpstackvar(a, b) < 0
}
// Sort the list of stack variables. Autos after anything else,
// within autos, unused after used, within used, things with
// pointers first, zeroed things first, and then decreasing size.
......
// 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 "testing"
// Test all code paths for cmpstackvarlt.
func TestCmpstackvar(t *testing.T) {
testdata := []struct {
a, b Node
lt bool
}{
{
Node{Class: PAUTO},
Node{Class: PFUNC},
false,
},
{
Node{Class: PFUNC},
Node{Class: PAUTO},
true,
},
{
Node{Class: PFUNC, Xoffset: 0},
Node{Class: PFUNC, Xoffset: 10},
true,
},
{
Node{Class: PFUNC, Xoffset: 20},
Node{Class: PFUNC, Xoffset: 10},
false,
},
{
Node{Class: PFUNC, Xoffset: 10},
Node{Class: PFUNC, Xoffset: 10},
false,
},
{
Node{Class: PAUTO, Used: true},
Node{Class: PAUTO, Used: false},
true,
},
{
Node{Class: PAUTO, Used: false},
Node{Class: PAUTO, Used: true},
false,
},
{
Node{Class: PAUTO, Type: &Type{Haspointers: 1}}, // haspointers -> false
Node{Class: PAUTO, Type: &Type{Haspointers: 2}}, // haspointers -> true
false,
},
{
Node{Class: PAUTO, Type: &Type{Haspointers: 2}}, // haspointers -> true
Node{Class: PAUTO, Type: &Type{Haspointers: 1}}, // haspointers -> false
true,
},
{
Node{Class: PAUTO, Type: &Type{}, Name: &Name{Needzero: true}},
Node{Class: PAUTO, Type: &Type{}, Name: &Name{Needzero: false}},
true,
},
{
Node{Class: PAUTO, Type: &Type{}, Name: &Name{Needzero: false}},
Node{Class: PAUTO, Type: &Type{}, Name: &Name{Needzero: true}},
false,
},
{
Node{Class: PAUTO, Type: &Type{Width: 1}, Name: &Name{}},
Node{Class: PAUTO, Type: &Type{Width: 2}, Name: &Name{}},
false,
},
{
Node{Class: PAUTO, Type: &Type{Width: 2}, Name: &Name{}},
Node{Class: PAUTO, Type: &Type{Width: 1}, Name: &Name{}},
true,
},
{
Node{Class: PAUTO, Type: &Type{}, Name: &Name{}, Sym: &Sym{Name: "abc"}},
Node{Class: PAUTO, Type: &Type{}, Name: &Name{}, Sym: &Sym{Name: "xyz"}},
true,
},
{
Node{Class: PAUTO, Type: &Type{}, Name: &Name{}, Sym: &Sym{Name: "abc"}},
Node{Class: PAUTO, Type: &Type{}, Name: &Name{}, Sym: &Sym{Name: "abc"}},
false,
},
{
Node{Class: PAUTO, Type: &Type{}, Name: &Name{}, Sym: &Sym{Name: "xyz"}},
Node{Class: PAUTO, Type: &Type{}, Name: &Name{}, Sym: &Sym{Name: "abc"}},
false,
},
}
for _, d := range testdata {
got := cmpstackvarlt(&d.a, &d.b)
if got != d.lt {
t.Errorf("want %#v < %#v", d.a, d.b)
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment