forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cyclic_test.go
129 lines (110 loc) · 2.08 KB
/
cyclic_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 linkedlist
import (
"reflect"
"testing"
)
func fillList(list *Cyclic, n int) {
for i := 1; i <= n; i++ {
list.Add(i)
}
}
func TestAdd(t *testing.T) {
list := NewCyclic()
fillList(list, 3)
want := []interface{}{1, 2, 3}
var got []interface{}
var start *Node
start = list.Head
for i := 0; i < list.Size; i++ {
got = append(got, start.Val)
start = start.Next
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got: %v, want: %v", got, want)
}
}
func TestWalk(t *testing.T) {
list := NewCyclic()
fillList(list, 3)
want := 1
got := list.Walk()
if got.Val != want {
t.Errorf("got: %v, want: nil", got)
}
}
func TestRotate(t *testing.T) {
type testCase struct {
param int
wantToReturn int
}
list := NewCyclic()
fillList(list, 3)
testCases := []testCase{
{1, 2},
{3, 2},
{6, 2},
{7, 3},
{-2, 1},
{5, 3},
{8, 2},
{-8, 3},
}
for idx, tCase := range testCases {
list.Rotate(tCase.param)
got := list.Head.Val
if got != tCase.wantToReturn {
t.Errorf("got: %v, want: %v for test id %v", got, tCase.wantToReturn, idx)
}
}
}
func TestDelete(t *testing.T) {
list := NewCyclic()
fillList(list, 3)
want := 2
wantSize := 2
list.Delete()
got := list.Head.Val
if want != got {
t.Errorf("got: %v, want: %v", got, want)
}
if wantSize != list.Size {
t.Errorf("got: %v, want: %v", got, want)
}
}
func TestDestroy(t *testing.T) {
list := NewCyclic()
fillList(list, 3)
wantSize := 0
list.Destroy()
got := list.Head
if got != nil {
t.Errorf("got: %v, want: nil", got)
}
if wantSize != list.Size {
t.Errorf("got: %v, want: %v", got, wantSize)
}
}
func TestJosephusProblem(t *testing.T) {
type testCase struct {
param int
wantToReturn int
listCount int
}
testCases := []testCase{
{5, 4, 8},
{3, 8, 8},
{8, 5, 8},
{8, 5, 8},
{2, 14, 14},
{13, 56, 58},
{7, 5, 5},
}
for _, tCase := range testCases {
list := NewCyclic()
fillList(list, tCase.listCount)
got := JosephusProblem(list, tCase.param)
if got != tCase.wantToReturn {
t.Errorf("got: %v, want: %v", got, tCase.wantToReturn)
}
}
}