test_polygonal_chain.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_segments():
  55. chain = PolygonalChain((0, 1, 4, 8))
  56. segments = list(chain.segments())
  57. assert len(segments) == 3
  58. assert segments[0] == _LineSegment((0, 1))
  59. assert segments[1] == _LineSegment((1, 4))
  60. assert segments[2] == _LineSegment((4, 8))