File: unicode_test.go

package info (click to toggle)
golang-x-text 0.0~git20161013.0.c745997-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 17,868 kB
  • ctags: 6,073
  • sloc: makefile: 28
file content (178 lines) | stat: -rw-r--r-- 4,224 bytes parent folder | download | duplicates (3)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2015 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 unicode

import (
	"testing"

	"golang.org/x/text/transform"
)

func TestUTF8Decoder(t *testing.T) {
	testCases := []struct {
		desc    string
		src     string
		notEOF  bool // the inverse of atEOF
		sizeDst int
		want    string
		nSrc    int
		err     error
	}{{
		desc: "empty string, empty dest buffer",
	}, {
		desc:    "empty string",
		sizeDst: 8,
	}, {
		desc:    "empty string, streaming",
		notEOF:  true,
		sizeDst: 8,
	}, {
		desc:    "ascii",
		src:     "abcde",
		sizeDst: 8,
		want:    "abcde",
		nSrc:    5,
	}, {
		desc:    "ascii and error",
		src:     "ab\x80de",
		sizeDst: 7,
		want:    "ab\ufffdde",
		nSrc:    5,
	}, {
		desc:    "valid two-byte sequence",
		src:     "a\u0300bc",
		sizeDst: 7,
		want:    "a\u0300bc",
		nSrc:    5,
	}, {
		desc:    "valid three-byte sequence",
		src:     "a\u0300中",
		sizeDst: 7,
		want:    "a\u0300中",
		nSrc:    6,
	}, {
		desc:    "valid four-byte sequence",
		src:     "a中\U00016F50",
		sizeDst: 8,
		want:    "a中\U00016F50",
		nSrc:    8,
	}, {
		desc:    "short source buffer",
		src:     "abc\xf0\x90",
		notEOF:  true,
		sizeDst: 10,
		want:    "abc",
		nSrc:    3,
		err:     transform.ErrShortSrc,
	}, {
		// We don't check for the maximal subpart of an ill-formed subsequence
		// at the end of an open segment.
		desc:    "complete invalid that looks like short at end",
		src:     "abc\xf0\x80",
		notEOF:  true,
		sizeDst: 10,
		want:    "abc", // instead of "abc\ufffd\ufffd",
		nSrc:    3,
		err:     transform.ErrShortSrc,
	}, {
		desc:    "incomplete sequence at end",
		src:     "a\x80bc\xf0\x90",
		sizeDst: 9,
		want:    "a\ufffdbc\ufffd",
		nSrc:    6,
	}, {
		desc:    "invalid second byte",
		src:     "abc\xf0dddd",
		sizeDst: 10,
		want:    "abc\ufffddddd",
		nSrc:    8,
	}, {
		desc:    "invalid second byte at end",
		src:     "abc\xf0d",
		sizeDst: 10,
		want:    "abc\ufffdd",
		nSrc:    5,
	}, {
		desc:    "invalid third byte",
		src:     "a\u0300bc\xf0\x90dddd",
		sizeDst: 12,
		want:    "a\u0300bc\ufffddddd",
		nSrc:    11,
	}, {
		desc:    "invalid third byte at end",
		src:     "a\u0300bc\xf0\x90d",
		sizeDst: 12,
		want:    "a\u0300bc\ufffdd",
		nSrc:    8,
	}, {
		desc:    "invalid fourth byte, tight buffer",
		src:     "a\u0300bc\xf0\x90\x80d",
		sizeDst: 9,
		want:    "a\u0300bc\ufffdd",
		nSrc:    9,
	}, {
		desc:    "invalid fourth byte at end",
		src:     "a\u0300bc\xf0\x90\x80",
		sizeDst: 8,
		want:    "a\u0300bc\ufffd",
		nSrc:    8,
	}, {
		desc:    "invalid fourth byte and short four byte sequence",
		src:     "a\u0300bc\xf0\x90\x80\xf0\x90\x80",
		notEOF:  true,
		sizeDst: 20,
		want:    "a\u0300bc\ufffd",
		nSrc:    8,
		err:     transform.ErrShortSrc,
	}, {
		desc:    "valid four-byte sequence overflowing short buffer",
		src:     "a\u0300bc\xf0\x90\x80\x80",
		notEOF:  true,
		sizeDst: 8,
		want:    "a\u0300bc",
		nSrc:    5,
		err:     transform.ErrShortDst,
	}, {
		desc:    "invalid fourth byte at end short, but short dst",
		src:     "a\u0300bc\xf0\x90\x80\xf0\x90\x80",
		notEOF:  true,
		sizeDst: 8,
		// More bytes would fit in the buffer, but this seems to require a more
		// complicated and slower algorithm.
		want: "a\u0300bc", // instead of "a\u0300bc"
		nSrc: 5,
		err:  transform.ErrShortDst,
	}, {
		desc:    "short dst for error",
		src:     "abc\x80",
		notEOF:  true,
		sizeDst: 5,
		want:    "abc",
		nSrc:    3,
		err:     transform.ErrShortDst,
	}, {
		desc:    "adjusting short dst buffer",
		src:     "abc\x80ef",
		notEOF:  true,
		sizeDst: 6,
		want:    "abc\ufffd",
		nSrc:    4,
		err:     transform.ErrShortDst,
	}}
	tr := UTF8.NewDecoder()
	for i, tc := range testCases {
		b := make([]byte, tc.sizeDst)
		nDst, nSrc, err := tr.Transform(b, []byte(tc.src), !tc.notEOF)
		if err != tc.err {
			t.Errorf("%d:%s: error was %v; want %v", i, tc.desc, err, tc.err)
		}
		if got := string(b[:nDst]); got != tc.want {
			t.Errorf("%d:%s: result was %q: want %q", i, tc.desc, got, tc.want)
		}
		if nSrc != tc.nSrc {
			t.Errorf("%d:%s: nSrc was %d; want %d", i, tc.desc, nSrc, tc.nSrc)
		}
	}
}