forked from cbergoon/merkletree
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bucketpools_test.go
171 lines (154 loc) · 3.75 KB
/
bucketpools_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package merkletree
import (
"bytes"
"crypto/sha256"
"testing"
)
func TestBucketpools_CalculateHash(t *testing.T) {
content1 := []byte("")
content2 := []byte("Hello.kkk")
content3 := []byte("779 3")
bucket1 := NewBucket(uint64(16), "test1")
h1 := sha256.New()
h1.Write(content1)
hash1 := h1.Sum(nil)
bucket2 := NewBucket(uint64(2), "test2")
bucket2.Content.Write(content2)
h2 := sha256.New()
h2.Write(content2)
hash2 := h2.Sum(nil)
bucket3 := NewBucket(uint64(128), "test3")
bucket3.Content.Write(content3)
h3 := sha256.New()
h3.Write(content3)
hash3 := h3.Sum(nil)
tables := []struct {
bucket *Bucket
hash []byte
}{
{bucket1, hash1},
{bucket2, hash2},
{bucket3, hash3},
}
for _, table := range tables {
value, _ := table.bucket.CalculateHash()
if !bytes.Equal(value, table.hash) {
t.Errorf("Value of %v was incorrect, got: %v, want: %v.", table.bucket, value, table.hash)
}
}
}
func TestBucketpools_Get(t *testing.T) {
// TO DO: More test cases
size1 := uint64(0)
bp1 := NewBucketPool(0, size1, "test1")
size2 := uint64(16)
bp2 := NewBucketPool(2, size2, "test2")
tables := []struct {
pool *BucketPool
bucket Bucket
err bool
}{
{
pool: bp1,
bucket: Bucket{Content: bytes.NewBuffer(make([]byte, 0, size1)), Topic: "test2", size: size1},
err: true,
},
{
pool: bp2,
bucket: Bucket{Content: bytes.NewBuffer(make([]byte, 0, size2)), Topic: "test2", size: size2},
err: false,
},
}
for _, table := range tables {
bucket, errValue := table.pool.Get()
// Set err to true if error is not nil
var err bool
if errValue != nil {
err = true
}
if bucket.size != table.bucket.size {
t.Errorf("size of bucket incorrect: got %v but expected %v", bucket.size, table.bucket.size)
}
if err != table.err {
t.Errorf("error was incorrect. got %v but expected %v", err, table.err)
}
}
}
func TestBucketpools_Put(t *testing.T) {
size1 := uint64(16)
bp1 := NewBucketPool(2, size1, "test1")
size2 := uint64(32)
bp2 := NewBucketPool(2, size2, "test2")
<-bp2.c
tables := []struct {
pool *BucketPool
bucket Bucket
success bool
}{
{
pool: bp1,
bucket: Bucket{Content: bytes.NewBuffer(make([]byte, 0, size1)), Topic: "test2", size: size1},
success: false,
},
{
pool: bp1,
bucket: Bucket{Content: bytes.NewBuffer(make([]byte, 0, size2)), Topic: "test1", size: size2},
success: false,
},
{
pool: bp2,
bucket: Bucket{Content: bytes.NewBuffer(make([]byte, 0, size2)), Topic: "test2", size: size2},
success: true,
},
}
for _, table := range tables {
success := table.pool.Put(table.bucket)
if success != table.success {
t.Errorf("error: got %v but expected %v", success, table.success)
}
}
}
func TestBucketpools_WriteContent(t *testing.T) {
tables := []struct {
bucket *Bucket
content []byte
success bool
}{
{
bucket: NewBucket(uint64(16), ""),
content: []byte("too long."),
success: false,
},
{
bucket: NewBucket(uint64(16), ""),
content: []byte("ok"),
success: true,
},
}
for _, table := range tables {
success := table.bucket.WriteContent(table.content)
if success != table.success {
t.Errorf("error: got %v but expected %v", success, table.success)
}
}
}
func TestBucketpools_ReadContent(t *testing.T) {
bucket1 := NewBucket(uint64(256), "test")
teststrings := []string{
"Testing",
" ",
" ReadContent method.",
"",
"with more \n élements ",
}
for _, teststring := range teststrings {
bucket1.WriteContent([]byte(teststring))
}
storageBucket1 := bucketToStorage(*bucket1)
content, _ := storageBucket1.ReadContent()
for i := 0; i < len(content); i++ {
if string(content[i]) != teststrings[i] {
t.Errorf("error: got %s but expected %s", content[i], teststrings[i])
}
}
}