util: fix a bug with not copying slice of len=1 in ArrayReverse
ArrayReverse copies it's argument only if it's len is > 1. It needs to be consistent in all cases.
This commit is contained in:
parent
d07d6f3371
commit
24bb66e606
2 changed files with 10 additions and 5 deletions
|
@ -2,10 +2,6 @@ package util
|
||||||
|
|
||||||
// ArrayReverse returns a reversed version of the given byte slice.
|
// ArrayReverse returns a reversed version of the given byte slice.
|
||||||
func ArrayReverse(b []byte) []byte {
|
func ArrayReverse(b []byte) []byte {
|
||||||
// Protect from big.Ints that have 1 len bytes.
|
|
||||||
if len(b) < 2 {
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
dest := make([]byte, len(b))
|
dest := make([]byte, len(b))
|
||||||
for i, j := 0, len(b)-1; i <= j; i, j = i+1, j-1 {
|
for i, j := 0, len(b)-1; i <= j; i, j = i+1, j-1 {
|
||||||
dest[i], dest[j] = b[j], b[i]
|
dest[i], dest[j] = b[j], b[i]
|
||||||
|
|
|
@ -30,7 +30,16 @@ var testCases = []struct {
|
||||||
|
|
||||||
func TestArrayReverse(t *testing.T) {
|
func TestArrayReverse(t *testing.T) {
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
have := ArrayReverse(tc.arr)
|
arg := make([]byte, len(tc.arr))
|
||||||
|
copy(arg, tc.arr)
|
||||||
|
|
||||||
|
have := ArrayReverse(arg)
|
||||||
require.Equal(t, tc.rev, have)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue