-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathsimhash_test.go
62 lines (53 loc) · 1.57 KB
/
simhash_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
// Copyright 2013 Matthew Fonda. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package simhash
import (
"fmt"
"testing"
)
func TestSimhash(t *testing.T) {
var fp = []uint64{
Simhash(&WordFeatureSet{[]byte("this is a test phrase")}),
Simhash(&WordFeatureSet{[]byte("this is a test phrass")}),
Simhash(&WordFeatureSet{[]byte("foo bar")}),
}
if Compare(fp[0], fp[1]) != 2 {
t.Errorf("Comparison failed")
}
if Compare(fp[0], fp[2]) != 29 {
t.Errorf("Comparison failed")
}
}
var words = [][]byte{
[]byte("one"),
[]byte("two"),
[]byte("three"),
[]byte("four"),
[]byte("five"),
}
var shingleTests = []struct {
words [][]byte
w int
expected [][]byte
}{
{words, 1, words},
{words, 2, [][]byte{[]byte("one two"), []byte("two three"), []byte("three four"), []byte("four five")}},
{words, 3, [][]byte{[]byte("one two three"), []byte("two three four"), []byte("three four five")}},
{words, 4, [][]byte{[]byte("one two three four"), []byte("two three four five")}},
{words, 5, [][]byte{[]byte("one two three four five")}},
{words, 6, [][]byte{[]byte("one two three four five")}},
}
func TestShingle(t *testing.T) {
for _, tt := range shingleTests {
actual := Shingle(tt.w, tt.words)
if !equal(actual, tt.expected) {
t.Errorf("Shingle(%d, %v): expected %v, got %v", tt.w, tt.words, tt.expected, actual)
}
}
}
// Checks of two given [][]byte are equal
// TODO: is there a better way to do this?
func equal(a, b [][]byte) bool {
return fmt.Sprintf("%v", a) == fmt.Sprintf("%v", b)
}