Exemplo n.º 1
0
 def test_integer_comparison(self):
     assert IntInterval([2, 2]) <= 3
     assert IntInterval([1, 3]) >= 0
     assert IntInterval([2, 2]) == 2
     assert IntInterval([2, 2]) != 3
Exemplo n.º 2
0
 def test_sub_operator(self, first, second, result):
     assert first - second == IntInterval(result)
Exemplo n.º 3
0
 def test_iadd_operator(self):
     range_ = IntInterval([1, 2])
     range_ += IntInterval([1, 2])
     assert range_ == IntInterval([2, 4])
Exemplo n.º 4
0
 def test_supports_strings_with_bounds(self):
     interval = IntInterval('[1, 3]')
     assert interval.lower == 1
     assert interval.upper == 3
     assert interval.lower_inc
     assert interval.upper_inc
Exemplo n.º 5
0
 def test_supports_exact_ranges_as_strings(self):
     interval = IntInterval('3')
     assert interval.lower == 3
     assert interval.upper == 3
     assert interval.lower_inc
     assert interval.upper_inc
Exemplo n.º 6
0
 def test_hyphen_format(self, number_range, lower, upper):
     interval = IntInterval(number_range)
     assert interval.lower == lower
     assert interval.upper == upper
Exemplo n.º 7
0
 def test_supports_infinity(self):
     interval = IntInterval((-inf, inf))
     assert interval.lower == -inf
     assert interval.upper == inf
     assert not interval.lower_inc
     assert not interval.upper_inc
Exemplo n.º 8
0
class TestIntervalProperties(object):
    @mark.parametrize(
        ('number_range', 'length'),
        (
            ([1, 4], 3),
            ([-1, 1], 2),
            ((-inf, inf), inf),
            ((1, inf), inf),
        )
    )
    def test_length(self, number_range, length):
        assert IntInterval(number_range).length == length

    @mark.parametrize(
        ('number_range', 'radius'),
        (
            ([1, 4], 1.5),
            ([-1, 1], 1.0),
            ([-4, -1], 1.5),
            ((-inf, inf), inf),
            ((1, inf), inf),
        )
    )
    def test_radius(self, number_range, radius):
        assert IntInterval(number_range).radius == radius

    @mark.parametrize(
        ('number_range', 'centre'),
        (
            ([1, 4], 2.5),
            ([-1, 1], 0),
            ([-4, -1], -2.5),
            ((1, inf), inf),
        )
    )
    def test_centre(self, number_range, centre):
        assert IntInterval(number_range).centre == centre

    @mark.parametrize(
        ('interval', 'is_open'),
        (
            (IntInterval((2, 3)), True),
            (IntInterval.from_string('(2, 5)'), True),
            (IntInterval.from_string('[3, 4)'), False),
            (IntInterval.from_string('(4, 5]'), False),
            (IntInterval.from_string('3 - 4'), False),
            (IntInterval([4, 5]), False),
            (IntInterval.from_string('[4, 5]'), False)
        )
    )
    def test_is_open(self, interval, is_open):
        assert interval.is_open == is_open

    @mark.parametrize(
        ('interval', 'is_closed'),
        (
            (IntInterval((2, 3)), False),
            (IntInterval.from_string('(2, 5)'), False),
            (IntInterval.from_string('[3, 4)'), False),
            (IntInterval.from_string('(4, 5]'), False),
            (IntInterval.from_string('3 - 4'), True),
            (IntInterval([4, 5]), True),
            (IntInterval.from_string('[4, 5]'), True)
        )
    )
    def test_closed(self, interval, is_closed):
        assert interval.is_closed == is_closed

    @mark.parametrize(
        ('interval', 'empty'),
        (
            (IntInterval((2, 3)), True),
            (IntInterval([2, 3]), False),
            (IntInterval([2, 2]), False),
            (IntInterval.from_string('[2, 2)'), True),
            (IntInterval.from_string('(2, 2]'), True),
            (IntInterval.from_string('[2, 3)'), False),
            (IntInterval((2, 10)), False),
        )
    )
    def test_empty(self, interval, empty):
        assert interval.empty == empty

    @mark.parametrize(
        ('interval', 'degenerate'),
        (
            (IntInterval((2, 4)), False),
            (IntInterval.from_string('[2, 2]'), True),
            (IntInterval.from_string('[0, 0)'), True),
        )
    )
    def test_degenerate(self, interval, degenerate):
        assert interval.degenerate == degenerate

    @mark.parametrize(
        ('interval', 'discrete'),
        (
            (IntInterval((2, 3)), True),
            (IntInterval(5), True),
            (FloatInterval(3.5), False),
            (DecimalInterval(Decimal('2.4')), False),
            (DateTimeInterval(datetime(2002, 1, 1)), False),
            (DateInterval(date(2002, 1, 1)), True)
        )
    )
    def test_discrete(self, interval, discrete):
        assert interval.discrete == discrete
