71 lines
1.7 KiB
Go
71 lines
1.7 KiB
Go
package fileUtilities
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"testing"
|
|
)
|
|
|
|
func Test_createParityFile(t *testing.T) {
|
|
type args struct {
|
|
in1 io.Reader
|
|
in2 io.Reader
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
wantOut string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "create parity file",
|
|
args: args{
|
|
in1: bytes.NewBuffer([]byte{0, 1, 2, 3, 4, 5, 6, 7}),
|
|
in2: bytes.NewBuffer([]byte{7, 6, 5, 4, 3, 2, 1, 0}),
|
|
},
|
|
wantOut: string([]byte{7, 7, 7, 7, 7, 7, 7, 7}),
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "in1 is longer than in2",
|
|
args: args{
|
|
in1: bytes.NewBuffer([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8}),
|
|
in2: bytes.NewBuffer([]byte{7, 6, 5, 4, 3, 2, 1, 0}),
|
|
},
|
|
wantOut: string([]byte{7, 7, 7, 7, 7, 7, 7, 7, 8}),
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "in2 is longer than in1",
|
|
args: args{
|
|
in1: bytes.NewBuffer([]byte{0, 1, 2, 3, 4, 5, 6, 7}),
|
|
in2: bytes.NewBuffer([]byte{7, 6, 5, 4, 3, 2, 1, 0, 54}),
|
|
},
|
|
wantOut: string([]byte{7, 7, 7, 7, 7, 7, 7, 7, 54}),
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "parity recreates original file",
|
|
args: args{
|
|
in1: bytes.NewBuffer([]byte{0, 1, 2, 3, 4, 5, 6, 7}),
|
|
in2: bytes.NewBuffer([]byte{7, 7, 7, 7, 7, 7, 7, 7, 54}),
|
|
},
|
|
wantOut: string([]byte{7, 6, 5, 4, 3, 2, 1, 0, 54}),
|
|
wantErr: false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
out := &bytes.Buffer{}
|
|
err := parityStream(tt.args.in1, tt.args.in2, out)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("parityStream() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if gotOut := out.String(); gotOut != tt.wantOut {
|
|
t.Errorf("parityStream() gotOut = %v, want %v", gotOut, tt.wantOut)
|
|
}
|
|
})
|
|
}
|
|
}
|