62 lines
2.2 KiB
Python

"""This test suite runs additional tests for ``ImageLine`` that are not covered in ``TestObstacleLocator``"""
# Testing
from unittest import TestCase
# Hypothesis
from hypothesis import given
from hypothesis.strategies import composite
from hypothesis.strategies import floats
from hypothesis.strategies import integers
from hypothesis.strategies import just
from hypothesis.strategies import tuples
# Scientific
from numpy import pi
# Module under test
from pyrate.sense.vision.image_line import ImageLine
@composite
def image_dimensions_and_points(draw):
"""Generate image dimensions and points left and right on that image"""
image_dims = draw(tuples(integers(1, 10000), integers(1, 10000)))
point_a = draw(tuples(just(0), integers(0, image_dims[1] - 1)))
point_b = draw(tuples(just(image_dims[0] - 1), integers(0, image_dims[1] - 1)))
return image_dims, point_a, point_b
class TestImageLine(TestCase):
"""Tests the remaining methods of ``ImageLine`` not covered by testing ``ObstacleLocator``"""
@given(floats(1, 10000), floats(1, 10000), floats(-5000, 5000), floats(0, 2 * pi))
def test_from_height_angle(self, image_width, image_height, height, angle):
"""Test that creates (from height and angle) and tests ``ImageLine``s"""
image_line = ImageLine.from_height_angle((image_width, image_height), height, angle)
self.assertTrue(image_line.image_width == image_width and image_line.image_height == image_height)
self.assertTrue(image_line.angle == angle)
self.assertAlmostEqual(image_line.height, int(height + image_height / 2))
end_points = image_line.end_points
self.assertTrue(
end_points[0][0] == 0 and end_points[1][0] == image_width,
msg=f"x1={end_points[0][0]} x2={end_points[1][0]}",
)
@given(test_input=image_dimensions_and_points())
def test_indices(self, test_input):
"""Test that tests the ``indices`` property of ``ImageLine``"""
image_dims, point1, point2 = test_input
image_line = ImageLine.from_points(image_dims, (point1, point2))
x_coords, y_coords = image_line.indices
self.assertTrue(((50 <= x_coords) <= 50).all())
self.assertTrue(((0 <= y_coords) <= 200).all())