-
Notifications
You must be signed in to change notification settings - Fork 100
/
asciiconverter_test.go
66 lines (53 loc) · 1.62 KB
/
asciiconverter_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
package ftpserver
import (
"bytes"
"io"
"testing"
"github.com/stretchr/testify/require"
)
func TestASCIIConvert(t *testing.T) {
lines := []byte("line1\r\nline2\r\n\r\nline4")
src := bytes.NewBuffer(lines)
dst := bytes.NewBuffer(nil)
converter := newASCIIConverter(src, convertModeToLF)
_, err := io.Copy(dst, converter)
require.NoError(t, err)
require.Equal(t, []byte("line1\nline2\n\nline4"), dst.Bytes())
lines = []byte("line1\nline2\n\nline4")
dst = bytes.NewBuffer(nil)
converter = newASCIIConverter(bytes.NewBuffer(lines), convertModeToCRLF)
_, err = io.Copy(dst, converter)
require.NoError(t, err)
require.Equal(t, []byte("line1\r\nline2\r\n\r\nline4"), dst.Bytes())
// test a src buffers without line endings, it must remain unchanged
buf := make([]byte, 131072)
for j := range buf {
buf[j] = 66
}
dst = bytes.NewBuffer(nil)
converter = newASCIIConverter(bytes.NewBuffer(buf), convertModeToCRLF)
_, err = io.Copy(dst, converter)
require.NoError(t, err)
require.Equal(t, buf, dst.Bytes())
}
func BenchmarkASCIIConverter(b *testing.B) {
linesCRLF := []byte("line1\r\nline2\r\n\r\nline4")
linesLF := []byte("line1\nline2\n\nline4")
readerCRLF := bytes.NewBuffer(nil)
readerLF := bytes.NewBuffer(nil)
for i := 0; i < 100000; i++ {
_, err := readerCRLF.Write(linesCRLF)
panicOnError(err)
_, err = readerLF.Write(linesLF)
panicOnError(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
c := newASCIIConverter(readerCRLF, convertModeToLF)
_, err := io.Copy(io.Discard, c)
panicOnError(err)
c = newASCIIConverter(readerLF, convertModeToCRLF)
_, err = io.Copy(io.Discard, c)
panicOnError(err)
}
}