def test___getitem__(): """Test indexing for ValueSet object.""" def test_TypeError(valueset, index): "test TypeError raising for indexing." with pytest.raises(TypeError): valueset._values[index] def test_IndexError(valueset, index): "test IndexError raising for indexing." with pytest.raises(IndexError): valueset._values[index] v = ValueSet([1, 2, 'a', 'b', False, True, Interval(100, 1000), Point(1.0)]) test_TypeError(v, '') test_IndexError(v, 8) assert v[0] == 1 assert v[0] is v._values[0] assert v[1] == 2 assert v[1] is v._values[1] assert v[2] == 'a' assert v[2] is v._values[2] assert v[3] == 'b' assert v[3] is v._values[3] assert v[4] is False assert v[4] is v._values[4] assert v[5] is True assert v[5] is v._values[5] assert v[6] == Interval(100, 1000) assert v[6] is v._values[6] assert v[7] == Point(1.0) assert v[7] is v._values[7]
def test___eq__(): """Test == operator.""" VS1, VS2 = ValueSet([]), ValueSet([]) assert VS1 == VS2 VS1, VS2 = ValueSet([1]), ValueSet([1]) assert VS1 == VS2 # Test out of order VS1, VS2 = ValueSet([1, 3, 5]), ValueSet([5, 3, 1]) assert VS1 == VS2 VS1 = ValueSet([1, 'b', 3, 'c', 'a', 5]) VS2 = ValueSet([5, 3, 1, 'a', 'b', 'c']) assert VS1 == VS2 VS1 = ValueSet([1, 'b', 3, 'c', 'a', 5, Interval(1, 10), Point(1.0)]) VS2 = ValueSet([Interval(1, 10), 5, 3, 1, Point(1.0), 'a', 'b', 'c']) assert VS1 == VS2 VS1 = ValueSet([Point(1.0, 1.0)]) VS2 = ValueSet([Point(1.0)]) assert not VS1 == VS2 # test type mismatch for Interval VS1, VS2 = ValueSet([Interval(1, 10)]), ValueSet([Interval(1.0, 10.0)]) assert VS1 != VS2 VS1, VS2 = ValueSet([LineSegment(Point(1.0), Point(2.0))]), ValueSet([LineSegment(Point(1.0), Point(2.0))]) assert VS1 == VS2 VS1, VS2 = ValueSet([LineSegment(Point(1.0), Point(2.0))]), ValueSet([LineSegment(Point(-1.0), Point(-2.0))]) assert VS1 != VS2
def test___iter__(): """Test iterator for ValueSet object.""" v = ValueSet([Interval(100, 105), 5, 3, 1, 'a', Interval(2.0, 10.0), 'b', True, 'c', False, Point(1.0)]) assert [i for i in v.__iter__()] == v._values
def test___len__(): """Test len() function for ValueSet object.""" v = ValueSet([Interval(100, 105), 5, 3, 1, 'a', Interval(2.0, 10.0), 'b', True, 'c', False, Point(1.0)]) v2 = ValueSet([]) assert len(v) == 11 assert len(v2) == 0
def test___nonzero__(): """Test boolean behavior for ValueSet.""" v = ValueSet([Interval(100, 105), 5, 3, 1, 'a', Interval(2.0, 10.0), 'b', True, 'c', False, Point(1.0)]) assert v v2 = ValueSet([]) assert not v2
def test___deepcopy__(): """Test copy.deepcopy for ValueSet object.""" import copy v = ValueSet([1, 3, 5, 'a', 'b', 'c', False, True, Interval(100, 105), Interval(2.0, 10.0), Point(1.0)]) v_copy = copy.deepcopy(v) assert v == v_copy assert v is not v_copy assert v[8] is not v_copy[8] assert v[9] is not v_copy[9] assert v[10] is not v_copy[10]
def test___getitem__(): """Test indexing for Interval.""" def test_IndexError(interval, index): """Test IndexError catching.""" with pytest.raises(IndexError) as excinfo: interval[index] def test_TypeError(interval, index): """Test TypeError catching.""" with pytest.raises(TypeError) as excinfo: Interval[index] test_TypeError(Interval(0, 1), "") test_IndexError(Interval(0, 1), -1) test_IndexError(Interval(0, 1), 2)
def test___repr__(): """Test repr() for ValueSet object.""" v1 = ValueSet([1, 3, 5, 'a', 'b', 'c', False, True, Interval(100, 105), Interval(2.0, 10.0), Point(1.0)]) v2 = ValueSet([Interval(100, 105), 5, 3, 1, Point(1.0), 'a', Interval(2.0, 10.0), 'b', True, 'c', False]) v3 = ValueSet([]) assert v1.__repr__() == "V(1, 3, 5, a, b, c, False, True, I(100, 105), I(2.0, 10.0), P(1.0))" # test out of order assert v2.__repr__() == "V(1, 3, 5, a, b, c, False, True, I(100, 105), I(2.0, 10.0), P(1.0))" # test empty assert v3.__repr__() == "V()"
def test_discretize(): """Test Interval - discrete list conversion.""" i = Interval(0, 10) assert i.discretize() == [v for v in range(i[0], i[1] + 1)] assert i.discretize(2) == [0, 2, 4, 6, 8, 10] assert i.discretize(3) == [0, 3, 6, 9] i = Interval(0.0, 2.0) assert i.discretize() == [0.0, 1.0, 2.0] assert i.discretize(.5) == [0.0, 0.50, 1.0, 1.5, 2.0] i = Interval(0L, 2L) assert i.discretize() == [0L, 1L, 2L] assert i.discretize(2L) == [0L, 2L]
def test___deepcopy__(): """Test copy.deepcopy for Interval object.""" from copy import deepcopy # test ints i = Interval(0, 1) i_copy = deepcopy(i) assert i == i_copy assert i_copy is not deepcopy(i) # test floats i = Interval(0.0, 1.0) i_copy = deepcopy(i) assert i == i_copy assert i_copy is not deepcopy(i) # test longs i = Interval(0L, 1L) i_copy = deepcopy(i) assert i == i_copy assert i_copy is not deepcopy(i)
def test___contains__(): """Test in operator for ValueSet object.""" v = ValueSet([1, 2, 'a', 'b', False, True, Interval(100, 1000), Point(1.0), LineSegment(Point(0.0), Point(1.0))]) assert 1 in v assert 2 in v assert 'a' in v assert 'b' in v assert False in v assert True in v assert LineSegment(Point(0.0), Point(1.0)) in v assert not LineSegment(Point(10.0), Point(1.0)) in v assert Interval(100, 1000) in v assert Point(1.0) in v assert not Interval(400, 500) in v assert not Interval(100.0, 1000.0) in v assert not Interval(100L, 1000L) in v assert not Point('x') in v
def test___init__(): """Test ValueSet Constructor""" def test_TypeError(valueset): """Test an individual ValueSet construction.""" with pytest.raises(TypeError) as excinfo: ValueSet(valueset) # test value_set errors test_TypeError(1) test_TypeError(1.0) test_TypeError("") test_TypeError(object) test_TypeError(Interval(0, 10)) test_TypeError(Point(0.0)) VS1 = ValueSet([Interval(20, 100)]) VS = ValueSet([LineSegment(Point(1.0), Point(2.0)), LineSegment(Point(1.0), Point(2.0))]) assert VS == ValueSet([LineSegment(Point(1.0), Point(2.0))])
def test___repr__(): """Test str(Context).""" ahead_rs = RelationSymbol('Ahead', 4) behind_rs = RelationSymbol('Behind', 4) pm_rs = RelationSymbol('PM', 1) am_rs = RelationSymbol('AM', 1) vocabulary = Vocabulary(['C1', 'C2'], [ahead_rs, behind_rs, pm_rs, am_rs], ['V1', 'V2']) f = Formula(vocabulary, 'Ahead', 'C1', 'V1') a = Attribute('hour', [Interval(0, 23)]) a2 = Attribute('minute', [Interval(0, 59)]) r_pm = Relation('R1(h1) <=> h1 > 11', ['hour'], 1) r_am = Relation('R2(h1) <=> h1 <= 11', ['hour'], 2) r_ahead = Relation( 'R3(h1,m1,hhh2,mm2) <=> h1 > hhh2 or (h1 = hhh2 and m1 > mm2)', ['hour', 'minute', 'hour', 'minute'], 3) r_behind = Relation( 'R4(h1,m1,h2,m2) <=> h1 <= h2 or (h1 = h2 and m1 < m2)', ['hour', 'minute', 'hour', 'minute'], 4) attribute_structure = AttributeStructure(a, a2, r_ahead, r_behind, r_pm, r_am) objects = ['s1', 's2'] attribute_system = AttributeSystem(attribute_structure, objects) p = ConstantAssignment(vocabulary, attribute_system, { 'C1': 's1', 'C2': 's2' }) assumption_base = AssumptionBase(f) named_state = NamedState(attribute_system, p) C = Context(assumption_base, named_state) assert repr(C) == "hour(s1): {V(I(0, 23))}\n" + \ "hour(s2): {V(I(0, 23))}\n" + \ "minute(s1): {V(I(0, 59))}\n" + \ "minute(s2): {V(I(0, 59))}\n" + \ "CA{'C2': 's2', 'C1': 's1'}\n" + \ "AB(Ahead(C1, V1))"
def test___setitem__(): """Test ValueSet[key] = value assignment for ValueSet object.""" def test_TypeError(valueset, key, value): """Test TypeError raising in index assignment.""" with pytest.raises(TypeError) as excinfo: valueset[key] = value def test_ValueError(valueset, key, value): """Test ValueError raising in index assignment.""" with pytest.raises(ValueError) as excinfo: valueset[key] = value def test_AttributeError(valueset, key, value): """Test AttributeError raising in index assignment.""" with pytest.raises(AttributeError) as excinfo: valueset[key] = value def test_IndexError(valueset, key, value): """Test IndexError raising in index assignment.""" with pytest.raises(IndexError) as excinfo: valueset[key] = value v = ValueSet([1, 3, 5, 'a', 'b', 'c', False, True, Interval(100, 105), Interval(2.0, 10.0), Point(1.0)]) v[0] = 1000 assert v[0] == 1000 # test invalid key types test_TypeError(v, '', 1) test_TypeError(v, object, 1) test_TypeError(v, None, 1) test_TypeError(v, 1.0, 1) test_TypeError(v, 1L, 1) # test duplicate value catching test_ValueError(v, 1, 1) test_IndexError(v, 11, -37) test_TypeError(v, 1, object)
def test___deepcopy__(): """Test copy.deepcopy for Context object.""" ahead_rs = RelationSymbol('Ahead', 4) behind_rs = RelationSymbol('Behind', 4) pm_rs = RelationSymbol('PM', 1) am_rs = RelationSymbol('AM', 1) vocabulary = Vocabulary(['C1', 'C2'], [ahead_rs, behind_rs, pm_rs, am_rs], ['V1', 'V2']) f = Formula(vocabulary, 'Ahead', 'C1', 'V1') a = Attribute('hour', [Interval(0, 23)]) a2 = Attribute('minute', [Interval(0, 59)]) r_pm = Relation('R1(h1) <=> h1 > 11', ['hour'], 1) r_am = Relation('R2(h1) <=> h1 <= 11', ['hour'], 2) r_ahead = Relation( 'R3(h1,m1,hhh2,mm2) <=> h1 > hhh2 or (h1 = hhh2 and m1 > mm2)', ['hour', 'minute', 'hour', 'minute'], 3) r_behind = Relation( 'R4(h1,m1,h2,m2) <=> h1 <= h2 or (h1 = h2 and m1 < m2)', ['hour', 'minute', 'hour', 'minute'], 4) attribute_structure = AttributeStructure(a, a2, r_ahead, r_behind, r_pm, r_am) objects = ['s1', 's2'] attribute_system = AttributeSystem(attribute_structure, objects) p = ConstantAssignment(vocabulary, attribute_system, { 'C1': 's1', 'C2': 's2' }) assumption_base = AssumptionBase(f) named_state = NamedState(attribute_system, p) C = Context(assumption_base, named_state) from copy import deepcopy C_copy = deepcopy(C)
def test_collapse_intervals(): """Test Interval collapsing.""" intervals = [ Interval(-10, 0), Interval(-1, 10), Interval(-4, 5), Interval(9, 13), Interval(11, 15), Interval(-4, 5), Interval(-100, -99), Interval(-10.0, 0.0), Interval(-1.0, 10.0), Interval(-4.0, 5.0), Interval(9.0, 13.0), Interval(11.0, 15.0), Interval(-4.0, 5.0), Interval(-100.0, -99.0), Interval(-10L, 0L), Interval(-1L, 10L), Interval(-4L, 5L), Interval(9L, 13L), Interval(11L, 15L), Interval(-4L, 5L), Interval(-100L, -99L)] out = [ Interval(-100, -99), Interval(-10, 15), Interval(-100.0, -99.0), Interval(-10.0, 15.0), Interval(-100L, -99L), Interval(-10L, 15L)] assert Interval.collapse_intervals(intervals) == out
def test__split_by_types(): """Test split_by_types used in parsing.""" def test_AttributeError(values): """Test AttributeError raising in split_by_types.""" with pytest.raises(AttributeError) as excinfo: ValueSet._split_by_types(values) def test_TypeError(values): """Test TypeError raising in split_by_types.""" with pytest.raises(TypeError) as excinfo: ValueSet._split_by_types(values) # test error raising i = Interval(1, 10) i._is_different_object = True ValueSet.add_object_type("_is_different_object") test_AttributeError([i]) test_TypeError([object]) # test output types = ValueSet._split_by_types( [1, 2, 1.0, 1.5, 1L, 2L, 'a', 'b', True, False, Interval(0, 10), Interval(0.0, 10.0), Interval(0L, 10L), Point(1.0), Point(1.0, 1.0), Point('x'), LineSegment(Point(0.0), Point(1.0)), LineSegment(Point("x", "x", "x"), Point("x", "x", "x"))]) d = { int: [1, 2], float: [1.0, 1.5], long: [1L, 2L], str: ['a', 'b'], bool: [True, False], "_is_Interval": [Interval(0, 10), Interval(0.0, 10.0), Interval(0L, 10L)], "_is_Point": [Point(1.0), Point(1.0, 1.0), Point('x')], "_is_LineSegment": [LineSegment(Point(0.0), Point(1.0)), LineSegment(Point("x", "x", "x"), Point("x", "x", "x"))]} empty = ValueSet._split_by_types([]) assert empty == {} assert d == types
def test_collapse_intervals(): """Test Interval collapsing.""" intervals = [ Interval(-10, 0), Interval(-1, 10), Interval(-4, 5), Interval(9, 13), Interval(11, 15), Interval(-4, 5), Interval(-100, -99), Interval(-10.0, 0.0), Interval(-1.0, 10.0), Interval(-4.0, 5.0), Interval(9.0, 13.0), Interval(11.0, 15.0), Interval(-4.0, 5.0), Interval(-100.0, -99.0), Interval(-10L, 0L), Interval(-1L, 10L), Interval(-4L, 5L), Interval(9L, 13L), Interval(11L, 15L), Interval(-4L, 5L), Interval(-100L, -99L) ] out = [ Interval(-100, -99), Interval(-10, 15), Interval(-100.0, -99.0), Interval(-10.0, 15.0), Interval(-100L, -99L), Interval(-10L, 15L) ] assert Interval.collapse_intervals(intervals) == out
def test___repr__(): """Test repr() of Interval object.""" assert repr(Interval(0, 1)) == "I(0, 1)" assert repr(Interval(0.0, 1.0)) == "I(0.0, 1.0)" assert repr(Interval(0L, 1L)) == "I(0, 1)"
def test___hash__(): """Test hash of Interval.""" assert hash(Interval(0, 1)) == 3713080549409410656 assert hash(Interval(0.0, 1.0)) == 3713080549409410656 assert hash(Interval(0L, 1L)) == 3713080549409410656
def test__key(): """Test key used for hashing; just 2-tuple.""" assert Interval(0, 1)._key() == (0, 1) assert Interval(0.0, 1.0)._key() == (0.0, 1.0) assert Interval(0L, 1L)._key() == (0L, 1L)
def test___lt__(): """Test < operator for Interval; [i, s](i' s').""" # test standard less than assert Interval(-1, 0) < Interval(1, 2) assert Interval(-1.0, 0.0) < Interval(1.0, 2.0) assert Interval(-1L, 0L) < Interval(1L, 2L) # test strictly less than assert not Interval(-1, 0) < Interval(0, 1) assert not Interval(-1.0, 0.0) < Interval(0.0, 1.0) assert not Interval(-1L, 0L) < Interval(0L, 1L) # test not less than assert not Interval(8, 9) < Interval(0, 1) assert not Interval(8.0, 9.0) < Interval(0.0, 1.0) assert not Interval(8L, 9L) < Interval(0L, 1L)
def test___eq__(): """Test == operator for Interval object.""" assert Interval(0, 1) == Interval(0, 1) assert Interval(0.0, 1.0) == Interval(0.0, 1.0) assert Interval(0L, 1L) == Interval(0L, 1L) assert not Interval(0, 1) == Interval(0.0, 1.0) assert not Interval(0, 1) == Interval(0L, 1L) assert not Interval(0.0, 1.0) == Interval(0, 1) assert not Interval(0.0, 1.0) == Interval(0L, 1L) assert not Interval(0L, 1L) == Interval(0, 1) assert not Interval(0L, 1L) == Interval(0.0, 1.0)
def test___contains__(): """Test in operator for Interval.""" def test_TypeError(interval, key): """Test TypeError catching.""" with pytest.raises(TypeError) as excinfo: key in interval # test standard greater than or equal to assert Interval(3, 8) not in Interval(0, 5) assert Interval(3.0, 8.0) not in Interval(0.0, 5.0) assert Interval(3L, 8L) not in Interval(0L, 5L) # test [i s](i' s') assert Interval(-5, -3) not in Interval(0, 5) assert Interval(-5.0, -3.0) not in Interval(0.0, 5.0) assert Interval(-5L, -3L) not in Interval(0L, 5L) # test (i' s')[i s] assert Interval(90, 100) not in Interval(8, 9) assert Interval(90.0, 100.0) not in Interval(8.0, 9.0) assert Interval(90L, 100L) not in Interval(8L, 9L) # test [i (i' s] s') assert Interval(-2, 3) not in Interval(0, 5) assert Interval(-2.0, 3.0) not in Interval(0.0, 5.0) assert Interval(-2L, 3L) not in Interval(0L, 5L) # test [i (i' s') s] assert Interval(-5, 10) not in Interval(0, 5) assert Interval(-5.0, 10.0) not in Interval(0.0, 5.0) assert Interval(-5L, 10L) not in Interval(0L, 5L) # test [i' (i s) s'] assert Interval(-5, 10) in Interval(-10, 15) assert Interval(-5.0, 10.0) in Interval(-10.0, 15.0) assert Interval(-5L, 10L) in Interval(-10L, 15L) # test (i', s') == (i, s) assert Interval(-10, 15) in Interval(-10, 15) assert Interval(-10.0, 15.0) in Interval(-10.0, 15.0) assert Interval(-10L, 15L) in Interval(-10L, 15L) # test type mismatch test_TypeError(1.0, Interval(6, 7)) test_TypeError(1L, Interval(6, 7)) test_TypeError(1, Interval(6.0, 7.0)) test_TypeError(1L, Interval(6.0, 7.0)) test_TypeError(1.0, Interval(6L, 7L)) test_TypeError(1, Interval(6L, 7L))
def test___and__(): """Test & operator for Interval.""" def test_ValueError(i1, i2): """Test ValueError catching.""" with pytest.raises(ValueError) as excinfo: i1 & i2 i1, i2 = Interval(-200, 1000), Interval(50, 100) assert i1 & i2 == Interval(50, 100) i1, i2 = Interval(0, 10), Interval(50, 100) test_ValueError(i1, i2) i1, i2 = Interval(0, 55), Interval(50, 100) assert i1 & i2 == Interval(50, 55) i1, i2 = Interval(70, 80), Interval(50, 100) assert i1 & i2 == Interval(70, 80) i1, i2 = Interval(75, 125), Interval(50, 100) assert i1 & i2 == Interval(75, 100) i1, i2 = Interval(105, 125), Interval(50, 100) test_ValueError(i1, i2)
def test___ne__(): """Test != operator for Interval object.""" assert not Interval(0, 1) != Interval(0, 1) assert not Interval(0.0, 1.0) != Interval(0.0, 1.0) assert not Interval(0L, 1L) != Interval(0L, 1L) assert Interval(0, 1) != Interval(0.0, 1.0) assert Interval(0, 1) != Interval(0L, 1L) assert Interval(0.0, 1.0) != Interval(0, 1) assert Interval(0.0, 1.0) != Interval(0L, 1L) assert Interval(0L, 1L) != Interval(0, 1) assert Interval(0L, 1L) != Interval(0.0, 1.0)
def test_TypeError(inf, sup): """Test TypeError catching.""" with pytest.raises(TypeError) as excinfo: Interval(inf, sup)
def test_ValueError(inf, sup): """Test ValueError catching.""" with pytest.raises(ValueError) as excinfo: Interval(inf, sup)
def test___repr__(): """ Test repr().""" from vivid.classes.interval import Interval a = Attribute("label", ["1", Interval(40, 50), True, 4.0, 6L]) print a.__repr__() assert a.__repr__() == "\"label: {4.0,6,1,True,I(40, 50)}\""
def test___ge__(): """Implement overloaded >= operator for Interval; (i',[i, s'), s]""" # test standard greater than or equal to assert Interval(3, 8) >= Interval(0, 5) assert Interval(3.0, 8.0) >= Interval(0.0, 5.0) assert Interval(3L, 8L) >= Interval(0L, 5L) # test [i s](i' s') assert not Interval(-5, -3) >= Interval(0, 5) assert not Interval(-5.0, -3.0) >= Interval(0.0, 5.0) assert not Interval(-5L, -3L) >= Interval(0L, 5L) # test (i' s')[i s] assert not Interval(90, 100) >= Interval(8, 9) assert not Interval(90.0, 100.0) >= Interval(8.0, 9.0) assert not Interval(90L, 100L) >= Interval(8L, 9L) # test [i (i' s] s') assert not Interval(-2, 3) >= Interval(0, 5) assert not Interval(-2.0, 3.0) >= Interval(0.0, 5.0) assert not Interval(-2L, 3L) >= Interval(0L, 5L) # test [i (i' s') s] assert not Interval(-5, 10) >= Interval(0, 5) assert not Interval(-5.0, 10.0) >= Interval(0.0, 5.0) assert not Interval(-5L, 10L) >= Interval(0L, 5L) # test [i' (i s) s'] assert not Interval(-5, 10) >= Interval(-10, 15) assert not Interval(-5.0, 10.0) >= Interval(-10.0, 15.0) assert not Interval(-5L, 10L) >= Interval(-10L, 15L) # test (i', s') == (i, s) assert not Interval(-10, 15) >= Interval(-10, 15) assert not Interval(-10.0, 15.0) >= Interval(-10.0, 15.0) assert not Interval(-10L, 15L) >= Interval(-10L, 15L)
def test___gt__(): """Test > operator for Interval; (i' s')[i, s].""" # test standard less than assert Interval(1, 2) > Interval(-1, 0) assert Interval(1.0, 2.0) > Interval(-1.0, 0.0) assert Interval(1L, 2L) > Interval(-1L, 0L) # test strictly greater than assert not Interval(0, 1) > Interval(-1, 0) assert not Interval(0.0, 1.0) > Interval(-1.0, 0.0) assert not Interval(0L, 1L) > Interval(-1L, 0L) # test not greater than assert not Interval(0, 1) > Interval(8, 9) assert not Interval(0.0, 1.0) > Interval(8.0, 9.0) assert not Interval(0L, 1L) > Interval(8L, 9L)