|
| 1 | +package addtwonumbers |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestAddTwoNumbers(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + name string |
| 11 | + l1 *ListNode |
| 12 | + l2 *ListNode |
| 13 | + want *ListNode |
| 14 | + }{ |
| 15 | + { |
| 16 | + name: "example 1: 342 + 465 = 807", |
| 17 | + l1: createList([]int{2, 4, 3}), |
| 18 | + l2: createList([]int{5, 6, 4}), |
| 19 | + want: createList([]int{7, 0, 8}), |
| 20 | + }, |
| 21 | + { |
| 22 | + name: "example 2: 0 + 0 = 0", |
| 23 | + l1: createList([]int{0}), |
| 24 | + l2: createList([]int{0}), |
| 25 | + want: createList([]int{0}), |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "example 3: 9999999 + 9999 = 10009998", |
| 29 | + l1: createList([]int{9, 9, 9, 9, 9, 9, 9}), |
| 30 | + l2: createList([]int{9, 9, 9, 9}), |
| 31 | + want: createList([]int{8, 9, 9, 9, 0, 0, 0, 1}), |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "different lengths: 123 + 4567 = 4690", |
| 35 | + l1: createList([]int{3, 2, 1}), |
| 36 | + l2: createList([]int{7, 6, 5, 4}), |
| 37 | + want: createList([]int{0, 9, 6, 4}), |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "carry propagation: 99 + 1 = 100", |
| 41 | + l1: createList([]int{9, 9}), |
| 42 | + l2: createList([]int{1}), |
| 43 | + want: createList([]int{0, 0, 1}), |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "single digit with carry: 5 + 5 = 10", |
| 47 | + l1: createList([]int{5}), |
| 48 | + l2: createList([]int{5}), |
| 49 | + want: createList([]int{0, 1}), |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "no carry: 1 + 2 = 3", |
| 53 | + l1: createList([]int{1}), |
| 54 | + l2: createList([]int{2}), |
| 55 | + want: createList([]int{3}), |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "l1 longer: 12345 + 67 = 12412", |
| 59 | + l1: createList([]int{5, 4, 3, 2, 1}), |
| 60 | + l2: createList([]int{7, 6}), |
| 61 | + want: createList([]int{2, 1, 4, 2, 1}), |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "l2 longer: 12 + 3456 = 3468", |
| 65 | + l1: createList([]int{2, 1}), |
| 66 | + l2: createList([]int{6, 5, 4, 3}), |
| 67 | + want: createList([]int{8, 6, 4, 3}), |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "multiple carries: 999 + 999 = 1998", |
| 71 | + l1: createList([]int{9, 9, 9}), |
| 72 | + l2: createList([]int{9, 9, 9}), |
| 73 | + want: createList([]int{8, 9, 9, 1}), |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + for _, tt := range tests { |
| 78 | + t.Run(tt.name, func(t *testing.T) { |
| 79 | + got := addTwoNumbers(tt.l1, tt.l2) |
| 80 | + if !equalLists(got, tt.want) { |
| 81 | + t.Errorf("addTwoNumbers() = %v, want %v", listToSlice(got), listToSlice(tt.want)) |
| 82 | + } |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func createList(vals []int) *ListNode { |
| 88 | + if len(vals) == 0 { |
| 89 | + return nil |
| 90 | + } |
| 91 | + head := &ListNode{Val: vals[0]} |
| 92 | + current := head |
| 93 | + for i := 1; i < len(vals); i++ { |
| 94 | + current.Next = &ListNode{Val: vals[i]} |
| 95 | + current = current.Next |
| 96 | + } |
| 97 | + return head |
| 98 | +} |
| 99 | + |
| 100 | +func listToSlice(head *ListNode) []int { |
| 101 | + var result []int |
| 102 | + current := head |
| 103 | + for current != nil { |
| 104 | + result = append(result, current.Val) |
| 105 | + current = current.Next |
| 106 | + } |
| 107 | + return result |
| 108 | +} |
| 109 | + |
| 110 | +func equalLists(l1, l2 *ListNode) bool { |
| 111 | + return reflect.DeepEqual(listToSlice(l1), listToSlice(l2)) |
| 112 | +} |
0 commit comments