From 4145e55f77031758a3e5f42014fe009c9b391d36 Mon Sep 17 00:00:00 2001 From: "Oleg E. Peil" Date: Thu, 10 Mar 2016 18:43:49 +0100 Subject: [PATCH] Added a simple Python test of ATM library --- test/plovasp/CMakeLists.txt | 3 +- test/plovasp/atm/mytest.py | 57 ++++++++++++++++++++++++++++++++++++ test/plovasp/atm/test_atm.py | 38 ++++++++++++++++++++++++ test/plovasp/run_suite.py | 2 +- 4 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 test/plovasp/atm/mytest.py create mode 100644 test/plovasp/atm/test_atm.py diff --git a/test/plovasp/CMakeLists.txt b/test/plovasp/CMakeLists.txt index 802bd759..a8252c64 100644 --- a/test/plovasp/CMakeLists.txt +++ b/test/plovasp/CMakeLists.txt @@ -5,7 +5,8 @@ set(TestSuites plotools proj_group proj_shell - vaspio) + vaspio + atm) FILE(COPY ${TestSuites} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) FILE(COPY run_suite.py DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) diff --git a/test/plovasp/atm/mytest.py b/test/plovasp/atm/mytest.py new file mode 100644 index 00000000..fb0c64e8 --- /dev/null +++ b/test/plovasp/atm/mytest.py @@ -0,0 +1,57 @@ +r""" +Module defining a custom TestCase with extra functionality. +""" + +import unittest +import numpy as np +import difflib + +class MyTestCase(unittest.TestCase): + """ + Custom TestCase class supporting additional equality checks: + - numpy array equality + - file equality + """ + def __init__(self, *args, **kwargs): + """ + Initializes a custom equality function for comparing numpy arrays. + """ + super(MyTestCase, self).__init__(*args, **kwargs) + np.set_printoptions(suppress=True) + self.addTypeEqualityFunc(np.ndarray, self.is_arrays_equal) + + def is_arrays_equal(self, arr1, arr2, msg=None): + """ + Raises self.failureException is arrays arr1 and arr2 + are not equal. + """ + if not np.allclose(arr1, arr2): + raise self.failureException(msg) + + def assertFileEqual(self, file1, file2): + """ + Compares two files using difflib. + Empty lines are ignored. + Files are assumed to be relatively small; + the data is truncated for files larger than MAX_SIZE bytes. + """ + MAX_SIZE = 100000 + with open(file1, 'r') as f1: + str1 = f1.read(MAX_SIZE) + with open(file2, 'r') as f2: + str2 = f2.read(MAX_SIZE) +# +# Make a diff +# +# Remove empty lines + lstr1 = filter(lambda s: s.strip() != '', str1.splitlines(True)) + lstr2 = filter(lambda s: s.strip() != '', str2.splitlines(True)) +# diff + delta = difflib.unified_diff(lstr1, lstr2) +# combine delta's to a string + diff = ''.join(delta) +# if 'diff' is non-empty, files are different + if diff: + return self.fail("Files '%s' and '%s' differ"%(file1, file2)) + + diff --git a/test/plovasp/atm/test_atm.py b/test/plovasp/atm/test_atm.py new file mode 100644 index 00000000..f0fc9595 --- /dev/null +++ b/test/plovasp/atm/test_atm.py @@ -0,0 +1,38 @@ + +import os + +import numpy as np +import applications.dft.converters.plovasp.atm as atm +import mytest + +################################################################################ +# +# TestProjectorShell +# +################################################################################ +class TestProjectorShell(mytest.MyTestCase): + """ + Class: + + ProjectorShell(sh_pars, proj_raw) + + Scenarios: + - **if** a correct input is given **compare** output arrays + """ +# Scenario 1 + def test_example(self): + eigs = np.array([-1.5, -1.309017, -1.0, -0.5]) + en = -0.55 + itt = np.array([[1, 0, 1, 2, 3]]).T + + res = atm.dos_tetra_weights_3d(eigs, en, itt)[:, 0] + + r_should = np.zeros(4) + r_should[0] = 0.000309016992226; + r_should[1] = 0.000381966005939; + r_should[2] = 0.000618033984453; + r_should[3] = 0.017232002550965; + + self.assertEqual(res, r_should) + + diff --git a/test/plovasp/run_suite.py b/test/plovasp/run_suite.py index 26b4e100..ec9a97b6 100644 --- a/test/plovasp/run_suite.py +++ b/test/plovasp/run_suite.py @@ -29,6 +29,6 @@ if __name__ == '__main__': else: print "Failed tests:" for failure in results.failures: - print failurep[0].__str__() + print failure[0].__str__() raise SystemExit(1)