Skip to content

Commit ee5486d

Browse files
author
openset
committed
Add: Sum of Two Integers
1 parent 82cd834 commit ee5486d

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
11
package sum_of_two_integers
2+
3+
func getSum(a int, b int) int {
4+
for b != 0 {
5+
a, b = a^b, (a&b)<<1
6+
}
7+
return a
8+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
11
package sum_of_two_integers
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
a int
7+
b int
8+
expected int
9+
}
10+
11+
func TestGetSum(t *testing.T) {
12+
tests := [...]caseType{
13+
{
14+
a: 1,
15+
b: 2,
16+
expected: 3,
17+
},
18+
{
19+
a: -2,
20+
b: 3,
21+
expected: 1,
22+
},
23+
}
24+
for _, tc := range tests {
25+
output := getSum(tc.a, tc.b)
26+
if output != tc.expected {
27+
t.Fatalf("input: %v %v, output: %v, expected: %v", tc.a, tc.b, output, tc.expected)
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)