This repository has been archived by the owner on Apr 10, 2019. It is now read-only.
forked from ekzhu/lsh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_test.go
75 lines (70 loc) · 1.58 KB
/
basic_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
package lsh
import (
"strconv"
"testing"
)
func Test_NewBasicLsh(t *testing.T) {
lsh := NewBasicLsh(5, 5, 100, 5.0)
if len(lsh.tables) != 5 {
t.Error("Lsh init fail")
}
}
func Test_Insert(t *testing.T) {
lsh := NewBasicLsh(100, 5, 5, 5.0)
points := randomPoints(10, 100, 32.0)
for i, p := range points {
lsh.Insert(p, strconv.Itoa(i))
}
for _, table := range lsh.tables {
if len(table) == 0 {
t.Error("Insert fail")
}
}
}
func Test_Query(t *testing.T) {
lsh := NewBasicLsh(100, 5, 5, 5.0)
points := randomPoints(10, 100, 32.0)
insertedKeys := make([]string, 10)
for i, p := range points {
lsh.Insert(p, strconv.Itoa(i))
insertedKeys[i] = strconv.Itoa(i)
}
// Use the inserted points as queries, and
// verify that we can get back each query itself
for i, key := range insertedKeys {
found := false
for _, foundKey := range lsh.Query(points[i]) {
if foundKey == key {
found = true
}
}
if !found {
t.Error("Query fail")
}
}
}
func Test_Delete(t *testing.T) {
lsh := NewBasicLsh(100, 5, 5, 5.0)
points := randomPoints(10, 100, 32.0)
for i, p := range points {
lsh.Insert(p, strconv.Itoa(i))
}
for i, p := range points {
lsh.Delete(strconv.Itoa(i))
for _, table := range lsh.tables {
if len(table) != len(points)-(i+1) {
t.Errorf("Failed to delete point %v. Expected to have %v points, found %v.", i, len(points)-(i+1), len(table))
}
}
found := false
for _, foundKey := range lsh.Query(p) {
if foundKey == strconv.Itoa(i) {
found = true
}
}
if found {
t.Errorf("Failed to delete point %v.", i)
}
}
Test_Insert(t)
}