def test_registry_add_only_accept_integers():
    from binlog.registry import Registry

    r = Registry()

    with pytest.raises(TypeError):
        r.add(None)
def test_registry_multiplesegment_invert(values):
    r = Registry()

    for point in values:
        r.add(point)

    assert (~~r).acked == r.acked
def test_registry_add_starts_range(data):
    from binlog.registry import Registry

    r = Registry()

    assert r.add(data)
    assert r.acked == list([(data, data)])
def test_registry_double_add():
    from binlog.registry import Registry

    r = Registry()

    assert r.add(0)
    assert not r.add(0)
def test_registry_is_iterator(points):
    from binlog.registry import Registry

    r = Registry()
    for p in points:
        r.add(p)

    assert list(r) == sorted(points)
def test_registry_contains(data, point):
    from binlog.registry import Registry

    r = Registry()

    for i in data:
        r.add(i)

    assert (point in r) == (point in data)
def test_registry_is_always_sorted(data):
    from binlog.registry import Registry

    r = Registry()

    for i in data:
        r.add(i)

    assert list(r.acked) == sorted(r.acked)  # sorted always returns list
def test_registries_support_intersection__same_point():
    from binlog.registry import Registry, S

    registry_a = Registry(acked=list([S(0, 0)]))
    registry_b = Registry(acked=list([S(0, 0)]))

    registry_x1 = registry_a & registry_b
    registry_x2 = registry_b & registry_a

    assert registry_x1.acked == registry_x2.acked == list([S(0, 0)])
def test_registries_support_intersection__start_and_end_are_consecutive():
    from binlog.registry import Registry, S

    registry_a = Registry(acked=list([S(0, 10)]))
    registry_b = Registry(acked=list([S(11, 20)]))

    registry_x1 = registry_a & registry_b
    registry_x2 = registry_b & registry_a

    assert registry_x1.acked == registry_x2.acked == list([])
def test_registries_support_intersection__overlap_segments():
    from binlog.registry import Registry, S

    registry_a = Registry(acked=list([S(0, 10)]))
    registry_b = Registry(acked=list([S(5, 20)]))

    registry_x1 = registry_a & registry_b
    registry_x2 = registry_b & registry_a

    assert registry_x1.acked == registry_x2.acked == list([S(5, 10)])
def test_registries_support_union_one_point():
    from binlog.registry import Registry, S

    registry_a = Registry(acked=list([S(0, 0)]))
    registry_b = Registry()

    registry_x1 = registry_a | registry_b
    registry_x2 = registry_b | registry_a

    assert registry_x1.acked == registry_x2.acked == list([S(0, 0)])
def test_registries_support_union__bigger_segment():
    from binlog.registry import Registry, S

    registry_a = Registry(acked=list([S(0, 10)]))
    registry_b = Registry(acked=list([S(5, 8)]))

    registry_x1 = registry_a | registry_b
    registry_x2 = registry_b | registry_a

    assert registry_x1.acked == registry_x2.acked == list([S(0, 10)])
def test_registry_add_randomized_range(data):
    from binlog.registry import Registry

    r = Registry()

    l = list(range(data, data + 101)) * random.randint(1, 3)
    random.shuffle(l)

    for i in l:
        r.add(i)

    assert (data, data + 100) in r.acked
def test_registryiterseek_backward_threesegments_seek(a, b, c, d, e, f, seeks):
    assume(len(set([a, b, c, d, e, f])) == 6)
    a, b, c, d, e, f = sorted([a, b, c, d, e, f])
    assume(b + 1 < c and d + 1 < e)

    from binlog.registry import RegistryIterSeek

    r = RegistryIterSeek(Registry([S(a, b), S(c, d), S(e, f)]),
                         direction=Direction.B)

    for s in list(seeks):
        r.seek(s)
        if s < a:
            with pytest.raises(StopIteration):
                next(r)
        elif a <= s <= b or c <= s <= d or e <= s <= f:
            assert next(r) == s, s
        elif b <= s <= c:
            assert next(r) == b, s
        elif d <= s <= e:
            assert next(r) == d, s
        elif s > f:
            assert next(r) == f, s
        else:
            assert False, "Non defined %d" % s
def test_registryiterseek_backward_onesegment_unset(a, b):
    assume(len(set([a, b])) == 2)
    a, b = sorted([a, b])

    from binlog.registry import RegistryIterSeek

    r = RegistryIterSeek(Registry([S(a, b)]), direction=Direction.B)

    assert list(r) == list(chain(range(b, a - 1, -1)))
def test_registryiterseek_forward_twosegments_unset(a, b, c, d):
    assume(len(set([a, b, c, d])) == 4)
    a, b, c, d = sorted([a, b, c, d])
    assume(b + 1 < c)

    from binlog.registry import RegistryIterSeek

    r = RegistryIterSeek(Registry([S(a, b), S(c, d)]))

    assert list(r) == list(chain(range(a, b + 1), range(c, d + 1)))
def test_registryiterseek_forward_onesegment_unset(a, b):
    assume(len(set([a, b])) == 2)

    from binlog.registry import RegistryIterSeek

    a, b = sorted([a, b])

    r = RegistryIterSeek(Registry([S(a, b)]))

    assert list(r) == list(chain(range(a, b + 1)))
