test_uniqueidallocator.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. from panda3d.core import UniqueIdAllocator
  2. # Value from panda/src/putil/uniqueIdAllocator.cxx, not published
  3. IndexEnd = 0xFFFFFFFF
  4. def test_inclusive_allocation():
  5. allocator = UniqueIdAllocator(0, 0)
  6. assert allocator.allocate() == 0
  7. assert allocator.allocate() == IndexEnd
  8. def test_normal_allocation():
  9. allocator = UniqueIdAllocator(0, 10)
  10. for i in range(10 + 1):
  11. assert allocator.allocate() == i
  12. assert allocator.allocate() == IndexEnd
  13. def test_min_value_allocation():
  14. allocator = UniqueIdAllocator(1, 5)
  15. for i in range(1, 5 + 1):
  16. assert allocator.allocate() == i
  17. assert allocator.allocate() == IndexEnd
  18. def test_regular_is_allocated():
  19. allocator = UniqueIdAllocator(1, 5)
  20. for i in range(1, 5 + 1):
  21. assert not allocator.is_allocated(i)
  22. for i in range(1, 5 + 1):
  23. assert allocator.is_allocated(allocator.allocate())
  24. def test_bounded_is_allocated():
  25. allocator = UniqueIdAllocator(1, 5)
  26. for i in range(1, 5 + 1):
  27. assert allocator.is_allocated(allocator.allocate())
  28. assert not allocator.is_allocated(0) # Out of range, left side
  29. assert not allocator.is_allocated(10) # Out of range, right side
  30. def test_initial_reserve_id():
  31. allocator = UniqueIdAllocator(1, 3)
  32. assert not allocator.is_allocated(2)
  33. allocator.initial_reserve_id(2)
  34. assert allocator.is_allocated(2)
  35. assert allocator.allocate() == 1
  36. assert allocator.allocate() == 3
  37. assert allocator.allocate() == IndexEnd
  38. def test_initial_reserve_id_exhaustion():
  39. allocator = UniqueIdAllocator(1, 3)
  40. for i in range(1, 3 + 1):
  41. allocator.initial_reserve_id(i)
  42. assert allocator.allocate() == IndexEnd
  43. def test_free():
  44. allocator = UniqueIdAllocator(0, 0)
  45. assert allocator.allocate() == 0
  46. assert allocator.is_allocated(0)
  47. assert allocator.free(0)
  48. assert not allocator.is_allocated(0)
  49. def test_free_reallocation():
  50. allocator = UniqueIdAllocator(1, 5)
  51. for i in range(1, 5 + 1):
  52. assert allocator.allocate() == i
  53. assert allocator.is_allocated(i)
  54. for i in range(1, 5 + 1):
  55. assert allocator.free(i)
  56. for i in range(1, 5 + 1):
  57. assert not allocator.is_allocated(i)
  58. assert allocator.allocate() == i
  59. assert allocator.allocate() == IndexEnd
  60. def test_free_unallocated():
  61. allocator = UniqueIdAllocator(0, 2)
  62. assert allocator.allocate() == 0
  63. assert allocator.free(0)
  64. for i in range(0, 2 + 1):
  65. assert not allocator.free(i)
  66. def test_free_bounds():
  67. allocator = UniqueIdAllocator(1, 3)
  68. assert not allocator.free(0) # Out of range, left side
  69. assert not allocator.free(4) # Out of range, right side
  70. def test_free_reallocation_mid():
  71. allocator = UniqueIdAllocator(1, 5)
  72. for i in range(1, 5 + 1):
  73. assert allocator.allocate() == i
  74. assert allocator.is_allocated(i)
  75. assert allocator.free(2)
  76. assert allocator.free(3)
  77. assert allocator.allocate() == 2
  78. assert allocator.allocate() == 3
  79. assert allocator.allocate() == IndexEnd
  80. def test_free_initial_reserve_id():
  81. allocator = UniqueIdAllocator(1, 3)
  82. allocator.initial_reserve_id(1)
  83. assert allocator.free(1)
  84. assert allocator.allocate() == 2
  85. assert allocator.allocate() == 3
  86. assert allocator.allocate() == 1
  87. assert allocator.allocate() == IndexEnd
  88. def test_fraction_used():
  89. allocator = UniqueIdAllocator(1, 4)
  90. assert allocator.fraction_used() == 0
  91. for fraction in (0.25, 0.5, 0.75, 1):
  92. allocator.allocate()
  93. assert allocator.fraction_used() == fraction
  94. assert allocator.allocate() == IndexEnd