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 return
} }
// Copy bytes
//
// Returns an exact copy of the provided bytes // Returns an exact copy of the provided bytes
func CopyBytes(b []byte) (copiedBytes []byte) { func CopyBytes(b []byte) (copiedBytes []byte) {
copiedBytes = make([]byte, len(b)) return append([]byte{}, b...)
copy(copiedBytes, b)
return
} }
func HasHexPrefix(str string) bool { 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) { func (s *BytesSuite) TestCopyBytes(c *checker.C) {
data1 := []byte{1, 2, 3, 4} data := [][]byte{
exp1 := []byte{1, 2, 3, 4} []byte{0x00, 0x7f, 0x80, 0xff}, // interesting bytes
res1 := CopyBytes(data1) []byte{}, // empty case
c.Assert(res1, checker.DeepEquals, exp1) }
// 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) { func (s *BytesSuite) TestIsHex(c *checker.C) {