-
Notifications
You must be signed in to change notification settings - Fork 1
/
set_test.go
173 lines (160 loc) · 4.11 KB
/
set_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package simplehstore
import (
"testing"
"github.com/xyproto/pinterface"
)
const setname = "testset"
func TestSet(t *testing.T) {
Verbose = true
//host := New() // locally
host := NewHost(defaultConnectionString)
defer host.Close()
set, err := NewSet(host, setname)
if err != nil {
t.Error(err)
}
set.Clear()
if err := set.Add(testdata1); err != nil {
t.Errorf("Error, could not add item to set! %s", err)
}
items, err := set.All()
if err != nil {
t.Errorf("Error when retrieving set! %s", err)
}
if len(items) != 1 {
t.Errorf("Error, wrong set length! %d", len(items))
}
if (len(items) > 0) && (items[0] != testdata1) {
t.Errorf("Error, wrong set contents! %v", items)
}
if err := set.Add(testdata2); err != nil {
t.Errorf("Error, could not add item to set! %s", err)
}
if err := set.Add(testdata3); err != nil {
t.Errorf("Error, could not add item to set! %s", err)
}
// Add an element twice. This is a set, so the element should only appear once.
if err := set.Add(testdata3); err != nil {
t.Errorf("Error, could not add item to set! %s", err)
}
items, err = set.All()
if err != nil {
t.Errorf("Error when retrieving set! %s", err)
}
if len(items) != 3 {
t.Errorf("Error, wrong set length! %d\n%v\n", len(items), items)
}
err = set.Remove()
if err != nil {
t.Errorf("Error, could not remove set! %s", err)
}
// Check that set qualifies for the ISet interface
var _ pinterface.ISet = set
}
func TestRawSet(t *testing.T) {
Verbose = true
//host := New() // locally
host := NewHost(defaultConnectionString)
defer host.Close()
set, err := NewSet(host, setname)
if err != nil {
t.Error(err)
}
set.Clear()
if err := set.Add(testdata1); err != nil {
t.Errorf("Error, could not add item to set! %s", err)
}
items, err := set.All()
if err != nil {
t.Errorf("Error when retrieving set! %s", err)
}
if len(items) != 1 {
t.Errorf("Error, wrong set length! %d", len(items))
}
if (len(items) > 0) && (items[0] != testdata1) {
t.Errorf("Error, wrong set contents! %v", items)
}
if err := set.Add(testdata2); err != nil {
t.Errorf("Error, could not add item to set! %s", err)
}
if err := set.Add(testdata3); err != nil {
t.Errorf("Error, could not add item to set! %s", err)
}
// Add an element twice. This is a set, so the element should only appear once.
if err := set.Add(testdata3); err != nil {
t.Errorf("Error, could not add item to set! %s", err)
}
items, err = set.All()
if err != nil {
t.Errorf("Error when retrieving set! %s", err)
}
if len(items) != 3 {
t.Errorf("Error, wrong set length! %d\n%v\n", len(items), items)
}
err = set.Remove()
if err != nil {
t.Errorf("Error, could not remove set! %s", err)
}
// Check that set qualifies for the ISet interface
var _ pinterface.ISet = set
}
func TestDupliSet(t *testing.T) {
host := NewHost(defaultConnectionString)
defer host.Close()
letters, err := NewSet(host, "letters")
if err != nil {
t.Error(err)
}
defer letters.Remove()
if err := letters.Add("a"); err != nil {
t.Error(err)
}
if err := letters.Add("a"); err != nil {
t.Error(err)
}
x, err := letters.All()
if err != nil {
t.Error(err)
}
if len(x) != 1 {
t.Error("The set should have length 1 after adding two identical items")
}
if err := letters.Add("b"); err != nil {
t.Error(err)
}
y, err := letters.All()
if err != nil {
t.Error(err)
}
if len(y) != 2 {
t.Error("The set should have length 2 after adding two different items")
}
}
func TestCount(t *testing.T) {
host := NewHost(defaultConnectionString)
defer host.Close()
letters, err := NewSet(host, "letters")
if err != nil {
t.Error(err)
}
defer letters.Remove()
if err := letters.Add("a"); err != nil {
t.Error(err)
}
if err := letters.Add("a"); err != nil {
t.Error(err)
}
if count, err := letters.Count(); err != nil {
t.Error(err)
} else if count != 1 {
t.Error("The set should have length 1 after adding two identical items")
}
if err := letters.Add("b"); err != nil {
t.Error(err)
}
if count, err := letters.Count(); err != nil {
t.Error(err)
} else if count != 2 {
t.Error("The set should have length 2 after adding two different items")
}
}