-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathda_test.go
129 lines (110 loc) · 2.19 KB
/
da_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package double_array
import (
"reflect"
"testing"
)
func getData() []Item {
data := []string{
"abc",
"abcd",
"文字",
"全角",
"全角文字",
"x",
"y",
"z",
"xyzabc",
"good",
"漢字",
}
ret := make([]Item, len(data))
for i, item := range data {
ret[i] = Item(item)
}
return ret
}
func TestDoubleArray_Lookup(t *testing.T) {
data := getData()
da, err := NewDoubleArray(data)
if err != nil {
t.Error(err)
}
testData := []struct {
item string
exists bool
}{
{
item: "ab",
exists: false,
},
{
item: "bc",
exists: false,
},
{
item: "abc",
exists: true,
},
{
item: "abc",
exists: true,
},
{
item: "漢字",
exists: true,
},
{
item: "ひらがな",
exists: false,
},
}
inverse := ToInverseID(da)
for _, tt := range testData {
itemID := da.Lookup([]rune(tt.item))
if !tt.exists {
if itemID != ItemNotFound {
t.Errorf("Item wrongly found: %s", tt.item)
}
continue
}
if itemID == ItemNotFound {
t.Errorf("Item not found: %s", tt.item)
continue
}
deserialized := Deserialize(da, itemID, inverse)
if deserialized != tt.item {
t.Errorf("Deserialization failed: expected %s, actual: %s", tt.item, deserialized)
}
}
}
func TestDoubleArray_Scan(t *testing.T) {
data := getData()
da, err := NewDoubleArray(data)
if err != nil {
t.Error(err)
}
testData := []struct {
text string
expected []string
}{
{
text: "昔は全角文字が表示できないコンピューターも多かった。",
expected: []string{"全角文字"},
},
{
text: "昔は全角文字が表示できないコンピューターも多かった。文字の形を保存しておくためのメモリが不足していたためだ。",
expected: []string{"文字", "全角文字"},
},
}
inverse := ToInverseID(da)
for _, tt := range testData {
actual := make([]string, 0)
da.Scan([]rune(tt.text), func(i, j int, id ItemID) {
deserialized := Deserialize(da, id, inverse)
actual = append(actual, deserialized)
})
if !reflect.DeepEqual(tt.expected, actual) {
t.Errorf("failed to extract entries: expected: %v, actual %v", tt.expected, actual)
}
}
}