Exemplo n.º 9
0
 def test_length(self, number_range, length):
     assert IntInterval(number_range).length == length
Exemplo n.º 10
0
def test_hyphenized(interval, string):
    assert IntInterval(interval).hyphenized == string
Exemplo n.º 11
0
from pytest import mark

from intervals import DecimalInterval, IntInterval


@mark.parametrize(('interval1', 'interval2', 'result'),
                  ((IntInterval([1, 3]), IntInterval([1, 3]), True),
                   (DecimalInterval.from_string('[1, 3)'),
                    DecimalInterval.from_string('[3, 4]'), True),
                   (DecimalInterval.from_string('[3, 4]'),
                    DecimalInterval.from_string('[1, 3)'), True),
                   (DecimalInterval.from_string('[1, 3]'),
                    DecimalInterval.from_string('[4, 5]'), False),
                   (DecimalInterval.from_string('[2, 4)'),
                    DecimalInterval.from_string('[0, 1]'), False),
                   (DecimalInterval.from_string('[1, 3)'),
                    DecimalInterval.from_string('(3, 4]'), False)))
def test_is_connected(interval1, interval2, result):
    assert interval1.is_connected(interval2) is result
Exemplo n.º 12
0
 def test_contains_operator_for_non_inclusive_interval(self, value):
     assert value not in IntInterval((-1, 2))
Exemplo n.º 13
0
 def test_contains_operator_for_inclusive_interval(self, value):
     assert value in IntInterval([-1, 2])
Exemplo n.º 14
0
class TestComparisonOperators(object):
    @mark.parametrize(('comparison', 'result'), (
        (IntInterval([1, 3]) == IntInterval([1, 3]), True),
        (IntInterval([1, 3]) == IntInterval([1, 4]), False),
        (IntInterval([inf, inf]) == inf, True),
        (IntInterval([3, 3]) == 3, True),
        (IntInterval([3, 3]) == 5, False),
        (IntInterval([3, 3]) == 'something', False),
        (
            IntInterval([3, 3]) ==
            DateInterval([date(2011, 1, 1), date(2011, 1, 1)]),
            False
        ),
        (IntInterval.from_string('(,)') == None, False)  # noqa
    ))
    def test_eq_operator(self, comparison, result):
        assert comparison is result

    @mark.parametrize(('comparison', 'result'), (
        (IntInterval([1, 3]) != IntInterval([1, 3]), False),
        (IntInterval([1, 3]) != IntInterval([1, 4]), True),
        (IntInterval([inf, inf]) != inf, False),
        (IntInterval([3, 3]) != 3, False),
        (IntInterval([3, 3]) != 5, True),
        (IntInterval([3, 3]) != 'something', True),
        (IntInterval.from_string('(,)') != None, True)  # noqa
    ))
    def test_ne_operator(self, comparison, result):
        assert comparison is result

    @mark.parametrize(('comparison', 'result'), (
        (IntInterval([1, 3]) > IntInterval([0, 2]), True),
        (IntInterval((1, 4)) > 1, False),
        (IntInterval((1, 6)) > [1, 6], False),
        (IntInterval((1, 6)) > 0, True)
    ))
    def test_gt_operator(self, comparison, result):
        assert comparison is result

    @mark.parametrize(('comparison', 'result'), (
        (IntInterval([1, 3]) >= IntInterval([0, 2]), True),
        (IntInterval((1, 4)) >= 1, False),
        (IntInterval((1, 6)) >= [1, 6], False),
        (IntInterval((1, 6)) >= 0, True)
    ))
    def test_ge_operator(self, comparison, result):
        assert comparison is result

    @mark.parametrize(('comparison', 'result'), (
        (IntInterval([0, 2]) < IntInterval([1, 3]), True),
        (IntInterval([2, 3]) < IntInterval([2, 3]), False),
        (IntInterval([2, 5]) < 6, True),
        (IntInterval([2, 5]) < 5, False),
        (IntInterval([2, 5]) < inf, True)
    ))
    def test_lt_operator(self, comparison, result):
        assert comparison is result

    @mark.parametrize(('comparison', 'result'), (
        (IntInterval([0, 2]) <= IntInterval([1, 3]), True),
        (IntInterval([1, 3]) <= IntInterval([1, 3]), True),
        (IntInterval([1, 7]) <= 8, True),
        (IntInterval([1, 6]) <= 5, False),
        (IntInterval([1, 5]) <= inf, True)
    ))
    def test_le_operator(self, comparison, result):
        assert comparison is result

    def test_integer_comparison(self):
        assert IntInterval([2, 2]) <= 3
        assert IntInterval([1, 3]) >= 0
        assert IntInterval([2, 2]) == 2
        assert IntInterval([2, 2]) != 3

    @mark.parametrize('value', (
        IntInterval([0, 2]),
        1,
        1.0,
        (-1, 1),
    ))
    def test_contains_operator_for_inclusive_interval(self, value):
        assert value in IntInterval([-1, 2])

    @mark.parametrize('value', (
        IntInterval([0, 2]),
        2,
        [-1, 1],
    ))
    def test_contains_operator_for_non_inclusive_interval(self, value):
        assert value not in IntInterval((-1, 2))

    @mark.parametrize(('interval1', 'interval2', 'expected'), (
        (IntInterval((0, 2)), IntInterval((0, 2)), True),
        (IntInterval([0, 2]), IntInterval([0, 2]), True),
        (
            IntInterval.from_string('[0, 2)'),
            IntInterval.from_string('[0, 2)'),
            True
        ),
        (
            IntInterval.from_string('(0, 2]'),
            IntInterval.from_string('(0, 2]'),
            True
        ),
        (IntInterval((0, 2)), IntInterval((1, 2)), False),
        (IntInterval((0, 2)), IntInterval((0, 1)), False),
        (IntInterval((0, 2)), IntInterval([0, 1]), False),
        (IntInterval((0, 2)), FloatInterval((0, 1)), False),
    ))
    def test_hash_operator_with_interval_attributes(
        self,
        interval1,
        interval2,
        expected
    ):
        actual = (interval1.__hash__() == interval2.__hash__())
        assert actual == expected

    @mark.parametrize(('contains_check', 'expected'), (
        (IntInterval([0, 2]) in {IntInterval([0, 2]): ''}, True),
        (IntInterval([0, 2]) in {IntInterval((0, 2)): ''}, False),
        (IntInterval([0, 2]) in set([IntInterval([0, 2])]), True),
    ))
    def test_hash_operator_with_collections(self, contains_check, expected):
        assert contains_check is expected
