mirror of
https://github.com/kopia/kopia.git
synced 2026-01-28 16:23:04 -05:00
* refactor: move from io/ioutil to io and os package The io/ioutil package has been deprecated as of Go 1.16, see https://golang.org/doc/go1.16#ioutil. This commit replaces the existing io/ioutil functions with their new definitions in io and os packages. Signed-off-by: Eng Zer Jun <engzerjun@gmail.com> * chore: remove //nolint:gosec for os.ReadFile At the time of this commit, the G304 rule of gosec does not include the `os.ReadFile` function. We remove `//nolint:gosec` temporarily until https://github.com/securego/gosec/pull/706 is merged. Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
116 lines
2.0 KiB
Go
116 lines
2.0 KiB
Go
package gather
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
var sample1 = []byte("hello! how are you? nice to meet you.")
|
|
|
|
func TestGatherBytes(t *testing.T) {
|
|
// split the 'whole' into equivalent Bytes slicings in some interesting ways
|
|
cases := []struct {
|
|
whole []byte
|
|
sliced Bytes
|
|
}{
|
|
{
|
|
whole: []byte{},
|
|
sliced: Bytes{},
|
|
},
|
|
{
|
|
whole: []byte{},
|
|
sliced: Bytes{Slices: [][]byte{
|
|
nil,
|
|
}},
|
|
},
|
|
{
|
|
whole: []byte{},
|
|
sliced: Bytes{Slices: [][]byte{
|
|
nil,
|
|
{},
|
|
nil,
|
|
}},
|
|
},
|
|
{
|
|
whole: sample1,
|
|
sliced: FromSlice(sample1),
|
|
},
|
|
{
|
|
whole: sample1,
|
|
sliced: Bytes{Slices: [][]byte{
|
|
nil,
|
|
sample1,
|
|
nil,
|
|
}},
|
|
},
|
|
{
|
|
whole: sample1,
|
|
sliced: Bytes{Slices: [][]byte{
|
|
sample1[0:20],
|
|
sample1[20:],
|
|
}},
|
|
},
|
|
{
|
|
whole: sample1,
|
|
sliced: Bytes{Slices: [][]byte{
|
|
sample1[0:20],
|
|
nil, // zero-length
|
|
{}, // zero-length
|
|
sample1[20:],
|
|
}},
|
|
},
|
|
{
|
|
whole: sample1,
|
|
sliced: Bytes{Slices: [][]byte{
|
|
sample1[0:10],
|
|
sample1[10:25],
|
|
sample1[25:30],
|
|
sample1[30:31],
|
|
sample1[31:],
|
|
}},
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
b := tc.sliced
|
|
|
|
// length
|
|
if got, want := b.Length(), len(tc.whole); got != want {
|
|
t.Errorf("unexpected length: %v, want %v", got, want)
|
|
}
|
|
|
|
// reader
|
|
all, err := io.ReadAll(b.Reader())
|
|
if err != nil {
|
|
t.Errorf("unable to read: %v", err)
|
|
}
|
|
|
|
if !bytes.Equal(all, tc.whole) {
|
|
t.Errorf("unexpected data read %v, want %v", string(all), string(tc.whole))
|
|
}
|
|
|
|
// GetBytes
|
|
all = b.ToByteSlice()
|
|
if !bytes.Equal(all, tc.whole) {
|
|
t.Errorf("unexpected data from GetBytes() %v, want %v", string(all), string(tc.whole))
|
|
}
|
|
|
|
// AppendSectionTo - test exhaustively all combinationf os start, length
|
|
var tmp WriteBuffer
|
|
defer tmp.Close()
|
|
|
|
for i := 0; i <= len(tc.whole); i++ {
|
|
for j := i; j <= len(tc.whole); j++ {
|
|
tmp.Reset()
|
|
|
|
b.AppendSectionTo(&tmp, i, j-i)
|
|
|
|
require.Equal(t, tmp.ToByteSlice(), tc.whole[i:j])
|
|
}
|
|
}
|
|
}
|
|
}
|