You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.4KB

  1. from .lib import TestBase, FileCreator
  2. from smmap.util import (
  3. MapWindow,
  4. MapRegion,
  5. MapRegionList,
  6. ALLOCATIONGRANULARITY,
  7. is_64_bit,
  8. align_to_mmap
  9. )
  10. import os
  11. import sys
  12. class TestMMan(TestBase):
  13. def test_window(self):
  14. wl = MapWindow(0, 1) # left
  15. wc = MapWindow(1, 1) # center
  16. wc2 = MapWindow(10, 5) # another center
  17. wr = MapWindow(8000, 50) # right
  18. assert wl.ofs_end() == 1
  19. assert wc.ofs_end() == 2
  20. assert wr.ofs_end() == 8050
  21. # extension does nothing if already in place
  22. maxsize = 100
  23. wc.extend_left_to(wl, maxsize)
  24. assert wc.ofs == 1 and wc.size == 1
  25. wl.extend_right_to(wc, maxsize)
  26. wl.extend_right_to(wc, maxsize)
  27. assert wl.ofs == 0 and wl.size == 1
  28. # an actual left extension
  29. pofs_end = wc2.ofs_end()
  30. wc2.extend_left_to(wc, maxsize)
  31. assert wc2.ofs == wc.ofs_end() and pofs_end == wc2.ofs_end()
  32. # respects maxsize
  33. wc.extend_right_to(wr, maxsize)
  34. assert wc.ofs == 1 and wc.size == maxsize
  35. wc.extend_right_to(wr, maxsize)
  36. assert wc.ofs == 1 and wc.size == maxsize
  37. # without maxsize
  38. wc.extend_right_to(wr, sys.maxsize)
  39. assert wc.ofs_end() == wr.ofs and wc.ofs == 1
  40. # extend left
  41. wr.extend_left_to(wc2, maxsize)
  42. wr.extend_left_to(wc2, maxsize)
  43. assert wr.size == maxsize
  44. wr.extend_left_to(wc2, sys.maxsize)
  45. assert wr.ofs == wc2.ofs_end()
  46. wc.align()
  47. assert wc.ofs == 0 and wc.size == align_to_mmap(wc.size, True)
  48. def test_region(self):
  49. with FileCreator(self.k_window_test_size, "window_test") as fc:
  50. half_size = fc.size // 2
  51. rofs = align_to_mmap(4200, False)
  52. rfull = MapRegion(fc.path, 0, fc.size)
  53. rhalfofs = MapRegion(fc.path, rofs, fc.size)
  54. rhalfsize = MapRegion(fc.path, 0, half_size)
  55. # offsets
  56. assert rfull.ofs_begin() == 0 and rfull.size() == fc.size
  57. assert rfull.ofs_end() == fc.size # if this method works, it works always
  58. assert rhalfofs.ofs_begin() == rofs and rhalfofs.size() == fc.size - rofs
  59. assert rhalfsize.ofs_begin() == 0 and rhalfsize.size() == half_size
  60. assert rfull.includes_ofs(0) and rfull.includes_ofs(fc.size - 1) and rfull.includes_ofs(half_size)
  61. assert not rfull.includes_ofs(-1) and not rfull.includes_ofs(sys.maxsize)
  62. # auto-refcount
  63. assert rfull.client_count() == 1
  64. rfull2 = rfull
  65. assert rfull.client_count() == 1, "no auto-counting"
  66. # window constructor
  67. w = MapWindow.from_region(rfull)
  68. assert w.ofs == rfull.ofs_begin() and w.ofs_end() == rfull.ofs_end()
  69. def test_region_list(self):
  70. with FileCreator(100, "sample_file") as fc:
  71. fd = os.open(fc.path, os.O_RDONLY)
  72. try:
  73. for item in (fc.path, fd):
  74. ml = MapRegionList(item)
  75. assert len(ml) == 0
  76. assert ml.path_or_fd() == item
  77. assert ml.file_size() == fc.size
  78. finally:
  79. os.close(fd)
  80. def test_util(self):
  81. assert isinstance(is_64_bit(), bool) # just call it
  82. assert align_to_mmap(1, False) == 0
  83. assert align_to_mmap(1, True) == ALLOCATIONGRANULARITY