generated from OtusGolang/home_work
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_test.go
51 lines (40 loc) · 1.1 KB
/
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
package hw04lrucache
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestList(t *testing.T) {
t.Run("empty list", func(t *testing.T) {
l := NewList()
require.Equal(t, 0, l.Len())
require.Nil(t, l.Front())
require.Nil(t, l.Back())
})
t.Run("complex", func(t *testing.T) {
l := NewList()
l.PushFront(10) // [10]
l.PushBack(20) // [10, 20]
l.PushBack(30) // [10, 20, 30]
require.Equal(t, 3, l.Len())
middle := l.Front().Next // 20
l.Remove(middle) // [10, 30]
require.Equal(t, 2, l.Len())
for i, v := range [...]int{40, 50, 60, 70, 80} {
if i%2 == 0 {
l.PushFront(v)
} else {
l.PushBack(v)
}
} // [80, 60, 40, 10, 30, 50, 70]
require.Equal(t, 7, l.Len())
require.Equal(t, 80, l.Front().Value)
require.Equal(t, 70, l.Back().Value)
l.MoveToFront(l.Front()) // [80, 60, 40, 10, 30, 50, 70]
l.MoveToFront(l.Back()) // [70, 80, 60, 40, 10, 30, 50]
elems := make([]int, 0, l.Len())
for i := l.Front(); i != nil; i = i.Next {
elems = append(elems, i.Value.(int))
}
require.Equal(t, []int{70, 80, 60, 40, 10, 30, 50}, elems)
})
}