def test_composition_of_arbitrary_dimensions(self): C1 = ContinuousDimension(name='x', min=0, max=1) C2 = ContinuousDimension(name='x', min=1, max=2) C3 = C1 - C2 D = DiscreteDimension(name='x', min=0, max=1) with pytest.raises(TypeError): C1.intersection(D) with pytest.raises(TypeError): C3.intersection(D) with pytest.raises(TypeError): D.intersection(C1)
class TestContinuousDimension(unittest.TestCase): def setUp(self): self.empty = ContinuousDimension(name='x', min=0, max=0, include_min=False, include_max=False) self.should_be_empty = ContinuousDimension(name='x', min=0, max=0, include_min=False, include_max=True) self.should_be_empty_too = ContinuousDimension(name='x', min=0, max=0, include_min=True, include_max=False) self.should_contain_zero = ContinuousDimension(name='x', min=0, max=0, include_min=True, include_max=True) self.closed = ContinuousDimension(name='x', min=0, max=1) self.left_open = ContinuousDimension(name='x', min=0, max=1, include_min=False) self.right_open = ContinuousDimension(name='x', min=0, max=1, include_max=False) self.open = ContinuousDimension(name='x', min=0, max=1, include_min=False, include_max=False) self.inner = ContinuousDimension(name='x', min=0.2, max=0.8) self.outer = ContinuousDimension(name='x', min=-0.2, max=1.2) self.left_overlapping = ContinuousDimension(name='x', min=-0.2, max=0.8) self.right_overlapping = ContinuousDimension(name='x', min=0.2, max=1.2) self.inner_wrongly_named = ContinuousDimension(name='y', min=0.2, max=0.8) self.one_to_five = ContinuousDimension(name='x', min=1, max=5) self.six_to_ten = ContinuousDimension(name='x', min=6, max=10) def test_string_representation(self): self.assertTrue(str(self.empty) == "x: (0.00, 0.00)") self.assertTrue(str(self.should_be_empty) == "x: (0.00, 0.00)") self.assertTrue(str(self.should_be_empty_too) == "x: (0.00, 0.00)") self.assertTrue(str(self.should_contain_zero) == "x: [0.00, 0.00]") self.assertTrue(str(self.closed) == "x: [0.00, 1.00]") self.assertTrue(str(self.left_open) == "x: (0.00, 1.00]") self.assertTrue(str(self.right_open) == "x: [0.00, 1.00)") self.assertTrue(str(self.open) == "x: (0.00, 1.00)") self.assertTrue(str(self.inner) == "x: [0.20, 0.80]") self.assertTrue(str(self.outer) == "x: [-0.20, 1.20]") self.assertTrue(str(self.left_overlapping) == "x: [-0.20, 0.80]") self.assertTrue(str(self.right_overlapping) == "x: [0.20, 1.20]") self.assertTrue(str(self.inner_wrongly_named) == "y: [0.20, 0.80]") def test_point_containment(self): self.assertTrue(0 not in self.empty and 0 not in self.should_be_empty and 0 not in self.should_be_empty_too and 0 in self.should_contain_zero) self.assertTrue(-1 not in self.closed and -1 not in self.left_open and -1 not in self.right_open and -1 not in self.open) self.assertTrue(0 in self.closed and 0 not in self.left_open and 0 in self.right_open and 0 not in self.open) self.assertTrue(0.5 in self.closed and 0.5 in self.left_open and 0.5 in self.right_open and 0.5 in self.open) self.assertTrue(1 in self.closed and 1 in self.left_open and 1 not in self.right_open and 1 not in self.open) self.assertTrue(2 not in self.closed and 2 not in self.left_open and 2 not in self.right_open and 2 not in self.open) def test_continuous_dimension_containment(self): self.assertTrue(self.open in self.closed) self.assertTrue(self.left_open in self.closed) self.assertTrue(self.right_open in self.closed) self.assertTrue(self.left_open not in self.open) self.assertTrue(self.right_open not in self.open) self.assertTrue(self.closed not in self.open) self.assertTrue(self.left_open not in self.right_open) self.assertTrue(self.right_open not in self.left_open) self.assertTrue(self.inner in self.closed) self.assertTrue(self.inner in self.open) self.assertTrue(self.inner in self.left_open) self.assertTrue(self.inner in self.right_open) self.assertTrue(self.closed in self.outer) self.assertTrue(self.open in self.outer) self.assertTrue(self.left_open in self.outer) self.assertTrue(self.right_open in self.outer) self.assertTrue(self.inner_wrongly_named not in self.closed) self.assertTrue(self.inner_wrongly_named not in self.open) self.assertTrue(self.inner_wrongly_named not in self.left_open) self.assertTrue(self.inner_wrongly_named not in self.right_open) def test_continuous_dimension_set_operations(self): self.assertTrue(self.inner in self.inner.union(self.closed)) self.assertTrue(self.inner in self.inner.intersection(self.closed)) self.assertTrue(self.open in self.open.intersection(self.closed)) self.assertTrue(self.closed not in self.open.intersection(self.closed)) self.assertTrue(self.closed in self.open.union(self.closed)) self.assertTrue(self.closed in self.left_open.union(self.right_open)) self.assertTrue( self.left_open.intersection(self.right_open) in self.open) def test_random(self): self.assertTrue(self.empty.random() is None) self.assertTrue(self.outer.random() in self.outer) for _ in range(1000): self.assertTrue(self.one_to_five.random() not in self.six_to_ten)
class TestContinuousDimension: def setup_method(self, method): self.empty = ContinuousDimension(name='x', min=0, max=0, include_min=False, include_max=False) self.unbounded_continuous = ContinuousDimension(name='x', min=0, max=math.inf) self.unbounded_discrete = DiscreteDimension(name='x', min=0, max=math.inf) self.should_be_empty = ContinuousDimension(name='x', min=0, max=0, include_min=False, include_max=True) self.should_be_empty_too = ContinuousDimension(name='x', min=0, max=0, include_min=True, include_max=False) self.should_contain_zero = ContinuousDimension(name='x', min=0, max=0, include_min=True, include_max=True) self.closed = ContinuousDimension(name='x', min=0, max=1) self.left_open = ContinuousDimension(name='x', min=0, max=1, include_min=False) self.right_open = ContinuousDimension(name='x', min=0, max=1, include_max=False) self.open = ContinuousDimension(name='x', min=0, max=1, include_min=False, include_max=False) self.inner = ContinuousDimension(name='x', min=0.2, max=0.8) self.outer = ContinuousDimension(name='x', min=-0.2, max=1.2) self.left_overlapping = ContinuousDimension(name='x', min=-0.2, max=0.8) self.right_overlapping = ContinuousDimension(name='x', min=0.2, max=1.2) self.inner_wrongly_named = ContinuousDimension(name='y', min=0.2, max=0.8) self.one_to_five = ContinuousDimension(name='x', min=1, max=5) self.six_to_ten = ContinuousDimension(name='x', min=6, max=10) def test_string_representation(self): assert str(self.empty) == "x: (0.00, 0.00)" assert str(self.should_be_empty) == "x: (0.00, 0.00)" assert str(self.should_be_empty_too) == "x: (0.00, 0.00)" assert str(self.should_contain_zero) == "x: [0.00, 0.00]" assert str(self.closed) == "x: [0.00, 1.00]" assert str(self.left_open) == "x: (0.00, 1.00]" assert str(self.right_open) == "x: [0.00, 1.00)" assert str(self.open) == "x: (0.00, 1.00)" assert str(self.inner) == "x: [0.20, 0.80]" assert str(self.outer) == "x: [-0.20, 1.20]" assert str(self.left_overlapping) == "x: [-0.20, 0.80]" assert str(self.right_overlapping) == "x: [0.20, 1.20]" assert str(self.inner_wrongly_named) == "y: [0.20, 0.80]" def test_point_containment(self): assert (0 not in self.empty and 0 not in self.should_be_empty and 0 not in self.should_be_empty_too and 0 in self.should_contain_zero) assert (-1 not in self.closed and -1 not in self.left_open and -1 not in self.right_open and -1 not in self.open) assert (0 in self.closed and 0 not in self.left_open and 0 in self.right_open and 0 not in self.open) assert (0.5 in self.closed and 0.5 in self.left_open and 0.5 in self.right_open and 0.5 in self.open) assert (1 in self.closed and 1 in self.left_open and 1 not in self.right_open and 1 not in self.open) assert (2 not in self.closed and 2 not in self.left_open and 2 not in self.right_open and 2 not in self.open) def test_continuous_dimension_containment(self): assert self.open in self.closed assert self.left_open in self.closed assert self.right_open in self.closed assert self.left_open not in self.open assert self.right_open not in self.open assert self.closed not in self.open assert self.left_open not in self.right_open assert self.right_open not in self.left_open assert self.inner in self.closed assert self.inner in self.open assert self.inner in self.left_open assert self.inner in self.right_open assert self.closed in self.outer assert self.open in self.outer assert self.left_open in self.outer assert self.right_open in self.outer assert self.inner_wrongly_named not in self.closed assert self.inner_wrongly_named not in self.open assert self.inner_wrongly_named not in self.left_open assert self.inner_wrongly_named not in self.right_open def test_continuous_dimension_set_operations(self): assert self.inner in self.inner.union(self.closed) assert self.inner in self.inner.intersection(self.closed) assert self.open in self.open.intersection(self.closed) assert self.closed not in self.open.intersection(self.closed) assert self.closed in self.open.union(self.closed) assert self.closed in self.left_open.union(self.right_open) assert self.left_open.intersection(self.right_open) in self.open def test_random(self): with pytest.raises(ValueError): self.empty.random() with pytest.raises(ValueError): self.unbounded_continuous.random() with pytest.raises(OverflowError): self.unbounded_discrete.random() assert self.outer.random() in self.outer for _ in range(1000): assert self.one_to_five.random() not in self.six_to_ten