Repository navigation
Expand file tree
/
Copy patharray_list_test.go
More file actions
85 lines (73 loc) · 1.49 KB
/
Copy patharray_list_test.go
File metadata and controls
85 lines (73 loc) · 1.49 KB
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
package algorithms
import "testing"
func TestArrayListAdd(t *testing.T) {
a := &ArrayList[int]{}
a.Add(1)
a.Add(2)
a.Add(3)
if a.Len() != 3 {
t.Fatalf("expected len 3, got %d", a.Len())
}
val, ok := a.Get(2)
if !ok || val != 3 {
t.Fatalf("expected 3, got %d", val)
}
}
func TestArrayListInsertAt(t *testing.T) {
a := &ArrayList[string]{}
a.Add("a")
a.Add("c")
a.InsertAt(1, "b")
val, ok := a.Get(1)
if !ok || val != "b" {
t.Fatalf("expected b, got %s", val)
}
if a.Len() != 3 {
t.Fatalf("expected len 3, got %d", a.Len())
}
}
func TestArrayListInsertAtBounds(t *testing.T) {
a := &ArrayList[int]{}
if a.InsertAt(-1, 1) {
t.Fatal("expected false for negative index")
}
if a.InsertAt(1, 1) {
t.Fatal("expected false for out of bounds index")
}
}
func TestArrayListRemoveAt(t *testing.T) {
a := &ArrayList[int]{}
a.Add(1)
a.Add(2)
a.Add(3)
a.RemoveAt(1)
val, ok := a.Get(1)
if !ok || val != 3 {
t.Fatalf("expected 3, got %d", val)
}
if a.Len() != 2 {
t.Fatalf("expected len 2, got %d", a.Len())
}
}
func TestArrayListRemoveAtOutOfBounds(t *testing.T) {
a := &ArrayList[int]{}
if a.RemoveAt(0) {
t.Fatal("expected false on empty list")
}
}
func TestArrayListSet(t *testing.T) {
a := &ArrayList[int]{}
a.Add(1)
a.Set(0, 10)
val, ok := a.Get(0)
if !ok || val != 10 {
t.Fatalf("expected 10, got %d", val)
}
}
func TestArrayListGetOutOfBounds(t *testing.T) {
a := &ArrayList[int]{}
_, ok := a.Get(0)
if ok {
t.Fatal("expected false on empty list")
}
}