TestHitChecker.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from unittest.mock import patch
  2. from cura.HitChecker import HitChecker
  3. from cura.OneAtATimeIterator import OneAtATimeIterator
  4. from cura.Scene.CuraSceneNode import CuraSceneNode
  5. def test_anyTwoNodesBlockEachOther_True():
  6. node1 = CuraSceneNode(no_setting_override=True)
  7. node2 = CuraSceneNode(no_setting_override=True)
  8. # node1 and node2 block each other
  9. hit_map = {
  10. node1: {node1: 0, node2: 1},
  11. node2: {node1: 1, node2: 0}
  12. }
  13. with patch.object(HitChecker, "_buildHitMap", return_value=hit_map):
  14. hit_checker = HitChecker([node1, node2])
  15. assert hit_checker.anyTwoNodesBlockEachOther([node1, node2])
  16. assert hit_checker.anyTwoNodesBlockEachOther([node2, node1])
  17. def test_anyTwoNodesBlockEachOther_False():
  18. node1 = CuraSceneNode(no_setting_override=True)
  19. node2 = CuraSceneNode(no_setting_override=True)
  20. # node1 blocks node2, but node2 doesn't block node1
  21. hit_map = {
  22. node1: {node1: 0, node2: 1},
  23. node2: {node1: 0, node2: 0}
  24. }
  25. with patch.object(HitChecker, "_buildHitMap", return_value=hit_map):
  26. hit_checker = HitChecker([node1, node2])
  27. assert not hit_checker.anyTwoNodesBlockEachOther([node1, node2])
  28. assert not hit_checker.anyTwoNodesBlockEachOther([node2, node1])
  29. def test_canPrintBefore():
  30. node1 = CuraSceneNode(no_setting_override=True)
  31. node2 = CuraSceneNode(no_setting_override=True)
  32. node3 = CuraSceneNode(no_setting_override=True)
  33. # nodes can be printed only in order node1 -> node2 -> node3
  34. hit_map = {
  35. node1: {node1: 0, node2: 0, node3: 0},
  36. node2: {node1: 1, node2: 0, node3: 0},
  37. node3: {node1: 1, node2: 1, node3: 0},
  38. }
  39. with patch.object(HitChecker, "_buildHitMap", return_value=hit_map):
  40. hit_checker = HitChecker([node1, node2, node3])
  41. assert hit_checker.canPrintBefore(node1, [node2])
  42. assert hit_checker.canPrintBefore(node1, [node3])
  43. assert hit_checker.canPrintBefore(node1, [node2, node3])
  44. assert hit_checker.canPrintBefore(node1, [node3, node2])
  45. assert hit_checker.canPrintBefore(node2, [node3])
  46. assert not hit_checker.canPrintBefore(node2, [node1])
  47. assert not hit_checker.canPrintBefore(node2, [node1, node3])
  48. assert not hit_checker.canPrintBefore(node2, [node3, node1])
  49. assert not hit_checker.canPrintBefore(node3, [node1])
  50. assert not hit_checker.canPrintBefore(node3, [node2])
  51. assert not hit_checker.canPrintBefore(node3, [node1, node2])
  52. assert not hit_checker.canPrintBefore(node3, [node2, node1])
  53. def test_canPrintAfter():
  54. node1 = CuraSceneNode(no_setting_override=True)
  55. node2 = CuraSceneNode(no_setting_override=True)
  56. node3 = CuraSceneNode(no_setting_override=True)
  57. # nodes can be printed only in order node1 -> node2 -> node3
  58. hit_map = {
  59. node1: {node1: 0, node2: 0, node3: 0},
  60. node2: {node1: 1, node2: 0, node3: 0},
  61. node3: {node1: 1, node2: 1, node3: 0},
  62. }
  63. with patch.object(HitChecker, "_buildHitMap", return_value=hit_map):
  64. hit_checker = HitChecker([node1, node2, node3])
  65. assert not hit_checker.canPrintAfter(node1, [node2])
  66. assert not hit_checker.canPrintAfter(node1, [node3])
  67. assert not hit_checker.canPrintAfter(node1, [node2, node3])
  68. assert not hit_checker.canPrintAfter(node1, [node3, node2])
  69. assert hit_checker.canPrintAfter(node2, [node1])
  70. assert not hit_checker.canPrintAfter(node2, [node3])
  71. assert not hit_checker.canPrintAfter(node2, [node1, node3])
  72. assert not hit_checker.canPrintAfter(node2, [node3, node1])
  73. assert hit_checker.canPrintAfter(node3, [node1])
  74. assert hit_checker.canPrintAfter(node3, [node2])
  75. assert hit_checker.canPrintAfter(node3, [node1, node2])
  76. assert hit_checker.canPrintAfter(node3, [node2, node1])
  77. def test_calculateScore():
  78. node1 = CuraSceneNode(no_setting_override=True)
  79. node2 = CuraSceneNode(no_setting_override=True)
  80. node3 = CuraSceneNode(no_setting_override=True)
  81. hit_map = {
  82. node1: {node1: 0, node2: 0, node3: 0}, # sum is 0
  83. node2: {node1: 1, node2: 0, node3: 0}, # sum is 1
  84. node3: {node1: 1, node2: 1, node3: 0}, # sum is 2
  85. }
  86. with patch.object(HitChecker, "_buildHitMap", return_value=hit_map):
  87. hit_checker = HitChecker([node1, node2, node3])
  88. # score is a diff between sums
  89. assert hit_checker.calculateScore(node1, node2) == -1
  90. assert hit_checker.calculateScore(node2, node1) == 1
  91. assert hit_checker.calculateScore(node1, node3) == -2
  92. assert hit_checker.calculateScore(node3, node1) == 2
  93. assert hit_checker.calculateScore(node2, node3) == -1
  94. assert hit_checker.calculateScore(node3, node2) == 1
  95. def test_canPrintNodesInProvidedOrder():
  96. node1 = CuraSceneNode(no_setting_override=True)
  97. node2 = CuraSceneNode(no_setting_override=True)
  98. node3 = CuraSceneNode(no_setting_override=True)
  99. # nodes can be printed only in order node1 -> node2 -> node3
  100. hit_map = {
  101. node1: {node1: 0, node2: 0, node3: 0}, # 0
  102. node2: {node1: 1, node2: 0, node3: 0}, # 1
  103. node3: {node1: 1, node2: 1, node3: 0}, # 2
  104. }
  105. with patch.object(HitChecker, "_buildHitMap", return_value=hit_map):
  106. hit_checker = HitChecker([node1, node2, node3])
  107. assert hit_checker.canPrintNodesInProvidedOrder([node1, node2, node3])
  108. assert not hit_checker.canPrintNodesInProvidedOrder([node1, node3, node2])
  109. assert not hit_checker.canPrintNodesInProvidedOrder([node2, node1, node3])
  110. assert not hit_checker.canPrintNodesInProvidedOrder([node2, node3, node1])
  111. assert not hit_checker.canPrintNodesInProvidedOrder([node3, node1, node2])
  112. assert not hit_checker.canPrintNodesInProvidedOrder([node3, node2, node1])