-
Notifications
You must be signed in to change notification settings - Fork 25
/
delta_test.go
112 lines (99 loc) · 1.95 KB
/
delta_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
package gitgo
import (
"bytes"
"io"
"io/ioutil"
"log"
"os"
"reflect"
"testing"
)
func Test_PatchDelta(t *testing.T) {
suite := [][3]string{
[3]string{"test_data/test-delta.c", "test_data/test-delta-new.delta", "test_data/test-delta-new.c"},
[3]string{"test_data/zlib.c", "test_data/zlib-delta", "test_data/zlib-changed.c"},
}
for _, trpl := range suite {
fin, err := os.Open(trpl[0])
if err != nil {
t.Error(err)
continue
}
defer fin.Close()
deltaf, err := os.Open(trpl[1])
if err != nil {
t.Error(err)
continue
}
defer deltaf.Close()
expectedf, err := os.Open(trpl[2])
if err != nil {
t.Error(err)
continue
}
defer expectedf.Close()
restored, err := patchDelta(fin, deltaf)
if err != nil {
t.Error(err)
continue
}
bts1, err := ioutil.ReadAll(expectedf)
if err != nil {
t.Error(err)
continue
}
bts2, err := ioutil.ReadAll(restored)
if err != nil {
t.Error(err)
continue
}
if len(bts1) != len(bts2) {
t.Errorf("Expected %d bytes and received %d", len(bts1), len(bts2))
log.Printf("%q", bts2)
continue
}
if !reflect.DeepEqual(bts1, bts2) {
t.Errorf("delta application failed")
continue
}
}
}
func Test_parseVarInt(t *testing.T) {
type pair struct {
b []byte
i int
}
inputs := []pair{
pair{[]byte{145, 46}, 5905},
pair{[]byte{137, 49}, 6281},
}
for _, p := range inputs {
input := p.b
expected := p.i
result, err := parseVarInt(bytes.NewBuffer(input))
if err != nil {
t.Error(err)
return
}
if result != expected {
t.Errorf("Expected %d and received %d", expected, result)
}
}
}
// readersEqual checks that two readers have the same contents
func readersEqual(r1, r2 io.Reader) bool {
bts, err := ioutil.ReadAll(r1)
if err != nil {
panic(err)
}
bts2, err := ioutil.ReadAll(r2)
if err != nil {
panic(err)
}
log.Printf("1: %s", bts)
log.Printf("2: %s", bts2)
if !reflect.DeepEqual(bts, bts2) {
return false
}
return true
}