From 37be2e215c336e4b3ac4e3645136f2432de7990b Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 23 Aug 2019 15:44:49 +0300 Subject: [PATCH] array: add a test for even num of elements And drop duplicating code from _pkg.dev. --- _pkg.dev/wire/util/slice/slice.go | 15 ------------ _pkg.dev/wire/util/slice/slice_test.go | 33 -------------------------- pkg/util/array_test.go | 11 ++++++++- 3 files changed, 10 insertions(+), 49 deletions(-) delete mode 100644 _pkg.dev/wire/util/slice/slice.go delete mode 100644 _pkg.dev/wire/util/slice/slice_test.go diff --git a/_pkg.dev/wire/util/slice/slice.go b/_pkg.dev/wire/util/slice/slice.go deleted file mode 100644 index fdb93adc3..000000000 --- a/_pkg.dev/wire/util/slice/slice.go +++ /dev/null @@ -1,15 +0,0 @@ -package slice - -// Reverse return a reversed version of the given byte slice. -func Reverse(b []byte) []byte { - // Protect from big.Ints that have 1 len bytes. - if len(b) < 2 { - return b - } - - dest := make([]byte, len(b)) - for i, j := 0, len(b)-1; i < j+1; i, j = i+1, j-1 { - dest[i], dest[j] = b[j], b[i] - } - return dest -} diff --git a/_pkg.dev/wire/util/slice/slice_test.go b/_pkg.dev/wire/util/slice/slice_test.go deleted file mode 100644 index e65c63f65..000000000 --- a/_pkg.dev/wire/util/slice/slice_test.go +++ /dev/null @@ -1,33 +0,0 @@ -package slice - -import ( - "bytes" - "testing" -) - -func TestSliceReverse(t *testing.T) { - arr := []byte{0x01, 0x02, 0x03, 0x04} - have := Reverse(arr) - want := []byte{0x04, 0x03, 0x02, 0x01} - if bytes.Compare(have, want) != 0 { - t.Fatalf("expected %v got %v", want, have) - } -} -func TestSliceReverseOddNumberOfElements(t *testing.T) { - arr := []byte{0x01, 0x02, 0x03, 0x04, 0x05} - have := Reverse(arr) - want := []byte{0x05, 0x04, 0x03, 0x02, 0x01} - if bytes.Compare(have, want) != 0 { - t.Fatalf("expected %v got %v", want, have) - } -} - -// This tests a bug that occured with arrays of size 1 -func TestSliceReverseLen2(t *testing.T) { - arr := []byte{0x01} - have := Reverse(arr) - want := []byte{0x01} - if bytes.Compare(have, want) != 0 { - t.Fatalf("expected %v got %v", want, have) - } -} diff --git a/pkg/util/array_test.go b/pkg/util/array_test.go index f8d78c979..13ab01330 100644 --- a/pkg/util/array_test.go +++ b/pkg/util/array_test.go @@ -5,7 +5,16 @@ import ( "testing" ) -func TestArrayReverse(t *testing.T) { +func TestArrayEvenReverse(t *testing.T) { + arr := []byte{0x01, 0x02, 0x03, 0x04} + have := ArrayReverse(arr) + want := []byte{0x04, 0x03, 0x02, 0x01} + if !bytes.Equal(have, want) { + t.Fatalf("expected %v got %v", want, have) + } +} + +func TestArrayOddReverse(t *testing.T) { arr := []byte{0x01, 0x02, 0x03, 0x04, 0x05} have := ArrayReverse(arr) want := []byte{0x05, 0x04, 0x03, 0x02, 0x01}