Exemplo n.º 15
0
def test_int_with_interval_containing_multiple_points(number_range):
    with raises(TypeError):
        int(IntInterval(number_range))
Exemplo n.º 16
0
 def test_radius(self, number_range, radius):
     assert IntInterval(number_range).radius == radius
Exemplo n.º 17
0
def test_str_representation(interval, string):
    assert str(IntInterval(interval)) == string
Exemplo n.º 18
0
 def test_centre(self, number_range, centre):
     assert IntInterval(number_range).centre == centre
Exemplo n.º 19
0
 def test_support_range_object(self):
     interval = IntInterval(IntInterval((1, 3)))
     assert interval.lower == 1
     assert interval.upper == 3
     assert not interval.lower_inc
     assert not interval.upper_inc
Exemplo n.º 20
0
 def test_intersection(self, first, second, intersection):
     IntInterval(first) & IntInterval(second) == IntInterval(intersection)
Exemplo n.º 21
0
 def test_supports_strings_with_spaces(self):
     interval = IntInterval('1 - 3')
     assert interval.lower == 1
     assert interval.upper == 3
     assert interval.lower_inc
     assert interval.upper_inc
Exemplo n.º 22
0
 def test_sup(self, first, second, result):
     assert first.sup(second) == IntInterval(result)
Exemplo n.º 23
0
 def test_empty_string_as_lower_bound(self):
     interval = IntInterval('[,1)')
     assert interval.lower == -inf
     assert interval.upper == 1
     assert interval.lower_inc
     assert not interval.upper_inc
Exemplo n.º 24
0
from infinity import inf
from pytest import mark, raises

from intervals import IntInterval


@mark.parametrize(('interval', 'string'),
                  (((1, 3), '(1, 3)'), ([1, 1], '[1, 1]'), ([1, inf], '[1,]')))
def test_str_representation(interval, string):
    assert str(IntInterval(interval)) == string


@mark.parametrize(('number_range', 'empty'), (
    (IntInterval((2, 3)), True),
    (IntInterval([2, 3]), False),
    (IntInterval([2, 2]), False),
    (IntInterval.from_string('[2, 2)'), True),
    (IntInterval.from_string('(2, 2]'), True),
    (IntInterval.from_string('[2, 3)'), False),
    (IntInterval((2, 10)), False),
))
def test_bool(number_range, empty):
    assert bool(IntInterval(number_range)) != empty


@mark.parametrize(('number_range', 'coerced_value'), (
    ([5, 5], 5),
    ([2, 2], 2),
))
def test_int_with_single_point_interval(number_range, coerced_value):
    assert int(IntInterval(number_range)) == coerced_value
Exemplo n.º 25
0
 def test_add_operator(self, first, second, result):
     assert first + second == IntInterval(result)
Exemplo n.º 26
0
def test_bool(number_range, empty):
    assert bool(IntInterval(number_range)) != empty
