From 61e2b8ec598e33b0d55a0652f86eeb075de3dc9d Mon Sep 17 00:00:00 2001 From: Oleg Zaytsev Date: Fri, 9 Dec 2022 13:05:34 +0100 Subject: [PATCH] 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 TryBot-Result: Gopher Robot Reviewed-by: Keith Randall Reviewed-by: Than McIntosh Auto-Submit: Keith Randall Reviewed-by: Keith Randall --- src/runtime/string_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) 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) -- 2.48.1