From f5bbd3cf223b32f7090a01f0f2f78b48a3f3341d Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Tue, 3 Dec 2019 16:39:54 +0100 Subject: [PATCH] ensure functionality through unit testing --- python/tests/test_Table.py | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 python/tests/test_Table.py diff --git a/python/tests/test_Table.py b/python/tests/test_Table.py new file mode 100644 index 000000000..577de84d2 --- /dev/null +++ b/python/tests/test_Table.py @@ -0,0 +1,46 @@ +import pytest +import numpy as np + +from damask import Table + +@pytest.fixture +def default(): + """Simple Table.""" + x = np.ones(65).reshape((5,13)) + return Table(x,{'F':(3,3),'v':(3,),'s':(1,)},['test data','contains only ones']) + + +class TestTable: + + def test_get_tensor(self,default): + d = default.get_array('F') + assert np.allclose(d,1.0) and d.shape[1:] == (3,3) + + def test_get_vector(self,default): + d = default.get_array('v') + assert np.allclose(d,1.0) and d.shape[1:] == (3,) + + def test_write_read_str(self,default,tmpdir): + default.to_ASCII(str(tmpdir.join('default.txt'))) + new = Table.from_ASCII(str(tmpdir.join('default.txt'))) + assert all(default.data==new.data) + + def test_write_read_file(self,default,tmpdir): + with open(tmpdir.join('default.txt'),'w') as f: + default.to_ASCII(f) + with open(tmpdir.join('default.txt')) as f: + new = Table.from_ASCII(f) + assert all(default.data==new.data) + + def test_set_array(self,default): + default.set_array('F',np.zeros((5,3,3)),'set to zero') + d=default.get_array('F') + assert np.allclose(d,0.0) and d.shape[1:] == (3,3) + + def test_get_labels(self,default): + assert default.get_labels() == ['F','v','s'] + + def test_add_array(self,default): + d = np.random.random((5,9)) + default.add_array('nine',d,'random data') + assert np.allclose(d,default.get_array('nine'))