-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_decimator.py
382 lines (352 loc) · 18.2 KB
/
test_decimator.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import unittest
from decimator import Decimator
# numeric constructor arguments
integers = [0, 1, 2, 5, 10, 11, 13, 53, 99, 100, 101, 103, 451564, 515445105, 1654651654651616]
floats = [0.0, 0.1, 0.10, 0.11, 0.213, 0.1215135, 1.0, 1.1, 1.11, 1.0000, 1.1000, 121.10, 1.3261, 13.0, 5.3, 9.9, 10.0, 10111.56144564684654, 10.3, 4515.64, 5.15445105, 1.6546516546516165]
failures = [-1, -0.12]
# string constructor arguments
integer_strings = ['0', '1', '2', '5', '10', '11', '13', '53', '99', '100', '101', '103', '451564', '515445105', '1654651654651616']
float_strings = ['0.0', '0.1', '0.10', '0.11', '0.213', '0.1215135', '1.0', '1.1', '1.11', '1.0000', '1.1000', '121.10', '1.3261', '13.0', '5.3', '9.9', '10.0', '10111.56144564684654', '10.3', '4515.64', '5.15445105', '1.654651654651616']
failure_strings = ['-1', '-0.12']
# lists for decimators with numeric constructor args
integer_decimators = []
float_decimators = []
failure_decimators = []
# lists for decimators with string constructor args
integer_string_decimators = []
float_string_decimators = []
failure_string_decimators = []
# instantiate decimators with numeric constructor args
for integer in integers:
integer_decimators.append(Decimator(integer))
for float_value in floats:
float_decimators.append(Decimator(float_value))
for failure_value in failures:
try:
failure_decimators.append(Decimator(failure_value))
except Exception as error:
print(f'Trying to create Decimator object with {failure_value}:\n{error}\n')
# instantiate decimators with string constructor args
for integer_string in integer_strings:
integer_string_decimators.append(Decimator(integer_string))
for float_string in float_strings:
float_string_decimators.append(Decimator(float_string))
for failure_string in failure_strings:
try:
failure_string_decimators.append(Decimator(failure_string))
except Exception as error:
print(f'Trying to create Decimator object with {failure_string}:\n{error}\n')
class TestDecimator(unittest.TestCase):
def test_0_num_int(self):
"""
Use __str__ method of Decimator objects to test validity of data structures
for Decimators instantiated with int's
"""
print('f\nIn test_0_num_int')
print('--------------------------')
for decimator in integer_decimators:
print(f'Decimator number: {decimator.number}')
print(f'Decimator positive_decimals: {decimator.positive_decimals}')
print(f'Decimator negative_decimals: {decimator.negative_decimals}')
string = str(decimator)
self.assertEqual(int(decimator.integer_string)+float(decimator.fraction_string), int(string))
def test_1_num_float(self):
"""
TUse __str__ method of Decimator objects to test validity of data structures
for Decimators instantiated with floats
"""
print('f\nIn test_1_num_float')
print('--------------------------')
for decimator in float_decimators:
print(f'Decimator number: {decimator.number}')
print(f'Decimator string: {str(decimator)}')
print(f'Decimator float string: {float(str(decimator))}')
print(f'Decimator integer: {decimator.integer_string}')
print(f'Decimator fraction: {decimator.fraction_string}')
print(f'Decimator positive_decimals: {decimator.positive_decimals}')
print(f'Decimator negative_decimals: {decimator.negative_decimals}')
string = str(decimator)
value_a = float(int(decimator.integer_string))+float(decimator.fraction_string)
value_b = float(string)
difference = value_a-value_b
print(f'Value A: {str(value_a)}')
print(f'Value B: {str(value_b)}\n')
self.assertTrue(abs(difference) < 10**((-1)*(decimator.fraction_precision)))
def test_2_str_int(self):
"""
Use __str__ method of Decimator objects to test validity of data structures
for Decimators instantiated with int strings
"""
print('f\nIn test_2_str_int')
print('--------------------------')
for decimator in integer_string_decimators:
print(f'Decimator number: {decimator.number}')
print(f'Decimator positive_decimals: {decimator.positive_decimals}')
print(f'Decimator negative_decimals: {decimator.negative_decimals}')
string = str(decimator)
self.assertEqual(int(decimator.integer_string)+float(decimator.fraction_string), int(string))
def test_3_str_float(self):
"""
Use __str__ method of Decimator objects to test validity of data structures
for Decimators instantiated with float strings
"""
print('f\nIn test_3_str_float')
print('--------------------------')
for decimator in float_string_decimators:
print(f'Decimator number: {decimator.number}')
print(f'Decimator string: {str(decimator)}')
print(f'Decimator float string: {float(str(decimator))}')
print(f'Decimator integer: {decimator.integer_string}')
print(f'Decimator fraction: {decimator.fraction_string}')
print(f'Decimator positive_decimals: {decimator.positive_decimals}')
print(f'Decimator negative_decimals: {decimator.negative_decimals}')
string = str(decimator)
value_a = float(int(decimator.integer_string))+float(decimator.fraction_string)
value_b = float(string)
difference = value_a-value_b
print(f'Value A: {str(value_a)}')
print(f'Value B: {str(value_b)}\n')
self.assertTrue(abs(difference) < 10**((-1)*(decimator.fraction_precision)))
########################################################
# get_value
def test_4_get_value_num_int(self):
"""
Test get_value method of Decimator objects
"""
print('f\nIn test_4_get_value_num_int')
print('--------------------------')
for decimator in integer_decimators:
print(f'Decimator number: {decimator.number}')
print(f'Decimator positive_decimals: {decimator.positive_decimals}')
print(f'Decimator negative_decimals: {decimator.negative_decimals}')
string = str(decimator)
self.assertEqual(decimator.number, decimator.get_value())
def test_5_get_value_num_float(self):
"""
Test get_value method of Decimator objects
"""
print('f\nIn test_5_get_value_num_float')
print('--------------------------')
for decimator in float_decimators:
print(f'Decimator number: {decimator.number}')
print(f'Decimator string: {str(decimator)}')
print(f'Decimator float string: {float(str(decimator))}')
print(f'Decimator integer: {decimator.integer_string}')
print(f'Decimator fraction: {decimator.fraction_string}')
print(f'Decimator positive_decimals: {decimator.positive_decimals}')
print(f'Decimator negative_decimals: {decimator.negative_decimals}')
string = str(decimator)
self.assertEqual(decimator.number, decimator.get_value())
def test_6_get_value_str_int(self):
"""
Test get_value method of Decimator objects
"""
print('f\nIn test_6_get_value_str_int')
print('--------------------------')
for decimator in integer_string_decimators:
print(f'Decimator number: {decimator.number}')
print(f'Decimator positive_decimals: {decimator.positive_decimals}')
print(f'Decimator negative_decimals: {decimator.negative_decimals}')
string = str(decimator)
self.assertEqual(int(decimator.number), decimator.get_value())
def test_7_get_value_str_float(self):
"""
Test get_value method of Decimator objects
"""
print('f\nIn test_7_get_value_str_float')
print('--------------------------')
for decimator in float_string_decimators:
print(f'Decimator number: {decimator.number}')
print(f'Decimator string: {str(decimator)}')
print(f'Decimator float string: {float(str(decimator))}')
print(f'Decimator integer: {decimator.integer_string}')
print(f'Decimator fraction: {decimator.fraction_string}')
print(f'Decimator positive_decimals: {decimator.positive_decimals}')
print(f'Decimator negative_decimals: {decimator.negative_decimals}')
string = str(decimator)
self.assertEqual(float(decimator.number), decimator.get_value())
########################################################
# shift
def test_8_shift_positive_num_int(self):
"""
Test shift method of Decimator objects, with positive shift for numeric int constructor args
"""
print('f\nIn test_8_shift_positive_num_int')
print('--------------------------')
shifts = [0,1,2,3,10,20]
for decimator in integer_decimators:
for shift in shifts:
print()
print(f'Decimator number: {decimator.number}')
print(f'Decimator positive_decimals: {decimator.positive_decimals}')
print(f'Decimator negative_decimals: {decimator.negative_decimals}')
print(f'Shift: {shift}')
string = str(decimator)
shifted_decimator = decimator.shift(shift)
print(f'Shifted decimator: {str(shifted_decimator)}')
self.assertEqual(decimator.get_value()*10**shift, shifted_decimator.get_value())
def test_9_shift_negative_num_int(self):
"""
Test shift method of Decimator objects, with negative shift for numeric int constructor args
"""
print('f\nIn test_9_shift_negative_num_int')
print('--------------------------')
shifts = [-1,-2,-3,-10,-20]
for decimator in integer_decimators:
for shift in shifts:
print()
print(f'Decimator number: {decimator.number}')
print(f'Decimator positive_decimals: {decimator.positive_decimals}')
print(f'Decimator negative_decimals: {decimator.negative_decimals}')
print(f'Shift: {shift}')
string = str(decimator)
shifted_decimator = decimator.shift(shift)
print(f'Shifted decimator: {str(shifted_decimator)}')
value_a = decimator.get_value()*10**shift
value_b = shifted_decimator.get_value()
difference = abs(value_a - value_b)
print(f'Value A: {str(value_a)}')
print(f'Value B: {str(value_b)}')
self.assertTrue(abs(difference) < 10 ** ((-1) * (shifted_decimator.fraction_precision)))
def test_10_shift_positive_num_float(self):
"""
Test shift method of Decimator objects, with positive shift for numeric float constructor args
"""
print('f\nIn test_10_shift_positive_num_float')
print('--------------------------')
shifts = [0,1,2,3,10,20]
for decimator in float_decimators:
for shift in shifts:
print()
print(f'Decimator number: {decimator.number}')
print(f'Decimator positive_decimals: {decimator.positive_decimals}')
print(f'Decimator negative_decimals: {decimator.negative_decimals}')
print(f'Shift: {shift}')
string = str(decimator)
shifted_decimator = decimator.shift(shift)
print(f'Shifted decimator: {str(shifted_decimator)}')
value_a = decimator.get_value()*10**shift
value_b = shifted_decimator.get_value()
difference = abs(value_a - value_b)
print(f'Value A: {str(value_a)}')
print(f'Value B: {str(value_b)}')
self.assertTrue(abs(difference) < 10 ** max(shift-15, shift-decimator.fraction_precision))
def test_11_shift_negative_num_float(self):
"""
Test shift method of Decimator objects, with negative shift for numeric float constructor args
"""
print('f\nIn test_11_shift_negative_num_float')
print('--------------------------')
shifts = [-1,-2,-3,-10,-20]
for decimator in float_decimators:
for shift in shifts:
print()
print(f'Decimator number: {decimator.number}')
print(f'Decimator positive_decimals: {decimator.positive_decimals}')
print(f'Decimator negative_decimals: {decimator.negative_decimals}')
print(f'Shift: {shift}')
string = str(decimator)
shifted_decimator = decimator.shift(shift)
print(f'Shifted decimator: {str(shifted_decimator)}')
value_a = decimator.get_value()*10**shift
value_b = shifted_decimator.get_value()
difference = abs(value_a - value_b)
print(f'Value A: {str(value_a)}')
print(f'Value B: {str(value_b)}')
self.assertTrue(abs(difference) < 10 ** ((-1) * (shifted_decimator.fraction_precision - 1)))
def test_12_shift_positive_string_int(self):
"""
Test shift method of Decimator objects, with positive shift for string int constructor args
"""
print('f\nIn test_12_shift_positive_string_int')
print('--------------------------')
shifts = [0,1,2,3,10,20]
for decimator in integer_string_decimators:
for shift in shifts:
print()
print(f'Decimator number: {decimator.number}')
print(f'Decimator positive_decimals: {decimator.positive_decimals}')
print(f'Decimator negative_decimals: {decimator.negative_decimals}')
print(f'Shift: {shift}')
string = str(decimator)
shifted_decimator = decimator.shift(shift)
print(f'Shifted decimator: {str(shifted_decimator)}')
self.assertEqual(decimator.get_value()*10**shift, shifted_decimator.get_value())
def test_13_shift_negative_string_int(self):
"""
Test shift method of Decimator objects, with negative shift for string int constructor args
"""
print('f\nIn test_13_shift_negative_string_int')
print('--------------------------')
shifts = [-1,-2,-3,-10,-20]
for decimator in integer_string_decimators:
for shift in shifts:
print()
print(f'Decimator number: {decimator.number}')
print(f'Decimator positive_decimals: {decimator.positive_decimals}')
print(f'Decimator negative_decimals: {decimator.negative_decimals}')
print(f'Shift: {shift}')
string = str(decimator)
shifted_decimator = decimator.shift(shift)
print(f'Shifted decimator: {str(shifted_decimator)}')
value_a = decimator.get_value()*10**shift
value_b = shifted_decimator.get_value()
difference = abs(value_a - value_b)
print(f'Value A: {str(value_a)}')
print(f'Value B: {str(value_b)}')
self.assertTrue(abs(difference) < 10 ** ((-1) * (shifted_decimator.fraction_precision)))
def test_14_shift_positive_string_float(self):
"""
Test shift method of Decimator objects, with positive shift for string float constructor args
"""
print('f\nIn test_14_shift_positive_string_float')
print('--------------------------')
shifts = [0,1,2,3,10,20]
for decimator in float_string_decimators:
for shift in shifts:
print()
print(f'Decimator number: {decimator.number}')
print(f'Decimator positive_decimals: {decimator.positive_decimals}')
print(f'Decimator negative_decimals: {decimator.negative_decimals}')
print(f'Shift: {shift}')
string = str(decimator)
shifted_decimator = decimator.shift(shift)
print(f'Shifted decimator: {str(shifted_decimator)}')
value_a = decimator.get_value()*10**shift
value_b = shifted_decimator.get_value()
difference = abs(value_a - value_b)
print(f'Value A: {str(value_a)}')
print(f'Value B: {str(value_b)}')
print(f'Fraction precision: {shifted_decimator.fraction_precision}')
self.assertTrue( abs(difference) < 10 ** ( (-1)*(shifted_decimator.fraction_precision-1) ) )
def test_15_shift_negative_string_float(self):
"""
Test shift method of Decimator objects, with negative shift for string float constructor args
"""
print('f\nIn test_15_shift_negative_string_float')
print('--------------------------')
shifts = [-1,-2,-3,-10,-20]
for decimator in float_string_decimators:
for shift in shifts:
print()
print(f'Decimator number: {decimator.number}')
print(f'Decimator positive_decimals: {decimator.positive_decimals}')
print(f'Decimator negative_decimals: {decimator.negative_decimals}')
print(f'Shift: {shift}')
string = str(decimator)
shifted_decimator = decimator.shift(shift)
print(f'Shifted decimator: {str(shifted_decimator)}')
value_a = decimator.get_value()*10**shift
value_b = shifted_decimator.get_value()
difference = abs(value_a - value_b)
print(f'Value A: {str(value_a)}')
print(f'Value B: {str(value_b)}')
try:
self.assertEqual(decimator.get_value() * 10 ** shift, shifted_decimator.get_value())
except AssertionError as error:
try:
self.assertTrue(abs(difference) < 10 ** shift-decimator.fraction_precision)
except AssertionError as error:
self.assertTrue(abs(difference) < 10 ** decimator.fraction_precision)
if __name__ == '__main__':
unittest.main()