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___repr__(): """Test repr(LineSegment).""" p1, p2 = Point(1.0), Point(2.0) p3, p4 = Point(1.0, 1.0, 1.0), Point(2.0, 2.0, 2.0) a = LineSegment(p1, p2) b = LineSegment(p3, p4) assert repr(a) == "L(P(1.0),P(2.0))" assert repr(b) == "L(P(1.0,1.0,1.0),P(2.0,2.0,2.0))"
def test_unstringify(): """Test unstringify function for LineSegment.""" p1, p2 = Point(1.0), Point(2.0) p3, p4 = Point(1.0, 1.0, 1.0), Point(2.0, 2.0, 2.0) a = LineSegment(p1, p2) b = LineSegment(p3, p4) assert a == LineSegment.unstringify("L(P(1.0),P(2.0))") assert b == LineSegment.unstringify("L(P(1.0,1.0,1.0),P(2.0,2.0,2.0))")
def test_meets(): """Test meets function used in LRB paper.""" spacetime_loc = Point(0.0, 0.0, 0.0, 0.0) worldline_1_start = Point(-10.0, -10.0, -10.0, -10.0) worldline_1_end = Point(10.0, 10.0, 10.0, 10.0) worldline_2_start = Point(10.0, 10.0, 10.0, 10.0) worldline_2_end = Point(-10.0, -10.0, -10.0, -10.0) assert spacetime_loc.meets( worldline_1_start, worldline_1_end, worldline_2_start, worldline_2_end)
def test_can_observe(): """Test can_observe function used in LRB paper.""" point = Point(1.0, 1.0, 1.0, 1.0) spacetime_loc = Point(0.0, 0.0, 0.0, 0.0) worldline_start = Point(-10.0, -10.0, -10.0, -10.0) worldline_end = Point(10.0, 10.0, 10.0, 10.0) assert point.can_observe(spacetime_loc, worldline_start, worldline_end) spacetime_loc = Point(1.0, 1.0, 1.0, 1.0) assert point.can_observe(spacetime_loc, worldline_start, worldline_end)
def test___deepcopy__(): """Test copy.deepcopy.""" from copy import deepcopy a = LineSegment(Point(1.0), Point(2.0)) b = deepcopy(a) assert a == b assert a is not b assert a[0] is not b[0] assert a[1] is not b[1]
def test___init__(): """Test LineSegment constructor.""" def test_ValueError(start_point, end_point): """Test constructor for ValueErrors with given params.""" with pytest.raises(ValueError) as excinfo: LineSegment(start_point, end_point) test_ValueError(None, None) test_ValueError(None, object) test_ValueError(object, object) test_ValueError(Point(1.0), None) test_ValueError(Point(1.0), Point('x')) test_ValueError(Point(1.0), Point(1.0, 1.0))
def test___deepcopy__(): """Test copy.deepcopy for Point object.""" from copy import deepcopy p1 = Point(1.0) p2 = Point(1.0, 2.0) p1_copy = deepcopy(p1) p2_copy = deepcopy(p2) assert p1 == p1_copy assert p1 is not p1_copy assert p2 == p2_copy assert p2 is not p2_copy
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_meets(): """Test meets wrapper for Point function.""" a = LineSegment(Point(0.0, 0.0), Point(5.0, 5.0)) b = LineSegment(Point(0.0, 0.0), Point(5.0, 5.0)) p1 = Point(0.0, 0.0) p2 = Point(1.0, 1.0) p3 = Point(5.0, 5.0) assert LineSegment.meets(p1, a, b) assert LineSegment.meets(p2, a, b) assert LineSegment.meets(p3, a, b) c = LineSegment(Point(5.0, 0.0), Point(0.0, 5.0)) p4 = Point(2.5, 2.5) assert not LineSegment.meets(p1, a, c) assert not LineSegment.meets(p2, a, c) assert not LineSegment.meets(p3, a, c) assert LineSegment.meets(p4, a, c)
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___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___eq__(): """Test == operator for Point object.""" p1 = Point(1.0) p2 = Point(1.0) p3 = Point(1.0, 2.0, 3.0) p4 = Point(1.0, 2.0, 3.0) p5 = Point('x') p6 = Point('x') p7 = Point('x', 'x', 'x') p8 = Point('x', 'x', 'x') assert p1 == p2 assert p3 == p4 assert p5 == p6 assert p7 == p8
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___getitem__(): """Test indexing.""" def test_TypeError(index, segment): """Test constructor for TypeErrors with given params.""" with pytest.raises(TypeError) as excinfo: segment[index] def test_IndexError(index, segment): """Test constructor for IndexErrors with given params.""" with pytest.raises(IndexError) as excinfo: segment[index] p1, p2 = Point(1.0), Point(2.0) a = LineSegment(p1, p2) assert a[0] == p1 assert a[1] == p2 test_TypeError(a, a) test_TypeError('', a) test_TypeError(None, a) test_TypeError(object, a) test_IndexError(-1, a) test_IndexError(2, a)
def test_not_same_point(): """Test not_same_point function used in LRB paper.""" point_1 = Point(1.0) point_2 = Point(0.0) assert point_1.not_same_point(point_2) point_1 = Point(1.0, 1.0) point_2 = Point(0.0, 0.0) assert point_1.not_same_point(point_2) point_1 = Point(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0) point_2 = Point(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0) assert point_1.not_same_point(point_2) point_1 = Point(1.000000000000001) point_2 = Point(0.000000000000001) assert point_1.not_same_point(point_2)
def test_unstringify(): """Test Point object reconstruction.""" def test_ValueError(point_string): """Test constructor for ValueErrors with given params.""" with pytest.raises(ValueError) as excinfo: Point.unstringify(point_string) test_ValueError('asdf') test_ValueError('P()') test_ValueError('P(xx)') test_ValueError('P(,2)') p1 = Point('x') p2 = Point('x', 'x') p3 = Point(1.0) p4 = Point(1.0, 1.0) assert Point.unstringify(str(p1)) == Point('x') assert Point.unstringify(str(p2)) == Point('x', 'x') assert Point.unstringify(str(p3)) == Point(1.0) assert Point.unstringify(str(p4)) == Point(1.0, 1.0)
def test_clocks_unequal(): """Test clocks_unequal function used in LRB paper.""" point_1 = Point(1.0, 1.0) point_2 = Point(0.0, 0.0) assert point_1.clocks_unequal(point_2) point_1 = Point(1.0, 1.0, 1.0, 1.0) point_2 = Point(0.0, 0.0, 0.0, 0.0) assert point_1.clocks_unequal(point_2) point_1 = Point(0.0, 1.000000000000001) point_2 = Point(0.0, 0.000000000000001) assert point_1.clocks_unequal(point_2)
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___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___init__(): """Test Point constructor.""" def test_TypeError(*dimension_values): """Test constructor for TypeErrors with given params.""" with pytest.raises(TypeError) as excinfo: Point(*dimension_values) def test_ValueError(*dimension_values): """Test constructor for ValueErrors with given params.""" with pytest.raises(ValueError) as excinfo: Point(*dimension_values) test_ValueError() test_TypeError(1) test_TypeError('') test_TypeError(None) test_TypeError(object) coords = (1.0, 2.0, 3.0) p = Point(*coords) assert p._coordinate is not coords
def test_total_ordering(): """Test .""" a = LineSegment(Point(1.0), Point(2.0)) b = LineSegment(Point(0.0), Point(2.0)) c = LineSegment(Point(1.0), Point(3.0)) d = LineSegment(Point(0.0), Point(3.0)) e = LineSegment(Point('x'), Point('x')) assert a <= a assert not a < a assert a >= a assert not a > a assert a <= b assert a < b assert a <= c assert a < c assert a <= d assert a < d assert a <= e assert a < e assert b >= a assert b > a assert c >= a assert c > a assert d >= a assert d > a assert e >= a assert e > a assert a < b < d < e a = LineSegment(Point(1.0, 1.0, 1.0, 1.0), Point(2.0, 2.0, 2.0, 2.0)) b = LineSegment(Point(0.0, 0.0, 0.0, 0.0), Point(2.0, 2.0, 2.0, 2.0)) c = LineSegment(Point(1.0, 1.0, 1.0, 1.0), Point(3.0, 3.0, 3.0, 3.0)) d = LineSegment(Point(0.0, 0.0, 0.0, 0.0), Point(3.0, 3.0, 3.0, 3.0)) e = LineSegment(Point('x', 'x', 'x', 'x'), Point('x', 'x', 'x', 'x')) assert a <= a assert not a < a assert a >= a assert not a > a assert a <= b assert a < b assert a <= c assert a < c assert a <= d assert a < d assert a <= e assert a < e assert b >= a assert b > a assert c >= a assert c > a assert d >= a assert d > a assert e >= a assert e > a assert a < b < d < e a = LineSegment(Point(1.0, 1.0), Point(2.0, 2.0)) b = LineSegment(Point(0.0, 1.0), Point(2.0, 2.0)) assert not a < b assert not a > b assert not a <= b assert not a >= b
def test___ne__(): """Test != operator.""" assert not LineSegment(Point(1.0), Point(1.0)) != LineSegment(Point(1.0), Point(1.0)) assert LineSegment(Point(1.0), Point(1.0)) != LineSegment(Point('x'), Point('x')) assert LineSegment(Point(1.0), Point(1.0)) != LineSegment(Point(1.0, 1.0), Point(1.0, 1.0))
def test___eq__(): """Test == operator.""" assert LineSegment(Point(1.0), Point(1.0)) == LineSegment(Point(1.0), Point(1.0)) assert not LineSegment(Point(1.0), Point(1.0)) == LineSegment(Point('x'), Point('x')) assert not LineSegment(Point(1.0), Point(1.0)) == LineSegment(Point(1.0, 1.0), Point(1.0, 1.0))
def test__key(): """Test key used in hashing.""" a = LineSegment(Point(1.0), Point(2.0)) assert a._key() == (Point(1.0), Point(2.0))
def test___hash__(): """Test hash(LineSegment).""" p1, p2 = Point(1.0), Point(2.0) a = LineSegment(p1, p2) assert hash(a) == 2847735836036288514
def test___contains__(): """Test in operator.""" def test_AttributeError(member, segment): """Test constructor for AttributeErrors with given params.""" with pytest.raises(AttributeError) as excinfo: member in segment a = LineSegment(Point(1.0), Point(2.0)) b = LineSegment(Point(0.0), Point(2.0)) c = LineSegment(Point(1.0), Point(3.0)) d = LineSegment(Point(0.0), Point(3.0)) e = LineSegment(Point('x'), Point('x')) p = Point(1.5) test_AttributeError('', a) test_AttributeError(None, a) test_AttributeError(object, a) assert a in a assert a in b assert b not in c assert a in c assert a in d assert a in e assert p in a assert p in b assert p in c assert p in d assert p in e a = LineSegment(Point(1.0, 1.0, 1.0), Point(2.0, 2.0, 2.0)) b = LineSegment(Point(0.0, 0.0, 0.0), Point(2.0, 2.0, 2.0)) c = LineSegment(Point(1.0, 1.0, 1.0), Point(3.0, 3.0, 3.0)) d = LineSegment(Point(0.0, 0.0, 0.0), Point(3.0, 3.0, 3.0)) e = LineSegment(Point('x', 'x', 'x'), Point('x', 'x', 'x')) p = Point(1.5, 1.5, 1.5) assert a in a assert a in b assert b not in c assert a in c assert a in d assert a in e assert p in a assert p in b assert p in c assert p in d assert p in e