Files
LeetCode-Go/structures/ListNode_test.go
2020-08-07 10:07:44 +08:00

69 lines
1.4 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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) })
}