-
Notifications
You must be signed in to change notification settings - Fork 1
/
embedding_test.go
94 lines (76 loc) · 2.35 KB
/
embedding_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
package golsh
import (
"reflect"
"testing"
)
func TestFakeRandom(t *testing.T) {
r := newFakeRandom([]float32{1.0, 2.0, 3.0})
if got, expected := r.draw(), float32(1.0); expected != got {
t.Fatalf("expected %f but got %f", expected, got)
}
if got, expected := r.draw(), float32(2.0); expected != got {
t.Fatalf("expected %f but got %f", expected, got)
}
if got, expected := r.draw(), float32(3.0); expected != got {
t.Fatalf("expected %f but got %f", expected, got)
}
}
func TestNewEmbedding(t *testing.T) {
r := newFakeRandom([]float32{1, 2, 3, 4})
got := newEmbedding(2, 4, &r)
if got, expected := len(got.normals), 2; got != expected {
t.Fatalf("expected %d but got %d", expected, got)
}
if got, expected := got.normals[0], []float32{1, 2, 3, 4}; !reflect.DeepEqual(got, expected) {
// t.Fatalf("expected %v but got %v", expected, got)
}
if got, expected := got.normals[1], []float32{1, 2, 3, 4}; !reflect.DeepEqual(got, expected) {
// t.Fatalf("expected %v but got %v", expected, got)
}
}
func TestNormal(t *testing.T) {
r := newFakeRandom([]float32{1.0, 2.0})
if got, expected := normal(2, &r), []float32{1.0, 2.0}; !reflect.DeepEqual(expected, got) {
// t.Fatalf("expected %f but got %f", expected, got)
}
}
func TestEmbed(t *testing.T) {
normalA := []float32{1.0, 0.0}
normalB := []float32{0.0, 1.0}
embedding := embedding{[][]float32{normalA, normalB}}
got := embedding.embed(1, []float32{1.0, 0.0})
if expected := "1-10"; got != expected {
t.Fatalf("expected %s but got %s", expected, got)
}
}
func TestDimension(t *testing.T) {
vecA := []float32{1.0, 1.0}
vecB := []float32{1.0, 0.0}
vecC := []float32{0.0, 0.0}
if got, expected := dimension(vecA, vecB), true; !reflect.DeepEqual(expected, got) {
t.Fatalf("expected %t but got %t", expected, got)
}
if got, expected := dimension(vecA, vecC), false; !reflect.DeepEqual(expected, got) {
t.Fatalf("expected %t but got %t", expected, got)
}
}
func TestBitString(t *testing.T) {
got := bitToString([]bool{true, false, true, true})
if expected := "1011"; got != expected {
t.Fatalf("expected %s but got %s", expected, got)
}
}
type fakeRandom struct {
index int
set []float32
}
func newFakeRandom(set []float32) fakeRandom {
return fakeRandom{-1, set}
}
func (f *fakeRandom) draw() float32 {
f.index++
if f.index > len(f.set)-1 {
f.index = 0
}
return f.set[f.index]
}