Example #1
0
def do_scramble(items, tfb, dbt):
    import itertools
    for c in itertools.permutations(items):
        cf = ChainFinder()
        load_items(cf, c)
        assert cf.trees_from_bottom == tfb
        assert cf.descendents_by_top == dbt
        cf = ChainFinder()
        for b in c:
            load_items(cf, [b])
        assert cf.trees_from_bottom == tfb
        assert cf.descendents_by_top == dbt
Example #2
0
def test_branch():
    cf = ChainFinder()
    assert cf.trees_from_bottom == {}
    assert cf.descendents_by_top == {}
    ITEMS = [BHO(i) for i in range(7)]
    B301 = BHO(301, 3, 10)
    B302, B303, B304 = [BHO(i) for i in range(302, 305)]

    load_items(cf, [B302])
    assert cf.trees_from_bottom == {302: [302, 301]}
    assert cf.descendents_by_top == {301: {302}}

    load_items(cf, [B304])
    assert cf.trees_from_bottom == {302: [302, 301], 304: [304, 303]}
    assert cf.descendents_by_top == {301: {302}, 303: {304}}

    load_items(cf, [B303])
    assert cf.trees_from_bottom == {304: [304, 303, 302, 301]}
    assert cf.descendents_by_top == {301: {304}}

    load_items(cf, ITEMS)
    assert cf.trees_from_bottom == {
        6: [6, 5, 4, 3, 2, 1, 0, -1],
        304: [304, 303, 302, 301]
    }
    assert cf.descendents_by_top == {-1: {6}, 301: {304}}

    load_items(cf, [B301])
    assert cf.trees_from_bottom == {
        6: [6, 5, 4, 3, 2, 1, 0, -1],
        304: [304, 303, 302, 301, 3, 2, 1, 0, -1]
    }
    assert cf.descendents_by_top == {-1: {6, 304}}
Example #3
0
def test_basics():
    cf = ChainFinder()
    assert cf.trees_from_bottom == {}
    assert cf.descendents_by_top == {}
    ITEMS = [BHO(i) for i in range(6)]

    load_items(cf, [ITEMS[0]])
    assert cf.trees_from_bottom == {0: [0, -1]}
    assert cf.descendents_by_top == {-1: {0}}

    load_items(cf, [ITEMS[1]])
    assert cf.trees_from_bottom == {1: [1, 0, -1]}
    assert cf.descendents_by_top == {-1: {1}}

    load_items(cf, ITEMS[0:2])
    assert cf.trees_from_bottom == {1: [1, 0, -1]}
    assert cf.descendents_by_top == {-1: {1}}

    load_items(cf, [ITEMS[4]])
    assert cf.trees_from_bottom == {1: [1, 0, -1], 4: [4, 3]}
    assert cf.descendents_by_top == {-1: {1}, 3: {4}}

    load_items(cf, [ITEMS[3]])
    assert cf.trees_from_bottom == {1: [1, 0, -1], 4: [4, 3, 2]}
    assert cf.descendents_by_top == {-1: {1}, 2: {4}}

    load_items(cf, [ITEMS[5]])
    assert cf.trees_from_bottom == {1: [1, 0, -1], 5: [5, 4, 3, 2]}
    assert cf.descendents_by_top == {-1: {1}, 2: {5}}

    load_items(cf, [ITEMS[2]])
    assert cf.trees_from_bottom == {5: [5, 4, 3, 2, 1, 0, -1]}
    assert cf.descendents_by_top == {-1: {5}}

    do_scramble(ITEMS, cf.trees_from_bottom, cf.descendents_by_top)
Example #4
0
def test_all_orphans():
    I1 = BHO(1)
    I2 = BHO(2)
    I3 = BHO(3)
    cf = ChainFinder()
    load_items(cf, [I2, I3, I1])
    assert cf.trees_from_bottom == {3: [3, 2, 1, 0]}
    assert cf.descendents_by_top == {0: {3}}
Example #5
0
    def test_find_ancestral_path(self):

        ITEMS = [BHO(i) for i in range(5)]
        B201 = BHO(201, 2, 110)
        B202, B203, B204 = [BHO(i) for i in range(202, 205)]

        cf = ChainFinder()
        items = ITEMS + [B202, B203, B204]
        load_items(cf, items)

        load_items(cf, [B201])

        old_chain_endpoint, new_chain_endpoint = 4, 204

        old_subpath, new_subpath = cf.find_ancestral_path(old_chain_endpoint, new_chain_endpoint)
        assert old_subpath == [4, 3, 2]
        assert new_subpath == [204, 203, 202, 201, 2]
Example #6
0
def test_0123():
    I0 = BHO(0)
    I1 = BHO(1)
    I2 = BHO(2)
    I3 = BHO(3, 1)
    cf = ChainFinder()
    load_items(cf, [I0, I2, I3, I1])
    assert cf.trees_from_bottom == {2: [2, 1, 0, -1], 3: [3, 1, 0, -1]}
    assert cf.descendents_by_top == {-1: {2, 3}}
Example #7
0
def test_branch_switch():
    cf = ChainFinder()
    assert cf.trees_from_bottom == {}
    assert cf.descendents_by_top == {}
    ITEMS = [BHO(i) for i in range(4)]
    B201 = BHO(201, 2, 10)
    B202, B203, B204 = [BHO(i) for i in range(202, 205)]

    items = ITEMS + [B201, B202, B203, B204]
    tfb = {204: [204, 203, 202, 201, 2, 1, 0, -1], 3: [3, 2, 1, 0, -1]}
    dbt = {-1: {3, 204}}
    do_scramble(items, tfb, dbt)
Example #8
0
def test_longest_chain_endpoint():
    cf = ChainFinder()
    ITEMS = [BHO(i) for i in range(5)]
    B201 = BHO(201, 2, 110)
    B202, B203, B204 = [BHO(i) for i in range(202, 205)]

    def node_weight_f(h):
        if h == -1:
            return 0
        if h == 201:
            return 110
        return 10

    items = ITEMS + [B201, B202, B203, B204]
    load_items(cf, items)
Example #9
0
def test_large():
    ITEMS = [BHO(i) for i in range(10000)]
    cf = ChainFinder()
    load_items(cf, ITEMS)
    old_subpath, new_subpath = cf.find_ancestral_path(5000, 9000)
Example #10
0
 def test_large(self):
     ITEMS = [BHO(i) for i in range(10000)]
     cf = ChainFinder()
     load_items(cf, ITEMS)
     old_subpath, new_subpath = cf.find_ancestral_path(5000, 9000)