Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testsuite: add test for is_valid_type. #3080

Merged
merged 8 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/python/espressomd/utils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import math
cimport numpy as np
cimport cython
import numpy as np
Expand Down Expand Up @@ -131,7 +132,7 @@ def to_char_pointer(s):

"""
if isinstance(s, unicode):
s = (< unicode > s).encode('utf8')
s = ( < unicode > s).encode('utf8')
return s


Expand All @@ -147,7 +148,7 @@ def to_str(s):
if isinstance(s, unicode):
return < unicode > s
elif isinstance(s, bytes):
return (< bytes > s).decode('ascii')
return ( < bytes > s).decode('ascii')
else:
raise ValueError('Unknown string type {}'.format(type(s)))

Expand Down Expand Up @@ -284,7 +285,10 @@ def is_valid_type(value, t):
Extended checks for numpy int and float types.

"""

if value is None:
return False
if np.isnan(value).any() or np.isinf(value).any():
return False
if t == int:
return isinstance(value, (int, np.integer, np.long))
elif t == float:
Expand Down
1 change: 1 addition & 0 deletions testsuite/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ python_test(FILE time_series.py MAX_NUM_PROC 1)
python_test(FILE linear_momentum.py)
python_test(FILE linear_momentum_lb.py MAX_NUM_PROC 2 LABELS gpu)
python_test(FILE mmm1d.py MAX_NUM_PROC 2 LABELS gpu)
python_test(FILE utils.py MAX_NUM_PROC 1)

if(PY_H5PY)
python_test(FILE h5md.py MAX_NUM_PROC 2)
Expand Down
20 changes: 20 additions & 0 deletions testsuite/python/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import numpy as np

import unittest as ut
import espressomd.utils


class UtilsTest(ut.TestCase):
jngrad marked this conversation as resolved.
Show resolved Hide resolved

def test_is_valid_type(self):
self.assertFalse(espressomd.utils.is_valid_type(np.inf, float))
self.assertFalse(espressomd.utils.is_valid_type(np.nan, float))
self.assertFalse(espressomd.utils.is_valid_type(None, float))
self.assertFalse(espressomd.utils.is_valid_type(float('nan'), float))
self.assertFalse(espressomd.utils.is_valid_type(float('inf'), float))
self.assertFalse(espressomd.utils.is_valid_type(np.inf, int))
self.assertFalse(espressomd.utils.is_valid_type(np.nan, int))
self.assertFalse(espressomd.utils.is_valid_type(None, int))

if __name__ == "__main__":
ut.main()