summaryrefslogtreecommitdiff
path: root/go/sum-of-multiples/sum_of_multiples_test.go
blob: 9ab0bb2a233cfa2fa5c179108f1421616750c695 (plain)
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
package summultiples

import "testing"

var test35 = []struct {
	limit int
	sum   int
}{
	{1, 0},
	{4, 3},
	{10, 23},
	{100, 2318},
	{1000, 233168},
}

var varTests = []struct {
	divisors []int
	limit    int
	sum      int
}{
	{[]int{7, 13, 17}, 20, 51},
	{[]int{43, 47}, 10000, 2203160},
	{[]int{5, 10, 12}, 10000, 13331672},
	{[]int{1, 1}, 10000, 49995000},
	// Note: The following test case deviates from the README.
	// The README specifies some rather odd defaults, whereas
	// this has the more logical approach of not implementing any
	// defaults, which causes the resulting sum to be zero.
	// See discussion in:
	// https://github.com/exercism/xgo/issues/256 and
	// https://github.com/exercism/x-common/issues/198
	{[]int{}, 10000, 0},
}

func Test35(t *testing.T) {
	sum35 := MultipleSummer(3, 5)
	for _, test := range test35 {
		s := sum35(test.limit)
		if s != test.sum {
			t.Fatalf("Sum to %d returned %d, want %d.", test.limit, s, test.sum)
		}
	}
}

func TestVar(t *testing.T) {
	for _, test := range varTests {
		sv := MultipleSummer(test.divisors...)
		s := sv(test.limit)
		if s != test.sum {
			t.Fatalf("Sum of multiples of %v to %d returned %d, want %d.",
				test.divisors, test.limit, s, test.sum)
		}
	}
}

func Benchmark35(b *testing.B) {
	sum35 := MultipleSummer(3, 5)
	b.ResetTimer() // bench just the sum function
	for i := 0; i < b.N; i++ {
		for _, test := range test35 {
			sum35(test.limit)
		}
	}
}

func BenchmarkVar(b *testing.B) {
	// bench combined time to bind sum function and call it.
	for i := 0; i < b.N; i++ {
		for _, test := range varTests {
			MultipleSummer(test.divisors...)(test.limit)
		}
	}
}