forked from FactomProject/FactomCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvarint_test.go
More file actions
64 lines (56 loc) · 1.36 KB
/
varint_test.go
File metadata and controls
64 lines (56 loc) · 1.36 KB
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
package common_test
import (
"bytes"
"fmt"
"math/rand"
"testing"
"github.com/FactomProject/FactomCode/common"
)
func TestVarInt(t *testing.T) {
for i := 0; i < 1000; i++ {
var out bytes.Buffer
v := make([]uint64, 10)
for j := 0; j < len(v); j++ {
var m uint64 // 64 bit mask
sw := rand.Int63() % 4 // Pick a random choice
switch sw {
case 0:
m = 0xFF // Random byte
case 1:
m = 0xFFFF // Random 16 bit integer
case 2:
m = 0xFFFFFFFF // Random 32 bit integer
case 3:
m = 0xFFFFFFFFFFFFFFFF // Random 64 bit integer
}
n := uint64(rand.Int63() + (rand.Int63() << 32))
v[j] = n & m
}
for j := 0; j < len(v); j++ { // Encode our entire array of numbers
err := common.EncodeVarInt(&out, v[j])
if err != nil {
fmt.Println(err)
t.Fail()
return
}
// fmt.Printf("%x ",v[j])
}
// fmt.Println( "Length: ",out.Len())
data := out.Bytes()
// PrtData(data)
// fmt.Println()
sdata := data // Decode our entire array of numbers, and
var dv uint64 // check we got them back correctly.
for k := 0; k < 1000; k++ {
data = sdata
for j := 0; j < len(v); j++ {
dv, data = common.DecodeVarInt(data)
if dv != v[j] {
fmt.Printf("Values don't match: decode:%x expected:%x (%d)\n", dv, v[j], j)
t.Fail()
return
}
}
}
}
}