-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathfeature_test.go
100 lines (84 loc) · 2.79 KB
/
feature_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// 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 (
"golang.org/x/text/unicode/norm"
"testing"
)
func TestNewFeature(t *testing.T) {
expected := uint64(8811532157352841348)
f := NewFeature([]byte("test string"))
if f.Weight() != 1 {
t.Errorf("feature.Weight(): expected 1, actual %d", f.Weight())
}
if f.Sum() != expected {
t.Errorf("feature.Sum(): expected %d, actual %d", expected, f.Sum())
}
}
func TestNewFeatureWithWeight(t *testing.T) {
weight := 10
expected := uint64(8811532157352841348)
f := NewFeatureWithWeight([]byte("test string"), weight)
if f.Weight() != weight {
t.Errorf("feature.Weight(): expected %d, actual %d", weight, f.Weight())
}
if f.Sum() != expected {
t.Errorf("feature.Sum(): expected %d, actual %d", expected, f.Sum())
}
}
func TestFeatureSet(t *testing.T) {
text := []byte("here's a test string.")
fs := NewWordFeatureSet(text)
expected := []Feature{
NewFeature([]byte("here's")),
NewFeature([]byte("a")),
NewFeature([]byte("test")),
NewFeature([]byte("string")),
}
actual := fs.GetFeatures()
for i := 0; i < len(actual); i++ {
if actual[i].Sum() != expected[i].Sum() {
t.Errorf("feature.Sum(): expected %d, actual %d", expected[i].Sum(), actual[i].Sum())
}
if actual[i].Weight() != expected[i].Weight() {
t.Errorf("feature.Weight(): expected %d, actual %d", expected[i].Weight(), actual[i].Weight())
}
}
}
func TestUnicodeWordFeatureSet(t *testing.T) {
text := []byte("la fin d'un bel après-midi d'été")
fs := NewUnicodeWordFeatureSet(text, norm.NFKC)
expected := []Feature{
NewFeature([]byte("la")),
NewFeature([]byte("fin")),
NewFeature([]byte("d'un")),
NewFeature([]byte("bel")),
NewFeature([]byte("après-midi")),
NewFeature([]byte("d'été")),
}
actual := fs.GetFeatures()
for i := 0; i < len(actual); i++ {
if actual[i].Sum() != expected[i].Sum() {
t.Errorf("feature.Sum(): expected %d, actual %d", expected[i].Sum(), actual[i].Sum())
}
if actual[i].Weight() != expected[i].Weight() {
t.Errorf("feature.Weight(): expected %d, actual %d", expected[i].Weight(), actual[i].Weight())
}
}
}
func TestGetFeatures(t *testing.T) {
actual := getFeatures([]byte("test string"), boundaries)
expected := []Feature{NewFeature([]byte("test")), NewFeature([]byte("string"))}
if len(actual) != len(expected) {
t.Errorf("getFeatures returned wrong number of features")
}
for i := 0; i < len(actual); i++ {
if actual[i].Sum() != expected[i].Sum() {
t.Errorf("feature.Sum(): expected %d, actual %d", expected[i].Sum(), actual[i].Sum())
}
if actual[i].Weight() != expected[i].Weight() {
t.Errorf("feature.Weight(): expected %d, actual %d", expected[i].Weight(), actual[i].Weight())
}
}
}