mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-24 21:56:43 +00:00
common: cleanup CopyBytes and associated test
This commit is contained in:
parent
dae698cd6d
commit
d297ea7dd8
2 changed files with 16 additions and 10 deletions
|
|
@ -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 {
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue