test_PythonUtil.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from direct.showbase import PythonUtil
  2. def test_queue():
  3. q = PythonUtil.Queue()
  4. assert q.isEmpty()
  5. q.clear()
  6. assert q.isEmpty()
  7. q.push(10)
  8. assert not q.isEmpty()
  9. q.push(20)
  10. assert not q.isEmpty()
  11. assert len(q) == 2
  12. assert q.front() == 10
  13. assert q.back() == 20
  14. assert q.top() == 10
  15. assert q.top() == 10
  16. assert q.pop() == 10
  17. assert len(q) == 1
  18. assert not q.isEmpty()
  19. assert q.pop() == 20
  20. assert len(q) == 0
  21. assert q.isEmpty()
  22. def test_flywheel():
  23. f = PythonUtil.flywheel(['a','b','c','d'], countList=[11,20,3,4])
  24. obj2count = {}
  25. for obj in f:
  26. obj2count.setdefault(obj, 0)
  27. obj2count[obj] += 1
  28. assert obj2count['a'] == 11
  29. assert obj2count['b'] == 20
  30. assert obj2count['c'] == 3
  31. assert obj2count['d'] == 4
  32. f = PythonUtil.flywheel([1,2,3,4], countFunc=lambda x: x*2)
  33. obj2count = {}
  34. for obj in f:
  35. obj2count.setdefault(obj, 0)
  36. obj2count[obj] += 1
  37. assert obj2count[1] == 2
  38. assert obj2count[2] == 4
  39. assert obj2count[3] == 6
  40. assert obj2count[4] == 8
  41. f = PythonUtil.flywheel([1,2,3,4], countFunc=lambda x: x, scale = 3)
  42. obj2count = {}
  43. for obj in f:
  44. obj2count.setdefault(obj, 0)
  45. obj2count[obj] += 1
  46. assert obj2count[1] == 1 * 3
  47. assert obj2count[2] == 2 * 3
  48. assert obj2count[3] == 3 * 3
  49. assert obj2count[4] == 4 * 3
  50. def test_unescape_html_string():
  51. assert PythonUtil.unescapeHtmlString('asdf') == 'asdf'
  52. assert PythonUtil.unescapeHtmlString('as+df') == 'as df'
  53. assert PythonUtil.unescapeHtmlString('as%32df') == 'as2df'
  54. assert PythonUtil.unescapeHtmlString('asdf%32') == 'asdf2'
  55. def test_priority_callbacks():
  56. l = []
  57. def a(l=l):
  58. l.append('a')
  59. def b(l=l):
  60. l.append('b')
  61. def c(l=l):
  62. l.append('c')
  63. pc = PythonUtil.PriorityCallbacks()
  64. pc.add(a)
  65. pc()
  66. assert l == ['a']
  67. del l[:]
  68. bItem = pc.add(b)
  69. pc()
  70. assert 'a' in l
  71. assert 'b' in l
  72. assert len(l) == 2
  73. del l[:]
  74. pc.remove(bItem)
  75. pc()
  76. assert l == ['a']
  77. del l[:]
  78. pc.add(c, 2)
  79. bItem = pc.add(b, 10)
  80. pc()
  81. assert l == ['a', 'c', 'b']
  82. del l[:]
  83. pc.remove(bItem)
  84. pc()
  85. assert l == ['a', 'c']
  86. del l[:]
  87. pc.clear()
  88. pc()
  89. assert len(l) == 0