-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsum_test.py
49 lines (37 loc) · 1.54 KB
/
rsum_test.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
import math
import unittest
from random import random
from rsum import robust_sum
from validate import validate_sequence as validate
class TestRobustSum(unittest.TestCase):
def test_sum(self):
self.assertEqual(robust_sum([1, 64], [-1e-64, 1e64]), (-1e-64, 65, 1e64))
self.assertEqual(robust_sum([0], [0]), (0,))
self.assertEqual(robust_sum([0], [1]), (1,))
self.assertEqual(robust_sum([1, 1e64], [1e-64, 2]), (1e-64, 3, 1e64))
self.assertEqual(robust_sum([1], [1e-64, 1e-16]), (1e-64, 1e-16, 1))
self.assertEqual(robust_sum([0], [1]), (1,))
for i in xrange(-10, 10 + 1):
for j in xrange(-10, 10 + 1):
self.assertEqual(robust_sum([i], [j]), (i + j,))
self.assertTrue(
validate(robust_sum(
[5.711861227349496e-133, 1e-116],
[5.711861227349496e-133, 1e-116],
))
)
nois = [0] * 10
expect = [0] * 10
for i in xrange(0, 10):
nois[i] = math.pow(2, -1000 + 53 * i)
expect[i] = math.pow(2, -999 + 53 * i)
x = robust_sum(nois, nois)
self.assertEqual(x, tuple(expect))
self.assertTrue(validate(x))
self.assertEqual(robust_sum([0], [1, 1e64]), (1, 1e64))
s = [0]
for i in xrange(0, 1000):
s = robust_sum(s, [random() * math.pow(2, random()*1800-900)])
self.assertTrue(validate(s))
suite = unittest.TestLoader().loadTestsFromTestCase(TestRobustSum)
unittest.TextTestRunner(verbosity=4).run(suite)