-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_strategy.py
65 lines (52 loc) · 2.17 KB
/
test_strategy.py
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
import math
import cmath
from unittest import TestCase
from abc import ABC
class DiscriminantStrategy(ABC):
def calculate_discriminant(self, a, b, c):
pass
class OrdinaryDiscriminantStrategy(DiscriminantStrategy):
def calculate_discriminant(self, a, b, c):
return b*b - 4*a*c
class RealDiscriminantStrategy(DiscriminantStrategy):
def calculate_discriminant(self, a, b, c):
result = b*b-4*a*c
return result if result >= 0 else float('nan')
class QuadraticEquationSolver:
def __init__(self, strategy):
self.strategy = strategy
def solve(self, a, b, c):
""" Returns a pair of complex (!) values """
disc = complex(self.strategy.calculate_discriminant(a, b, c), 0)
root_disc = cmath.sqrt(disc)
return (
(-b + root_disc) / (2 * a),
(-b - root_disc) / (2 * a)
)
class Evaluate(TestCase):
def test_positive_ordinary(self):
strategy = OrdinaryDiscriminantStrategy()
solver = QuadraticEquationSolver(strategy)
results = solver.solve(1, 10, 16)
self.assertEqual(complex(-2, 0), results[0])
self.assertEqual(complex(-8, 0), results[1])
def test_positive_real(self):
strategy = RealDiscriminantStrategy()
solver = QuadraticEquationSolver(strategy)
results = solver.solve(1, 10, 16)
self.assertEqual(complex(-2, 0), results[0])
self.assertEqual(complex(-8, 0), results[1])
def test_negative_ordinary(self):
strategy = OrdinaryDiscriminantStrategy()
solver = QuadraticEquationSolver(strategy)
results = solver.solve(1, 4, 5)
self.assertEqual(complex(-2, 1), results[0])
self.assertEqual(complex(-2, -1), results[1])
def test_negative_real(self):
strategy = RealDiscriminantStrategy()
solver = QuadraticEquationSolver(strategy)
results = solver.solve(1, 4, 5)
self.assertTrue(math.isnan(results[0].real))
self.assertTrue(math.isnan(results[1].real))
self.assertTrue(math.isnan(results[0].imag))
self.assertTrue(math.isnan(results[1].imag))