test_polygonal_circuit.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import pytest
  2. from freesurfer_surface import PolygonalCircuit, Vertex
  3. def test_init_tuple():
  4. circuit = PolygonalCircuit((1, 2, 3, 5))
  5. assert circuit.vertex_indices == (1, 2, 3, 5)
  6. def test_init_list():
  7. circuit = PolygonalCircuit([1, 2, 3, 5])
  8. assert circuit.vertex_indices == (1, 2, 3, 5)
  9. def test_init_iterator():
  10. circuit = PolygonalCircuit(iter([1, 2, 3, 5]))
  11. assert circuit.vertex_indices == (1, 2, 3, 5)
  12. def test_init_invalid_type():
  13. with pytest.raises(Exception):
  14. PolygonalCircuit((0, 1, 2.0))
  15. with pytest.raises(Exception):
  16. PolygonalCircuit((Vertex(2, 3, 4),))
  17. with pytest.raises(Exception):
  18. PolygonalCircuit((0, 1, Vertex(2, 3, 4)))
  19. @pytest.mark.parametrize(('source_vertex_indices', 'expected_vertex_indices'), [
  20. ((1,), (1,)),
  21. ((1, 2), (1, 2)),
  22. ((2, 1), (1, 2)),
  23. ((1, 2, 3), (1, 2, 3)),
  24. ((2, 3, 1), (1, 2, 3)),
  25. ((3, 1, 2), (1, 2, 3)),
  26. ((1, 3, 2), (1, 2, 3)),
  27. ((2, 1, 3), (1, 2, 3)),
  28. ((3, 2, 1), (1, 2, 3)),
  29. ((1, 2, 3, 5), (1, 2, 3, 5)),
  30. ((2, 3, 5, 1), (1, 2, 3, 5)),
  31. ((3, 5, 1, 2), (1, 2, 3, 5)),
  32. ((2, 1, 5, 3), (1, 2, 3, 5)),
  33. ((5, 3, 2, 1), (1, 2, 3, 5)),
  34. ])
  35. def test__normalize(source_vertex_indices, expected_vertex_indices):
  36. # pylint: disable=protected-access
  37. circuit = PolygonalCircuit(source_vertex_indices)
  38. assert expected_vertex_indices == circuit._normalize()._vertex_indices
  39. def test_eq():
  40. assert PolygonalCircuit((0, 1)) == PolygonalCircuit((0, 1))
  41. assert PolygonalCircuit((0, 1)) == PolygonalCircuit((1, 0))
  42. assert PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((0, 1, 2))
  43. assert PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((1, 2, 0))
  44. assert PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((2, 0, 1))
  45. # pylint: disable=unneeded-not
  46. assert not PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((0, 1, 4))
  47. assert not PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((0, 4, 2))
  48. assert not PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((4, 1, 2))
  49. def test_eq_reverse():
  50. assert PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((2, 1, 0))
  51. assert PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((0, 2, 1))
  52. assert PolygonalCircuit((0, 1, 2, 4)) == PolygonalCircuit((4, 2, 1, 0))
  53. assert PolygonalCircuit((0, 1, 2, 4)) == PolygonalCircuit((1, 0, 4, 2))
  54. def test_hash():
  55. assert hash(PolygonalCircuit((0, 1, 2))) \
  56. == hash(PolygonalCircuit((0, 1, 2)))
  57. assert hash(PolygonalCircuit((0, 1, 2))) \
  58. == hash(PolygonalCircuit((1, 2, 0)))
  59. assert hash(PolygonalCircuit((0, 1, 2))) \
  60. == hash(PolygonalCircuit((2, 0, 1)))
  61. assert hash(PolygonalCircuit((0, 1, 2))) \
  62. != hash(PolygonalCircuit((0, 1, 4)))
  63. assert hash(PolygonalCircuit((0, 1, 2))) \
  64. != hash(PolygonalCircuit((0, 4, 2)))
  65. assert hash(PolygonalCircuit((0, 1, 2))) \
  66. != hash(PolygonalCircuit((4, 1, 2)))
  67. def test_hash_reverse():
  68. assert hash(PolygonalCircuit((0, 1, 2))) \
  69. == hash(PolygonalCircuit((2, 1, 0)))
  70. assert hash(PolygonalCircuit((0, 1, 2))) \
  71. == hash(PolygonalCircuit((0, 2, 1)))
  72. assert hash(PolygonalCircuit((0, 1, 2, 4))) \
  73. == hash(PolygonalCircuit((4, 2, 1, 0)))
  74. assert hash(PolygonalCircuit((0, 1, 2, 4))) \
  75. == hash(PolygonalCircuit((1, 0, 4, 2)))
  76. assert hash(PolygonalCircuit((0, 1, 2, 4))) \
  77. != hash(PolygonalCircuit((1, 4, 0, 2)))
  78. def test_adjacent_vertex_indices_1():
  79. chain = PolygonalCircuit((0, 1, 4, 8))
  80. singles = list(chain.adjacent_vertex_indices(1))
  81. assert len(singles) == 4
  82. assert singles[0] == (0,)
  83. assert singles[1] == (1,)
  84. assert singles[2] == (4,)
  85. assert singles[3] == (8,)
  86. def test_adjacent_vertex_indices_2():
  87. chain = PolygonalCircuit((0, 1, 4, 8))
  88. pairs = list(chain.adjacent_vertex_indices(2))
  89. assert len(pairs) == 4
  90. assert pairs[0] == (0, 1)
  91. assert pairs[1] == (1, 4)
  92. assert pairs[2] == (4, 8)
  93. assert pairs[3] == (8, 0)
  94. def test_adjacent_vertex_indices_3():
  95. chain = PolygonalCircuit((0, 1, 4, 8))
  96. triplets = list(chain.adjacent_vertex_indices(3))
  97. assert len(triplets) == 4
  98. assert triplets[0] == (0, 1, 4)
  99. assert triplets[1] == (1, 4, 8)
  100. assert triplets[2] == (4, 8, 0)
  101. assert triplets[3] == (8, 0, 1)
  102. def test_adjacent_vertex_indices_4():
  103. chain = PolygonalCircuit((0, 1, 4, 8))
  104. quadruples = list(chain.adjacent_vertex_indices(4))
  105. assert len(quadruples) == 4
  106. assert quadruples[0] == (0, 1, 4, 8)
  107. assert quadruples[1] == (1, 4, 8, 0)
  108. assert quadruples[2] == (4, 8, 0, 1)
  109. assert quadruples[3] == (8, 0, 1, 4)
  110. def test_adjacent_vertex_indices_5():
  111. chain = PolygonalCircuit((0, 1, 4, 8))
  112. quintuples = list(chain.adjacent_vertex_indices(5))
  113. assert len(quintuples) == 4
  114. assert quintuples[0] == (0, 1, 4, 8, 0)
  115. assert quintuples[1] == (1, 4, 8, 0, 1)
  116. assert quintuples[2] == (4, 8, 0, 1, 4)
  117. assert quintuples[3] == (8, 0, 1, 4, 8)