-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
chunkreader_test.go
114 lines (104 loc) · 2.56 KB
/
chunkreader_test.go
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
// Copyright (c) 2024 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package utf16chunk_test
import (
"io"
"testing"
"unicode/utf16"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mau.fi/mautrix-gvoice/pkg/libgv/utf16chunk"
)
type fakeReader struct {
chunks [][]byte
}
func (fr *fakeReader) Read(p []byte) (n int, err error) {
if len(fr.chunks) == 0 {
return 0, io.EOF
}
n = copy(p, fr.chunks[0])
if n == len(fr.chunks[0]) {
fr.chunks = fr.chunks[1:]
} else {
fr.chunks[0] = fr.chunks[0][n:]
}
return
}
type testCase struct {
name string
chunks []string
expected []string
}
var testCases = []testCase{
{
name: "SingleChunk",
chunks: []string{"5\nhello"},
expected: []string{"hello"},
},
{
name: "SingleChunkEmoji",
chunks: []string{"3\n🐈️"},
expected: []string{"🐈️"},
},
{
name: "MultipleChunksInOne",
chunks: []string{"5\nhello5\nworld"},
expected: []string{"hello", "world"},
},
{
name: "MultipleChunksInOneWithEmoji",
chunks: []string{"5\nhello3\n🐈️"},
expected: []string{"hello", "🐈️"},
},
{
name: "OneChunkInMultipleParts",
chunks: []string{"15\nhello 🐈️ world"},
expected: []string{"hello 🐈️ world"},
},
{
name: "ChunkWithNewline",
chunks: []string{"15\nhello\n🐈️\nworld"},
expected: []string{"hello\n🐈️\nworld"},
},
}
func TestReader_ReadChunk(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
fr := &fakeReader{chunks: make([][]byte, len(tc.chunks))}
for i, chunk := range tc.chunks {
fr.chunks[i] = []byte(chunk)
}
cbr := utf16chunk.NewReader(fr)
for _, expected := range tc.expected {
chunk, err := cbr.ReadChunk()
require.NoError(t, err)
require.Equal(t, expected, string(chunk))
}
_, err := cbr.ReadChunk()
require.ErrorIs(t, err, io.EOF)
})
}
}
func FuzzUTF16Length(f *testing.F) {
fuzzTests := []string{
"meow",
"🐈️",
"åäö",
"🐈️åäö",
"🐈️🐈️🐈️🐈️🐈️🐈️🐈️🐈️🐈️🐈️🐈️🐈️🐈️🐈️🐈️",
"世界",
"🤔",
"\U00010000",
}
for _, val := range fuzzTests {
f.Add(val)
}
f.Fuzz(func(t *testing.T, input string) {
expected := utf16.Encode([]rune(input))
got, _ := utf16chunk.UTF16Length([]byte(input), 1<<31)
assert.Equal(t, len(expected), got, "input: %q", input)
})
}