neoneo-go/pkg/util/slice/array_test.go
Anna Shaleva 8162e9033d *: replace slice.Copy with bytes.Clone
And refactor some code a bit, don't use bytes.Clone where type-specific
helpers may be used instead.

Close #2907.

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
2024-03-05 13:54:10 +03:00

60 lines
1,016 B
Go

package slice
import (
"bytes"
"testing"
"github.com/stretchr/testify/require"
)
var testCases = []struct {
arr []byte
rev []byte
}{
{
arr: []byte{},
rev: []byte{},
},
{
arr: []byte{0x01},
rev: []byte{0x01},
},
{
arr: []byte{0x01, 0x02, 0x03, 0x04},
rev: []byte{0x04, 0x03, 0x02, 0x01},
},
{
arr: []byte{0x01, 0x02, 0x03, 0x04, 0x05},
rev: []byte{0x05, 0x04, 0x03, 0x02, 0x01},
},
}
func TestCopyReverse(t *testing.T) {
for _, tc := range testCases {
arg := bytes.Clone(tc.arr)
require.Equal(t, tc.arr, arg)
have := CopyReverse(arg)
require.Equal(t, tc.rev, have)
// test that argument was copied
for i := range have {
have[i] = ^have[i]
}
require.Equal(t, tc.arr, arg)
Reverse(arg)
require.Equal(t, tc.rev, arg)
if len(tc.arr) > 1 {
require.NotEqual(t, tc.arr, arg)
}
}
}
func TestClean(t *testing.T) {
for _, tc := range testCases[1:] { // Empty one will be equal.
cp := bytes.Clone(tc.arr)
Clean(cp)
require.NotEqual(t, tc.arr, cp)
}
}