summaryrefslogtreecommitdiff
path: root/libgo/go/exp/utf8string/string_test.go
blob: 28511b2f5f159274bce88245387f6f317be27ff1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package utf8string

import (
	"math/rand"
	"testing"
	"unicode/utf8"
)

var testStrings = []string{
	"",
	"abcd",
	"☺☻☹",
	"日a本b語ç日ð本Ê語þ日¥本¼語i日©",
	"日a本b語ç日ð本Ê語þ日¥本¼語i日©日a本b語ç日ð本Ê語þ日¥本¼語i日©日a本b語ç日ð本Ê語þ日¥本¼語i日©",
	"\x80\x80\x80\x80",
}

func TestScanForwards(t *testing.T) {
	for _, s := range testStrings {
		runes := []rune(s)
		str := NewString(s)
		if str.RuneCount() != len(runes) {
			t.Errorf("%s: expected %d runes; got %d", s, len(runes), str.RuneCount())
			break
		}
		for i, expect := range runes {
			got := str.At(i)
			if got != expect {
				t.Errorf("%s[%d]: expected %c (%U); got %c (%U)", s, i, expect, expect, got, got)
			}
		}
	}
}

func TestScanBackwards(t *testing.T) {
	for _, s := range testStrings {
		runes := []rune(s)
		str := NewString(s)
		if str.RuneCount() != len(runes) {
			t.Errorf("%s: expected %d runes; got %d", s, len(runes), str.RuneCount())
			break
		}
		for i := len(runes) - 1; i >= 0; i-- {
			expect := runes[i]
			got := str.At(i)
			if got != expect {
				t.Errorf("%s[%d]: expected %c (%U); got %c (%U)", s, i, expect, expect, got, got)
			}
		}
	}
}

func randCount() int {
	if testing.Short() {
		return 100
	}
	return 100000
}

func TestRandomAccess(t *testing.T) {
	for _, s := range testStrings {
		if len(s) == 0 {
			continue
		}
		runes := []rune(s)
		str := NewString(s)
		if str.RuneCount() != len(runes) {
			t.Errorf("%s: expected %d runes; got %d", s, len(runes), str.RuneCount())
			break
		}
		for j := 0; j < randCount(); j++ {
			i := rand.Intn(len(runes))
			expect := runes[i]
			got := str.At(i)
			if got != expect {
				t.Errorf("%s[%d]: expected %c (%U); got %c (%U)", s, i, expect, expect, got, got)
			}
		}
	}
}

func TestRandomSliceAccess(t *testing.T) {
	for _, s := range testStrings {
		if len(s) == 0 || s[0] == '\x80' { // the bad-UTF-8 string fools this simple test
			continue
		}
		runes := []rune(s)
		str := NewString(s)
		if str.RuneCount() != len(runes) {
			t.Errorf("%s: expected %d runes; got %d", s, len(runes), str.RuneCount())
			break
		}
		for k := 0; k < randCount(); k++ {
			i := rand.Intn(len(runes))
			j := rand.Intn(len(runes) + 1)
			if i > j { // include empty strings
				continue
			}
			expect := string(runes[i:j])
			got := str.Slice(i, j)
			if got != expect {
				t.Errorf("%s[%d:%d]: expected %q got %q", s, i, j, expect, got)
			}
		}
	}
}

func TestLimitSliceAccess(t *testing.T) {
	for _, s := range testStrings {
		str := NewString(s)
		if str.Slice(0, 0) != "" {
			t.Error("failure with empty slice at beginning")
		}
		nr := utf8.RuneCountInString(s)
		if str.Slice(nr, nr) != "" {
			t.Error("failure with empty slice at end")
		}
	}
}