-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathsinglylinkedlist_test.go
115 lines (103 loc) · 2.32 KB
/
singlylinkedlist_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
package linkedlist
import (
"reflect"
"testing"
)
func TestSingly(t *testing.T) {
list := NewSingly[int]()
list.AddAtBeg(1)
list.AddAtBeg(2)
list.AddAtBeg(3)
t.Run("Test AddAtBeg()", func(t *testing.T) {
want := []any{3, 2, 1}
got := []any{}
current := list.Head
got = append(got, current.Val)
for current.Next != nil {
current = current.Next
got = append(got, current.Val)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got: %v, want: %v", got, want)
}
})
list.AddAtEnd(4)
t.Run("Test AddAtEnd()", func(t *testing.T) {
want := []any{3, 2, 1, 4}
got := []any{}
current := list.Head
got = append(got, current.Val)
for current.Next != nil {
current = current.Next
got = append(got, current.Val)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got: %v, want: %v", got, want)
}
})
t.Run("Test DelAtBeg()", func(t *testing.T) {
want := any(3)
got, ok := list.DelAtBeg()
if !ok {
t.Error("unexpected not-ok")
}
if got != want {
t.Errorf("got: %v, want: %v", got, want)
}
})
t.Run("Test DelAtEnd()", func(t *testing.T) {
want := any(4)
got, ok := list.DelAtEnd()
if !ok {
t.Error("unexpected not-ok")
}
if got != want {
t.Errorf("got: %v, want: %v", got, want)
}
})
t.Run("Test Count()", func(t *testing.T) {
want := 2
got := list.Count()
if got != want {
t.Errorf("got: %v, want: %v", got, want)
}
})
list2 := Singly[int]{}
list2.AddAtBeg(1)
list2.AddAtBeg(2)
list2.AddAtBeg(3)
list2.AddAtBeg(4)
list2.AddAtBeg(5)
list2.AddAtBeg(6)
t.Run("Test Reverse()", func(t *testing.T) {
want := []any{1, 2, 3, 4, 5, 6}
got := []any{}
list2.Reverse()
current := list2.Head
got = append(got, current.Val)
for current.Next != nil {
current = current.Next
got = append(got, current.Val)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got: %v, want: %v", got, want)
}
})
t.Run("Test ReversePartition()", func(t *testing.T) {
want := []any{1, 5, 4, 3, 2, 6}
got := []any{}
err := list2.ReversePartition(2, 5)
if err != nil {
t.Errorf("Incorrect boundary conditions entered%v", err)
}
current := list2.Head
got = append(got, current.Val)
for current.Next != nil {
current = current.Next
got = append(got, current.Val)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got: %v, want: %v", got, want)
}
})
}