summaryrefslogtreecommitdiff
path: root/libgo/go/utf8/string_test.go
blob: 9dd847247306de6209e69d77c967c3da719367fc (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
// 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 utf8_test

import (
	"rand"
	"testing"
	. "utf8"
)

func TestScanForwards(t *testing.T) {
	for _, s := range testStrings {
		runes := []int(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 := []int(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)
			}
		}
	}
}

const randCount = 100000

func TestRandomAccess(t *testing.T) {
	for _, s := range testStrings {
		if len(s) == 0 {
			continue
		}
		runes := []int(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 := []int(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 := RuneCountInString(s)
		if str.Slice(nr, nr) != "" {
			t.Error("failure with empty slice at end")
		}
	}
}