-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_func_test.go
44 lines (40 loc) · 1.24 KB
/
io_func_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
package io
import (
"bytes"
"testing"
"time"
)
func TestSwap(t *testing.T) {
r1 := NewReadWriter(ReadFunc(func(p []byte) (int, error) {
<-time.After(time.Second)
return copy(p, []byte("r1")), nil
}), WriteFunc(func(p []byte) (int, error) {
t.Log(string(p))
return len(p), nil
}))
r2 := NewReadWriter(ReadFunc(func(p []byte) (int, error) {
<-time.After(time.Second)
return copy(p, []byte("r2")), nil
}), WriteFunc(func(p []byte) (int, error) {
t.Log(string(p))
return len(p), nil
}))
Swap(r1, r2)
}
func TestReadPrefix(t *testing.T) {
r := bytes.NewReader([]byte("hello world woworld"))
t.Log(ReadPrefix(r, []byte("llo"))) //llo nil
t.Log(ReadPrefix(r, []byte("wor"))) //wor nil
t.Log(ReadPrefix(r, []byte("wor"))) //wor nil
t.Log(ReadPrefix(r, []byte("llo"))) //EOF
t.Log(ReadPrefix(r, []byte("aaa"))) //EOF
t.Log(ReadPrefix(r, []byte("aaa"))) //EOF
r = bytes.NewReader([]byte("hello world woworld"))
t.Log(ReadPrefix(r, []byte("lo"))) //lo nil
t.Log(ReadPrefix(r, []byte("lo"))) // EOF
}
func TestSplitWithLength(t *testing.T) {
t.Log(SplitWithLength([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 4))
t.Log(SplitWithLength([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 1))
t.Log(SplitWithLength([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 0))
}