-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpending_chain_test.go
70 lines (63 loc) · 1.67 KB
/
pending_chain_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
package secureio
import (
"bytes"
"fmt"
"math/rand"
"testing"
"github.com/stretchr/testify/require"
)
func BenchmarkSetLeadingOnes(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
setLeadingOnes64(int(uint8(i)))
}
}
func BenchmarkPendingChainIsSet_Set(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
pendingChainIsSetSet(0x123456789abcdef, uint64(i))
}
}
func BenchmarkPendingChain_Merge(b *testing.B) {
var chain pendingChain
var fragmentHdr messageFragmentHeadersData
for _, size := range []uint64{1, 1000, 10000} {
b.Run(fmt.Sprintf("size%d", size), func(b *testing.B) {
data := make([]byte, size)
b.SetBytes(int64(size))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
chain.Init(60000)
for pos := uint64(0); pos < 60000; pos += size {
fragmentHdr.StartPos = pos
_, _ = chain.Merge(&fragmentHdr, data)
}
}
})
}
}
func TestPendingChain_Merge(t *testing.T) {
data := make([]byte, 65536)
rand.Seed(0)
for i := 0; i < 1000; i++ {
totalLength := 1 + rand.Uint64()%65536
var chain pendingChain
var fragmentHdr messageFragmentHeadersData
chain.Init(totalLength)
fragmentLength := 1 + rand.Uint64()%totalLength
data = data[:totalLength]
rand.Read(data)
for pos := uint64(0); pos < totalLength; pos += fragmentLength {
fragmentHdr.StartPos = pos
dataCmp, err := chain.Merge(&fragmentHdr, data[pos:u64min(pos+fragmentLength, totalLength)])
require.NoError(t, err)
if dataCmp != nil {
require.True(t, pos+fragmentLength >= totalLength, fmt.Sprint(pos, fragmentLength, totalLength))
require.True(t, bytes.Compare(data, dataCmp) == 0)
}
}
}
}