Skip to content

Commit 999244e

Browse files
authored
collatz-conjecture: add generator (#958)
Add .meta/gen.go to generate cases_test.go. Make use of zero-value of expectError bool. Update test program to use generated test case array. For #605.
1 parent 7e2173d commit 999244e

File tree

3 files changed

+115
-42
lines changed

3 files changed

+115
-42
lines changed
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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("collatz-conjecture", &j, t); err != nil {
17+
log.Fatal(err)
18+
}
19+
}
20+
21+
type OneCase struct {
22+
Description string
23+
Number int
24+
Expected interface{}
25+
}
26+
27+
// The JSON structure we expect to be able to unmarshal into
28+
type js struct {
29+
Cases []OneCase
30+
}
31+
32+
func (c OneCase) Valid() bool {
33+
valid, _ := determineExpected(c.Expected)
34+
return valid
35+
}
36+
37+
func (c OneCase) Answer() int {
38+
_, answer := determineExpected(c.Expected)
39+
return answer
40+
}
41+
42+
// determineExpected examines an .Expected interface{} object and determines
43+
// whether a test case is valid(bool) and has an answer or expects an error.
44+
// returning valid and answer.
45+
func determineExpected(expected interface{}) (bool, int) {
46+
ans, ok := expected.(float64)
47+
if ok {
48+
return ok, int(ans)
49+
}
50+
return false, 0
51+
}
52+
53+
// template applied to above data structure generates the Go test cases
54+
var tmpl = `package collatzconjecture
55+
56+
{{.Header}}
57+
58+
var testCases = []struct {
59+
description string
60+
input int
61+
expectError bool
62+
expected int
63+
}{
64+
{{range .J.Cases}}{
65+
description: "{{.Description}}",
66+
input: {{.Number}},
67+
{{if .Valid}} expected: {{.Answer}},
68+
{{- else}} expectError: true,
69+
{{- end}}
70+
},
71+
{{end}}}
72+
`
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package collatzconjecture
2+
3+
// Source: exercism/problem-specifications
4+
// Commit: 25c4479 Collatz-conjecture: remove trailing space in test name (#839)
5+
// Problem Specifications Version: 1.1.1
6+
7+
var testCases = []struct {
8+
description string
9+
input int
10+
expectError bool
11+
expected int
12+
}{
13+
{
14+
description: "zero steps for one",
15+
input: 1,
16+
expected: 0,
17+
},
18+
{
19+
description: "divide if even",
20+
input: 16,
21+
expected: 4,
22+
},
23+
{
24+
description: "even and odd steps",
25+
input: 12,
26+
expected: 9,
27+
},
28+
{
29+
description: "Large number of even and odd steps",
30+
input: 1000000,
31+
expected: 152,
32+
},
33+
{
34+
description: "zero is an error",
35+
input: 0,
36+
expectError: true,
37+
},
38+
{
39+
description: "negative value is an error",
40+
input: -15,
41+
expectError: true,
42+
},
43+
}

exercises/collatz-conjecture/collatz_conjecture_test.go

-42
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,6 @@ import (
44
"testing"
55
)
66

7-
var testCases = []struct {
8-
description string
9-
input int
10-
expectError bool
11-
expected int
12-
}{
13-
{
14-
description: "zero steps for one",
15-
input: 1,
16-
expectError: false,
17-
expected: 0,
18-
},
19-
{
20-
description: "divide if even",
21-
input: 16,
22-
expectError: false,
23-
expected: 4,
24-
},
25-
{
26-
description: "even and old steps",
27-
input: 12,
28-
expectError: false,
29-
expected: 9,
30-
},
31-
{
32-
description: "large number of even and odd steps",
33-
input: 1000000,
34-
expectError: false,
35-
expected: 152,
36-
},
37-
{
38-
description: "zero is an error",
39-
input: 0,
40-
expectError: true,
41-
},
42-
{
43-
description: "negative value is an error",
44-
input: -15,
45-
expectError: true,
46-
},
47-
}
48-
497
func TestCollatzConjecture(t *testing.T) {
508
for _, testCase := range testCases {
519
steps, err := CollatzConjecture(testCase.input)

0 commit comments

Comments
 (0)