test_PythonUtil.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. from direct.showbase import PythonUtil
  2. import pytest
  3. def test_queue():
  4. q = PythonUtil.Queue()
  5. assert q.isEmpty()
  6. q.clear()
  7. assert q.isEmpty()
  8. q.push(10)
  9. assert not q.isEmpty()
  10. q.push(20)
  11. assert not q.isEmpty()
  12. assert len(q) == 2
  13. assert q.front() == 10
  14. assert q.back() == 20
  15. assert q.top() == 10
  16. assert q.top() == 10
  17. assert q.pop() == 10
  18. assert len(q) == 1
  19. assert not q.isEmpty()
  20. assert q.pop() == 20
  21. assert len(q) == 0
  22. assert q.isEmpty()
  23. def test_flywheel():
  24. f = PythonUtil.flywheel(['a','b','c','d'], countList=[11,20,3,4])
  25. obj2count = {}
  26. for obj in f:
  27. obj2count.setdefault(obj, 0)
  28. obj2count[obj] += 1
  29. assert obj2count['a'] == 11
  30. assert obj2count['b'] == 20
  31. assert obj2count['c'] == 3
  32. assert obj2count['d'] == 4
  33. f = PythonUtil.flywheel([1,2,3,4], countFunc=lambda x: x*2)
  34. obj2count = {}
  35. for obj in f:
  36. obj2count.setdefault(obj, 0)
  37. obj2count[obj] += 1
  38. assert obj2count[1] == 2
  39. assert obj2count[2] == 4
  40. assert obj2count[3] == 6
  41. assert obj2count[4] == 8
  42. f = PythonUtil.flywheel([1,2,3,4], countFunc=lambda x: x, scale = 3)
  43. obj2count = {}
  44. for obj in f:
  45. obj2count.setdefault(obj, 0)
  46. obj2count[obj] += 1
  47. assert obj2count[1] == 1 * 3
  48. assert obj2count[2] == 2 * 3
  49. assert obj2count[3] == 3 * 3
  50. assert obj2count[4] == 4 * 3
  51. def test_unescape_html_string():
  52. assert PythonUtil.unescapeHtmlString('asdf') == 'asdf'
  53. assert PythonUtil.unescapeHtmlString('as+df') == 'as df'
  54. assert PythonUtil.unescapeHtmlString('as%32df') == 'as2df'
  55. assert PythonUtil.unescapeHtmlString('asdf%32') == 'asdf2'
  56. def test_priority_callbacks():
  57. l = []
  58. def a(l=l):
  59. l.append('a')
  60. def b(l=l):
  61. l.append('b')
  62. def c(l=l):
  63. l.append('c')
  64. pc = PythonUtil.PriorityCallbacks()
  65. pc.add(a)
  66. pc()
  67. assert l == ['a']
  68. del l[:]
  69. bItem = pc.add(b)
  70. pc()
  71. assert 'a' in l
  72. assert 'b' in l
  73. assert len(l) == 2
  74. del l[:]
  75. pc.remove(bItem)
  76. pc()
  77. assert l == ['a']
  78. del l[:]
  79. pc.add(c, 2)
  80. bItem = pc.add(b, 10)
  81. pc()
  82. assert l == ['a', 'c', 'b']
  83. del l[:]
  84. pc.remove(bItem)
  85. pc()
  86. assert l == ['a', 'c']
  87. del l[:]
  88. pc.clear()
  89. pc()
  90. assert len(l) == 0
  91. def test_weighted_choice():
  92. # Test PythonUtil.weightedChoice() with no valid list.
  93. with pytest.raises(IndexError):
  94. PythonUtil.weightedChoice([])
  95. # Create a sample choice list.
  96. # This contains a few tuples containing only a weight
  97. # and an arbitrary item.
  98. choicelist = [(3, 'item1'), (1, 'item2'), (7, 'item3')]
  99. # These are the items that we expect.
  100. items = ['item1', 'item2', 'item3']
  101. # Test PythonUtil.weightedChoice() with our choice list.
  102. item = PythonUtil.weightedChoice(choicelist)
  103. # Assert that what we got was at least an available item.
  104. assert item in items
  105. # Create yet another sample choice list, but with a couple more items.
  106. choicelist = [(2, 'item1'), (25, 'item2'), (14, 'item3'), (5, 'item4'),
  107. (7, 'item5'), (3, 'item6'), (6, 'item7'), (50, 'item8')]
  108. # Set the items that we expect again.
  109. items = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8']
  110. # The sum of all of the weights is 112.
  111. weightsum = 2 + 25 + 14 + 5 + 7 + 3 + 6 + 50
  112. # Test PythonUtil.weightedChoice() with the sum.
  113. item = PythonUtil.weightedChoice(choicelist, sum=weightsum)
  114. # Assert that we got a valid item (most of the time this should be 'item8').
  115. assert item in items
  116. # Test PythonUtil.weightedChoice(), but with an invalid sum.
  117. item = PythonUtil.weightedChoice(choicelist, sum=1)
  118. # Assert that we got 'item1'.
  119. assert item == items[0]
  120. # Create a bogus random function.
  121. rnd = lambda: 0.5
  122. # Test PythonUtil.weightedChoice() with the bogus function.
  123. item = PythonUtil.weightedChoice(choicelist, rng=rnd, sum=weightsum)
  124. # Assert that we got 'item6'.
  125. # We expect 'item6' because 0.5 multiplied by 112 is 56.0.
  126. # When subtracting that number by each weight, it will reach 0
  127. # by the time it hits 'item6' in the iteration.
  128. assert item == items[5]
  129. def test_serial():
  130. gen = PythonUtil.SerialNumGen()
  131. assert gen.next() == 0
  132. assert next(gen) == 1
  133. assert next(gen) == 2
  134. assert gen.next() == 3
  135. def test_alphabet_counter():
  136. counter = PythonUtil.AlphabetCounter()
  137. assert next(counter) == 'A'
  138. assert counter.next() == 'B'
  139. assert counter.next() == 'C'
  140. assert next(counter) == 'D'
  141. for i in range(26 - 4):
  142. next(counter)
  143. assert next(counter) == 'AA'
  144. assert next(counter) == 'AB'