-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities_test.go
354 lines (294 loc) · 9.38 KB
/
utilities_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
utilities_test.go
Description:
Tests of the extra functions located in utilities.go.
*/
package modelchecking
import (
"testing"
)
/*
TestAtomicProposition_Subset1
Description:
Checks that the function correctly identifies when one set is a slice is a subset of another.
*/
func TestAtomicProposition_Subset1(t *testing.T) {
// Create Slice of AtomicPropositions
ap1 := AtomicProposition{Name: "A"}
ap2 := AtomicProposition{Name: "B"}
ap3 := AtomicProposition{Name: "C"}
apSlice1 := []AtomicProposition{ap1, ap2, ap3}
apSlice2 := []AtomicProposition{ap1, ap3}
// Test
tf, err := SliceSubset(apSlice2, apSlice1)
if err != nil {
t.Errorf("SliceSubset encountered an error! %v", err)
}
if !tf {
t.Errorf("The function does not correctly identify that slice 2 is a subset of slice 1.")
}
}
/*
TestAtomicProposition_Subset2
Description:
Checks that the function correctly identifies when one set is NOT a slice is a subset of another.
*/
func TestAtomicProposition_Subset2(t *testing.T) {
// Create Slice of AtomicPropositions
ap1 := AtomicProposition{Name: "A"}
ap2 := AtomicProposition{Name: "B"}
ap3 := AtomicProposition{Name: "C"}
apSlice1 := []AtomicProposition{ap1, ap2, ap3}
apSlice2 := []AtomicProposition{ap1, ap3}
// Test
tf, err := SliceSubset(apSlice1, apSlice2)
if err != nil {
t.Errorf("SliceSubset encountered an error! %v", err)
}
if tf {
t.Errorf("The function does not correctly identify that slice 1 is NOT a subset of slice 2.")
}
}
/*
TestUtilities_SliceSubset3
Description:
Verifies that SliceSubset knows how to fail when there are two different types given to it.
*/
func TestUtilities_SliceSubset3(t *testing.T) {
// Create Slice of AtomicPropositions
ap1 := AtomicProposition{Name: "A"}
ap2 := AtomicProposition{Name: "B"}
ap3 := AtomicProposition{Name: "C"}
apSlice1 := []AtomicProposition{ap1, ap2, ap3}
tempSlice := []string{"2", "36"}
_, err := SliceSubset(apSlice1, tempSlice)
if err == nil {
t.Errorf("SliceSubset did not throw an error for invalid combination of slices.")
}
}
/*
TestUtilities_SliceEquals1
Description:
Verifies that SliceEquals can understand when two slices ARE NOT equal.
*/
func TestUtilities_SliceEquals1(t *testing.T) {
// Create Slice of AtomicPropositions
ap1 := AtomicProposition{Name: "A"}
ap2 := AtomicProposition{Name: "B"}
ap3 := AtomicProposition{Name: "C"}
apSlice1 := []AtomicProposition{ap1, ap2, ap3}
apSlice2 := []AtomicProposition{ap1, ap3}
// Test
tf, err := SliceEquals(apSlice1, apSlice2)
if err != nil {
t.Errorf("There was an issue computing SliceEquals(): %v", err)
}
if tf {
t.Errorf("The function incorrectly claims that the slices are equal!")
}
}
/*
TestUtilities_SliceEquals2
Description:
Verifies that SliceEquals can understand when two slices ARE equal.
*/
func TestUtilities_SliceEquals2(t *testing.T) {
// Create Slice of AtomicPropositions
ap1 := AtomicProposition{Name: "A"}
ap2 := AtomicProposition{Name: "B"}
ap3 := AtomicProposition{Name: "C"}
apSlice1 := []AtomicProposition{ap1, ap2, ap3}
// apSlice2 := []AtomicProposition{ap1, ap3}
// Test
tf, err := SliceEquals(apSlice1, apSlice1)
if err != nil {
t.Errorf("There was an issue computing SliceEquals(): %v", err)
}
if !tf {
t.Errorf("The function incorrectly claims that the slices are not equal!")
}
}
/*
TestUtilities_GetBeverageVendingMachineTS1
Description:
Verifies that the beverage vending machine transition system has the appropriate number of states.
*/
func TestUtilities_GetBeverageVendingMachineTS1(t *testing.T) {
// Create
ts0 := GetBeverageVendingMachineTS()
// Test
if len(ts0.S) != 4 {
t.Errorf("There were not four states in the transition system's state space.")
}
}
/*
TestUtilities_SliceCartesianProduct1
Description:
Verifies that a simple Cartesian product has 4 elements.
*/
func TestUtilities_SliceCartesianProduct1(t *testing.T) {
// Create
ts0 := GetBeverageVendingMachineTS()
s0 := ts0.S[0]
s1 := ts0.S[1]
S1 := []TransitionSystemState{s0, s1}
// Test
cp1, err := SliceCartesianProduct(S1, S1)
if err != nil {
t.Errorf("There was an error computing the cartesian product: %v", err)
}
cp1Converted := cp1.([][]TransitionSystemState)
if len(cp1Converted) != 4 {
t.Errorf("There were not 4 tuples in the cartesian product. Instead there were %v.", len(cp1Converted))
}
}
/*
TestUtilities_SliceCartesianProduct1
Description:
Verifies that a simple Cartesian product has the correct tuple as its first element.
*/
func TestUtilities_SliceCartesianProduct2(t *testing.T) {
// Create
ts0 := GetBeverageVendingMachineTS()
s0 := ts0.S[0]
s1 := ts0.S[1]
S1 := []TransitionSystemState{s0, s1}
// Test
cp1, err := SliceCartesianProduct(S1, S1)
if err != nil {
t.Errorf("There was an error computing the cartesian product: %v", err)
}
cp1Converted := cp1.([][]TransitionSystemState)
if (!s0.Equals(cp1Converted[0][0])) || (!s0.Equals(cp1Converted[0][1])) {
t.Errorf("There were not 4 tuples in the cartesian product. Instead there were %v.", len(cp1Converted))
}
}
/*
TestUtilities_AppendIfUnique1
Description:
Verifies that the Append function works properly for a single string to AppendIfUnique.
The extra string is NOT part of the original list.
*/
func TestUtilities_AppendIfUnique1(t *testing.T) {
// Create a simple slice of strings
slice1 := []string{"A", "B", "C"}
string1 := "Temp"
// Append string.
slice2 := AppendIfUnique(slice1, string1)
if len(slice2) != 4 {
t.Errorf("The new slice was expected to have length %v, but was %v instead.", 4, len(slice2))
}
if slice2[len(slice2)-1] != string1 {
t.Errorf("The new slice should have final element \"%v\", but was \"%v\".", string1, slice2[len(slice2)-1])
}
}
/*
TestUtilities_AppendIfUnique2
Description:
Verifies that the Append function works properly for a single string to AppendIfUnique.
The extra string IS part of the original list.
*/
func TestUtilities_AppendIfUnique2(t *testing.T) {
// Create a simple slice of strings
slice1 := []string{"A", "B", "C"}
string1 := "A"
// Append string.
slice2 := AppendIfUnique(slice1, string1)
if len(slice2) != 3 {
t.Errorf("The new slice was expected to have length %v, but was %v instead.", 3, len(slice2))
}
if slice2[len(slice2)-1] != "C" {
t.Errorf("The new slice should have final element \"%v\", but was \"%v\".", "C", slice2[len(slice2)-1])
}
}
/*
TestUtilities_AppendIfUnique3
Description:
Verifies that the Append function works properly for a string slice to AppendIfUnique.
The string slice IS NOT COMPLETELY part of the original list.
*/
func TestUtilities_AppendIfUnique3(t *testing.T) {
// Create a simple slice of strings
slice1 := []string{"A", "B", "C"}
slice2 := []string{"B", "D"}
// Append string.
slice3 := AppendIfUnique(slice1, slice2...)
if len(slice3) != 4 {
t.Errorf("The new slice was expected to have length %v, but was %v instead.", 4, len(slice3))
}
if slice3[len(slice3)-1] != slice2[len(slice2)-1] {
t.Errorf("The new slice should have final element \"%v\", but was \"%v\".", slice2[len(slice2)-1], slice3[len(slice3)-1])
}
}
/*
TestUtilities_AppendIfUnique4
Description:
Verifies that the Append function works properly for a string slice to AppendIfUnique.
The string slice IS COMPLETELY part of the original list.
*/
func TestUtilities_AppendIfUnique4(t *testing.T) {
// Create a simple slice of strings
slice1 := []string{"A", "B", "C"}
slice2 := []string{"B", "A"}
// Append string.
slice3 := AppendIfUnique(slice1, slice2...)
if len(slice3) != 3 {
t.Errorf("The new slice was expected to have length %v, but was %v instead.", 3, len(slice3))
}
if slice3[len(slice3)-1] != "C" {
t.Errorf("The new slice should have final element \"%v\", but was \"%v\".", "C", slice3[len(slice3)-1])
}
}
/*
TestUtilities_AppendIfUnique5
Description:
Verifies that the Append function works properly for a single string to AppendIfUnique.
The string slice IS COMPLETELY part of the original list.
*/
func TestUtilities_AppendIfUnique5(t *testing.T) {
// Create a simple slice of strings
slice1 := []string{}
slice2 := []string{"B", "A"}
// Append string.
slice3 := AppendIfUnique(slice1, slice2...)
if len(slice3) != 2 {
t.Errorf("The new slice was expected to have length %v, but was %v instead.", 2, len(slice3))
}
if slice3[len(slice3)-1] != slice2[len(slice2)-1] {
t.Errorf("The new slice should have final element \"%v\", but was \"%v\".", slice2[len(slice2)-1], slice3[len(slice3)-1])
}
}
/*
TestUtilities_FindInSlice1
Description:
Verifies that FindInSlice() works for a given slice of strings.
*/
func TestUtilities_FindInSlice1(t *testing.T) {
//Create a simple slice.
slice1 := []string{"A", "B", "C", "E"}
target1 := "E"
foundIndex, tf := FindInSlice(target1, slice1)
if !tf {
t.Errorf("The function FindInSlice() could not find the target even though it exists in slice1!")
}
if foundIndex != 3 {
t.Errorf("Expected that the foundIndex would be 3, but received %v.", foundIndex)
}
}
/*
TestUtilities_FindInSlice2
Description:
Verifies that FindInSlice() works for a given slice of strings. The target string is NOT in the slice.
*/
func TestUtilities_FindInSlice2(t *testing.T) {
//Create a simple slice.
slice1 := []string{"A", "B", "C", "E"}
target1 := "D"
foundIndex, tf := FindInSlice(target1, slice1)
if tf {
t.Errorf("The function FindInSlice() found the target even though it DOES NOT exist in slice1!")
}
if foundIndex != -1 {
t.Errorf("Expected that the foundIndex would be -1, but received %v.", foundIndex)
}
}