common: cleanup CopyBytes and associated test

This commit is contained in:
Joubin Muhammad Houshyar 2016-06-09 19:11:36 -04:00
parent dae698cd6d
commit d297ea7dd8
2 changed files with 16 additions and 10 deletions

View file

@ -107,14 +107,9 @@ func ReadVarInt(buff []byte) (ret uint64) {
return
}
// Copy bytes
//
// Returns an exact copy of the provided bytes
func CopyBytes(b []byte) (copiedBytes []byte) {
copiedBytes = make([]byte, len(b))
copy(copiedBytes, b)
return
return append([]byte{}, b...)
}
func HasHexPrefix(str string) bool {

View file

@ -76,10 +76,21 @@ func (s *BytesSuite) TestReadVarInt(c *checker.C) {
}
func (s *BytesSuite) TestCopyBytes(c *checker.C) {
data1 := []byte{1, 2, 3, 4}
exp1 := []byte{1, 2, 3, 4}
res1 := CopyBytes(data1)
c.Assert(res1, checker.DeepEquals, exp1)
data := [][]byte{
[]byte{0x00, 0x7f, 0x80, 0xff}, // interesting bytes
[]byte{}, // empty case
}
// test
for i := 0; i < len(data); i++ {
have := CopyBytes(data[i])
if data[i] != nil {
// make sure we're not just returning the input arg
c.Assert(&have, checker.Not(checker.Equals), &data[i])
}
// verify deep copy
c.Assert(have, checker.DeepEquals, data[i])
}
}
func (s *BytesSuite) TestIsHex(c *checker.C) {