-
Notifications
You must be signed in to change notification settings - Fork 7
/
digit_sum_test.go
95 lines (90 loc) · 1.5 KB
/
digit_sum_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
package numbertheory
import (
"fmt"
"testing"
)
func ExampleDigitSum() {
n := DigitSum(9045, 10)
fmt.Printf("The digit sum of 9045 in base-10 is %v.\n", n)
// Output:
// The digit sum of 9045 in base-10 is 18.
}
func TestDigitSum(t *testing.T) {
cases := []struct {
name string
n int
b int
expected int
}{
{
name: "9045 in base 1",
n: 9045,
b: 1,
expected: 9045,
},
{
name: "9045 in base 0 (error)",
n: 9045,
b: 0,
expected: 0,
},
{
name: "-9045 in base 10",
n: -9045,
b: 0,
expected: 0,
},
{
name: "9045 in base 10",
n: 9045,
b: 10,
expected: 18,
},
{
name: "9045 in base 2",
n: 9045,
b: 2,
expected: 7,
},
{
name: "9045 in base 31",
n: 9045,
b: 31,
expected: 45,
},
{
name: "9045 in base 9045",
n: 9045,
b: 9044,
expected: 2,
},
{
name: "9045 in base 9045",
n: 9045,
b: 9045,
expected: 1,
},
{
name: "9045 in base 9046",
n: 9045,
b: 9046,
expected: 9045,
},
}
for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
actual := DigitSum(c.n, c.b)
if c.expected != actual {
t.Logf("expected %v, got %v", c.expected, actual)
t.FailNow()
}
})
}
}
func BenchmarkDigitSum(b *testing.B) {
for i := 0; i < b.N; i++ {
DigitSum(i, 10)
}
}