Skip to content

Commit b0e96e5

Browse files
committed
all-your-base: add test generator
- with putting assumption into hints
1 parent 7dc4717 commit b0e96e5

File tree

5 files changed

+264
-196
lines changed

5 files changed

+264
-196
lines changed

exercises/all-your-base/.meta/gen.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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("all-your-base", &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+
Exercise string
24+
Version string
25+
Cases []oneCase
26+
}
27+
28+
// Test cases
29+
type oneCase struct {
30+
Description string
31+
Property string
32+
InputBase int `json:"input_base"`
33+
InputDigits []int `json:"input_digits"`
34+
OutputBase int `json:"output_base"`
35+
Expected []int
36+
}
37+
38+
func (o oneCase) Result() []int {
39+
if o.Expected == nil && len(o.InputDigits) == 1 && o.InputDigits[0] == 0 {
40+
return []int{0}
41+
}
42+
return o.Expected
43+
}
44+
45+
// Template to generate test cases.
46+
var tmpl = `package allyourbase
47+
48+
{{.Header}}
49+
50+
var testCases = []struct {
51+
description string
52+
inputBase int
53+
inputDigits []int
54+
outputBase int
55+
expected []int
56+
}{ {{range .J.Cases}}
57+
{
58+
description: {{printf "%q" .Description}},
59+
inputBase: {{printf "%d" .InputBase}},
60+
inputDigits: {{printf "%#v" .InputDigits}},
61+
outputBase: {{printf "%d" .OutputBase}},
62+
expected: {{printf "%#v" .Result}},
63+
},{{end}}
64+
}
65+
`
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## Implementation
2+
3+
No leading zeroes are allowed so zero is defined as `[0]`.
4+
Thus, `[]`, `[0, 0]` or `[0, 6, 0]` are wrong and should return empty.
Lines changed: 10 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -1,177 +1,21 @@
11
package allyourbase
22

