def test_creation_errors(): with pytest.raises( ArithmeticError, match= "`width` of interval must number higher or at least equal to 0."): IntervalFactory.midpoint_width(0, -1) with pytest.raises( ValueError, match= "The interval is invalid. `minimum` must be lower or equal to `maximum`" ): IntervalFactory.infimum_supremum(3, 1) with pytest.raises( ValueError, match="`precision` should be value from range `0` to `15`"): Interval(1, 3, precision=-1) Interval(1, 3, precision=16) with pytest.warns(UserWarning, match="Using default value of precision"): Interval(1, 3, precision="pre") with pytest.raises(ValueError, match="Cannot parse Interval from this definition"): IntervalFactory.parse_string("[1, 2.5, 5]") with pytest.raises(ValueError, match="Cannot parse Interval from this definition"): IntervalFactory.parse_string("[]") with pytest.raises(ValueError, match="Cannot parse Interval from this definition"): IntervalFactory.parse_string("[\"aa\", \"b\"]")
def test_union(i_a, i_b, i_c): assert i_a.union(i_b) == IntervalFactory.two_values(1, 5) assert i_b.union(i_c) == IntervalFactory.two_values(2, 7) with pytest.raises(ArithmeticError, match="do not intersect"): i_a.union(i_c)
def test_get_alpha_cut(fn_a: FuzzyNumber): assert fn_a.get_alpha_cut(0) == IntervalFactory.two_values(1, 3) assert fn_a.get_alpha_cut(0.25) == IntervalFactory.two_values(1.25, 2.75) assert fn_a.get_alpha_cut(0.5) == IntervalFactory.two_values(1.5, 2.5) assert fn_a.get_alpha_cut(0.75) == IntervalFactory.two_values(1.75, 2.25) assert fn_a.get_alpha_cut(1) == IntervalFactory.two_values(2, 2)
def test_mid_point(): a = IntervalFactory.two_values(1, 2) assert a.mid_point == 1.5 b = IntervalFactory.midpoint_width(2, 2) assert b.mid_point == 2
def test_intersection(i_a: Interval, i_b: Interval, i_c: Interval): assert i_a.intersection(i_b) == IntervalFactory.two_values(2, 3) assert i_b.intersection(i_c) == IntervalFactory.two_values(4, 5) with pytest.raises(ArithmeticError, match="do not intersect"): i_a.intersection(i_c)
def test_alpha_cuts(fn_a: FuzzyNumber): intervals = [ IntervalFactory.infimum_supremum(1, 3), IntervalFactory.infimum_supremum(2, 2) ] assert intervals == fn_a.alpha_cuts
def test_pow(i_a: Interval, i_d: Interval): assert i_a**2 == IntervalFactory.two_values(1, 9) assert i_a**3 == IntervalFactory.two_values(1, 27) assert i_d**2 == IntervalFactory.two_values(0, 9) assert i_d**3 == IntervalFactory.two_values(-8, 27)
def test_mul(i_a: Interval, i_b: Interval): assert i_a * 2 == IntervalFactory.two_values(i_a.min * 2, i_a.max * 2) assert 2 * i_a == IntervalFactory.two_values(i_a.min * 2, i_a.max * 2) assert i_a * i_b == IntervalFactory.two_values(i_a.min * i_b.min, i_a.max * i_b.max) with pytest.raises(TypeError, match="multiply sequence"): i_a * "str" "str" * i_a
def test_sub(i_a: Interval, i_b: Interval): assert i_a - 1 == IntervalFactory.two_values(i_a.min - 1, i_a.max - 1) assert 1 - i_a == IntervalFactory.two_values(1 - i_a.min, 1 - i_a.max) assert i_a - i_b == IntervalFactory.two_values(i_a.min - i_b.max, i_a.max - i_b.min) with pytest.raises(TypeError, match="unsupported operand"): i_a - "str" "str" - i_a
def test_creation_errors(): with pytest.raises(TypeError, match="must be a list"): FuzzyNumber(1, []) with pytest.raises(TypeError, match="must be a list"): FuzzyNumber([], 1) with pytest.raises( ValueError, match="Lists `alphas` and `alpha_cuts` must be of same length."): FuzzyNumber([1, 2, 3], [1, 2]) with pytest.raises(ValueError, match="All elements of `alphas` must be from range"): FuzzyNumber([0, 0.5, 1, 1.1], [None] * 4) with pytest.raises(ValueError, match="All elements of `alphas` must be int or float"): FuzzyNumber([0, 0.5, 1, "1.1"], [None] * 4) with pytest.raises(ValueError, match="Values in `alphas` are not unique"): FuzzyNumber([0, 0.5, 1, 0.5], [None] * 4) with pytest.raises(ValueError, match="`alphas` must contain both 0 and 1 alpha value"): FuzzyNumber([0, 0.5, 0.9], [None] * 3) with pytest.raises(ValueError, match="`alphas` must contain both 0 and 1 alpha value"): FuzzyNumber([0.1, 0.5, 1], [None] * 3) with pytest.raises(TypeError, match="All elements of `alpha_cuts` must be Interval"): FuzzyNumber([0, 1], [IntervalFactory.two_values(0, 1), 5]) with pytest.raises( ValueError, match= "Interval on lower alpha level has to contain the higher level alpha cuts" ): FuzzyNumber([0, 1], [ IntervalFactory.two_values(0, 1), IntervalFactory.two_values(2, 2) ]) with pytest.raises(ValueError, match="The fuzzy number is invalid"): FuzzyNumberFactory.triangular(5, 4, 3) with pytest.raises(ValueError, match="The fuzzy number is invalid"): FuzzyNumberFactory.trapezoidal(5, 1, 4, 3)
def test_add(i_a: Interval, i_b: Interval, i_e: Interval): assert i_a + 1 == IntervalFactory.two_values(i_a.min + 1, i_a.max + 1) assert 1 + i_a == IntervalFactory.two_values(i_a.min + 1, i_a.max + 1) assert i_a + i_b == IntervalFactory.two_values(i_a.min + i_b.min, i_a.max + i_b.max) assert i_a + i_e == IntervalFactory.two_values(i_a.min + i_e.min, i_a.max + i_e.max) with pytest.raises(TypeError, match="unsupported operand"): i_a + "str" "str" + i_a
def test_contains(): interval = IntervalFactory.infimum_supremum(1, 5) assert 2 in interval assert 1 in interval assert 3.59 in interval assert (0.999999 in interval) is False assert (5.0001 in interval) is False assert (IntervalFactory.two_values(6, 7) in interval) is False assert (IntervalFactory.two_values(0, 3) in interval) is False assert IntervalFactory.two_values(2, 4) in interval with pytest.raises(TypeError, match="Cannot test if object of type "): "str" in interval True in interval
def test_truediv(i_a: Interval, i_b: Interval, i_d: Interval): assert i_a / 2 == IntervalFactory.two_values(i_a.min / 2, i_a.max / 2) assert 2 / i_a == IntervalFactory.two_values(2 / i_a.min, 2 / i_a.max) assert i_a / i_b == IntervalFactory.two_values(i_a.min / i_b.max, i_a.max / i_b.min) with pytest.raises(TypeError, match="unsupported operand"): i_a / "str" with pytest.raises(ArithmeticError, match="Cannot divide by 0"): i_a / 0 with pytest.raises(ArithmeticError, match="Cannot divide by interval that contains `0`"): i_a / i_d
def test_contain(fn_a: FuzzyNumber): assert 2 in fn_a assert 1 in fn_a assert 1.1 in fn_a assert 2.9 in fn_a assert 3 in fn_a assert IntervalFactory.infimum_supremum(2.9, 3.1) in fn_a assert FuzzyNumberFactory.crisp_number(3) in fn_a assert (0.999 in fn_a) is False assert (3.001 in fn_a) is False with pytest.raises(TypeError, match="Cannot test if object of type"): "a" in fn_a
def test_creation(): assert isinstance(IntervalFactory.infimum_supremum(1, 3), Interval) assert isinstance(IntervalFactory.infimum_supremum(2, 5), Interval) assert isinstance(IntervalFactory.infimum_supremum(4, 7), Interval) assert isinstance(IntervalFactory.infimum_supremum(-2, 3), Interval) assert isinstance(IntervalFactory.infimum_supremum(-1, 1), Interval) assert isinstance(IntervalFactory.empty(), Interval) interval = IntervalFactory.two_values(1.0000000000001, 3.000000009, precision=2) assert interval == IntervalFactory.two_values(1, 3, precision=2) interval = IntervalFactory.parse_string("[1, 2.5]") assert interval.min == 1 assert interval.max == 2.5
def test_apply_function(i_a: Interval, i_b: Interval): # position argument not supported by the function with pytest.raises(TypeError, match="too many positional arguments"): i_a.apply_function(math.log2, 5) # keyword argument that does not exist with pytest.raises(TypeError, match="got an unexpected keyword argument 'c'"): i_a.apply_function(math.log2, c=5) assert i_a.apply_function(math.log2).min == pytest.approx( math.log2(i_a.min), 0.00001) assert i_a.apply_function(math.log2).max == pytest.approx( math.log2(i_a.max), 0.00001) assert i_b.apply_function(math.cos).min == pytest.approx(-1, 0.00001) assert i_b.apply_function(math.cos).max == pytest.approx(0.28366, 0.00001) assert i_a.apply_function( math.pow, 2, number_elements=10) == IntervalFactory.two_values(1, 9)
def test_union_hull(i_a: Interval, i_c: Interval): assert i_a.union_hull(i_c) == IntervalFactory.two_values(1, 7)
def test_neg(i_a: Interval, i_d: Interval): assert -i_a == IntervalFactory.two_values(i_a.min * (-1), i_a.max * (-1)) assert -i_d == IntervalFactory.two_values(i_d.min * (-1), i_d.max * (-1))
def test_degenerate_interval(): assert IntervalFactory.infimum_supremum(2, 2).degenerate assert IntervalFactory.infimum_supremum(2, 3).degenerate is False