-
Notifications
You must be signed in to change notification settings - Fork 1
/
intrusive_list_test.go
78 lines (61 loc) · 1.87 KB
/
intrusive_list_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
package list
import "testing"
// Define a struct that embeds the List structure
type S struct {
a, b int
*List[S]
}
func TestIntrusiveNext(t *testing.T) {
node1 := &S{a: 1, b: 2, List: &List[S]{}}
node2 := &S{a: 3, b: 4, List: &List[S]{}}
node1.SetNext(node2)
if got := node1.Next(); got != node2 {
t.Errorf("Expected next node to be %+v, got %+v", node2, got)
}
}
func TestIntrusiveSetNext(t *testing.T) {
node1 := &S{a: 1, b: 2, List: &List[S]{}}
node2 := &S{a: 3, b: 4, List: &List[S]{}}
node1.SetNext(node2)
if got := node1.Next(); got != node2 {
t.Errorf("Expected next node to be %+v, got %+v", node2, got)
}
}
func TestInsertNext(t *testing.T) {
node1 := &S{a: 1, b: 2, List: &List[S]{}}
node2 := &S{a: 3, b: 4, List: &List[S]{}}
node3 := &S{a: 5, b: 6, List: &List[S]{}}
node1.SetNext(node2)
node1.InsertNext(node3)
if got := node1.Next(); got != node3 {
t.Errorf("Expected next node to be %+v, got %+v", node3, got)
}
if got := node3.Next(); got != node2 {
t.Errorf("Expected next node of inserted node to be %+v, got %+v", node2, got)
}
}
func TestRemoveNext(t *testing.T) {
node1 := &S{a: 1, b: 2, List: &List[S]{}}
node2 := &S{a: 3, b: 4, List: &List[S]{}}
node3 := &S{a: 5, b: 6, List: &List[S]{}}
node1.SetNext(node2)
node2.SetNext(node3)
node1.RemoveNext()
if got := node1.Next(); got != node3 {
t.Errorf("Expected next node to be %+v, got %+v", node3, got)
}
}
func TestRemoveNextNil(t *testing.T) {
node1 := &S{a: 1, b: 2, List: &List[S]{}}
node1.RemoveNext() // Should not panic or cause an error
if got := node1.Next(); got != nil {
t.Errorf("Expected next node to be nil, got %+v", got)
}
}
func TestInsertNextNil(t *testing.T) {
node1 := &S{a: 1, b: 2, List: &List[S]{}}
node1.InsertNext(nil) // Should not panic or cause an error
if got := node1.Next(); got != nil {
t.Errorf("Expected next node to be nil, got %+v", got)
}
}