test_polygonal_circuit.py 5.0 KB

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