forked from xtaci/kcp-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fec_test.go
117 lines (109 loc) · 2.2 KB
/
fec_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
package kcp
import (
"encoding/binary"
"math/rand"
"testing"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func TestFECOther(t *testing.T) {
if newFEC(128, 0, 1) != nil {
t.Fail()
}
if newFEC(128, 0, 0) != nil {
t.Fail()
}
if newFEC(1, 10, 10) != nil {
t.Fail()
}
}
func TestFECNoLost(t *testing.T) {
fec := newFEC(128, 10, 3)
for i := 0; i < 100; i += 10 {
data := makefecgroup(i, 13)
for k := range data[fec.dataShards] {
fec.markData(data[k])
}
ecc := fec.Encode(data, fecHeaderSize, fecHeaderSize+4)
for k := range ecc {
fec.markFEC(ecc[k])
}
data = append(data, ecc...)
for k := range data {
f := fec.decodeBytes(data[k])
if recovered := fec.Decode(f); recovered != nil {
t.Fail()
}
}
}
}
func TestFECLost1(t *testing.T) {
fec := newFEC(128, 10, 3)
fec.next = fec.paws - 13
for i := 0; i < 100; i += 10 {
data := makefecgroup(i, 13)
for k := range data[fec.dataShards] {
fec.markData(data[k])
}
ecc := fec.Encode(data, fecHeaderSize, fecHeaderSize+4)
for k := range ecc {
fec.markFEC(ecc[k])
}
lost := rand.Intn(13)
for k := range data {
if k != lost {
f := fec.decodeBytes(data[k])
if recovered := fec.Decode(f); recovered != nil {
if lost > 10 {
t.Fail()
}
}
}
}
}
}
func TestFECLost2(t *testing.T) {
fec := newFEC(128, 10, 3)
for i := 0; i < 100; i += 10 {
data := makefecgroup(i, 13)
for k := range data[fec.dataShards] {
fec.markData(data[k])
}
ecc := fec.Encode(data, fecHeaderSize, fecHeaderSize+4)
for k := range ecc {
fec.markFEC(ecc[k])
}
lost1 := rand.Intn(13)
lost2 := rand.Intn(13)
for lost2 == lost1 {
lost2 = rand.Intn(13)
}
expect := 0
if lost1 < 10 {
expect++
}
if lost2 < 10 {
expect++
}
for k := range data {
if k != lost1 && k != lost2 {
f := fec.decodeBytes(data[k])
if recovered := fec.Decode(f); recovered != nil {
if len(recovered) != expect {
t.Fail()
}
}
}
}
}
}
func makefecgroup(start, size int) (group [][]byte) {
for i := 0; i < size; i++ {
data := make([]byte, fecHeaderSize+4)
binary.LittleEndian.PutUint32(data[fecHeaderSize:], uint32(start+i))
group = append(group, data)
}
return
}