| 1 |
package arrays |
| 2 |
|
| 3 |
import ( |
| 4 |
. "gopkg.in/check.v1" |
| 5 |
"testing" |
| 6 |
"fmt" |
| 7 |
) |
| 8 |
|
| 9 |
func Test(t *testing.T) { TestingT(t) } |
| 10 |
|
| 11 |
type TestSuite struct{} |
| 12 |
|
| 13 |
var _ = Suite(&TestSuite{}) |
| 14 |
|
| 15 |
func (s *TestSuite) TestXor(c *C) { |
| 16 |
//given |
| 17 |
data := []byte{0xFF, 0x00, 0xF0, 0x0F, 0x55, 0xAA, 0xBB, 0xCC} |
| 18 |
|
| 19 |
//when |
| 20 |
test_1 := Xor(data, []byte{0x00, 0xFF, 0x0F, 0xF0, 0xAA, 0x55, 0x44, 0x33}) |
| 21 |
test_2 := Xor(data, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}) |
| 22 |
|
| 23 |
//then |
| 24 |
c.Assert(test_1, DeepEquals, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}) |
| 25 |
c.Assert(test_2, DeepEquals, []byte{0x00, 0xFF, 0x0F, 0xF0, 0xAA, 0x55, 0x44, 0x33}) |
| 26 |
} |
| 27 |
|
| 28 |
func (s *TestSuite) TestSlice(c *C) { |
| 29 |
//given |
| 30 |
data := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8} |
| 31 |
|
| 32 |
//when |
| 33 |
test, err := Slice(data, 3) |
| 34 |
|
| 35 |
//then |
| 36 |
c.Assert(err, IsNil) |
| 37 |
c.Assert(len(test), Equals, 3) |
| 38 |
c.Assert(test[0], DeepEquals, []byte{0, 1, 2}) |
| 39 |
c.Assert(test[1], DeepEquals, []byte{3, 4, 5}) |
| 40 |
c.Assert(test[2], DeepEquals, []byte{6, 7, 8}) |
| 41 |
} |
| 42 |
|
| 43 |
func (s *TestSuite) TestSliceUnequal(c *C) { |
| 44 |
//given |
| 45 |
data := []byte{0, 1, 2, 3, 4, 5, 6, 7 } |
| 46 |
|
| 47 |
//when |
| 48 |
test, err := Slice(data, 3) |
| 49 |
|
| 50 |
//then |
| 51 |
fmt.Printf("err = %v\n", err) |
| 52 |
c.Assert(err, NotNil) |
| 53 |
c.Assert(test, IsNil) |
| 54 |
} |
| 55 |
|
| 56 |
func (s *TestSuite) TestSliceEmpty(c *C) { |
| 57 |
//given |
| 58 |
data := []byte{} |
| 59 |
|
| 60 |
//when |
| 61 |
test, err := Slice(data, 3) |
| 62 |
|
| 63 |
//then |
| 64 |
fmt.Printf("err = %v\n", err) |
| 65 |
c.Assert(err, NotNil) |
| 66 |
c.Assert(test, IsNil) |
| 67 |
} |
| 68 |
|
| 69 |
func (s *TestSuite) TestConcat(c *C) { |
| 70 |
//given |
| 71 |
a := []byte{1, 2, 3} |
| 72 |
b := []byte{4, 5} |
| 73 |
d := []byte{6} |
| 74 |
e := []byte{} |
| 75 |
f := []byte{7, 8, 9, 10} |
| 76 |
|
| 77 |
//when |
| 78 |
test := Concat(a, b, d, e, f) |
| 79 |
|
| 80 |
//then |
| 81 |
c.Assert(test, DeepEquals, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) |
| 82 |
} |
| 83 |
|
| 84 |
func (s *TestSuite) TestUnwrap(c *C) { |
| 85 |
//given |
| 86 |
a := []byte{1, 2, 3} |
| 87 |
b := []byte{4, 5} |
| 88 |
d := []byte{6} |
| 89 |
e := []byte{} |
| 90 |
f := []byte{7, 8, 9, 10} |
| 91 |
|
| 92 |
//when |
| 93 |
test := Unwrap([][]byte{a, b, d, e, f}) |
| 94 |
|
| 95 |
//then |
| 96 |
c.Assert(test, DeepEquals, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) |
| 97 |
} |
| 98 |
|
| 99 |
func (s *TestSuite) TestUInt32ToBytes(c *C) { |
| 100 |
//then |
| 101 |
c.Assert(UInt32ToBytes(0xFF), DeepEquals, []byte{0x00, 0x00, 0x00, 0xFF}) |
| 102 |
c.Assert(UInt32ToBytes(0xFFFFFFFE), DeepEquals, []byte{0xff, 0xff, 0xff, 0xfe}) |
| 103 |
} |
| 104 |
|
| 105 |
func (s *TestSuite) TestUInt64ToBytes(c *C) { |
| 106 |
//then |
| 107 |
c.Assert(UInt64ToBytes(0xFF), DeepEquals, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF}) |
| 108 |
c.Assert(UInt64ToBytes(0xFFFFFFFFFFFFFFFE), DeepEquals, []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}) |
| 109 |
} |