diff options
author | Joshua MacDonald <josh.macdonald@gmail.com> | 2015-01-25 23:17:17 -0800 |
---|---|---|
committer | Joshua MacDonald <josh.macdonald@gmail.com> | 2015-01-25 23:17:17 -0800 |
commit | b55fcf3dc4c775ef35dce116ab23d35a5f9ff326 (patch) | |
tree | 47a160fb2855434f17689a9028081b29bff8f1d8 /xdelta3/go/src/regtest.go | |
parent | 57c7df665788bbb524c544ce5dea66b1aade63d3 (diff) |
regtest.go tests a random stream and an offset copy
Diffstat (limited to 'xdelta3/go/src/regtest.go')
-rw-r--r-- | xdelta3/go/src/regtest.go | 100 |
1 files changed, 92 insertions, 8 deletions
diff --git a/xdelta3/go/src/regtest.go b/xdelta3/go/src/regtest.go index aee419d..d31e905 100644 --- a/xdelta3/go/src/regtest.go +++ b/xdelta3/go/src/regtest.go | |||
@@ -1,13 +1,17 @@ | |||
1 | package main | 1 | package main |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "bytes" | ||
5 | "fmt" | ||
4 | "io" | 6 | "io" |
5 | "io/ioutil" | 7 | "io/ioutil" |
6 | "xdelta" | 8 | "xdelta" |
7 | ) | 9 | ) |
8 | 10 | ||
9 | const ( | 11 | const ( |
12 | blocksize = 1<<20 | ||
10 | prog = "/Users/jmacd/src/xdelta/xdelta3/xdelta3" | 13 | prog = "/Users/jmacd/src/xdelta/xdelta3/xdelta3" |
14 | seed = 1422253499919909358 | ||
11 | ) | 15 | ) |
12 | 16 | ||
13 | func drain(f io.ReadCloser) <-chan []byte { | 17 | func drain(f io.ReadCloser) <-chan []byte { |
@@ -39,16 +43,11 @@ func write(f io.WriteCloser, b []byte) { | |||
39 | } | 43 | } |
40 | } | 44 | } |
41 | 45 | ||
42 | func main() { | 46 | func smokeTest(r *xdelta.Runner, p *xdelta.Program) { |
43 | x := xdelta.Program{prog} | ||
44 | smokeTest(x) | ||
45 | } | ||
46 | |||
47 | func smokeTest(p xdelta.Program) { | ||
48 | target := "Hello world!" | 47 | target := "Hello world!" |
49 | source := "Hello world, nice to meet you!" | 48 | source := "Hello world, nice to meet you!" |
50 | 49 | ||
51 | run, err := p.Exec(true, []string{"-e"}) | 50 | run, err := r.Exec(p, true, []string{"-e"}) |
52 | if err != nil { | 51 | if err != nil { |
53 | panic(err) | 52 | panic(err) |
54 | } | 53 | } |
@@ -62,7 +61,7 @@ func smokeTest(p xdelta.Program) { | |||
62 | panic(err) | 61 | panic(err) |
63 | } | 62 | } |
64 | 63 | ||
65 | run, err = p.Exec(true, []string{"-d"}) | 64 | run, err = r.Exec(p, true, []string{"-d"}) |
66 | if err != nil { | 65 | if err != nil { |
67 | panic(err) | 66 | panic(err) |
68 | } | 67 | } |
@@ -81,3 +80,88 @@ func smokeTest(p xdelta.Program) { | |||
81 | panic("It's not working!!!") | 80 | panic("It's not working!!!") |
82 | } | 81 | } |
83 | } | 82 | } |
83 | |||
84 | func copyStreams(r io.ReadCloser, w io.WriteCloser) { | ||
85 | _, err := io.Copy(w, r) | ||
86 | if err != nil { | ||
87 | panic(err) | ||
88 | } | ||
89 | err = r.Close() | ||
90 | if err != nil { | ||
91 | panic(err) | ||
92 | } | ||
93 | err = w.Close() | ||
94 | if err != nil { | ||
95 | panic(err) | ||
96 | } | ||
97 | } | ||
98 | |||
99 | func compareStreams(r1 io.ReadCloser, r2 io.ReadCloser, length int64) { | ||
100 | b1 := make([]byte, blocksize) | ||
101 | b2 := make([]byte, blocksize) | ||
102 | var idx int64 | ||
103 | for length > 0 { | ||
104 | c := blocksize | ||
105 | if length < blocksize { | ||
106 | c = int(length) | ||
107 | } | ||
108 | if _, err := io.ReadFull(r1, b1[0:c]); err != nil { | ||
109 | panic(err) | ||
110 | } | ||
111 | if _, err := io.ReadFull(r2, b2[0:c]); err != nil { | ||
112 | panic(err) | ||
113 | } | ||
114 | if bytes.Compare(b1[0:c], b2[0:c]) != 0 { | ||
115 | fmt.Println("B1 is", string(b1[0:c])) | ||
116 | fmt.Println("B2 is", string(b2[0:c])) | ||
117 | panic(fmt.Sprint("Bytes do not compare at ", idx)) | ||
118 | } | ||
119 | length -= int64(c) | ||
120 | idx += int64(c) | ||
121 | } | ||
122 | } | ||
123 | |||
124 | func offsetTest(r *xdelta.Runner, p *xdelta.Program, offset, length int64) { | ||
125 | enc, err := r.Exec(p, true, []string{"-e"}) | ||
126 | if err != nil { | ||
127 | panic(err) | ||
128 | } | ||
129 | dec, err := r.Exec(p, true, []string{"-d"}) | ||
130 | if err != nil { | ||
131 | panic(err) | ||
132 | } | ||
133 | |||
134 | read, write := io.Pipe() | ||
135 | |||
136 | go copyStreams(enc.Stdout, dec.Stdin) | ||
137 | go compareStreams(dec.Stdout, read, length) | ||
138 | |||
139 | empty(enc.Stderr) // Use these? | ||
140 | empty(dec.Stderr) | ||
141 | |||
142 | // TODO: seems possible to use one WriteRstreams call to generate | ||
143 | // the source and target for both encoder and decoder. Why not? | ||
144 | xdelta.WriteRstreams(seed, offset, length, enc.Srcin, enc.Stdin) | ||
145 | xdelta.WriteRstreams(seed, offset, length, dec.Srcin, write) | ||
146 | |||
147 | if err := enc.Cmd.Wait(); err != nil { | ||
148 | panic(err) | ||
149 | } | ||
150 | if err := dec.Cmd.Wait(); err != nil { | ||
151 | panic(err) | ||
152 | } | ||
153 | } | ||
154 | |||
155 | func main() { | ||
156 | r, err := xdelta.NewRunner() | ||
157 | if err != nil { | ||
158 | panic(err) | ||
159 | } | ||
160 | defer r.Cleanup() | ||
161 | |||
162 | prog := &xdelta.Program{prog} | ||
163 | |||
164 | smokeTest(r, prog) | ||
165 | |||
166 | offsetTest(r, prog, 0, 1024 << 20) | ||
167 | } | ||