-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnumbers_test.go
45 lines (37 loc) · 969 Bytes
/
numbers_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package database
import (
"reflect"
"testing"
)
var binUint64Tests = []struct {
v uint64
b []byte
}{
{1, []byte{0, 0, 0, 0, 0, 0, 0, 1}},
{18446744073709551616 - 1, []byte{255, 255, 255, 255, 255, 255, 255, 255}},
}
func TestUint64ToBin(t *testing.T) {
for row, test := range binUint64Tests {
b := uint64ToBin(test.v)
if !reflect.DeepEqual(b, test.b) {
t.Errorf("Row: %d returned unexpected binary, got: %v, wanted: %v", row, b, test.b)
}
}
}
func TestBinToUint64(t *testing.T) {
for row, test := range binUint64Tests {
v := binToUint64(test.b)
if v != test.v {
t.Errorf("Row: %d returned unexpected number, got: %d, wanted: %d", row, v, test.v)
}
}
}
func TestBinToUint64_invalidLength(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Invalid length not equal to 8 should panic")
}
}()
binToUint64(make([]byte, 1)) // should panic, must be equal to 8
binToUint64(make([]byte, 9)) // same as above
}