-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrun_test.py
More file actions
54 lines (41 loc) · 1.59 KB
/
run_test.py
File metadata and controls
54 lines (41 loc) · 1.59 KB
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
#!/usr/bin/env python3
# coding: utf8
env = globals().copy()
import sys
import os
import unittest
os.chdir(os.path.dirname(os.path.abspath(__file__)))
def runfile(name, skip=16, **kwargs):
filename = name + '.py'
with open(filename, encoding='utf8') as file:
source = file.read()
if source[0] == '\ufeff':
source = source[1:]
source = "\n".join([""] * skip + source.splitlines()[skip:]) + "\n"
code = compile(source, filename, 'exec')
vars = kwargs.copy()
vars['solutions'] = ()
exec(code, env, vars)
return vars.get('solutions')
########################################################################################################################
class TestRoots(unittest.TestCase):
def assert_roots(self, solutions, *ref):
solutions = sorted(solutions)
self.assertEqual(len(solutions), len(ref), "Wrong number of results")
for x, r in zip(solutions, ref):
self.assertAlmostEqual(x, r)
def test_sample0(self):
solutions = runfile('exercise1', a=1, b=2, c=3)
self.assert_roots(solutions)
def test_sample1(self):
solutions = runfile('exercise1', a=1, b=-2, c=1)
self.assert_roots(solutions, 1.)
def test_sample2(self):
solutions = runfile('exercise1', a=3, b=-2, c=-1)
self.assert_roots(solutions, -0.33333333333333, 1.)
def test_sampleL(self):
solutions = runfile('exercise1', a=0, b=-2, c=1)
self.assert_roots(solutions, 0.5)
if __name__ == '__main__':
test = unittest.main(exit=False)
sys.exit(not test.result.wasSuccessful())