Skip to content

Commit

Permalink
Add test for bytes.go.
Browse files Browse the repository at this point in the history
  • Loading branch information
justicezyx committed Jul 2, 2016
1 parent 496e23d commit 31ce283
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions util/bytes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package util

import (
"reflect"
"testing"
)

func TestBytesToUint64(t *testing.T) {
cases := []struct {
bytes []byte
int_val uint64
desc string
}{{
bytes: []byte{0x00, 0x00},
int_val: 0,
desc: "all zero",
}, {
bytes: []byte{0x01, 0x00},
int_val: 256,
desc: "one in first byte",
}}
for _, c := range cases {
got_bytes := make([]byte, 8, 8)
Uint64toBytes(got_bytes, c.int_val)
got_int64 := BytesToUint64(c.bytes)
if got_int64 != c.int_val || !reflect.DeepEqual(got_bytes[6:], c.bytes) {
t.Errorf("Failed: %v, got %v %v", c, got_bytes, got_int64)
}

got_bytes = make([]byte, 4, 4)
Uint32toBytes(got_bytes, uint32(c.int_val))
got_int32 := BytesToUint32(c.bytes)
if uint64(got_int32) != c.int_val || !reflect.DeepEqual(got_bytes[2:], c.bytes) {
t.Errorf("Failed: %v, got %v %v", c, got_bytes, got_int32)
}

got_bytes = make([]byte, 2, 2)
Uint16toBytes(got_bytes, uint16(c.int_val))
got_int16 := BytesToUint16(c.bytes)
if got_int16 != uint16(c.int_val) || !reflect.DeepEqual(got_bytes, c.bytes) {
t.Errorf("Failed: %v, got %v %v", c, got_bytes, got_int16)
}
}
}

0 comments on commit 31ce283

Please sign in to comment.