-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdjb2_test.go
74 lines (66 loc) · 1.45 KB
/
djb2_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
65
66
67
68
69
70
71
72
73
74
package cacher
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestDjb2Hasher(t *testing.T) {
h := newDjb2Hasher()
t.Run("should return the same value twice", func(t *testing.T) {
// given
s := "Hello"
// when
got1 := h.sumUint64(s)
got2 := h.sumUint64(s)
// then
assert.Equal(t, got1, got2)
})
t.Run("should return an expected value", func(t *testing.T) {
// given
expected := uint64(210676686969)
s := "Hello"
// when
got := h.sumUint64(s)
// then
assert.Equal(t, got, expected)
})
t.Run("should return different values when strings differ by 1 character", func(t *testing.T) {
// given
expected1 := uint64(210676686969)
expected2 := uint64(6952330670010)
s1 := "Hello"
s2 := "Hello!"
// when
got1 := h.sumUint64(s1)
got2 := h.sumUint64(s2)
// then
assert.NotEqual(t, got1, got2)
assert.Equal(t, expected1, got1)
assert.Equal(t, expected2, got2)
})
t.Run("should be case sensitive", func(t *testing.T) {
// given
s1 := "Hello"
s2 := "hello"
// when
got1 := h.sumUint64(s1)
got2 := h.sumUint64(s2)
// then
assert.NotEqual(t, got1, got2)
})
t.Run("should hash empty value (with many spaces)", func(t *testing.T) {
// given
s := " "
// when
got := h.sumUint64(s)
// then
assert.NotEmpty(t, got)
})
t.Run("should return 5381 when no chars", func(t *testing.T) {
// given
s := ""
// when
got := h.sumUint64(s)
// then
assert.Equal(t, uint64(5381), got)
})
}