test_polygonal_circuit.py 4.6 KB

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