-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve_test.go
116 lines (109 loc) · 2.42 KB
/
solve_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package maketen
import (
"bufio"
"bytes"
"os/exec"
"strings"
"testing"
)
func TestSolve0(t *testing.T) {
got := Solve()
if expected := 0; len(got) != expected {
t.Fatalf("expected %d solutions but got: %d", expected, len(got))
}
}
func TestSolve1(t *testing.T) {
var results []string
var count int
for x := 0; x <= 10; x++ {
got := Solve(NewInt(x))
for _, e := range got {
count++
results = append(results, e.String())
}
}
if expected := 1; count != expected {
t.Fatalf("expected %d solutions but got: %d", expected, count)
}
testResults(t, results)
}
func TestSolve2(t *testing.T) {
var results []string
var count int
for x := 0; x < 10; x++ {
for y := 0; y < 10; y++ {
got := Solve(NewInt(x), NewInt(y))
for _, e := range got {
count++
results = append(results, e.String())
}
}
}
if expected := 11; count != expected {
t.Fatalf("expected %d solutions but got: %d", expected, count)
}
testResults(t, results)
}
func TestSolve3(t *testing.T) {
var results []string
var count int
for x := 0; x < 10; x++ {
for y := 0; y < 10; y++ {
for z := 0; z < 10; z++ {
got := Solve(NewInt(x), NewInt(y), NewInt(z))
for _, e := range got {
count++
results = append(results, e.String())
}
}
}
}
if expected := 412; count != expected {
t.Fatalf("expected %d solutions but got: %d", expected, count)
}
testResults(t, results)
}
func TestSolve4(t *testing.T) {
var results []string
var count int
for x := 0; x < 10; x++ {
for y := 0; y < 10; y++ {
for z := 0; z < 10; z++ {
for w := 0; w < 10; w++ {
got := Solve(NewInt(x), NewInt(y), NewInt(z), NewInt(w))
for _, e := range got {
count++
results = append(results, e.String())
}
}
}
}
}
if expected := 23741; count != expected {
t.Fatalf("expected %d solutions but got: %d", expected, count)
}
testResults(t, results)
}
func testResults(t *testing.T, results []string) {
cmd := exec.Command("bc", "-l")
cmd.Stdin = strings.NewReader(strings.Join(results, "\n"))
out := new(bytes.Buffer)
cmd.Stdout = out
if err := cmd.Run(); err != nil {
t.Fatal(err)
}
scanner := bufio.NewScanner(out)
var i int
for scanner.Scan() {
i++
line := scanner.Text()
if line == "10" || strings.HasPrefix(line, "10.000000") ||
strings.HasPrefix(line, "9.9999999") {
continue
}
t.Errorf("got %s: %s", line, results[i-1])
}
if scanner.Err() != nil {
t.Fatal(scanner.Err())
}
}