forked from spring1843/go-dsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_sorted_list_test.go
40 lines (34 loc) · 1.08 KB
/
merge_sorted_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
package heap
import (
"testing"
"github.com/spring1843/go-dsa/linkedlist"
)
/*
TestMergeSortedLists tests solution(s) with the following signature and problem description:
func MergeSortedLists(lists []*linkedlist.Node) *linkedlist.Node
Given multiple sorted linked lists like {1->2, 1->3->4, 4->5}, join them into one
like 1->1->2->3->4->4->5.
*/
func TestMergeSortedLists(t *testing.T) {
tests := []struct {
sortedLinkedLists []string
merged string
}{
{[]string{""}, ""},
{[]string{"1"}, "1"},
{[]string{"1", ""}, "1"},
{[]string{"1", "1->2"}, "1->1->2"},
{[]string{"1", "1->2", "3->4->5"}, "1->1->2->3->4->5"},
{[]string{"1", "1->2", "1->2->3", "1->3->5", "1->2->4"}, "1->1->1->1->1->2->2->2->3->3->4->5"},
}
for i, test := range tests {
nodes := []*linkedlist.Node{}
for _, sortedLinkedList := range test.sortedLinkedLists {
nodes = append(nodes, linkedlist.Unserialize(sortedLinkedList))
}
got := linkedlist.Serialize(MergeSortedLists(nodes))
if got != test.merged {
t.Fatalf("Failed test case #%d. Want %q got %q", i, test.merged, got)
}
}
}