You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.3 KiB
64 lines
1.3 KiB
2 years ago
|
package mergesorted_test
|
||
|
|
||
|
import (
|
||
|
"testing"
|
||
|
|
||
|
ms "git.jxs.me/leetgo/mergesorted"
|
||
|
"github.com/stretchr/testify/require"
|
||
|
)
|
||
|
|
||
|
func buildList(values []int) *ms.ListNode {
|
||
|
var result *ms.ListNode
|
||
|
|
||
|
for i := len(values) - 1; i >= 0; i-- {
|
||
|
result = &ms.ListNode{Val: values[i], Next: result}
|
||
|
}
|
||
|
|
||
|
return result
|
||
|
}
|
||
|
|
||
|
func TestEmpty(t *testing.T) {
|
||
|
require.Nil(t, nil, ms.MergeTwoLists(nil, nil))
|
||
|
}
|
||
|
|
||
|
func TestEmptyRight(t *testing.T) {
|
||
|
left := buildList([]int{4, 5})
|
||
|
right := buildList([]int{})
|
||
|
res := ms.MergeTwoLists(left, right)
|
||
|
require.Equal(t, left, res)
|
||
|
}
|
||
|
|
||
|
func TestEmptyLeft(t *testing.T) {
|
||
|
left := buildList([]int{})
|
||
|
right := buildList([]int{1, 2})
|
||
|
res := ms.MergeTwoLists(left, right)
|
||
|
require.Equal(t, right, res)
|
||
|
}
|
||
|
|
||
|
func TestMix(t *testing.T) {
|
||
|
left := buildList([]int{1, 3})
|
||
|
right := buildList([]int{2, 4})
|
||
|
res := ms.MergeTwoLists(left, right)
|
||
|
|
||
|
expected := buildList([]int{1, 2, 3, 4})
|
||
|
require.Equal(t, expected, res)
|
||
|
}
|
||
|
|
||
|
func TestExample1(t *testing.T) {
|
||
|
left := buildList([]int{1, 3, 4})
|
||
|
right := buildList([]int{1, 2, 4})
|
||
|
res := ms.MergeTwoLists(left, right)
|
||
|
|
||
|
expected := buildList([]int{1, 1, 2, 3, 4, 4})
|
||
|
require.Equal(t, expected, res)
|
||
|
}
|
||
|
|
||
|
func TestExample2(t *testing.T) {
|
||
|
left := buildList([]int{})
|
||
|
right := buildList([]int{0})
|
||
|
res := ms.MergeTwoLists(left, right)
|
||
|
|
||
|
expected := buildList([]int{0})
|
||
|
require.Equal(t, expected, res)
|
||
|
}
|