-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytes_test.go
109 lines (95 loc) · 1.71 KB
/
bytes_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
package bytespool
import (
"bytes"
"crypto/rand"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"testing"
)
func TestNewBytes0(t *testing.T) {
i := make([]byte, 1024*1024*2)
n, err := rand.Read(i)
if err != nil {
log.Fatal(err)
}
fmt.Println("n:", n)
p := NewBytesPool(1024*1024*100, 1024*4)
b := p.NewBytes(1024 * 1024 * 100)
n, err = b.Write(i)
if err != nil {
log.Fatal(err)
}
fmt.Println("n:", n)
bb, err := ioutil.ReadAll(b)
if err != nil {
log.Fatal(err)
}
if !bytes.Equal(i, bb) {
t.Fatal("not equal")
} else {
t.Log("TestNewBytesBuffer passed")
}
}
func TestNewBytes1(t *testing.T) {
i := make([]byte, 1024*1024*2)
n, err := rand.Read(i)
if err != nil {
log.Fatal(err)
}
fmt.Println("n:", n)
p := NewBytesPool(1024*1024*100, 1024*4)
b := p.NewBytes(1024 * 1024 * 100)
n, err = b.Write(i)
if err != nil {
log.Fatal(err)
}
fmt.Println("n:", n)
bb := make([]byte, 10)
n, err = b.Read(bb)
if err != nil {
log.Fatal(err)
}
fmt.Println("n:", n)
bb1, err := ioutil.ReadAll(b)
if err != nil {
log.Fatal(err)
}
if !bytes.Equal(i[10:], bb1) {
t.Fatal("not equal")
}
if !bytes.Equal(i, append(bb, bb1...)) {
t.Fatal("not equal")
}
}
func TestJSONEncoder(t *testing.T) {
p := NewBytesPool(1024*100, 1024*4)
b := p.NewBytes(1024 * 100)
enc := json.NewEncoder(b)
i := make([]byte, 1024*2)
_, err := rand.Read(i)
if err != nil {
log.Fatal(err)
}
err = enc.Encode(map[string]interface{}{
"key1": "value1",
"key2": "value2",
"key3": i,
})
if err != nil {
log.Fatal(err)
}
/*
x, err := b.ReadAll()
if err != nil {
log.Fatal(err)
}
fmt.Println(string(x))
*/
h, err := ioutil.ReadAll(b)
if err != nil {
log.Fatal(err)
}
fmt.Println("hi", string(h))
}