Skip to content

Commit 20be2e5

Browse files
committed
Modified the way group tests compare output
Instead of comparing text output files the tests are now done on h5-files. The latter are compared with a finite precision eliminating thus a problem of platform-dependence of the results.
1 parent e24933d commit 20be2e5

11 files changed

+69
-79
lines changed

test/plovasp/proj_group/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.pyc
22
*.test
3+
*.test.h5

test/plovasp/proj_group/mytest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import unittest
66
import numpy as np
77
import difflib
8+
from pytriqs.utility.h5diff import h5diff
89

910
class MyTestCase(unittest.TestCase):
1011
"""
@@ -54,4 +55,16 @@ def assertFileEqual(self, file1, file2):
5455
if diff:
5556
return self.fail("Files '%s' and '%s' differ"%(file1, file2))
5657

58+
def assertH5FileEqual(self, file1, file2):
59+
"""
60+
Compares two files using difflib.
61+
Empty lines are ignored.
62+
Files are assumed to be relatively small;
63+
the data is truncated for files larger than MAX_SIZE bytes.
64+
"""
65+
try:
66+
h5diff(file1, file2, precision=1e-6)
67+
except RuntimeError as err:
68+
if "FAILED" in err:
69+
return self.fail("Files '%s' and '%s' differ"%(file1, file2))
5770

test/plovasp/proj_group/projortho.out

Lines changed: 0 additions & 10 deletions
This file was deleted.
2.48 KB
Binary file not shown.

test/plovasp/proj_group/projortho_2site.out

Lines changed: 0 additions & 22 deletions
This file was deleted.
2.88 KB
Binary file not shown.

test/plovasp/proj_group/projortho_normion.out

Lines changed: 0 additions & 22 deletions
This file was deleted.
2.88 KB
Binary file not shown.

test/plovasp/proj_group/test_all.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
if __name__ == '__main__':
77
# suite = unittest.TestLoader().discover('./')
8-
suite = unittest.TestLoader().discover('./', pattern='test_two*')
8+
suite = unittest.TestLoader().discover('./', pattern='test_one*')
99
unittest.TextTestRunner(verbosity=2, buffer=True).run(suite)
1010
# unittest.TextTestRunner(verbosity=2, buffer=False).run(suite)
1111

test/plovasp/proj_group/test_one_site.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pytriqs.applications.dft.converters.plovasp.inpconf import ConfigParameters
1010
from pytriqs.applications.dft.converters.plovasp.proj_shell import ProjectorShell
1111
from pytriqs.applications.dft.converters.plovasp.proj_group import ProjectorGroup
12+
from pytriqs.archive import HDFArchive
1213
import mytest
1314

1415
################################################################################
@@ -47,15 +48,22 @@ def test_ortho(self):
4748

4849
dens_mat, overl = self.proj_sh.density_matrix(self.el_struct)
4950

50-
testout = _rpath + 'projortho.out.test'
51-
with open(testout, 'wt') as f:
52-
f.write("density matrix: %s\n"%(dens_mat))
53-
f.write("overlap matrix: %s\n"%(overl))
51+
# testout = _rpath + 'projortho.out.test'
52+
# with open(testout, 'wt') as f:
53+
# f.write("density matrix: %s\n"%(dens_mat))
54+
# f.write("overlap matrix: %s\n"%(overl))
55+
testout = _rpath + 'projortho.test.h5'
56+
with HDFArchive(testout, 'w') as h5test:
57+
h5test['density_matrix'] = dens_mat
58+
h5test['overlap_matrix'] = overl
5459

60+
# FIXME: seems redundant, as 'overl' is written to the file anyway
5561
self.assertEqual(overl, np.eye(5))
5662

57-
expected_file = _rpath + 'projortho.out'
58-
self.assertFileEqual(testout, expected_file)
63+
# expected_file = _rpath + 'projortho.out'
64+
expected_file = _rpath + 'projortho.out.h5'
65+
# self.assertFileEqual(testout, expected_file)
66+
self.assertH5FileEqual(testout, expected_file)
5967

6068
# Scenario 2
6169
def test_ortho_normion(self):
@@ -64,14 +72,21 @@ def test_ortho_normion(self):
6472

6573
dens_mat, overl = self.proj_sh.density_matrix(self.el_struct)
6674

67-
testout = _rpath + 'projortho.out.test'
68-
with open(testout, 'wt') as f:
69-
f.write("density matrix: %s\n"%(dens_mat))
70-
f.write("overlap matrix: %s\n"%(overl))
75+
# testout = _rpath + 'projortho.out.test'
76+
# with open(testout, 'wt') as f:
77+
# f.write("density matrix: %s\n"%(dens_mat))
78+
# f.write("overlap matrix: %s\n"%(overl))
79+
testout = _rpath + 'projortho.test.h5'
80+
with HDFArchive(testout, 'w') as h5test:
81+
h5test['density_matrix'] = dens_mat
82+
h5test['overlap_matrix'] = overl
7183

84+
# FIXME: seems redundant, as 'overl' is written to the file anyway
7285
self.assertEqual(overl, np.eye(5))
7386

74-
expected_file = _rpath + 'projortho.out'
75-
self.assertFileEqual(testout, expected_file)
87+
# expected_file = _rpath + 'projortho.out'
88+
# self.assertFileEqual(testout, expected_file)
89+
expected_file = _rpath + 'projortho.out.h5'
90+
self.assertH5FileEqual(testout, expected_file)
7691

7792

0 commit comments

Comments
 (0)