Skip to content

Commit 61e2b8e

Browse files
colegagopherbot
authored andcommitted
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 <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Than McIntosh <[email protected]> Auto-Submit: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent b16e94d commit 61e2b8e

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/runtime/string_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,44 @@ func TestLargeStringConcat(t *testing.T) {
223223
}
224224
}
225225

226+
func TestConcatTempString(t *testing.T) {
227+
s := "bytes"
228+
b := []byte(s)
229+
n := testing.AllocsPerRun(1000, func() {
230+
if "prefix "+string(b)+" suffix" != "prefix bytes suffix" {
231+
t.Fatalf("strings are not equal: '%v' and '%v'", "prefix "+string(b)+" suffix", "prefix bytes suffix")
232+
}
233+
})
234+
if n != 0 {
235+
t.Fatalf("want 0 allocs, got %v", n)
236+
}
237+
}
238+
226239
func TestCompareTempString(t *testing.T) {
227240
s := strings.Repeat("x", sizeNoStack)
228241
b := []byte(s)
229242
n := testing.AllocsPerRun(1000, func() {
230243
if string(b) != s {
231244
t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s)
232245
}
246+
if string(b) < s {
247+
t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s)
248+
}
249+
if string(b) > s {
250+
t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s)
251+
}
233252
if string(b) == s {
234253
} else {
235254
t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s)
236255
}
256+
if string(b) <= s {
257+
} else {
258+
t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s)
259+
}
260+
if string(b) >= s {
261+
} else {
262+
t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s)
263+
}
237264
})
238265
if n != 0 {
239266
t.Fatalf("want 0 allocs, got %v", n)

0 commit comments

Comments
 (0)