Commit 9a25d705 authored by Philipp  Trunschke's avatar Philipp Trunschke

add simple unit tests for python interface

parent 9a5ffae7
......@@ -231,6 +231,10 @@ test: $(TEST_NAME)
./$(TEST_NAME) all
test_python: # build/libxerus.so build/python3/xerus.so
@export PYTHONPATH=build/python3:${PYTHONPATH}; export LD_LIBRARY_PATH=build:${LD_LIBRARY_PATH}; pytest src/pyTests
fullTest: $(TUTORIALS) $(TEST_NAME)
$(foreach x,$(TUTORIALS),./$(x)$(\n))
./$(TEST_NAME) all
......
import unittest
from itertools import product
ranges = lambda *args: product(*[range(arg) for arg in args])
import xerus as xe
import numpy as np
class TestArithmetic(unittest.TestCase):
def setUp(self):
self.dimension = [10, 15, 20]
self.ranks = [1, 8, 4, 1]
def test_tensor_linear_combination(self):
ten_a = xe.Tensor.ones(self.dimension)
ten_b = xe.Tensor.ones(self.dimension) * 4
ten_c = ten_a + ten_b
for (i, j, k) in ranges(*self.dimension):
self.assertAlmostEqual(ten_c[i, j, k], 5)
import unittest
from itertools import product
ranges = lambda *args: product(*[range(arg) for arg in args])
import xerus as xe
import numpy as np
class TestNumpyInterface(unittest.TestCase):
def setUp(self):
self.dimension = [10, 15, 20]
self.ranks = [1, 8, 4, 1]
def test_to_ndarray(self):
ten = xe.TTTensor.random(self.dimension, self.ranks[1:-1])
for lia in range(ten.degree()):
comp_ten = ten.get_component(lia)
comp_dim = comp_ten.dimensions
comp_nd = comp_ten.to_ndarray()
for (i, j, k) in ranges(*comp_dim):
self.assertEqual(comp_ten[[i,j,k]], comp_nd[i,j,k])
def test_from_ndarray(self):
arr = np.random.randn(50,50)
ten = xe.Tensor.from_ndarray(arr)
for lia in range(arr.shape[0]):
for lib in range(arr.shape[1]):
self.assertEqual(ten[[lia, lib]], arr[lia,lib])
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment