fix for merge conflict
[official-gcc.git] / libgo / go / utf8 / string_test.go
blob484d46fbffde68855c1a8e68662502aa7f033055
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package utf8_test
7 import (
8 "rand"
9 "testing"
10 . "utf8"
13 func TestScanForwards(t *testing.T) {
14 for _, s := range testStrings {
15 runes := []int(s)
16 str := NewString(s)
17 if str.RuneCount() != len(runes) {
18 t.Error("%s: expected %d runes; got %d", s, len(runes), str.RuneCount())
19 break
21 for i, expect := range runes {
22 got := str.At(i)
23 if got != expect {
24 t.Errorf("%s[%d]: expected %c (U+%04x); got %c (U+%04x)", s, i, expect, expect, got, got)
30 func TestScanBackwards(t *testing.T) {
31 for _, s := range testStrings {
32 runes := []int(s)
33 str := NewString(s)
34 if str.RuneCount() != len(runes) {
35 t.Error("%s: expected %d runes; got %d", s, len(runes), str.RuneCount())
36 break
38 for i := len(runes) - 1; i >= 0; i-- {
39 expect := runes[i]
40 got := str.At(i)
41 if got != expect {
42 t.Errorf("%s[%d]: expected %c (U+%04x); got %c (U+%04x)", s, i, expect, expect, got, got)
48 const randCount = 100000
50 func TestRandomAccess(t *testing.T) {
51 for _, s := range testStrings {
52 if len(s) == 0 {
53 continue
55 runes := []int(s)
56 str := NewString(s)
57 if str.RuneCount() != len(runes) {
58 t.Error("%s: expected %d runes; got %d", s, len(runes), str.RuneCount())
59 break
61 for j := 0; j < randCount; j++ {
62 i := rand.Intn(len(runes))
63 expect := runes[i]
64 got := str.At(i)
65 if got != expect {
66 t.Errorf("%s[%d]: expected %c (U+%04x); got %c (U+%04x)", s, i, expect, expect, got, got)
72 func TestRandomSliceAccess(t *testing.T) {
73 for _, s := range testStrings {
74 if len(s) == 0 || s[0] == '\x80' { // the bad-UTF-8 string fools this simple test
75 continue
77 runes := []int(s)
78 str := NewString(s)
79 if str.RuneCount() != len(runes) {
80 t.Error("%s: expected %d runes; got %d", s, len(runes), str.RuneCount())
81 break
83 for k := 0; k < randCount; k++ {
84 i := rand.Intn(len(runes))
85 j := rand.Intn(len(runes) + 1)
86 if i > j { // include empty strings
87 continue
89 expect := string(runes[i:j])
90 got := str.Slice(i, j)
91 if got != expect {
92 t.Errorf("%s[%d:%d]: expected %q got %q", s, i, j, expect, got)
98 func TestLimitSliceAccess(t *testing.T) {
99 for _, s := range testStrings {
100 str := NewString(s)
101 if str.Slice(0, 0) != "" {
102 t.Error("failure with empty slice at beginning")
104 nr := RuneCountInString(s)
105 if str.Slice(nr, nr) != "" {
106 t.Error("failure with empty slice at end")