Exemplo n.º 27
0
 def test_isub_operator(self):
     range_ = IntInterval([1, 3])
     range_ -= IntInterval([1, 2])
     assert range_ == IntInterval([-1, 2])
Exemplo n.º 28
0
def test_int_with_single_point_interval(number_range, coerced_value):
    assert int(IntInterval(number_range)) == coerced_value
Exemplo n.º 29
0
class TestArithmeticFunctions(object):
    @mark.parametrize(('first', 'second', 'result'), (
        (IntInterval([1, 3]), IntInterval([1, 2]), [1, 2]),
        (IntInterval([-2, 2]), 1, [-2, 1])
    ))
    def test_glb(self, first, second, result):
        assert first.glb(second) == IntInterval(result)

    @mark.parametrize(('first', 'second', 'result'), (
        (IntInterval([1, 3]), IntInterval([1, 2]), [1, 3]),
        (IntInterval([-2, 2]), 1, [1, 2])
    ))
    def test_lub(self, first, second, result):
        assert first.lub(second) == IntInterval(result)

    @mark.parametrize(('first', 'second', 'result'), (
        (IntInterval([1, 3]), IntInterval([1, 2]), [1, 2]),
        (IntInterval([-2, 2]), 1, [1, 1])
    ))
    def test_inf(self, first, second, result):
        assert first.inf(second) == IntInterval(result)

    @mark.parametrize(('first', 'second', 'result'), (
        (IntInterval([1, 3]), IntInterval([1, 2]), [1, 3]),
        (IntInterval([-2, 2]), 1, [-2, 2])
    ))
    def test_sup(self, first, second, result):
        assert first.sup(second) == IntInterval(result)
Exemplo n.º 30
0
class TestBinaryOperators(object):
    @mark.parametrize(('interval1', 'interval2', 'result'), (
        ('(2, 3]', '[3, 4)', IntInterval([3, 3])),
        ('(2, 10]', '[3, 40]', IntInterval([3, 10])),
        ('[0, 0]', '[0, 0]', IntInterval.from_string('[0, 0]')),
        ('[0, 0]', '(0, 0]', IntInterval.from_string('(0, 0]')),
        ('[0, 0)', '[0, 0]', IntInterval.from_string('[0, 0)')),
        ('(2, 2]', '(2, 2]', IntInterval.from_string('(2, 2]')),
        ('[2, 2)', '[2, 2)', IntInterval.from_string('[2, 2)')),
        ('(2, 3)', '[3, 4]', IntInterval.from_string('[3, 3)')),
        ('[2, 3]', '(3, 4)', IntInterval.from_string('(3, 3]')),
    ))
    def test_and_operator(
        self,
        interval1,
        interval2,
        result
    ):
        assert (
            IntInterval.from_string(interval1) &
            IntInterval.from_string(interval2) ==
            result
        )

    @mark.parametrize(('interval1', 'interval2'), (
        ('[2, 2]', '[5, 7)'),
        ('(2, 3]', '[4, 40]'),
        ('(2, 3)', '(3, 4)'),
    ))
    def test_and_operator_with_illegal_arguments(self, interval1, interval2):
        with raises(IllegalArgument):
            (
                IntInterval.from_string(interval1) &
                IntInterval.from_string(interval2)
            )

    @mark.parametrize(('interval1', 'interval2'), (
        ('[2, 2]', '[5, 7)'),
        ('(2, 3]', '[4, 40]'),
        ('(2, 3)', '(3, 4)'),
    ))
    def test_or_operator_with_illegal_arguments(self, interval1, interval2):
        with raises(IllegalArgument):
            (
                IntInterval.from_string(interval1) |
                IntInterval.from_string(interval2)
            )

    @mark.parametrize(('interval1', 'interval2', 'result'), (
        ('(2, 3]', '[3, 4)', IntInterval.from_string('(2, 4)')),
        ('(2, 10]', '[3, 40]', IntInterval.from_string('(2, 40]')),
        ('[0, 0]', '[0, 0]', IntInterval.from_string('[0, 0]')),
        ('[0, 0]', '(0, 0]', IntInterval.from_string('[0, 0]')),
        ('[0, 0)', '[0, 0]', IntInterval.from_string('[0, 0]')),
        ('(2, 2]', '(2, 2]', IntInterval.from_string('(2, 2]')),
        ('[2, 2)', '[2, 2)', IntInterval.from_string('[2, 2)')),
        ('(2, 3)', '[3, 4]', IntInterval.from_string('(2, 4]')),
        ('[2, 3]', '(3, 4)', IntInterval.from_string('[2, 4)')),
    ))
    def test_or_operator(
        self,
        interval1,
        interval2,
        result
    ):
        assert (
            IntInterval.from_string(interval1) |
            IntInterval.from_string(interval2) ==
            result
        )