forked from chrislusf/glow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
496e23d
commit 31ce283
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |