forked from im-kulikov/hrw
-
Notifications
You must be signed in to change notification settings - Fork 4
/
v1_test.go
80 lines (60 loc) · 1.86 KB
/
v1_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
75
76
77
78
79
80
package hrw
import (
"testing"
"github.com/stretchr/testify/require"
)
// Compatibility tests to keep behavior the same
// compared with the v1-versioned library functions.
func TestSortSliceByIndex(t *testing.T) {
actual := []string{"a", "b", "c", "d", "e", "f"}
expect := []string{"e", "a", "c", "f", "d", "b"}
testSlice := stringsToHashAndValueWithIndex(actual)
Sort(testSlice, WrapBytes(testKey))
result := hashAndValueToStrings(testSlice)
require.Equal(t, expect, result)
}
func TestSortSliceByWeightIndex(t *testing.T) {
actual := []string{"a", "b", "c", "d", "e", "f"}
weights := []float64{1, 1, 1, 0.2, 0.2, 0.2}
expect := []string{"a", "c", "b", "e", "f", "d"}
testSlice := stringsToHashAndValueWithIndex(actual)
SortWeighted(testSlice, weights, WrapBytes(testKey))
result := hashAndValueToStrings(testSlice)
require.Equal(t, expect, result)
}
func TestSortSliceByValue(t *testing.T) {
actual := []string{"a", "b", "c", "d", "e", "f"}
expect := []string{"d", "f", "c", "b", "a", "e"}
testSlice := stringsToHashAndValueWithValue(actual)
Sort(testSlice, WrapBytes(testKey))
result := hashAndValueToStrings(testSlice)
require.Equal(t, expect, result)
}
type hashAndValue struct {
hash uint64
val string
}
func (h hashAndValue) Hash() uint64 {
return h.hash
}
func stringsToHashAndValueWithIndex(ss []string) []hashAndValue {
res := make([]hashAndValue, 0, len(ss))
for i, s := range ss {
res = append(res, hashAndValue{hash: uint64(i), val: s})
}
return res
}
func stringsToHashAndValueWithValue(ss []string) []hashAndValue {
res := make([]hashAndValue, 0, len(ss))
for _, s := range ss {
res = append(res, hashAndValue{hash: WrapBytes([]byte(s)).Hash(), val: s})
}
return res
}
func hashAndValueToStrings(ss []hashAndValue) []string {
res := make([]string, 0, len(ss))
for _, s := range ss {
res = append(res, s.val)
}
return res
}