mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/compile: simplify constant switch case sorting
This sort is now only reachable for constant clauses for a non-interface switch expression value. Refactor a bit so that the few tests that remain are concise and easy to read. Add a test that string length takes priority over built-in string order. Change-Id: Iedaa11ff77049d5ad1bf14f54cbb8c3411d589a7 Reviewed-on: https://go-review.googlesource.com/26767 Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
parent
e26499153e
commit
a9266eef93
2 changed files with 48 additions and 182 deletions
|
|
@ -9,136 +9,38 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
func TestExprcmp(t *testing.T) {
|
||||
testdata := []struct {
|
||||
a, b caseClause
|
||||
want int
|
||||
func nodrune(r rune) *Node {
|
||||
return nodlit(Val{&Mpint{Val: *big.NewInt(int64(r)), Rune: true}})
|
||||
}
|
||||
|
||||
func nodflt(f float64) *Node {
|
||||
return nodlit(Val{&Mpflt{Val: *big.NewFloat(f)}})
|
||||
}
|
||||
|
||||
func TestCaseClauseByConstVal(t *testing.T) {
|
||||
tests := []struct {
|
||||
a, b *Node
|
||||
}{
|
||||
// Non-constants.
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nil, nil)},
|
||||
caseClause{node: Nod(OXXX, nil, nil), isconst: true},
|
||||
+1,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nil, nil), isconst: true},
|
||||
caseClause{node: Nod(OXXX, nil, nil)},
|
||||
-1,
|
||||
},
|
||||
// Type switches
|
||||
{
|
||||
caseClause{node: Nod(OXXX, Nodintconst(0), nil), isconst: true},
|
||||
caseClause{node: Nod(OXXX, Nodbool(true), nil), isconst: true},
|
||||
-1,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, Nodbool(true), nil), isconst: true},
|
||||
caseClause{node: Nod(OXXX, Nodintconst(1), nil), isconst: true},
|
||||
+1,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, &Node{Type: &Type{Etype: TBOOL, Vargen: 1}}, nil), isconst: true},
|
||||
caseClause{node: Nod(OXXX, &Node{Type: &Type{Etype: TINT, Vargen: 0}}, nil), isconst: true},
|
||||
+1,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, &Node{Type: &Type{Etype: TBOOL, Vargen: 1}}, nil), isconst: true},
|
||||
caseClause{node: Nod(OXXX, &Node{Type: &Type{Etype: TINT, Vargen: 1}}, nil), isconst: true},
|
||||
-1,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, &Node{Type: &Type{Etype: TBOOL, Vargen: 0}}, nil), isconst: true},
|
||||
caseClause{node: Nod(OXXX, &Node{Type: &Type{Etype: TINT, Vargen: 1}}, nil), isconst: true},
|
||||
-1,
|
||||
},
|
||||
// Constant values.
|
||||
// CTFLT
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpflt{Val: *big.NewFloat(0.1)}}), nil), isconst: true},
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpflt{Val: *big.NewFloat(0.2)}}), nil), isconst: true},
|
||||
-1,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpflt{Val: *big.NewFloat(0.1)}}), nil), isconst: true},
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpflt{Val: *big.NewFloat(0.1)}}), nil), isconst: true},
|
||||
0,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpflt{Val: *big.NewFloat(0.2)}}), nil), isconst: true},
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpflt{Val: *big.NewFloat(0.1)}}), nil), isconst: true},
|
||||
+1,
|
||||
},
|
||||
{nodflt(0.1), nodflt(0.2)},
|
||||
// CTINT
|
||||
{
|
||||
caseClause{node: Nod(OXXX, Nodintconst(0), nil), isconst: true},
|
||||
caseClause{node: Nod(OXXX, Nodintconst(1), nil), isconst: true},
|
||||
-1,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, Nodintconst(1), nil), isconst: true},
|
||||
caseClause{node: Nod(OXXX, Nodintconst(1), nil), isconst: true},
|
||||
0,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, Nodintconst(1), nil), isconst: true},
|
||||
caseClause{node: Nod(OXXX, Nodintconst(0), nil), isconst: true},
|
||||
+1,
|
||||
},
|
||||
{Nodintconst(0), Nodintconst(1)},
|
||||
// CTRUNE
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpint{Val: *big.NewInt('a'), Rune: true}}), nil), isconst: true},
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpint{Val: *big.NewInt('b'), Rune: true}}), nil), isconst: true},
|
||||
-1,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpint{Val: *big.NewInt('b'), Rune: true}}), nil), isconst: true},
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpint{Val: *big.NewInt('b'), Rune: true}}), nil), isconst: true},
|
||||
0,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpint{Val: *big.NewInt('b'), Rune: true}}), nil), isconst: true},
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpint{Val: *big.NewInt('a'), Rune: true}}), nil), isconst: true},
|
||||
+1,
|
||||
},
|
||||
{nodrune('a'), nodrune('b')},
|
||||
// CTSTR
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{"ab"}), nil), isconst: true},
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{"abc"}), nil), isconst: true},
|
||||
-1,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{"abc"}), nil), isconst: true},
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{"xyz"}), nil), isconst: true},
|
||||
-1,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{"abc"}), nil), isconst: true},
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{"abc"}), nil), isconst: true},
|
||||
0,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{"abc"}), nil), isconst: true},
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{"ab"}), nil), isconst: true},
|
||||
+1,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{"xyz"}), nil), isconst: true},
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{"abc"}), nil), isconst: true},
|
||||
+1,
|
||||
},
|
||||
// Everything else should compare equal.
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodnil(), nil), isconst: true},
|
||||
caseClause{node: Nod(OXXX, nodnil(), nil), isconst: true},
|
||||
0,
|
||||
},
|
||||
{nodlit(Val{"ab"}), nodlit(Val{"abc"})},
|
||||
{nodlit(Val{"ab"}), nodlit(Val{"xyz"})},
|
||||
{nodlit(Val{"abc"}), nodlit(Val{"xyz"})},
|
||||
}
|
||||
for i, d := range testdata {
|
||||
got := exprcmp(d.a, d.b)
|
||||
if d.want != got {
|
||||
t.Errorf("%d: exprcmp(a, b) = %d; want %d", i, got, d.want)
|
||||
t.Logf("\ta = caseClause{node: %#v, isconst: %v}", d.a.node, d.a.isconst)
|
||||
t.Logf("\tb = caseClause{node: %#v, isconst: %v}", d.b.node, d.b.isconst)
|
||||
for i, test := range tests {
|
||||
a := caseClause{node: Nod(OXXX, test.a, nil)}
|
||||
b := caseClause{node: Nod(OXXX, test.b, nil)}
|
||||
s := caseClauseByConstVal{a, b}
|
||||
if less := s.Less(0, 1); !less {
|
||||
t.Errorf("%d: caseClauseByConstVal(%v, %v) = false", i, test.a, test.b)
|
||||
}
|
||||
if less := s.Less(1, 0); less {
|
||||
t.Errorf("%d: caseClauseByConstVal(%v, %v) = true", i, test.a, test.b)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue