]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/gc: test temp string comparison with all ops
authorOleg Zaytsev <mail@olegzaytsev.com>
Fri, 9 Dec 2022 12:05:34 +0000 (13:05 +0100)
committerGopher Robot <gobot@golang.org>
Tue, 13 Dec 2022 14:05:23 +0000 (14:05 +0000)
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>
src/runtime/string_test.go

index 1ea7f5e481cc0d49f09624881b58e36bb381cd2c..cfc0ad7cde87864195aaccf84c3aba0b61e13ec1 100644 (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) {
        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)