2019-12-05 10:27:47 +05:30
|
|
|
import os
|
|
|
|
|
2019-12-03 21:09:54 +05:30
|
|
|
import pytest
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
from damask import Table
|
|
|
|
|
2019-12-05 10:27:47 +05:30
|
|
|
|
2019-12-03 21:09:54 +05:30
|
|
|
@pytest.fixture
|
|
|
|
def default():
|
|
|
|
"""Simple Table."""
|
2019-12-03 21:33:03 +05:30
|
|
|
x = np.ones((5,13))
|
2019-12-03 21:09:54 +05:30
|
|
|
return Table(x,{'F':(3,3),'v':(3,),'s':(1,)},['test data','contains only ones'])
|
|
|
|
|
2019-12-05 10:27:47 +05:30
|
|
|
@pytest.fixture
|
|
|
|
def reference_dir(reference_dir_base):
|
|
|
|
"""Directory containing reference results."""
|
|
|
|
return os.path.join(reference_dir_base,'Table')
|
2019-12-03 21:09:54 +05:30
|
|
|
|
|
|
|
class TestTable:
|
|
|
|
|
|
|
|
def test_get_tensor(self,default):
|
2019-12-05 10:40:27 +05:30
|
|
|
d = default.get('F')
|
2019-12-03 21:09:54 +05:30
|
|
|
assert np.allclose(d,1.0) and d.shape[1:] == (3,3)
|
|
|
|
|
|
|
|
def test_get_vector(self,default):
|
2019-12-05 10:40:27 +05:30
|
|
|
d = default.get('v')
|
2019-12-03 21:09:54 +05:30
|
|
|
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)
|
2019-12-05 10:27:47 +05:30
|
|
|
|
|
|
|
@pytest.mark.parametrize('fname',['datatype-mix.txt','whitespace-mix.txt'])
|
|
|
|
def test_read_strange(self,reference_dir,fname):
|
|
|
|
with open(os.path.join(reference_dir,fname)) as f:
|
|
|
|
new = Table.from_ASCII(f)
|
2019-12-03 21:09:54 +05:30
|
|
|
|
2019-12-05 10:40:27 +05:30
|
|
|
def test_set(self,default):
|
|
|
|
default.set('F',np.zeros((5,3,3)),'set to zero')
|
|
|
|
d=default.get('F')
|
2019-12-03 21:09:54 +05:30
|
|
|
assert np.allclose(d,0.0) and d.shape[1:] == (3,3)
|
|
|
|
|
2019-12-05 10:30:49 +05:30
|
|
|
def test_labels(self,default):
|
|
|
|
assert default.labels() == ['F','v','s']
|
2019-12-03 21:09:54 +05:30
|
|
|
|
2019-12-05 10:40:27 +05:30
|
|
|
def test_add(self,default):
|
2019-12-03 21:09:54 +05:30
|
|
|
d = np.random.random((5,9))
|
2019-12-05 10:40:27 +05:30
|
|
|
default.add('nine',d,'random data')
|
|
|
|
assert np.allclose(d,default.get('nine'))
|
2019-12-03 21:33:03 +05:30
|
|
|
|
|
|
|
|
|
|
|
def test_invalid_initialization(self,default):
|
2019-12-05 10:40:27 +05:30
|
|
|
x = default.get('v')
|
2019-12-03 21:33:03 +05:30
|
|
|
with pytest.raises(IndexError):
|
|
|
|
Table(x,{'F':(3,3)})
|
|
|
|
|
|
|
|
def test_invalid_set(self,default):
|
2019-12-05 10:40:27 +05:30
|
|
|
x = default.get('v')
|
2019-12-03 21:33:03 +05:30
|
|
|
with pytest.raises(ValueError):
|
2019-12-05 10:40:27 +05:30
|
|
|
default.set('F',x,'does not work')
|
2019-12-04 14:50:03 +05:30
|
|
|
|
2019-12-05 10:40:27 +05:30
|
|
|
def test_invalid_get(self,default):
|
2019-12-04 14:50:03 +05:30
|
|
|
with pytest.raises(KeyError):
|
2019-12-05 10:40:27 +05:30
|
|
|
default.get('n')
|