-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.go
81 lines (68 loc) · 1.63 KB
/
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
package main
import "fmt"
import "crypto/rand"
import "math/big"
func main() {
var list List
list = &MyLinkedList{}
testList(list)
list = &MyArrayList{}
testList(list)
}
func testList(list List) {
//Add 100 random elements to the list.
for i := 0; i < 100; i++ {
p := new(big.Int).SetInt64(100)
num, _ := rand.Int(rand.Reader, p)
list.add(int(num.Int64()))
}
assertEqual(100, list.size());
assertEqual(false, list.isEmpty());
// Clear the list out
list.clear();
assertEqual(0, list.size());
// Add in some specific elements
list.add(1); // [1]
list.add(2); // [1,2]
list.addAtIndex(0, 3); // [3,1,2]
val, _ := list.get(1)
assertEqual(1, val);
assertEqual(true, list.contains(3));
assertEqual(false, list.contains(7));
// Test removing an element
removed, _ := list.removeAtIndex(0); //remove element at index zero.
assertEqual(removed, 3); //3 should have been removed
assertEqual(2, list.size());
// Add again after removal.
list.add(4); //[1,2,4]
assertEqual(2, list.indexOf(4));
}
func assertEqual(a, b interface {}) {
if (a == b) == false {
fmt.Printf("FAIL! %v != %v\n", a, b)
} else {
fmt.Printf("PASS, %v == %v\n", a, b)
}
}
type List interface {
size() int
get(idx int) (int, error)
contains(val int) bool
add(val int)
remove(val int) bool
containsAll(collection []int) bool
addAll(collection []int) bool
clear()
set(idx int, val int) (int, error)
addAtIndex(idx int, val int) (int, error)
removeAtIndex(idx int) (int, error)
indexOf(val int) (int)
isEmpty() bool
}
func printList(list List) {
for i := 0; i< list.size(); i++ {
val, _ := list.get(i)
fmt.Printf("%v, ", val)
}
fmt.Println()
}