Esempio n. 1
0
def test_inequalities():
    assert Interval(1, 3) == (1, 3)
    assert (1, 3) == Interval(1, 3)

    assert Interval(1, 3) < (4, 6)
    assert not Interval(1, 3) < (3, 6)
    assert not Interval(1, 3) < (-3, -1)
    assert Interval(1, 3) <= (3, 6)
    assert Interval(1, 3) <= (2, 6)
    assert Interval(1, 3) <= (1, 6)
    assert Interval(3, 5) <= (1, 6)
    assert not Interval(1, 3) <= (-3, -1)
    assert not Interval(3, 6) <= Interval.open(1, 6)

    assert Interval(1, 3) < Interval.empty()
    assert Interval(1, 3) <= Interval.empty()

    assert Interval(7, 9) > (4, 6)
    assert not Interval(7, 9) > (4, 7)
    assert not Interval(7, 9) > (10, 12)
    assert Interval(7, 9) >= (4, 7)
    assert Interval(7, 9) >= (4, 8)
    assert Interval(7, 9) >= (4, 9)
    assert not Interval(7, 9) >= (10, 12)
    assert not Interval(4, 10) >= Interval.open(4, 9)

    assert Interval(7, 9) > Interval.empty()
    assert Interval(7, 9) >= Interval.empty()
Esempio n. 2
0
def test_equals():
    d = Interval(1, 3)
    assert d.equals((1, 3))
    assert not d.equals(None)
    assert not d.equals(Interval.closed_open(1, 3))
    assert Interval.empty().equals(Interval.empty())
    # Empty intervals are always equal
    assert Interval.open(1, 1).equals(Interval.open(2, 2)) 
    assert Interval.infinite().equals(Interval.infinite())
Esempio n. 3
0
def test_span_interval():
    assert Duration('1h').span_interval(Interval.open(3600, 2 * 3600),
                                        start_open=False) == span(
                                            3600, 2 * 3600, start_open=False)
    assert Duration('1h').span_interval(Interval.open(3600, 2 * 3600),
                                        start_open=True) == span(
                                            3600, 2 * 3600, start_open=True)
    assert Duration('1h').span_interval(Interval.closed(3600, 2 * 3600),
                                        start_open=False) == span(
                                            3600, 3 * 3600, start_open=False)
    assert Duration('1h').span_interval(Interval.closed(3600, 2 * 3600),
                                        start_open=True) == span(
                                            0, 2 * 3600, start_open=True)
Esempio n. 4
0
def test_reset_open_interval():
    points = [(0, 1), (1, 5), (2, 3)]
    pf = Points(points, uniform=False)
    assert pf(1) == 5
    pf.reset(domain=Interval.open(0, 2))
    assert pf(0) == 1
    assert pf(1) == 2
    assert pf(2) == 3
Esempio n. 5
0
def test_intersects():
    assert Interval.closed(1, 3).intersects(Interval.closed(2, 3))
    assert Interval.closed(1, 3).intersects((2, 3))
    assert Interval.closed(1, 3).intersects((1, 3))
    assert Interval.closed(1, 3).intersects(Interval.open(1, 3))

    assert Interval.closed(1, 3).intersects(Interval.closed(3, 4))
    assert not Interval.closed(1, 3).intersects(Interval.open(3, 4))
    assert not Interval.open(1, 3).intersects(Interval.closed(3, 4))

    assert Interval.point(3).intersects(Interval.closed(3, 4))
    assert Interval.point(3).intersects(Interval.closed(1, 3))
    assert not Interval.point(3).intersects(Interval.open(3, 4))
    assert not Interval.point(3).intersects(Interval.open(1, 3))

    assert Interval.closed(1, 3).intersects(Interval.closed(0, 1))
    assert not Interval.closed(1, 3).intersects(Interval.open(0, 1))
    assert not Interval.open(1, 3).intersects(Interval.closed(0, 1))

    assert not Interval.closed(1, 3).intersects(Interval.closed(4, 5))
    assert not Interval.closed(1, 3).intersects(Interval.closed(-2, 0))

    assert not Interval.closed(1, 3).intersects(Interval.empty())
    assert Interval.closed(1, 3).intersects(Interval.infinite())

    assert not Interval.point(1).intersects(Interval.open_closed(1, 2))
