File: HDF5UtilsTest.py

package info (click to toggle)
pymca 5.8.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 44,392 kB
  • sloc: python: 155,456; ansic: 15,843; makefile: 116; sh: 73; xml: 55
file content (62 lines) | stat: -rw-r--r-- 1,542 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
import shutil
import tempfile
import unittest
import h5py

from PyMca5.PyMcaIO import HDF5Utils


def _cause_segfault(*args, **kwargs):
    import ctypes

    i = ctypes.c_char(b"a")
    j = ctypes.pointer(i)
    c = 0
    while True:
        j[c] = b"a"
        c += 1


def _safe_cause_segfault(*args, **kwargs):
    return HDF5Utils.run_in_subprocess(_cause_segfault, *args, **kwargs)


class testHDF5Utils(unittest.TestCase):
    def setUp(self):
        self.path = tempfile.mkdtemp(prefix="pymca")

    def tearDown(self):
        shutil.rmtree(self.path)

    def testHdf5GroupKeys(self):
        filename = os.path.join(self.path, "test.h5")
        with h5py.File(filename, "w", track_order=True) as f:
            for i in range(5):
                f[str(i)] = i

        names = list(map(str, range(5)))
        self.assertEqual(HDF5Utils.get_hdf5_group_keys(filename), names)
        self.assertEqual(HDF5Utils.safe_hdf5_group_keys(filename), names)

    def testSegFault(self):
        self.assertEqual(_safe_cause_segfault(default=123), 123)


def getSuite(auto=True):
    testSuite = unittest.TestSuite()
    if auto:
        testSuite.addTest(unittest.TestLoader().loadTestsFromTestCase(testHDF5Utils))
    else:
        # use a predefined order
        testSuite.addTest(testHDF5Utils("testHdf5GroupKeys"))
        testSuite.addTest(testHDF5Utils("testSegFault"))
    return testSuite


def test(auto=False):
    unittest.TextTestRunner(verbosity=2).run(getSuite(auto=auto))


if __name__ == "__main__":
    test()