-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_equation.go
114 lines (109 loc) · 1.84 KB
/
test_equation.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
package test
import (
"flag"
"math"
"testing"
)
func matchString(a, b string) (bool, error) {
return a == b, nil
}
const precision = 1e-10
// TestEquation is a function to test a square equation solver
func TestEquation(EquationRoots func(a, b, c float64) (float64, float64)) {
testEquation := func(t *testing.T) {
flag.Set("test.v", "")
type args struct {
a float64
b float64
c float64
}
tests := []struct {
name string
args args
want float64
want1 float64
}{
{
name: "6x² + 11x - 35 = 0",
args: args{
6,
11,
-35,
},
want: -3.5,
want1: 5. / 3.,
},
{
name: "2x² - 4x - 2 = 0",
args: args{
2,
-4,
-2,
},
want: 1 - math.Sqrt(2),
want1: 1 + math.Sqrt(2),
},
{
name: "2x² - 64 = 0",
args: args{
2,
0,
-64,
},
want: -4 * math.Sqrt(2),
want1: 4 * math.Sqrt(2),
},
{
name: "x² - 16 = 0",
args: args{
1,
0,
-16,
},
want: -4,
want1: 4,
},
{
name: "x² - 7x = 0",
args: args{
1,
-7,
0,
},
want: 0,
want1: 7,
},
{
name: "2x² + 8x = 0",
args: args{
2,
8,
0,
},
want: -4,
want1: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1 := EquationRoots(tt.args.a, tt.args.b, tt.args.c)
t.Logf("Equation: %s\nRoots: %f, %f\nYour answer: %f, %f", tt.name, tt.want, tt.want1, got, got1)
if !(math.Abs(got-tt.want) < precision) {
t.Errorf("Incorrect x1")
}
if !(math.Abs(got1-tt.want1) < precision) {
t.Errorf("Incorrect x2")
}
})
}
}
testSuite := []testing.InternalTest{
{
Name: "TestEquation",
F: testEquation,
},
}
flag.Set("test.v", "=true")
testing.Init()
testing.Main(matchString, testSuite, nil, nil)
}