summaryrefslogtreecommitdiff
path: root/libgo/go/strings/strings_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'libgo/go/strings/strings_test.go')
-rw-r--r--libgo/go/strings/strings_test.go48
1 files changed, 44 insertions, 4 deletions
diff --git a/libgo/go/strings/strings_test.go b/libgo/go/strings/strings_test.go
index 3f0c7909094..ae17eba8c93 100644
--- a/libgo/go/strings/strings_test.go
+++ b/libgo/go/strings/strings_test.go
@@ -11,6 +11,7 @@ import (
"math/rand"
"reflect"
"runtime"
+ "strconv"
. "strings"
"testing"
"unicode"
@@ -649,10 +650,10 @@ func TestMap(t *testing.T) {
if unicode.Is(unicode.Latin, r) {
return r
}
- return '?'
+ return utf8.RuneError
}
m = Map(replaceNotLatin, "Hello\255World")
- expect = "Hello?World"
+ expect = "Hello\uFFFDWorld"
if m != expect {
t.Errorf("replace invalid sequence: expected %q got %q", expect, m)
}
@@ -677,6 +678,19 @@ func TestMap(t *testing.T) {
if m != s {
t.Errorf("encoding not handled correctly: expected %q got %q", s, m)
}
+
+ // 9. Check mapping occurs in the front, middle and back
+ trimSpaces := func(r rune) rune {
+ if unicode.IsSpace(r) {
+ return -1
+ }
+ return r
+ }
+ m = Map(trimSpaces, " abc 123 ")
+ expect = "abc123"
+ if m != expect {
+ t.Errorf("trimSpaces: expected %q got %q", expect, m)
+ }
}
func TestToUpper(t *testing.T) { runStringTests(t, ToUpper, "ToUpper", upperTests) }
@@ -1233,6 +1247,12 @@ func TestReplace(t *testing.T) {
if s := Replace(tt.in, tt.old, tt.new, tt.n); s != tt.out {
t.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt.in, tt.old, tt.new, tt.n, s, tt.out)
}
+ if tt.n == -1 {
+ s := ReplaceAll(tt.in, tt.old, tt.new)
+ if s != tt.out {
+ t.Errorf("ReplaceAll(%q, %q, %q) = %q, want %q", tt.in, tt.old, tt.new, s, tt.out)
+ }
+ }
}
}
@@ -1651,8 +1671,15 @@ func BenchmarkSplitNMultiByteSeparator(b *testing.B) {
}
func BenchmarkRepeat(b *testing.B) {
- for i := 0; i < b.N; i++ {
- Repeat("-", 80)
+ s := "0123456789"
+ for _, n := range []int{5, 10} {
+ for _, c := range []int{1, 2, 6} {
+ b.Run(fmt.Sprintf("%dx%d", n, c), func(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ Repeat(s[:n], c)
+ }
+ })
+ }
}
}
@@ -1695,3 +1722,16 @@ func BenchmarkIndexPeriodic(b *testing.B) {
})
}
}
+
+func BenchmarkJoin(b *testing.B) {
+ vals := []string{"red", "yellow", "pink", "green", "purple", "orange", "blue"}
+ for l := 0; l <= len(vals); l++ {
+ b.Run(strconv.Itoa(l), func(b *testing.B) {
+ b.ReportAllocs()
+ vals := vals[:l]
+ for i := 0; i < b.N; i++ {
+ Join(vals, " and ")
+ }
+ })
+ }
+}