-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver009_test.go
93 lines (84 loc) · 2.02 KB
/
solver009_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
package goeulerproject
import (
"errors"
"sort"
"testing"
)
type testCase11 struct {
in float64
want []float64
err error
}
func TestPythagoreanTriplet(t *testing.T) {
cases := []testCase11{
// test example
{
in: 12,
want: []float64{3, 4, 5},
err: nil,
},
// test problem solution
{
in: 1000,
want: []float64{200, 375, 425},
err: nil,
},
// test case without a valid solution
{
in: 50,
want: []float64{0, 0, 0},
err: errors.New("cannot find pythagorean triplet with sum: 50"),
},
// test invalid input
{
in: -5,
want: []float64{0, 0, 0},
err: errors.New("invalid input: -5 is smaller than minimal input of 3"),
},
}
// iterate over test cases
for _, c := range cases {
r1, r2, r3, err := PythagoreanTriplet(c.in)
got := []float64{r1, r2, r3}
// output is not always sorted
sort.Float64s(got)
// test similarity of expected and received value
if !isEqualSlice(got, c.want) {
t.Errorf("PythagoreanTriplet(%v) == %v, want %v\n",
c.in, got, c.want)
}
// if no error is returned, test if none is expected
if err == nil && c.err != nil {
t.Errorf("PythagoreanTriplet(%v) returned error %v, want %v\n",
c.in, err, c.err)
}
// if error is returned, test if an error is expected
if err != nil {
// if c.err is nil, print wanted and received error
// else if an error is wanted and received but error
// messages are not the same
// print wanted and received error
if c.err == nil {
t.Errorf("PythagoreanTriplet(%v) returned error %v, want %v\n",
c.in, err, c.err)
} else if err.Error() != c.err.Error() {
t.Errorf("PythagoreanTriplet(%v) returned error %v, want %v\n",
c.in, err, c.err)
}
}
}
}
// isEqualSlice returns true if the lenght of the input
// slices is identical and all elements of the input
// slices are identical, too.
func isEqualSlice(a, b []float64) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}