Skip to content

Commit

Permalink
integer to bytes conversion tests handle signed ints correctly. Thank…
Browse files Browse the repository at this point in the history
…s @c633
  • Loading branch information
liamsi committed Aug 4, 2016
1 parent 22d7236 commit 398371c
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions utils/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ func TestIntToBytes(t *testing.T) {
if int(binary.LittleEndian.Uint32(b)) != numInt {
t.Fatal("Conversion to bytes looks wrong!")
}
// TODO It is possible to call `IntToBytes` with a signed int < 0
// isn't that problematic?
// for instance this will fail:
// b := IntToBytes(-42)
// if int(binary.LittleEndian.Uint32(b)) != numInt {
// t.Fatal("Conversion to bytes looks wrong!")
// }
numInt = -42
b = IntToBytes(numInt)
if int32(binary.LittleEndian.Uint32(b)) != int32(numInt) {
t.Fatal("Conversion to bytes looks wrong!")
}
}

func TestULongToBytes(t *testing.T) {
Expand All @@ -58,5 +56,9 @@ func TestLongToBytes(t *testing.T) {
if int64(binary.LittleEndian.Uint64(b)) != numInt {
t.Fatal("Conversion to bytes looks wrong!")
}
// TODO similar problem as in TestIntToBytes
numInt = int64(-42)
b = LongToBytes(numInt)
if int64(binary.LittleEndian.Uint64(b)) != numInt {
t.Fatal("Conversion to bytes looks wrong!")
}
}

0 comments on commit 398371c

Please sign in to comment.