forked from cornelk/hashmap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util_test.go
64 lines (57 loc) · 1.54 KB
/
util_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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package hashmap
import (
"math"
"reflect"
"testing"
)
func TestLog2(t *testing.T) {
var fixtures = map[uintptr]uintptr{
0: 0,
1: 0,
2: 1,
3: 2,
4: 2,
5: 3,
}
for input, result := range fixtures {
output := log2(input)
if output != result {
t.Errorf("Log2 of %d should have been %d but was %d", input, result, output)
}
}
}
func TestKeyHash(t *testing.T) {
type testFixture struct {
input interface{}
output uintptr
}
var fixtures = []testFixture{
{input: "123", output: 9076048966884696828},
{input: []byte("123"), output: 9076048966884696828},
{input: int(1), output: 1754102016959854353},
{input: int8(1), output: 1754102016959854353},
{input: int16(-1), output: 6588593453755867710},
{input: int32(math.MaxInt32), output: 15638166383137924496},
{input: int64(math.MaxInt64), output: 4889285460913276945},
{input: uint(0), output: 9257401834698437112},
{input: uint8(123), output: 2662021623061770816},
{input: uint16(1234), output: 1663804089773015140},
{input: uint32(12345), output: 11667327197262824396},
{input: uint64(123456), output: 9063688366117729139},
{input: uintptr(1234567), output: 14770111569646361914},
}
for _, f := range fixtures {
output := getKeyHash(f.input)
if output != f.output {
t.Errorf("Key hash of %v and type %v should have been %d but was %d", f.input, reflect.TypeOf(f.input), f.output, output)
}
}
}
func TestKeyHashPanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("Invalid type did not panic")
}
}()
_ = getKeyHash(true)
}