-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.py
More file actions
executable file
·179 lines (138 loc) · 4.97 KB
/
testing.py
File metadata and controls
executable file
·179 lines (138 loc) · 4.97 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
# (C) 2018 Phicem
# This software is released under MIT license (provided in LICENSE.txt)
import math
import logging
import unittest
from pysics.units import *
from pysics.integrate import integ
from pysics.arrays import DataArray, sampleFunction
# TODO: use coverage !! ('coverage run ./battery.py' then 'coverage report' then 'coverage html' (open htmlcov/index.html))
class TestDataArrays(unittest.TestCase):
""" Behaviors to check
Item 1
Item 2
"""
def setUp(self):
time = np.array([0,1,2,3]) * s
output = np.array([1,2,3,4]) * kg
self.output_vs_time = DataArray(time, output)
x_no_unit = np.array([0,1,2,3])
y_no_unit = np.array([1,2,3,4])
self.xy_no_unit = DataArray(x_no_unit, y_no_unit)
def tearDown(self):
pass
def test_010_integ_only_one_point_in_interval(self):
result = self.output_vs_time.integ(0.5*s,1.5*s)
truth = 2*s*kg
error_message = "Incorrect result for integ"
self.assertEqual(result, truth, error_message)
def test_011_integ_interval_exactly_on_data(self):
result = self.output_vs_time.integ(1*s,2*s)
truth = 2.5*s*kg
error_message = "Incorrect result for integ"
#pdb.set_trace()
self.assertEqual(result, truth, error_message)
def test_012_integ_limits_are_checked(self):
with self.assertRaises(Exception):
result = self.output_vs_time.integ(0*s,4*s)
with self.assertRaises(Exception):
result = self.output_vs_time.integ(2*s,1*s)
def test_013_integ_array_without_units(self):
result = self.xy_no_unit.integ(0.5,1.5)
truth = 2
error_message = "Incorrect result for integ"
self.assertEqual(result, truth, error_message)
def test_020_sampleFunction(self):
def func(x):
return x/s*kg+1*kg
XY = sampleFunction(func, 0*s, 3*s, 4)
self.assertEqual(XY, self.output_vs_time)
def test_001_not_ascending_order(self):
revert = np.array([1,2,3,0])
with self.assertRaises(Exception):
DataArray(revert, revert)
with self.assertRaises(Exception):
DataArray(revert, revert*m)
with self.assertRaises(Exception):
DataArray(revert*m, revert)
class TestUnits(unittest.TestCase):
""" Behaviors to check
Item 1
Item 2
"""
def setUp(self):
pass
def tearDown(self):
pass
def test_010_disp_for_scalar(self):
# Regular value, simple unit
a = 20*kft
mystr1 = a.disp()
#print(mystr1)
self.assertEqual(mystr1, '6.096E+03 m [PHYS]', "Incorrect disp value")
mystr2 = a.disp('km')
self.assertEqual(mystr2, "6.096 km", "Incorrect disp value")
# Regular value, complex unit
b = 2*J/s
mystr3 = b.disp()
self.assertEqual(mystr3, '2.000 kg*m**2/s**3 [PHYS]', "Incorrect disp value")
mystr4 = b.disp('W')
self.assertEqual(mystr4, '2.000 W', "Incorrect disp value")
# Incorrect unit
c = 2*J
with self.assertRaises(Exception):
mystr5 = c.disp('W')
def test_020_automatic_factorize_units(self):
A = np.array([1*m, 2*m])
B = np.array([2, 3])*m
self.assertIsNone(np.testing.assert_array_equal(A+1*m, B))
def test_030_init_from_list(self):
A = [1, 2]*m
B = np.array([1*m, 2*m])
self.assertIsNone(np.testing.assert_array_equal(A, B))
def test_040_vectorized_function(self):
D = [1, 2, 20, 30]*m
def myfunc(d):
if d > 15*m:
return d+5*m
else:
return 0*m
myfunc_vect = np.vectorize(myfunc)
out = myfunc_vect(D)
out_expected = [0, 0, 25, 35]*m
self.assertIsNone(np.testing.assert_array_equal(out, out_expected))
class TestIntegrate(unittest.TestCase):
""" Behaviors to check
Item 1
Item 2
"""
def setUp(self):
pass
def tearDown(self):
pass
def test_010_integrate_with_units(self):
def func(x):
return x**2
a = 0.1*m
b = 13*m
I = integ(func, a, b)
I_expected = (b**3-a**3)/3
self.assertAlmostEqual(I, I_expected, delta = 10**-8*m**3)
def test_011_integrate_no_unit(self):
def func(x):
return 1/x
a = 0.1
b = 13
I = integ(func, a, b)
I_expected = np.log(b) - np.log(a)
self.assertAlmostEqual(I, I_expected)
full_suite_DataArray= unittest.TestLoader().loadTestsFromTestCase(TestDataArrays)
full_suite_Units = unittest.TestLoader().loadTestsFromTestCase(TestUnits)
full_suite_Integrate = unittest.TestLoader().loadTestsFromTestCase(TestIntegrate)
TOTAL = unittest.TestSuite()
TOTAL.addTests(full_suite_DataArray)
TOTAL.addTests(full_suite_Units)
TOTAL.addTests(full_suite_Integrate)
suite = TOTAL
unittest.TextTestRunner(verbosity=2).run(suite)