def test_registryiterseek_backward_twosegments_unset(a, b, c, d):
    assume(len(set([a, b, c, d])) == 4)
    a, b, c, d = sorted([a, b, c, d])
    assume(b + 1 < c)

    from binlog.registry import RegistryIterSeek

    r = RegistryIterSeek(Registry([S(a, b), S(c, d)]), direction=Direction.B)

    assert list(r) == list(chain(range(d, c - 1, -1), range(b, a - 1, -1)))
def test_registryiterseek_forward_threesegments_unset(a, b, c, d, e, f):
    assume(len(set([a, b, c, d, e, f])) == 6)
    a, b, c, d, e, f = sorted([a, b, c, d, e, f])
    assume(b + 1 < c and d + 1 < e)

    from binlog.registry import RegistryIterSeek

    r = RegistryIterSeek(Registry([S(a, b), S(c, d), S(e, f)]))

    assert list(r) == list(
        chain(range(a, b + 1), range(c, d + 1), range(e, f + 1)))
def test_registryiterseek_backward_threesegments_unset(a, b, c, d, e, f):
    assume(len(set([a, b, c, d, e, f])) == 6)
    a, b, c, d, e, f = sorted([a, b, c, d, e, f])
    assume(b + 1 < c and d + 1 < e)

    from binlog.registry import RegistryIterSeek

    r = RegistryIterSeek(Registry([S(a, b), S(c, d), S(e, f)]),
                         direction=Direction.B)

    assert list(r) == list(
        chain(range(f, e - 1, -1), range(d, c - 1, -1), range(b, a - 1, -1)))
def test_registry_onesegment_invert(a, b):
    start, end = min(a, b), max(a, b)

    r = ~Registry([S(start, end)])

    if start == S.MIN:
        if end == S.MIN:
            assert r.acked == [S(start + 1, S.MAX)]
        elif end == S.MAX:
            assert r.acked == []
    elif end == S.MAX:
        assert r.acked == [S(S.MIN, start - 1)]
    else:
        assert r.acked == [S(S.MIN, start - 1), S(end + 1, S.MAX)]
def test_registry_add_consecutive_left_side(data):
    from binlog.registry import Registry

    r = Registry()

    r.add(data)
    r.add(data - 1)

    assert (data - 1, data) in r.acked
def test_registry_add_consecutive_left_and_right(data):
    from binlog.registry import Registry

    r = Registry()

    r.add(data - 1)
    r.add(data + 1)
    r.add(data)

    assert (data - 1, data + 1) in r.acked
def test_registry_add_non_consecutive(data):
    from binlog.registry import Registry

    r = Registry()

    r.add(0)
    assert (0, 0) in r.acked

    r.add(data)
    assert (data, data) in r.acked
def test_registryiterseek_forward_onesegment_seek(a, b, seeks):
    assume(len(set([a, b])) == 2)
    a, b = sorted([a, b])

    from binlog.registry import RegistryIterSeek

    r = RegistryIterSeek(Registry([S(a, b)]))

    for s in list(seeks):
        r.seek(s)
        if s < a:
            assert next(r) == a, s
        elif a <= s <= b:
            assert next(r) == s, s
        elif s > b:
            with pytest.raises(StopIteration):
                next(r)
        else:
            assert False, "Non defined %d" % s
def test_registries_support_intersection(data_a, data_b):
    from binlog.registry import Registry

    registry_a = Registry()
    registry_b = Registry()

    for p in data_a:
        registry_a.add(p)

    for p in data_b:
        registry_b.add(p)

    registry_x = registry_a & registry_b

    ack_points = data_a & data_b
    non_ack_points = set(range(0, 20)) - ack_points

    for p in ack_points:
        assert p in registry_x

    for p in non_ack_points:
        assert p not in registry_x
def test_registryiterseek_forward_twosegments_seek(a, b, c, d, seeks):
    assume(len(set([a, b, c, d])) == 4)
    a, b, c, d = sorted([a, b, c, d])
    assume(b + 1 < c)

    from binlog.registry import RegistryIterSeek

    r = RegistryIterSeek(Registry([S(a, b), S(c, d)]))

    for s in list(seeks):
        r.seek(s)
        if s < a:
            assert next(r) == a, s
        elif a <= s <= b or c <= s <= d:
            assert next(r) == s, s
        elif b <= s <= c:
            assert next(r) == c, s
        elif s > d:
            with pytest.raises(StopIteration):
                next(r)
        else:
            assert False, "Non defined %d" % s
def test_registryiterseek_backward_empty_seek(seek_to):
    from binlog.registry import RegistryIterSeek

    r = RegistryIterSeek(Registry(), direction=Direction.B)
    r.seek(seek_to)
    assert not list(r)
def test_registry_empty_invert():
    r = ~Registry()
    assert r.acked == [S(S.MIN, S.MAX)]
def test_registryiterseek_forward_empty_seek(seek_to):
    from binlog.registry import RegistryIterSeek

    r = RegistryIterSeek(Registry())
    r.seek(seek_to)
    assert not list(r)