test_polygonal_chain.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import pytest
  2. from freesurfer_surface import PolygonalChain, PolygonalChainsNotOverlapingError, _LineSegment
  3. def test_init():
  4. chain = PolygonalChain((0, 21, 42, 84))
  5. assert tuple(chain.vertex_indices) == (0, 21, 42, 84)
  6. def test_reassign_vertex_indices():
  7. chain = PolygonalChain((0, 21, 42, 84))
  8. chain.vertex_indices = (1, 2, 3, 4)
  9. assert tuple(chain.vertex_indices) == (1, 2, 3, 4)
  10. def test_eq():
  11. assert PolygonalChain((0, 1, 2)) == PolygonalChain((0, 1, 2))
  12. # pylint: disable=unneeded-not
  13. assert not PolygonalChain((0, 1, 2)) == PolygonalChain((0, 1, 4))
  14. assert not PolygonalChain((0, 1, 2)) == PolygonalChain((0, 4, 2))
  15. assert not PolygonalChain((0, 1, 2)) == PolygonalChain((4, 1, 2))
  16. assert not PolygonalChain((0, 1, 2)) == PolygonalChain((1, 2, 3, 4))
  17. def test_repr():
  18. assert repr(PolygonalChain([])) \
  19. == 'PolygonalChain(vertex_indices=())'
  20. assert repr(PolygonalChain((0, 2, 1))) \
  21. == 'PolygonalChain(vertex_indices=(0, 2, 1))'
  22. assert repr(PolygonalChain((0, 2, 1, 4, 3))) \
  23. == 'PolygonalChain(vertex_indices=(0, 2, 1, 4, 3))'
  24. @pytest.mark.parametrize(('vertex_indices_a', 'vertex_indices_b', 'expected_vertex_indices'), [
  25. ((1, 2, 3), (3, 4), (1, 2, 3, 4)),
  26. ((1, 2, 3), (4, 3), (1, 2, 3, 4)),
  27. ((3, 2, 1), (3, 4), (4, 3, 2, 1)),
  28. ((3, 2, 1), (4, 3), (4, 3, 2, 1)),
  29. ((1,), (1,), (1,)),
  30. ((1, 2), (1,), (1, 2)),
  31. ((1, 2), (2,), (1, 2)),
  32. ((0, 3, 1, 5, 2), (3, 5, 2, 0), (3, 5, 2, 0, 3, 1, 5, 2)),
  33. ((98792, 98807, 98821), (98792, 98793), (98793, 98792, 98807, 98821)),
  34. ])
  35. def test_connect(vertex_indices_a, vertex_indices_b, expected_vertex_indices):
  36. chain = PolygonalChain(vertex_indices_a)
  37. chain.connect(PolygonalChain(vertex_indices_b))
  38. assert PolygonalChain(expected_vertex_indices) == chain
  39. @pytest.mark.parametrize(('vertex_indices_a', 'vertex_indices_b'), [
  40. ((1, 2, 3), (2, 4)),
  41. ])
  42. def test_connect_fail(vertex_indices_a, vertex_indices_b):
  43. chain = PolygonalChain(vertex_indices_a)
  44. with pytest.raises(PolygonalChainsNotOverlapingError):
  45. chain.connect(PolygonalChain(vertex_indices_b))
  46. @pytest.mark.parametrize(('vertex_indices_a', 'vertex_indices_b'), [
  47. ((1, 2, 3), ()),
  48. ((), (3, 4)),
  49. ])
  50. def test_connect_fail_empty(vertex_indices_a, vertex_indices_b):
  51. chain = PolygonalChain(vertex_indices_a)
  52. with pytest.raises(Exception):
  53. chain.connect(PolygonalChain(vertex_indices_b))
  54. def test_adjacent_vertex_indices_1():
  55. chain = PolygonalChain((0, 1, 4, 8))
  56. pairs = list(chain.adjacent_vertex_indices(1))
  57. assert len(pairs) == 4
  58. assert pairs[0] == (0,)
  59. assert pairs[1] == (1,)
  60. assert pairs[2] == (4,)
  61. assert pairs[3] == (8,)
  62. def test_adjacent_vertex_indices_2():
  63. chain = PolygonalChain((0, 1, 4, 8))
  64. pairs = list(chain.adjacent_vertex_indices(2))
  65. assert len(pairs) == 3
  66. assert pairs[0] == (0, 1)
  67. assert pairs[1] == (1, 4)
  68. assert pairs[2] == (4, 8)
  69. def test_adjacent_vertex_indices_3():
  70. chain = PolygonalChain((0, 1, 4, 8))
  71. pairs = list(chain.adjacent_vertex_indices(3))
  72. assert len(pairs) == 2
  73. assert pairs[0] == (0, 1, 4)
  74. assert pairs[1] == (1, 4, 8)
  75. def test_segments():
  76. chain = PolygonalChain((0, 1, 4, 8))
  77. segments = list(chain.segments())
  78. assert len(segments) == 3
  79. assert segments[0] == _LineSegment((0, 1))
  80. assert segments[1] == _LineSegment((1, 4))
  81. assert segments[2] == _LineSegment((4, 8))