Skip to content

Commit 9224024

Browse files
authored
Merge pull request #684 from leenipper/sum-of-multiples-add-generator
sum-of-multiples: create test case generator
2 parents 44ea27c + ac62d60 commit 9224024

File tree

4 files changed

+69
-19
lines changed

4 files changed

+69
-19
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"text/template"
6+
7+
"../../../gen"
8+
)
9+
10+
func main() {
11+
t, err := template.New("").Parse(tmpl)
12+
if err != nil {
13+
log.Fatal(err)
14+
}
15+
var j js
16+
if err := gen.Gen("sum-of-multiples", &j, t); err != nil {
17+
log.Fatal(err)
18+
}
19+
}
20+
21+
// The JSON structure we expect to be able to unmarshal into
22+
type js struct {
23+
Cases []struct {
24+
Description string
25+
Factors []int
26+
Limit int
27+
Expected int
28+
}
29+
}
30+
31+
// template applied to above data structure generates the Go test cases
32+
var tmpl = `package summultiples
33+
34+
{{.Header}}
35+
36+
var varTests = []struct {
37+
divisors []int
38+
limit int
39+
sum int
40+
}{
41+
{{range .J.Cases}}{ {{.Factors | printf "%#v"}}, {{.Limit}}, {{.Expected}}}, // {{.Description}}
42+
{{end}}}
43+
`
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package summultiples
2+
3+
// Source: exercism/x-common
4+
// Commit: 72b1496 sum-of-multiples: Fix canonical-data.json formatting
5+
// x-common version: 1.0.0
6+
7+
var varTests = []struct {
8+
divisors []int
9+
limit int
10+
sum int
11+
}{
12+
{[]int{3, 5}, 1, 0}, // multiples of 3 or 5 up to 1
13+
{[]int{3, 5}, 4, 3}, // multiples of 3 or 5 up to 4
14+
{[]int{3, 5}, 10, 23}, // multiples of 3 or 5 up to 10
15+
{[]int{3, 5}, 100, 2318}, // multiples of 3 or 5 up to 100
16+
{[]int{3, 5}, 1000, 233168}, // multiples of 3 or 5 up to 1000
17+
{[]int{7, 13, 17}, 20, 51}, // multiples of 7, 13 or 17 up to 20
18+
{[]int{4, 6}, 15, 30}, // multiples of 4 or 6 up to 15
19+
{[]int{5, 6, 8}, 150, 4419}, // multiples of 5, 6 or 8 up to 150
20+
{[]int{5, 25}, 51, 275}, // multiples of 5 or 25 up to 51
21+
{[]int{43, 47}, 10000, 2203160}, // multiples of 43 or 47 up to 10000
22+
{[]int{1}, 100, 4950}, // multiples of 1 up to 100
23+
{[]int{}, 10000, 0}, // multiples of an empty list up to 10000
24+
}

exercises/sum-of-multiples/example.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package summultiples
22

3-
const testVersion = 1
3+
const testVersion = 2
44

55
// SumMultiples returns the sum of the multiples of the given divisors
66
// up to, but not including, the given limit.

exercises/sum-of-multiples/sum_of_multiples_test.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,7 @@ package summultiples
22

33
import "testing"
44

5-
const targetTestVersion = 1
6-
7-
var varTests = []struct {
8-
divisors []int
9-
limit int
10-
sum int
11-
}{
12-
{[]int{3, 5}, 1, 0},
13-
{[]int{3, 5}, 4, 3},
14-
{[]int{3, 5}, 10, 23},
15-
{[]int{3, 5}, 100, 2318},
16-
{[]int{3, 5}, 1000, 233168},
17-
{[]int{7, 13, 17}, 20, 51},
18-
{[]int{43, 47}, 10000, 2203160},
19-
{[]int{5, 10, 12}, 10000, 13331672},
20-
{[]int{1, 1}, 10000, 49995000},
21-
{[]int{}, 10000, 0},
22-
}
5+
const targetTestVersion = 2
236

247
func TestTestVersion(t *testing.T) {
258
if testVersion != targetTestVersion {

0 commit comments

Comments
 (0)