-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathListNode_test.go
68 lines (58 loc) · 1.36 KB
/
ListNode_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
package structures
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_l2s(t *testing.T) {
ast := assert.New(t)
ast.Equal([]int{}, List2Ints(nil), "输入nil,没有返回[]int{}")
one2three := &ListNode{
Val: 1,
Next: &ListNode{
Val: 2,
Next: &ListNode{
Val: 3,
},
},
}
ast.Equal([]int{1, 2, 3}, List2Ints(one2three), "没有成功地转换成[]int")
limit := 100
overLimitList := Ints2List(make([]int, limit+1))
ast.Panics(func() { List2Ints(overLimitList) }, "转换深度超过 %d 限制的链条,没有 panic", limit)
}
func Test_s2l(t *testing.T) {
ast := assert.New(t)
ast.Nil(Ints2List([]int{}), "输入[]int{},没有返回nil")
ln := Ints2List([]int{1, 2, 3, 4, 5, 6, 7, 8, 9})
i := 1
for ln != nil {
ast.Equal(i, ln.Val, "对应的值不对")
ln = ln.Next
i++
}
}
func Test_getNodeWith(t *testing.T) {
ast := assert.New(t)
//
ln := Ints2List([]int{1, 2, 3, 4, 5, 6, 7, 8, 9})
val := 10
node := &ListNode{
Val: val,
}
tail := ln
for tail.Next != nil {
tail = tail.Next
}
tail.Next = node
expected := node
actual := ln.GetNodeWith(val)
ast.Equal(expected, actual)
}
func Test_Ints2ListWithCycle(t *testing.T) {
ast := assert.New(t)
ints := []int{1, 2, 3}
l := Ints2ListWithCycle(ints, -1)
ast.Equal(ints, List2Ints(l))
l = Ints2ListWithCycle(ints, 1)
ast.Panics(func() { List2Ints(l) })
}