test_polygonal_chain.py 3.4 KB

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