|
| 1 | +""" |
| 2 | +Unit tests for ``random_strategy``. |
| 3 | +""" |
| 4 | +import unittest |
| 5 | +import numpy |
| 6 | +from td3a_cpp.tutorial import pydot, cblas_ddot, cblas_sdot |
| 7 | +from td3a_cpp.tutorial.dot_cython import ( |
| 8 | + dot_product, dot_cython_array, |
| 9 | + dot_cython_array_optim, dot_array, |
| 10 | + dot_array_16, dot_array_16_sse |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +class TestTutorialDot(unittest.TestCase): |
| 15 | + |
| 16 | + def test_all_dot(self): |
| 17 | + va = numpy.random.randn(100).astype(numpy.float64) |
| 18 | + vb = numpy.random.randn(100).astype(numpy.float64) |
| 19 | + fcts = [dot_product, dot_cython_array, |
| 20 | + dot_cython_array_optim, dot_array, |
| 21 | + dot_array_16, dot_array_16_sse] |
| 22 | + res = [] |
| 23 | + for fct in fcts: |
| 24 | + r = fct(va, vb) |
| 25 | + res.append(r) |
| 26 | + for r in res[1:]: |
| 27 | + self.assertTrue(abs(res[0] - r) <= 1e-12) |
| 28 | + |
| 29 | + def test_ddot(self): |
| 30 | + va = numpy.random.randn(100).astype(numpy.float64) |
| 31 | + vb = numpy.random.randn(100).astype(numpy.float64) |
| 32 | + res1 = cblas_ddot(va, vb) |
| 33 | + res2 = numpy.dot(va, vb) |
| 34 | + self.assertTrue(abs(res1 - res2) <= 1e-4) |
| 35 | + |
| 36 | + def test_sdot(self): |
| 37 | + va = numpy.random.randn(100).astype(numpy.float32) |
| 38 | + vb = numpy.random.randn(100).astype(numpy.float32) |
| 39 | + res1 = cblas_sdot(va, vb) |
| 40 | + res2 = numpy.dot(va, vb) |
| 41 | + self.assertTrue(abs(res1 - res2) <= 1e-13) |
| 42 | + |
| 43 | + def test_pydot(self): |
| 44 | + va = numpy.random.randn(100).astype(numpy.float64) |
| 45 | + vb = numpy.random.randn(100).astype(numpy.float64) |
| 46 | + res1 = pydot(va, vb) |
| 47 | + res2 = numpy.dot(va, vb) |
| 48 | + self.assertTrue(abs(res1 - res2) <= 1e-13) |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == '__main__': |
| 52 | + unittest.main() |
0 commit comments