mirror of
https://github.com/golang/go
synced 2024-11-17 22:05:02 -07:00
cmd/compile/internal/gc: remove stringsCompare
Inlined the last occurrence of stringsCompare into exprcmp. Passes go build -a -toolexec 'toolstash -cmp' std cmd. Change-Id: I8fd99e3fbffc84283cc269368595cba950533066 Reviewed-on: https://go-review.googlesource.com/14872 Reviewed-by: Dave Cheney <dave@cheney.net> Run-TryBot: Dave Cheney <dave@cheney.net> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
0722a5e718
commit
c73df92be6
@ -778,7 +778,13 @@ func exprcmp(c1, c2 *caseClause) int {
|
||||
if len(a) > len(b) {
|
||||
return +1
|
||||
}
|
||||
return stringsCompare(a, b)
|
||||
if a == b {
|
||||
return 0
|
||||
}
|
||||
if a < b {
|
||||
return -1
|
||||
}
|
||||
return +1
|
||||
}
|
||||
|
||||
return 0
|
||||
|
144
src/cmd/compile/internal/gc/swt_test.go
Normal file
144
src/cmd/compile/internal/gc/swt_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
// 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/big"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestExprcmp(t *testing.T) {
|
||||
testdata := []struct {
|
||||
a, b caseClause
|
||||
want int
|
||||
}{
|
||||
// Non-constants.
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nil, nil), typ: caseKindExprVar},
|
||||
caseClause{node: Nod(OXXX, nil, nil), typ: caseKindExprConst},
|
||||
+1,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nil, nil), typ: caseKindExprConst},
|
||||
caseClause{node: Nod(OXXX, nil, nil), typ: caseKindExprVar},
|
||||
-1,
|
||||
},
|
||||
// Type switches
|
||||
{
|
||||
caseClause{node: Nod(OXXX, Nodintconst(0), nil), typ: caseKindExprConst},
|
||||
caseClause{node: Nod(OXXX, Nodbool(true), nil), typ: caseKindExprConst},
|
||||
-1,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, Nodbool(true), nil), typ: caseKindExprConst},
|
||||
caseClause{node: Nod(OXXX, Nodintconst(1), nil), typ: caseKindExprConst},
|
||||
+1,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, &Node{Type: &Type{Etype: TBOOL, Vargen: 1}}, nil), typ: caseKindExprConst},
|
||||
caseClause{node: Nod(OXXX, &Node{Type: &Type{Etype: TINT, Vargen: 0}}, nil), typ: caseKindExprConst},
|
||||
+1,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, &Node{Type: &Type{Etype: TBOOL, Vargen: 1}}, nil), typ: caseKindExprConst},
|
||||
caseClause{node: Nod(OXXX, &Node{Type: &Type{Etype: TINT, Vargen: 1}}, nil), typ: caseKindExprConst},
|
||||
-1,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, &Node{Type: &Type{Etype: TBOOL, Vargen: 0}}, nil), typ: caseKindExprConst},
|
||||
caseClause{node: Nod(OXXX, &Node{Type: &Type{Etype: TINT, Vargen: 1}}, nil), typ: caseKindExprConst},
|
||||
-1,
|
||||
},
|
||||
// Constant values.
|
||||
// CTFLT
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpflt{Val: *big.NewFloat(0.1)}}), nil), typ: caseKindExprConst},
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpflt{Val: *big.NewFloat(0.2)}}), nil), typ: caseKindExprConst},
|
||||
-1,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpflt{Val: *big.NewFloat(0.1)}}), nil), typ: caseKindExprConst},
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpflt{Val: *big.NewFloat(0.1)}}), nil), typ: caseKindExprConst},
|
||||
0,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpflt{Val: *big.NewFloat(0.2)}}), nil), typ: caseKindExprConst},
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpflt{Val: *big.NewFloat(0.1)}}), nil), typ: caseKindExprConst},
|
||||
+1,
|
||||
},
|
||||
// CTINT
|
||||
{
|
||||
caseClause{node: Nod(OXXX, Nodintconst(0), nil), typ: caseKindExprConst},
|
||||
caseClause{node: Nod(OXXX, Nodintconst(1), nil), typ: caseKindExprConst},
|
||||
-1,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, Nodintconst(1), nil), typ: caseKindExprConst},
|
||||
caseClause{node: Nod(OXXX, Nodintconst(1), nil), typ: caseKindExprConst},
|
||||
0,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, Nodintconst(1), nil), typ: caseKindExprConst},
|
||||
caseClause{node: Nod(OXXX, Nodintconst(0), nil), typ: caseKindExprConst},
|
||||
+1,
|
||||
},
|
||||
// CTRUNE
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpint{Val: *big.NewInt('a'), Rune: true}}), nil), typ: caseKindExprConst},
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpint{Val: *big.NewInt('b'), Rune: true}}), nil), typ: caseKindExprConst},
|
||||
-1,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpint{Val: *big.NewInt('b'), Rune: true}}), nil), typ: caseKindExprConst},
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpint{Val: *big.NewInt('b'), Rune: true}}), nil), typ: caseKindExprConst},
|
||||
0,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpint{Val: *big.NewInt('b'), Rune: true}}), nil), typ: caseKindExprConst},
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{&Mpint{Val: *big.NewInt('a'), Rune: true}}), nil), typ: caseKindExprConst},
|
||||
+1,
|
||||
},
|
||||
// CTSTR
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{"ab"}), nil), typ: caseKindExprConst},
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{"abc"}), nil), typ: caseKindExprConst},
|
||||
-1,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{"abc"}), nil), typ: caseKindExprConst},
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{"xyz"}), nil), typ: caseKindExprConst},
|
||||
-1,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{"abc"}), nil), typ: caseKindExprConst},
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{"abc"}), nil), typ: caseKindExprConst},
|
||||
0,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{"abc"}), nil), typ: caseKindExprConst},
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{"ab"}), nil), typ: caseKindExprConst},
|
||||
+1,
|
||||
},
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{"xyz"}), nil), typ: caseKindExprConst},
|
||||
caseClause{node: Nod(OXXX, nodlit(Val{"abc"}), nil), typ: caseKindExprConst},
|
||||
+1,
|
||||
},
|
||||
// Everything else should compare equal.
|
||||
{
|
||||
caseClause{node: Nod(OXXX, nodnil(), nil), typ: caseKindExprConst},
|
||||
caseClause{node: Nod(OXXX, nodnil(), nil), typ: caseKindExprConst},
|
||||
0,
|
||||
},
|
||||
}
|
||||
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, typ: %#v}", d.a.node, d.a.typ)
|
||||
t.Logf("\tb = caseClause{node: %#v, typ: %#v}", d.b.node, d.b.typ)
|
||||
}
|
||||
}
|
||||
}
|
@ -17,17 +17,6 @@ func atoi(s string) int {
|
||||
return int(n)
|
||||
}
|
||||
|
||||
// strings.Compare, introduced in Go 1.5.
|
||||
func stringsCompare(a, b string) int {
|
||||
if a == b {
|
||||
return 0
|
||||
}
|
||||
if a < b {
|
||||
return -1
|
||||
}
|
||||
return +1
|
||||
}
|
||||
|
||||
var atExitFuncs []func()
|
||||
|
||||
func AtExit(f func()) {
|
||||
|
Loading…
Reference in New Issue
Block a user