1# This file is part of h5py, a Python interface to the HDF5 library.
2#
3# http://www.h5py.org
4#
5# Copyright 2008-2013 Andrew Collette and contributors
6#
7# License:  Standard 3-clause BSD; see "license.txt" for full license terms
8#           and contributor agreement.
9
10import tempfile
11import shutil
12import os
13import numpy as np
14from h5py import File, special_dtype
15
16from .common import TestCase
17
18
19class TestFileID(TestCase):
20    def test_descriptor_core(self):
21        with File('TestFileID.test_descriptor_core', driver='core',
22                  backing_store=False, mode='x') as f:
23            assert isinstance(f.id.get_vfd_handle(), int)
24
25    def test_descriptor_sec2(self):
26        dn_tmp = tempfile.mkdtemp('h5py.lowtest.test_h5f.TestFileID.test_descriptor_sec2')
27        fn_h5 = os.path.join(dn_tmp, 'test.h5')
28        try:
29            with File(fn_h5, driver='sec2', mode='x') as f:
30                descriptor = f.id.get_vfd_handle()
31                self.assertNotEqual(descriptor, 0)
32                os.fsync(descriptor)
33        finally:
34            shutil.rmtree(dn_tmp)
35
36
37class TestCacheConfig(TestCase):
38    def test_simple_gets(self):
39        dn_tmp = tempfile.mkdtemp('h5py.lowtest.test_h5f.TestFileID.TestCacheConfig.test_simple_gets')
40        fn_h5 = os.path.join(dn_tmp, 'test.h5')
41        try:
42            with File(fn_h5, mode='x') as f:
43                hit_rate = f._id.get_mdc_hit_rate()
44                mdc_size = f._id.get_mdc_size()
45
46        finally:
47            shutil.rmtree(dn_tmp)
48
49    def test_hitrate_reset(self):
50        dn_tmp = tempfile.mkdtemp('h5py.lowtest.test_h5f.TestFileID.TestCacheConfig.test_hitrate_reset')
51        fn_h5 = os.path.join(dn_tmp, 'test.h5')
52        try:
53            with File(fn_h5, mode='x') as f:
54                hit_rate = f._id.get_mdc_hit_rate()
55                f._id.reset_mdc_hit_rate_stats()
56                hit_rate = f._id.get_mdc_hit_rate()
57                assert hit_rate == 0
58
59        finally:
60            shutil.rmtree(dn_tmp)
61
62    def test_mdc_config_get(self):
63        dn_tmp = tempfile.mkdtemp('h5py.lowtest.test_h5f.TestFileID.TestCacheConfig.test_mdc_config_get')
64        fn_h5 = os.path.join(dn_tmp, 'test.h5')
65        try:
66            with File(fn_h5, mode='x') as f:
67                conf = f._id.get_mdc_config()
68                f._id.set_mdc_config(conf)
69        finally:
70            shutil.rmtree(dn_tmp)
71
72
73class TestVlenData(TestCase):
74    def test_vlen_strings(self):
75        # Create file with dataset containing vlen arrays of vlen strings
76        dn_tmp = tempfile.mkdtemp('h5py.lowtest.test_h5f.TestVlenStrings.test_vlen_strings')
77        fn_h5 = os.path.join(dn_tmp, 'test.h5')
78        try:
79            with File(fn_h5, mode='w') as h:
80                vlen_str = special_dtype(vlen=str)
81                vlen_vlen_str = special_dtype(vlen=vlen_str)
82
83                ds = h.create_dataset('/com', (2,), dtype=vlen_vlen_str)
84                ds[0] = (np.array(["a", "b", "c"], dtype=vlen_vlen_str))
85                ds[1] = (np.array(["d", "e", "f","g"], dtype=vlen_vlen_str))
86
87            with File(fn_h5, "r") as h:
88                ds = h["com"]
89                assert ds[0].tolist() == [b'a', b'b', b'c']
90                assert ds[1].tolist() == [b'd', b'e', b'f', b'g']
91
92        finally:
93            shutil.rmtree(dn_tmp)
94