-
Notifications
You must be signed in to change notification settings - Fork 22
/
buffer_test.go
92 lines (80 loc) · 1.59 KB
/
buffer_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
package hbbft
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
)
func TestBufferDelete(t *testing.T) {
b := newBuffer()
txx := make([]Transaction, 10)
for i := 0; i < len(txx); i++ {
tx := &tx{rand.Uint64()}
txx[i] = tx
b.push(tx)
}
b.delete(txx[:4])
for _, tx := range txx[:4] {
for i := 0; i < len(b.data); i++ {
assert.False(t, bytes.Compare(tx.Hash(), b.data[i].Hash()) == 0)
}
}
}
func TestBufferPush(t *testing.T) {
var (
b = newBuffer()
n = 100
data = make([]*tx, n)
)
for i := 0; i < n; i++ {
v := &tx{rand.Uint64()}
data[i] = v
b.push(v)
}
assert.Equal(t, n, b.len())
for i := 0; i < n; i++ {
assert.Equal(t, data[i], b.data[i])
}
}
func TestBufferSample(t *testing.T) {
var (
b = newBuffer()
n = 1000
data = make([]*tx, n)
)
for i := 0; i < n; i++ {
v := &tx{uint64(i)}
data[i] = v
b.push(v)
}
txx := b.sample(10, 50)
assert.Equal(t, 10, len(txx))
// must be in range 0 - 50.
for _, v := range txx {
if v.(*tx).Value() > uint64(50) {
t.Fatal("err")
}
}
}
type tx struct {
Nonce uint64
}
func (t *tx) Hash() []byte {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, t.Nonce)
return buf
}
func (t *tx) String() string { return fmt.Sprintf("%d", t.Nonce) }
func (t *tx) Encode(w io.Writer) error {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, t.Value())
_, err := w.Write(buf)
return err
}
func (t *tx) Decode(r io.Reader) error {
return binary.Read(r, binary.LittleEndian, &t.Nonce)
}
func (t tx) Value() uint64 { return t.Nonce }