3-
import "testing"
4-
5-
// Note: ConvertToBase should accept leading zeroes in its input,
6-
// but never emit leading zeroes in its output.
7-
// Exception: If the value of the output is zero, represent it with a single zero.
8-
var testCases = []struct {
9-
description string
10-
inputBase uint64
11-
outputBase uint64
12-
inputDigits []uint64
13-
outputDigits []uint64
14-
error error
15-
}{
16-
{
17-
description: "single bit one to decimal",
18-
inputBase: 2,
19-
inputDigits: []uint64{1},
20-
outputBase: 10,
21-
outputDigits: []uint64{1},
22-
},
23-
{
24-
description: "binary to single decimal",
25-
inputBase: 2,
26-
inputDigits: []uint64{1, 0, 1},
27-
outputBase: 10,
28-
outputDigits: []uint64{5},
29-
},
30-
{
31-
description: "single decimal to binary",
32-
inputBase: 10,
33-
inputDigits: []uint64{5},
34-
outputBase: 2,
35-
outputDigits: []uint64{1, 0, 1},
36-
},
37-
{
38-
description: "binary to multiple decimal",
39-
inputBase: 2,
40-
inputDigits: []uint64{1, 0, 1, 0, 1, 0},
41-
outputBase: 10,
42-
outputDigits: []uint64{4, 2},
43-
},
44-
{
45-
description: "decimal to binary",
46-
inputBase: 10,
47-
inputDigits: []uint64{4, 2},
48-
outputBase: 2,
49-
outputDigits: []uint64{1, 0, 1, 0, 1, 0},
50-
},
51-
{
52-
description: "trinary to hexadecimal",
53-
inputBase: 3,
54-
inputDigits: []uint64{1, 1, 2, 0},
55-
outputBase: 16,
56-
outputDigits: []uint64{2, 10},
57-
},
58-
{
59-
description: "hexadecimal to trinary",
60-
inputBase: 16,
61-
inputDigits: []uint64{2, 10},
62-
outputBase: 3,
63-
outputDigits: []uint64{1, 1, 2, 0},
64-
},
65-
{
66-
description: "15-bit integer",
67-
inputBase: 97,
68-
inputDigits: []uint64{3, 46, 60},
69-
outputBase: 73,
70-
outputDigits: []uint64{6, 10, 45},
71-
},
72-
{
73-
description: "empty list",
74-
inputBase: 2,
75-
inputDigits: []uint64{},
76-
outputBase: 10,
77-
outputDigits: []uint64{0},
78-
error: nil,
79-
},
80-
{
81-
description: "single zero",
82-
inputBase: 10,
83-
inputDigits: []uint64{0},
84-
outputBase: 2,
85-
outputDigits: []uint64{0},
86-
},
87-
{
88-
description: "multiple zeros",
89-
inputBase: 10,
90-
inputDigits: []uint64{0, 0, 0},
91-
outputBase: 2,
92-
outputDigits: []uint64{0},
93-
},
94-
{
95-
description: "leading zeros",
96-
inputBase: 7,
97-
inputDigits: []uint64{0, 6, 0},
98-
outputBase: 10,
99-
outputDigits: []uint64{4, 2},
100-
},
101-
{
102-
description: "invalid positive digit",
103-
inputBase: 2,
104-
inputDigits: []uint64{1, 2, 1, 0, 1, 0},
105-
outputBase: 10,
106-
outputDigits: nil,
107-
error: ErrInvalidDigit,
108-
},
109-
{
110-
description: "first base is one",
111-
inputBase: 1,
112-
inputDigits: []uint64{},
113-
outputBase: 10,
114-
outputDigits: nil,
115-
error: ErrInvalidBase,
116-
},
117-
{
118-
description: "second base is one",
119-
inputBase: 2,
120-
inputDigits: []uint64{1, 0, 1, 0, 1, 0},
121-
outputBase: 1,
122-
outputDigits: nil,
123-
error: ErrInvalidBase,
124-
},
125-
{
126-
description: "first base is zero",
127-
inputBase: 0,
128-
inputDigits: []uint64{},
129-
outputBase: 10,
130-
outputDigits: nil,
131-
error: ErrInvalidBase,
132-
},
133-
{
134-
description: "second base is zero",
135-
inputBase: 10,
136-
inputDigits: []uint64{7},
137-
outputBase: 0,
138-
outputDigits: nil,
139-
error: ErrInvalidBase,
140-
},
141-
}
142-
143-
func digitsEqual(a, b []uint64) bool {
144-
if len(a) != len(b) {
145-
return false
146-
}
147-
148-
for i := 0; i < len(a); i++ {
149-
if a[i] != b[i] {
150-
return false
151-
}
152-
}
153-
154-
return true
155-
}
3+
import (
4+
"reflect"
5+
"testing"
6+
)
1567

1578
func TestConvertToBase(t *testing.T) {
1589
for _, c := range testCases {
159-
output, err := ConvertToBase(c.inputBase, c.inputDigits, c.outputBase)
160-
if err != c.error {
161-
t.Fatalf(`FAIL: %s
162-
Expected error: %v
163-
Got: %v`, c.description, c.error, err)
164-
}
165-
166-
if !digitsEqual(c.outputDigits, output) {
10+
output := ConvertToBase(c.inputBase, c.inputDigits, c.outputBase)
11+
if !reflect.DeepEqual(c.expected, output) {
16712
t.Fatalf(`FAIL: %s
16813
Input base: %d
169-
Input digits: %v
14+
Input digits: %#v
17015
Output base: %d
171-
Expected output digits: %v
172-
Got: %v`, c.description, c.inputBase, c.inputDigits, c.outputBase, c.outputDigits, output)
173-
} else {
174-
t.Logf("PASS: %s", c.description)
16+
Expected output digits: %#v
17+
Got: %#v`, c.description, c.inputBase, c.inputDigits, c.outputBase, c.expected, output)
17518
}
19+
t.Logf("PASS: %s", c.description)
17620
}
17721
}

0 commit comments

Comments
 (0)