1
0
mirror of https://github.com/golang/go synced 2024-11-17 08:04:46 -07:00

cmd/gc: test temp string comparison with all ops

The comment on `slicebytetostringtmp` mention that `==` operator does
not allocate []byte to string conversion, but the test was testing only
`==` and `!=` and the compiler actually optimizes all comparison
operators.

Also added a test for concatenation comparison, which also should not
allocate.

Change-Id: I6f4c5c4f238808138fa901732e1fd5b6ab25f725
Reviewed-on: https://go-review.googlesource.com/c/go/+/456415
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
Oleg Zaytsev 2022-12-09 13:05:34 +01:00 committed by Gopher Robot
parent b16e94d13d
commit 61e2b8ec59

View File

@ -223,6 +223,19 @@ func TestLargeStringConcat(t *testing.T) {
} }
} }
func TestConcatTempString(t *testing.T) {
s := "bytes"
b := []byte(s)
n := testing.AllocsPerRun(1000, func() {
if "prefix "+string(b)+" suffix" != "prefix bytes suffix" {
t.Fatalf("strings are not equal: '%v' and '%v'", "prefix "+string(b)+" suffix", "prefix bytes suffix")
}
})
if n != 0 {
t.Fatalf("want 0 allocs, got %v", n)
}
}
func TestCompareTempString(t *testing.T) { func TestCompareTempString(t *testing.T) {
s := strings.Repeat("x", sizeNoStack) s := strings.Repeat("x", sizeNoStack)
b := []byte(s) b := []byte(s)
@ -230,10 +243,24 @@ func TestCompareTempString(t *testing.T) {
if string(b) != s { if string(b) != s {
t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s) t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s)
} }
if string(b) < s {
t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s)
}
if string(b) > s {
t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s)
}
if string(b) == s { if string(b) == s {
} else { } else {
t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s) t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s)
} }
if string(b) <= s {
} else {
t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s)
}
if string(b) >= s {
} else {
t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s)
}
}) })
if n != 0 { if n != 0 {
t.Fatalf("want 0 allocs, got %v", n) t.Fatalf("want 0 allocs, got %v", n)