2021-07-18 12:55:37 +00:00
|
|
|
package slice
|
2018-03-02 15:24:09 +00:00
|
|
|
|
|
|
|
import (
|
2024-03-04 18:09:36 +00:00
|
|
|
"bytes"
|
2018-03-02 15:24:09 +00:00
|
|
|
"testing"
|
2019-11-24 14:55:50 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-03-02 15:24:09 +00:00
|
|
|
)
|
|
|
|
|
2020-02-06 08:56:34 +00:00
|
|
|
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},
|
|
|
|
},
|
2019-08-23 12:44:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 12:55:37 +00:00
|
|
|
func TestCopyReverse(t *testing.T) {
|
2020-02-06 08:56:34 +00:00
|
|
|
for _, tc := range testCases {
|
2024-03-04 18:09:36 +00:00
|
|
|
arg := bytes.Clone(tc.arr)
|
2021-07-18 13:32:10 +00:00
|
|
|
require.Equal(t, tc.arr, arg)
|
2020-02-06 08:59:46 +00:00
|
|
|
|
2021-07-18 12:55:37 +00:00
|
|
|
have := CopyReverse(arg)
|
2020-02-06 08:56:34 +00:00
|
|
|
require.Equal(t, tc.rev, have)
|
2020-02-06 08:59:46 +00:00
|
|
|
|
|
|
|
// test that argument was copied
|
|
|
|
for i := range have {
|
|
|
|
have[i] = ^have[i]
|
|
|
|
}
|
|
|
|
require.Equal(t, tc.arr, arg)
|
2021-07-18 13:08:23 +00:00
|
|
|
|
|
|
|
Reverse(arg)
|
|
|
|
require.Equal(t, tc.rev, arg)
|
2021-07-18 13:32:10 +00:00
|
|
|
if len(tc.arr) > 1 {
|
|
|
|
require.NotEqual(t, tc.arr, arg)
|
|
|
|
}
|
2020-02-06 08:56:34 +00:00
|
|
|
}
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
2022-09-01 19:05:34 +00:00
|
|
|
|
|
|
|
func TestClean(t *testing.T) {
|
|
|
|
for _, tc := range testCases[1:] { // Empty one will be equal.
|
2024-03-04 18:09:36 +00:00
|
|
|
cp := bytes.Clone(tc.arr)
|
2022-09-01 19:05:34 +00:00
|
|
|
Clean(cp)
|
|
|
|
require.NotEqual(t, tc.arr, cp)
|
|
|
|
}
|
|
|
|
}
|