diff options
Diffstat (limited to 'xdelta3/go/src/xdelta/test.go')
-rw-r--r-- | xdelta3/go/src/xdelta/test.go | 92 |
1 files changed, 50 insertions, 42 deletions
diff --git a/xdelta3/go/src/xdelta/test.go b/xdelta3/go/src/xdelta/test.go index 50ddeae..9eb47f6 100644 --- a/xdelta3/go/src/xdelta/test.go +++ b/xdelta3/go/src/xdelta/test.go | |||
@@ -43,8 +43,8 @@ type Run struct { | |||
43 | } | 43 | } |
44 | 44 | ||
45 | func (t *TestGroup) Panic(err error) { | 45 | func (t *TestGroup) Panic(err error) { |
46 | t.WaitGroup.Done() // For the caller | 46 | t.Done() // For the caller |
47 | t.WaitGroup.Wait() | 47 | t.Wait() |
48 | panic(err) | 48 | panic(err) |
49 | } | 49 | } |
50 | 50 | ||
@@ -52,23 +52,25 @@ func NewTestGroup() *TestGroup { | |||
52 | return &TestGroup{} | 52 | return &TestGroup{} |
53 | } | 53 | } |
54 | 54 | ||
55 | func (t *TestGroup) Drain(f io.ReadCloser) <-chan []byte { | 55 | func (t *TestGroup) Drain(f io.ReadCloser, desc string) <-chan []byte { |
56 | c := make(chan []byte) | 56 | c := make(chan []byte) |
57 | go func() { | 57 | go func() { |
58 | t.WaitGroup.Add(1) | 58 | t.Add(1) |
59 | //fmt.Println("Draining", desc) | ||
59 | if b, err := ioutil.ReadAll(f); err != nil { | 60 | if b, err := ioutil.ReadAll(f); err != nil { |
60 | t.Panic(err) | 61 | t.Panic(err) |
61 | } else { | 62 | } else { |
63 | //fmt.Println("Draining", desc, "--got it") | ||
62 | c <- b | 64 | c <- b |
63 | } | 65 | } |
64 | t.WaitGroup.Done() | 66 | t.Done() |
65 | }() | 67 | }() |
66 | return c | 68 | return c |
67 | } | 69 | } |
68 | 70 | ||
69 | func (t *TestGroup) Empty(f io.ReadCloser, desc string) { | 71 | func (t *TestGroup) Empty(f io.ReadCloser, desc string) { |
70 | go func() { | 72 | go func() { |
71 | t.WaitGroup.Add(1) | 73 | t.Add(1) |
72 | s := bufio.NewScanner(f) | 74 | s := bufio.NewScanner(f) |
73 | for s.Scan() { | 75 | for s.Scan() { |
74 | os.Stderr.Write([]byte(fmt.Sprint(desc, ": ", s.Text(), "\n"))) | 76 | os.Stderr.Write([]byte(fmt.Sprint(desc, ": ", s.Text(), "\n"))) |
@@ -76,61 +78,67 @@ func (t *TestGroup) Empty(f io.ReadCloser, desc string) { | |||
76 | if err := s.Err(); err != nil { | 78 | if err := s.Err(); err != nil { |
77 | t.Panic(err) | 79 | t.Panic(err) |
78 | } | 80 | } |
79 | t.WaitGroup.Done() | 81 | t.Done() |
80 | }() | 82 | }() |
81 | } | 83 | } |
82 | 84 | ||
83 | func (t *TestGroup) Write(what string, f io.WriteCloser, b []byte) { | 85 | func (t *TestGroup) Write(what string, f io.WriteCloser, b []byte) { |
86 | //fmt.Println("Write (", what, ") ", len(b), "bytes") | ||
84 | if _, err := f.Write(b); err != nil { | 87 | if _, err := f.Write(b); err != nil { |
85 | t.Panic(errors.New(fmt.Sprint(what, ":", err))) | 88 | t.Panic(errors.New(fmt.Sprint(what, ":", err))) |
86 | } | 89 | } |
90 | //fmt.Println("Write (", what, ") closing") | ||
87 | if err := f.Close(); err != nil { | 91 | if err := f.Close(); err != nil { |
88 | t.Panic(errors.New(fmt.Sprint(what, ":", err))) | 92 | t.Panic(errors.New(fmt.Sprint(what, ":", err))) |
89 | } | 93 | } |
90 | } | 94 | } |
91 | 95 | ||
92 | func (t *TestGroup) CopyStreams(r io.ReadCloser, w io.WriteCloser) { | 96 | func (t *TestGroup) CopyStreams(r io.ReadCloser, w io.WriteCloser) { |
93 | t.Add(1) | 97 | go func() { |
94 | _, err := io.Copy(w, r) | 98 | t.Add(1) |
95 | if err != nil { | 99 | _, err := io.Copy(w, r) |
96 | t.Panic(err) | 100 | if err != nil { |
97 | } | 101 | t.Panic(err) |
98 | err = r.Close() | ||
99 | if err != nil { | ||
100 | t.Panic(err) | ||
101 | } | ||
102 | err = w.Close() | ||
103 | if err != nil { | ||
104 | t.Panic(err) | ||
105 | } | ||
106 | t.Done() | ||
107 | } | ||
108 | |||
109 | func (t *TestGroup) CompareStreams(r1 io.ReadCloser, r2 io.ReadCloser, length int64) { | ||
110 | t.Add(1) | ||
111 | b1 := make([]byte, blocksize) | ||
112 | b2 := make([]byte, blocksize) | ||
113 | var idx int64 | ||
114 | for length > 0 { | ||
115 | c := blocksize | ||
116 | if length < blocksize { | ||
117 | c = int(length) | ||
118 | } | 102 | } |
119 | if _, err := io.ReadFull(r1, b1[0:c]); err != nil { | 103 | err = r.Close() |
104 | if err != nil { | ||
120 | t.Panic(err) | 105 | t.Panic(err) |
121 | } | 106 | } |
122 | if _, err := io.ReadFull(r2, b2[0:c]); err != nil { | 107 | err = w.Close() |
108 | if err != nil { | ||
123 | t.Panic(err) | 109 | t.Panic(err) |
124 | } | 110 | } |
125 | if bytes.Compare(b1[0:c], b2[0:c]) != 0 { | 111 | t.Done() |
126 | fmt.Println("B1 is", string(b1[0:c])) | 112 | }() |
127 | fmt.Println("B2 is", string(b2[0:c])) | 113 | } |
128 | t.Panic(errors.New(fmt.Sprint("Bytes do not compare at ", idx))) | 114 | |
115 | func (t *TestGroup) CompareStreams(r1 io.ReadCloser, r2 io.ReadCloser, length int64) { | ||
116 | go func() { | ||
117 | t.Add(1) | ||
118 | b1 := make([]byte, blocksize) | ||
119 | b2 := make([]byte, blocksize) | ||
120 | var idx int64 | ||
121 | for length > 0 { | ||
122 | c := blocksize | ||
123 | if length < blocksize { | ||
124 | c = int(length) | ||
125 | } | ||
126 | if _, err := io.ReadFull(r1, b1[0:c]); err != nil { | ||
127 | t.Panic(err) | ||
128 | } | ||
129 | if _, err := io.ReadFull(r2, b2[0:c]); err != nil { | ||
130 | t.Panic(err) | ||
131 | } | ||
132 | if bytes.Compare(b1[0:c], b2[0:c]) != 0 { | ||
133 | fmt.Println("B1 is", string(b1[0:c])) | ||
134 | fmt.Println("B2 is", string(b2[0:c])) | ||
135 | t.Panic(errors.New(fmt.Sprint("Bytes do not compare at ", idx))) | ||
136 | } | ||
137 | length -= int64(c) | ||
138 | idx += int64(c) | ||
129 | } | 139 | } |
130 | length -= int64(c) | 140 | t.Done() |
131 | idx += int64(c) | 141 | }() |
132 | } | ||
133 | t.Done() | ||
134 | } | 142 | } |
135 | 143 | ||
136 | func NewRunner() (*Runner, error) { | 144 | func NewRunner() (*Runner, error) { |