diff options
author | Oleg Zaytsev <mail@olegzaytsev.com> | 2022-12-09 13:05:34 +0100 |
---|---|---|
committer | Gopher Robot <gobot@golang.org> | 2022-12-13 14:05:23 +0000 |
commit | 61e2b8ec598e33b0d55a0652f86eeb075de3dc9d (patch) | |
tree | 7371857254e9df4927c4d1101138f3f60a2319d4 | |
parent | b16e94d13d0f9b84ed92563a12984190f91ead66 (diff) | |
download | go-61e2b8ec598e33b0d55a0652f86eeb075de3dc9d.tar.gz go-61e2b8ec598e33b0d55a0652f86eeb075de3dc9d.zip |
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>
-rw-r--r-- | src/runtime/string_test.go | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/runtime/string_test.go b/src/runtime/string_test.go index 1ea7f5e481..cfc0ad7cde 100644 --- a/src/runtime/string_test.go +++ b/src/runtime/string_test.go @@ -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) { s := strings.Repeat("x", sizeNoStack) b := []byte(s) @@ -230,10 +243,24 @@ func TestCompareTempString(t *testing.T) { 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 { + 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 string(b) >= s { + } else { + t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s) + } }) if n != 0 { t.Fatalf("want 0 allocs, got %v", n) |