test_PythonUtil.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_formatTimeCompact():
  52. ftc = PythonUtil.formatTimeCompact
  53. assert ftc(0) == '0s'
  54. assert ftc(1) == '1s'
  55. assert ftc(60) == '1m0s'
  56. assert ftc(64) == '1m4s'
  57. assert ftc(60*60) == '1h0m0s'
  58. assert ftc(24*60*60) == '1d0h0m0s'
  59. assert ftc(24*60*60 + 2*60*60 + 34*60 + 12) == '1d2h34m12s'
  60. def test_formatTimeExact():
  61. fte = PythonUtil.formatTimeExact
  62. assert fte(0) == '0s'
  63. assert fte(1) == '1s'
  64. assert fte(2) == '2s'
  65. assert fte(61) == '1m1s'
  66. assert fte(60) == '1m'
  67. assert fte(60*60) == '1h'
  68. assert fte(24*60*60) == '1d'
  69. assert fte((24*60*60) + (2 * 60)) == '1d0h2m'
  70. del fte
  71. def test_AlphabetCounter():
  72. tempList = []
  73. ac = PythonUtil.AlphabetCounter()
  74. for i in range(26*3):
  75. tempList.append(ac.next())
  76. assert tempList == [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  77. 'AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ',
  78. 'BA','BB','BC','BD','BE','BF','BG','BH','BI','BJ','BK','BL','BM','BN','BO','BP','BQ','BR','BS','BT','BU','BV','BW','BX','BY','BZ',]
  79. ac = PythonUtil.AlphabetCounter()
  80. num = 26 # A-Z
  81. num += (26*26) # AA-ZZ
  82. num += 26 # AAZ
  83. num += 1 # ABA
  84. num += 2 # ABC
  85. for i in range(num):
  86. x = ac.next()
  87. assert x == 'ABC'
  88. def test_unescape_html_string():
  89. assert PythonUtil.unescapeHtmlString('asdf') == 'asdf'
  90. assert PythonUtil.unescapeHtmlString('as+df') == 'as df'
  91. assert PythonUtil.unescapeHtmlString('as%32df') == 'as2df'
  92. assert PythonUtil.unescapeHtmlString('asdf%32') == 'asdf2'
  93. def test_priority_callbacks():
  94. l = []
  95. def a(l=l):
  96. l.append('a')
  97. def b(l=l):
  98. l.append('b')
  99. def c(l=l):
  100. l.append('c')
  101. pc = PythonUtil.PriorityCallbacks()
  102. pc.add(a)
  103. pc()
  104. assert l == ['a']
  105. del l[:]
  106. bItem = pc.add(b)
  107. pc()
  108. assert 'a' in l
  109. assert 'b' in l
  110. assert len(l) == 2
  111. del l[:]
  112. pc.remove(bItem)
  113. pc()
  114. assert l == ['a']
  115. del l[:]
  116. pc.add(c, 2)
  117. bItem = pc.add(b, 10)
  118. pc()
  119. assert l == ['a', 'c', 'b']
  120. del l[:]
  121. pc.remove(bItem)
  122. pc()
  123. assert l == ['a', 'c']
  124. del l[:]
  125. pc.clear()
  126. pc()
  127. assert len(l) == 0
  128. def test_weighted_choice():
  129. # Test PythonUtil.weightedChoice() with no valid list.
  130. with pytest.raises(IndexError):
  131. PythonUtil.weightedChoice([])
  132. # Create a sample choice list.
  133. # This contains a few tuples containing only a weight
  134. # and an arbitrary item.
  135. choicelist = [(3, 'item1'), (1, 'item2'), (7, 'item3')]
  136. # These are the items that we expect.
  137. items = ['item1', 'item2', 'item3']
  138. # Test PythonUtil.weightedChoice() with our choice list.
  139. item = PythonUtil.weightedChoice(choicelist)
  140. # Assert that what we got was at least an available item.
  141. assert item in items
  142. # Create yet another sample choice list, but with a couple more items.
  143. choicelist = [(2, 'item1'), (25, 'item2'), (14, 'item3'), (5, 'item4'),
  144. (7, 'item5'), (3, 'item6'), (6, 'item7'), (50, 'item8')]
  145. # Set the items that we expect again.
  146. items = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8']
  147. # The sum of all of the weights is 112.
  148. weightsum = 2 + 25 + 14 + 5 + 7 + 3 + 6 + 50
  149. # Test PythonUtil.weightedChoice() with the sum.
  150. item = PythonUtil.weightedChoice(choicelist, sum=weightsum)
  151. # Assert that we got a valid item (most of the time this should be 'item8').
  152. assert item in items
  153. # Test PythonUtil.weightedChoice(), but with an invalid sum.
  154. item = PythonUtil.weightedChoice(choicelist, sum=1)
  155. # Assert that we got 'item1'.
  156. assert item == items[0]
  157. # Create a bogus random function.
  158. rnd = lambda: 0.5
  159. # Test PythonUtil.weightedChoice() with the bogus function.
  160. item = PythonUtil.weightedChoice(choicelist, rng=rnd, sum=weightsum)
  161. # Assert that we got 'item6'.
  162. # We expect 'item6' because 0.5 multiplied by 112 is 56.0.
  163. # When subtracting that number by each weight, it will reach 0
  164. # by the time it hits 'item6' in the iteration.
  165. assert item == items[5]
  166. def test_serial():
  167. gen = PythonUtil.SerialNumGen()
  168. assert gen.next() == 0
  169. assert next(gen) == 1
  170. assert next(gen) == 2
  171. assert gen.next() == 3
  172. def test_alphabet_counter():
  173. counter = PythonUtil.AlphabetCounter()
  174. assert next(counter) == 'A'
  175. assert counter.next() == 'B'
  176. assert counter.next() == 'C'
  177. assert next(counter) == 'D'
  178. for i in range(26 - 4):
  179. next(counter)
  180. assert next(counter) == 'AA'
  181. assert next(counter) == 'AB'