generated from rokath/Best-README-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cobs_test.go
91 lines (82 loc) · 1.92 KB
/
cobs_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
package cobs_test
import (
"bytes"
"fmt"
"math/rand"
"testing"
cobs "github.com/rokath/cobs/cgo"
"github.com/tj/assert"
)
const (
max = 1000000
rounds = 1000
)
func TestDecodeTestData(t *testing.T) {
dec := make([]byte, 1024)
for _, x := range testData {
n, e := cobs.Decode(dec, x.enc)
assert.Nil(t, e)
act := dec[:n]
assert.Equal(t, x.dec, act)
}
}
func TestCDecodeTestData(t *testing.T) {
dec := make([]byte, 1024)
for _, x := range testData {
n, e := cobs.CDecode(dec, x.enc)
assert.Nil(t, e)
act := dec[:n]
assert.Equal(t, x.dec, act)
}
}
func TestCEncodeTestData(t *testing.T) {
enc := make([]byte, 1024)
for _, x := range testData {
n := cobs.CEncode(enc, x.dec)
act := enc[:n]
assert.Equal(t, x.enc, act)
}
}
// TestCEncodeCDecode256 tests on generated random byte numbers for random length 0-32767.
func TestCEncodeCDecode256(t *testing.T) {
for i := 0; i < rounds; i++ {
length := rand.Intn(max)
datBuf := make([]byte, max)
encBuf := make([]byte, 2*max)
decBuf := make([]byte, 2*max)
for i := 0; i < length; i++ {
b := uint8(rand.Intn(256)) // 0, ..., 0xFF
datBuf[i] = b
}
dat := datBuf[:length]
fmt.Println()
n := cobs.CEncode(encBuf, dat)
enc := encBuf[:n]
assert.False(t, bytes.Contains(enc, []byte{0}))
n, e := cobs.CDecode(decBuf, enc)
assert.True(t, e == nil)
dec := decBuf[:n]
assert.Equal(t, dat, dec)
}
}
func TestCEncodeDecode256(t *testing.T) {
for i := 0; i < rounds; i++ {
length := rand.Intn(max)
datBuf := make([]byte, max)
encBuf := make([]byte, 2*max)
decBuf := make([]byte, 2*max)
for i := 0; i < length; i++ {
b := uint8(rand.Intn(256)) // 0, ..., 0xFF
datBuf[i] = b
}
dat := datBuf[:length]
fmt.Println()
n := cobs.CEncode(encBuf, dat)
enc := encBuf[:n]
assert.False(t, bytes.Contains(enc, []byte{0}))
n, e := cobs.Decode(decBuf, enc)
assert.True(t, e == nil)
dec := decBuf[:n]
assert.Equal(t, dat, dec)
}
}