test_h5f.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. import tempfile
  10. import shutil
  11. import os
  12. import numpy as np
  13. from h5py import File, special_dtype
  14. from .common import TestCase
  15. class TestFileID(TestCase):
  16. def test_descriptor_core(self):
  17. with File('TestFileID.test_descriptor_core', driver='core',
  18. backing_store=False, mode='x') as f:
  19. assert isinstance(f.id.get_vfd_handle(), int)
  20. def test_descriptor_sec2(self):
  21. dn_tmp = tempfile.mkdtemp('h5py.lowtest.test_h5f.TestFileID.test_descriptor_sec2')
  22. fn_h5 = os.path.join(dn_tmp, 'test.h5')
  23. try:
  24. with File(fn_h5, driver='sec2', mode='x') as f:
  25. descriptor = f.id.get_vfd_handle()
  26. self.assertNotEqual(descriptor, 0)
  27. os.fsync(descriptor)
  28. finally:
  29. shutil.rmtree(dn_tmp)
  30. class TestCacheConfig(TestCase):
  31. def test_simple_gets(self):
  32. dn_tmp = tempfile.mkdtemp('h5py.lowtest.test_h5f.TestFileID.TestCacheConfig.test_simple_gets')
  33. fn_h5 = os.path.join(dn_tmp, 'test.h5')
  34. try:
  35. with File(fn_h5, mode='x') as f:
  36. hit_rate = f._id.get_mdc_hit_rate()
  37. mdc_size = f._id.get_mdc_size()
  38. finally:
  39. shutil.rmtree(dn_tmp)
  40. def test_hitrate_reset(self):
  41. dn_tmp = tempfile.mkdtemp('h5py.lowtest.test_h5f.TestFileID.TestCacheConfig.test_hitrate_reset')
  42. fn_h5 = os.path.join(dn_tmp, 'test.h5')
  43. try:
  44. with File(fn_h5, mode='x') as f:
  45. hit_rate = f._id.get_mdc_hit_rate()
  46. f._id.reset_mdc_hit_rate_stats()
  47. hit_rate = f._id.get_mdc_hit_rate()
  48. assert hit_rate == 0
  49. finally:
  50. shutil.rmtree(dn_tmp)
  51. def test_mdc_config_get(self):
  52. dn_tmp = tempfile.mkdtemp('h5py.lowtest.test_h5f.TestFileID.TestCacheConfig.test_mdc_config_get')
  53. fn_h5 = os.path.join(dn_tmp, 'test.h5')
  54. try:
  55. with File(fn_h5, mode='x') as f:
  56. conf = f._id.get_mdc_config()
  57. f._id.set_mdc_config(conf)
  58. finally:
  59. shutil.rmtree(dn_tmp)
  60. class TestVlenData(TestCase):
  61. def test_vlen_strings(self):
  62. # Create file with dataset containing vlen arrays of vlen strings
  63. dn_tmp = tempfile.mkdtemp('h5py.lowtest.test_h5f.TestVlenStrings.test_vlen_strings')
  64. fn_h5 = os.path.join(dn_tmp, 'test.h5')
  65. try:
  66. with File(fn_h5, mode='w') as h:
  67. vlen_str = special_dtype(vlen=str)
  68. vlen_vlen_str = special_dtype(vlen=vlen_str)
  69. ds = h.create_dataset('/com', (2,), dtype=vlen_vlen_str)
  70. ds[0] = (np.array(["a", "b", "c"], dtype=vlen_vlen_str))
  71. ds[1] = (np.array(["d", "e", "f","g"], dtype=vlen_vlen_str))
  72. with File(fn_h5, "r") as h:
  73. ds = h["com"]
  74. assert ds[0].tolist() == [b'a', b'b', b'c']
  75. assert ds[1].tolist() == [b'd', b'e', b'f', b'g']
  76. finally:
  77. shutil.rmtree(dn_tmp)