Esempio n. 6
0
def test_interval_indexes():
    points = Points([(0, 0), (1, 1), (2, 2), (3, 3)])

    i0, i1 = points._domain_indexes(Interval.closed(-2, -1))
    assert i0 == i1

    i0, i1 = points._domain_indexes(Interval.closed(-2, 0))
    assert i0 == 0
    assert i1 == 1

    i0, i1 = points._domain_indexes(Interval.open(-2, 0))
    assert i0 == i1

    i0, i1 = points._domain_indexes(Interval.closed(0, 0))
    assert i0 == 0
    assert i1 == 1

    i0, i1 = points._domain_indexes(Interval.open(0, 1))
    assert i0 == i1

    i0, i1 = points._domain_indexes(Interval.closed(0, 1))
    assert i0 == 0
    assert i1 == 2

    i0, i1 = points._domain_indexes(Interval.closed(2, 3))
    assert i0 == 2
    assert i1 == 4

    i0, i1 = points._domain_indexes(Interval.open(2, 3))
    assert i0 == 3
    assert i1 == 3

    i0, i1 = points._domain_indexes(Interval.closed(3, 3))
    assert i0 == 3
    assert i1 == 4

    i0, i1 = points._domain_indexes(Interval.open(3, 3))
    assert i0 == i1

    i0, i1 = points._domain_indexes(Interval.closed(3, 4))
    assert i0 == 3
    assert i1 == 4

    i0, i1 = points._domain_indexes(Interval.open(3, 4))
    assert i0 == i1
Esempio n. 7
0
def test_sample():
    original_quotes_points = Quote.mock_ohlcv_points([(1, 2.2, 0.9, 2),
                                                      (2, 3.1, 1.9, 3),
                                                      (3, 5.1, 2.9, 5)],
                                                     t_start=10,
                                                     t_step=1)
    original_quotes = [p[1] for p in original_quotes_points]
    f = Quotes(1, quote_points=original_quotes_points)

    quotes = f.sample()
    assert quotes == original_quotes

    quotes = f.sample(domain=Interval.open(11, 12))
    assert quotes == original_quotes[1:2]
Esempio n. 8
0
def test_extensions():
    d = Interval(1, 3)
    assert d.get_lte().equals(Interval.lte(3))
    assert d.get_gte().equals(Interval.gte(1))
    assert d.get_lt().equals(Interval.lt(1))
    assert d.get_gt().equals(Interval.gt(3))

    d = Interval.open(1, 3)
    assert d.get_lte().equals(Interval.lt(3))
    assert d.get_gte().equals(Interval.gt(1))
    assert d.get_lt().equals(Interval.lte(1))
    assert d.get_gt().equals(Interval.gte(3))

    d = Interval.empty()
    assert d.get_lte().is_empty
    assert d.get_gte().is_empty
    assert d.get_lt().is_empty
    assert d.get_gt().is_empty
Esempio n. 9
0
def test_count():
    assert Duration('1h').count(Interval.open(2 * 3600, 4 * 3600),
                                start_open=False) == 2
Esempio n. 10
0
def test_cast():
    assert bool(Interval.empty()) is False
    assert bool(Interval(0, 0)) is True
    assert list(Interval.empty()) == []
    assert list(Interval(0, 0)) == [0, 0]
    assert list(Interval.open(1, 20)) == [1, 20]
Esempio n. 11
0
def test_arithmetic():
    assert Interval(1, 3) + (2, 4) == (1, 4)
    assert (1, 3) + Interval(2, 4) == (1, 4)

    assert Interval.open(1, 3) + (2, 4) == Interval.open_closed(1, 4)
    assert (1, 3) + Interval.open(2, 4) == Interval.closed_open(1, 4)