Debian Codegraph @

encode_test.go

raw symbols SCIP
1 package scsu
2
3 import (
4 "bytes"
5 "errors"
6 "fmt"
7 "testing"
8 )
9
10 const (
11 referenceString = " ♪リンゴ可愛いや可愛いやリンゴ。半世紀も前に流行した「リンゴの歌」がぴったりするかもしれない。米アップルコンピュータ社のパソコン「マック(マッキントッシュ)」を、こよなく愛する人たちのことだ。「アップル信者」なんて言い方まである。"
12 )
13
14 func TestWriteString(t *testing.T) {
15 var b bytes.Buffer
16 e := NewWriter(&b)
17 n, err := e.WriteString("Москва")
18 if err != nil {
19 t.Fatal(err)
20 }
21 if n != 7 {
22 t.Fatalf("Unexpected len: %d", n)
23 }
24 if !bytes.Equal(b.Bytes(), []byte{0x12, 0x9C, 0xBE, 0xC1, 0xBA, 0xB2, 0xB0}) {
25 t.Fatalf("Content does not match: %v", b.Bytes())
26 }
27 }
28
29 func TestWriteRune(t *testing.T) {
30 var b bytes.Buffer
31 e := NewWriter(&b)
32 n, err := e.WriteRune('\u041C')
33 if err != nil {
34 t.Fatal(err)
35 }
36 if n != 2 {
37 t.Fatalf("Unexpected len: %d", n)
38 }
39 if !bytes.Equal(b.Bytes(), []byte{0x12, 0x9C}) {
40 t.Fatalf("Content does not match: %v", b.Bytes())
41 }
42 }
43
44 func TestEncodeRuneSlice(t *testing.T) {
45 var b bytes.Buffer
46 e := NewWriter(&b)
47 n, err := e.WriteRunes(RuneSlice([]rune("Москва")))
48 if err != nil {
49 t.Fatal(err)
50 }
51 if n != 7 {
52 t.Fatalf("Unexpected len: %d", n)
53 }
54 if !bytes.Equal(b.Bytes(), []byte{0x12, 0x9C, 0xBE, 0xC1, 0xBA, 0xB2, 0xB0}) {
55 t.Fatalf("Content does not match: %v", b.Bytes())
56 }
57 }
58
59 func TestEncodeAppend(t *testing.T) {
60 buf := make([]byte, 0, 8)
61 buf = append(buf, "head"...)
62 newBuf, err := Encode("body", buf)
63 if err != nil {
64 t.Fatal(err)
65 }
66 if string(newBuf) != "headbody" {
67 t.Fatalf("Unexpected buf: %v", newBuf)
68 }
69 buf = buf[:cap(buf)]
70 if cap(newBuf) != cap(buf) || &newBuf[cap(newBuf)-1] != &buf[cap(buf)-1] {
71 t.Fatal("buffer was reallocated")
72 }
73 }
74 func TestEncodeNilDst(t *testing.T) {
75 buf, err := Encode("test", nil)
76 if err != nil {
77 t.Fatal(err)
78 }
79 if string(buf) != "test" {
80 t.Fatalf("Unexpected buf: %v", buf)
81 }
82 }
83
84 func TestReferenceString(t *testing.T) {
85 var b bytes.Buffer
86 e := NewWriter(&b)
87 n, err := e.WriteString(referenceString)
88 if err != nil {
89 t.Fatal(err)
90 }
91 if n != 178 {
92 t.Fatalf("Unexpected len: %d", n)
93 }
94 if !bytes.Equal(b.Bytes(), []byte{
95 0x08, 0x00, 0x1B, 0x4C, 0xEA, 0x16, 0xCA, 0xD3, 0x94, 0x0F, 0x53, 0xEF, 0x61, 0x1B, 0xE5, 0x84,
96 0xC4, 0x0F, 0x53, 0xEF, 0x61, 0x1B, 0xE5, 0x84, 0xC4, 0x16, 0xCA, 0xD3, 0x94, 0x08, 0x02, 0x0F,
97 0x53, 0x4A, 0x4E, 0x16, 0x7D, 0x00, 0x30, 0x82, 0x52, 0x4D, 0x30, 0x6B, 0x6D, 0x41, 0x88, 0x4C,
98 0xE5, 0x97, 0x9F, 0x08, 0x0C, 0x16, 0xCA, 0xD3, 0x94, 0x15, 0xAE, 0x0E, 0x6B, 0x4C, 0x08, 0x0D,
99 0x8C, 0xB4, 0xA3, 0x9F, 0xCA, 0x99, 0xCB, 0x8B, 0xC2, 0x97, 0xCC, 0xAA, 0x84, 0x08, 0x02, 0x0E,
100 0x7C, 0x73, 0xE2, 0x16, 0xA3, 0xB7, 0xCB, 0x93, 0xD3, 0xB4, 0xC5, 0xDC, 0x9F, 0x0E, 0x79, 0x3E,
101 0x06, 0xAE, 0xB1, 0x9D, 0x93, 0xD3, 0x08, 0x0C, 0xBE, 0xA3, 0x8F, 0x08, 0x88, 0xBE, 0xA3, 0x8D,
102 0xD3, 0xA8, 0xA3, 0x97, 0xC5, 0x17, 0x89, 0x08, 0x0D, 0x15, 0xD2, 0x08, 0x01, 0x93, 0xC8, 0xAA,
103 0x8F, 0x0E, 0x61, 0x1B, 0x99, 0xCB, 0x0E, 0x4E, 0xBA, 0x9F, 0xA1, 0xAE, 0x93, 0xA8, 0xA0, 0x08,
104 0x02, 0x08, 0x0C, 0xE2, 0x16, 0xA3, 0xB7, 0xCB, 0x0F, 0x4F, 0xE1, 0x80, 0x05, 0xEC, 0x60, 0x8D,
105 0xEA, 0x06, 0xD3, 0xE6, 0x0F, 0x8A, 0x00, 0x30, 0x44, 0x65, 0xB9, 0xE4, 0xFE, 0xE7, 0xC2, 0x06,
106 0xCB, 0x82,
107 }) {
108 t.Fatalf("Content does not match: %v", b.Bytes())
109 }
110
111 }
112
113 func verifyEncode(t *testing.T, s string, invalid bool) {
114 b, err := EncodeStrict(s, nil)
115 if err != nil {
116 if invalid && errors.Is(err, ErrInvalidUTF8) {
117 return
118 }
119 t.Fatal(err)
120 } else {
121 if invalid {
122 t.Fatal("Expected an error")
123 }
124 }
125 s1, err := Decode(b)
126 if err != nil {
127 t.Fatal(err)
128 }
129 if s1 != s {
130 t.Fatalf("Strings dont match: Expected: '%s', actual: '%s'", s, s1)
131 }
132 if len(b)-len(s) > 1 {
133 t.Fatalf("Size increased too much (was %d, compressed %d)", len(s), len(b))
134 }
135 }
136
137 func TestEncodeDecode(t *testing.T) {
138 for _, s := range []string{
139 "🤷🏻‍♀😰😀",
140 "𬀀𛀿\u007f",
141 "翻😰😰",
142 "😰",
143 "00翻0",
144 "😰😰Ж😰",
145 "Тест可testТест",
146 "المؤتمر الدولي العاشر ليونيكود (Unicode Conference)، الذي سيعقد في 10-",
147 "סעיף א. כל בני אדם נולדו בני חורין ושווים בערכם ובזכויותיהם.",
148 "山自作久筋出難具固馬記式点連類無書着",
149 "\U0003f02c𬀀\U0002f03f𭀀\U0002f080\U0001403f𮀿",
150 "翫�000",
151 // This overrides window 0 with an extended range window which triggers a bug in the original implementation
152 // (can't use iPosition < 0x100 as a reliable check for a non-extended range)
153 "\U0001bef1𛱱\U0001cc71\U0001cff1\U0001baf1" +
154 "𗿱",
155 } {
156 s := s
157 t.Run("", func(t *testing.T) {
158 t.Parallel()
159 verifyEncode(t, s, false)
160 })
161 }
162
163 for _, s := range []string{
164 "�Ϳ͔\u0379Ϳ\xcd0\x8c",
165 "翻翻\x025翫翿\x025翫\xe7",
166 } {
167 s := s
168 t.Run("", func(t *testing.T) {
169 t.Parallel()
170 verifyEncode(t, s, true)
171 })
172 }
173 }
174
175 func TestEncoderReuse(t *testing.T) {
176 var e Encoder
177 s := StringRuneSource("á山тест")
178 buf, err := e.Encode(s, nil)
179 if err != nil {
180 t.Fatal(err)
181 }
182 buf1, err := e.Encode(s, nil)
183 if err != nil {
184 t.Fatal(err)
185 }
186 if !bytes.Equal(buf, buf1) {
187 t.Fatalf("buffers are not equal: %v, %v", buf, buf1)
188 }
189
190 buf = buf[:cap(buf)]
191 buf1 = buf1[:cap(buf1)]
192 if &buf[cap(buf)-1] == &buf1[cap(buf1)-1] {
193 t.Fatal("Buffers are the same")
194 }
195 }
196
197 func ExampleFindFirstEncodable() {
198 encodeOrPassthrough := func(s string) ([]byte, error) {
199 pos := FindFirstEncodable(s)
200 if pos >= 0 {
201 buf := make([]byte, pos, len(s))
202 // First, copy the pass-through part
203 copy(buf, s)
204 // ... then append the encoded tail.
205 buf, err := Encode(s[pos:], buf)
206 if err != nil {
207 return nil, err
208 }
209 return buf, nil
210 }
211 // The string only contains pass-through characters, save buffer allocation.
212 // The caller can check if the returned []byte is nil and use the original string instead.
213 return nil, nil
214 }
215 fmt.Println(encodeOrPassthrough("Sample ASCII"))
216 fmt.Println(encodeOrPassthrough("Sample Unicode 😀"))
217 // Output: [] <nil>
218 // [83 97 109 112 108 101 32 85 110 105 99 111 100 101 32 11 97 236 128] <nil>
219 }
220
221 func BenchmarkEncode(b *testing.B) {
222 b.ReportAllocs()
223 var buf []byte
224 for i := 0; i < b.N; i++ {
225 buf, _ = Encode(referenceString, buf)
226 buf = buf[:0]
227 }
228 }
229
230 func BenchmarkEncodeZeroAlloc(b *testing.B) {
231 var e Encoder
232 var buf []byte
233 b.ReportAllocs()
234 for i := 0; i < b.N; i++ {
235 buf, _ = e.Encode(StringRuneSource(referenceString), buf)
236 buf = buf[:0]
237 }
238 }