Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import unittest | |
| 6 import math | |
| 7 import numpy as np | |
| 8 | |
| 9 from libs.math.vectors import vsum | |
| 10 | |
| 11 BIG = 1e100 | |
| 12 LITTLE = 1 | |
| 13 | |
| 14 class VectorsTest(unittest.TestCase): | |
|
Martin Barbella
2016/12/02 19:52:37
2 blank lines before this one. Does gpylint compla
wrengr
2016/12/02 20:54:46
It really wants the numpy import to be grouped wit
Martin Barbella
2016/12/02 22:12:11
Yeah, those are fine to ignore.
stgao, katesonia:
Sharu Jiang
2016/12/02 22:26:12
No, somehow the presubmit won't check the 2 empty
| |
| 15 | |
| 16 def setUp(self): | |
| 17 self._xs = [BIG, LITTLE, -BIG] | |
| 18 | |
| 19 def testFsumKeepsPrecision(self): | |
| 20 """This test is to make sure that ``testVsum`` should actually work. | |
| 21 | |
| 22 N.B., because ``math.fsum`` uses Shewchuk's algorithm whereas ``vsum`` | |
| 23 uses Kahan's algorithm, the former is more precise than the latter. So | |
| 24 we need to exercise some caution when designing our tests. | |
| 25 """ | |
| 26 # N.B., with precision loss, we'll get 0 rather than LITTLE. | |
| 27 self.assertEqual(LITTLE, math.fsum(self._xs)) | |
| 28 | |
| 29 def testVsumKeepsPrecision(self): | |
|
Martin Barbella
2016/12/02 19:52:37
Please add docstrings for all tests. They're prett
wrengr
2016/12/02 20:54:46
None of the other tests in the repo have them, but
Martin Barbella
2016/12/02 22:12:11
Thanks. They're really helpful. We probably should
| |
| 30 vs = [np.array([x, -x, 2*x]) for x in self._xs] | |
| 31 total = vsum(vs) | |
| 32 self.assertIsNotNone(total) | |
| 33 | |
| 34 self.assertListEqual([LITTLE, -LITTLE, 2*LITTLE], total.tolist()) | |
| 35 | |
| 36 def testVsumEmptyWithoutShape(self): | |
| 37 self.assertIsNone(vsum([])) | |
| 38 | |
| 39 def testVSumEmptyWithShape(self): | |
| 40 expected_shape = (3,) | |
| 41 total = vsum([], shape=expected_shape) | |
| 42 self.assertIsNotNone(total) | |
| 43 self.assertTupleEqual(expected_shape, total.shape) | |
| 44 self.assertListEqual(np.zeros(expected_shape).tolist(), total.tolist()) | |
| 45 | |
| OLD | NEW |