-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path002_addtwonumbers_test.go
107 lines (83 loc) · 1.81 KB
/
002_addtwonumbers_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package leetcode
import (
"fmt"
"reflect"
"testing"
)
type ListNode struct {
Val int
Next *ListNode
}
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
first := &ListNode{}
current := first
var a, b, remainder, result int
for {
a = 0
b = 0
if l1 != nil {
a = l1.Val
}
if l2 != nil {
b = l2.Val
}
result = a + b + remainder
current.Val = result % 10
remainder = result / 10
if l1 != nil {
l1 = l1.Next
}
if l2 != nil {
l2 = l2.Next
}
if l1 == nil && l2 == nil {
break
}
current.Next = &ListNode{}
current = current.Next
}
if remainder > 0 {
current.Next = &ListNode{Val: remainder}
}
return first
}
// BELOW IS JUST FOR TESTING LOCALLY
func (self *ListNode) ToSlice() []int {
slice := make([]int, 1)
slice[0] = self.Val
current := self.Next
for {
if current == nil {
break
}
slice = append(slice, current.Val)
current = current.Next
}
return slice
}
func sliceToListNode(ints []int) *ListNode {
var previous, current *ListNode
for i := len(ints) - 1; i >= 0; i-- {
current = &ListNode{
Val: ints[i],
Next: previous,
}
previous = current
}
return current
}
func execTestCaseAddTwoNumbers(t *testing.T, l1 []int, l2 []int, expectation []int) {
sl1 := sliceToListNode(l1)
sl2 := sliceToListNode(l2)
exp := sliceToListNode(expectation)
t.Run(fmt.Sprintf("%v + %v", l1, l2), func(t *testing.T) {
if res := addTwoNumbers(sl1, sl2); !reflect.DeepEqual(res, exp) {
t.Errorf("Expected %v but got %v", expectation, res.ToSlice())
}
})
}
func TestAddTwoNumbers(t *testing.T) {
execTestCaseAddTwoNumbers(t, []int{2, 4, 3}, []int{5, 6, 4}, []int{7, 0, 8})
execTestCaseAddTwoNumbers(t, []int{0}, []int{0}, []int{0})
execTestCaseAddTwoNumbers(t, []int{9, 9, 9, 9, 9, 9, 9}, []int{9, 9, 9, 9}, []int{8, 9, 9, 9, 0, 0, 0, 1})
}