Esempio n. 1
0
def test_shared_suffix():
    st = gwrite(enlist("blowing blue glowing"))

    gr = greader(st)
    cur1 = fst.Cursor(gr)
    cur2 = fst.Cursor(gr)

    cur1.find_path(b("blo"))
    cur2.find_path(b("glo"))
    assert cur1.stack[-1].target == cur2.stack[-1].target
Esempio n. 2
0
def test_words():
    words = enlist("alfa alpaca amtrak bellow fellow fiona zebulon")
    with TempStorage() as st:
        gwrite(words, st)
        gr = greader(st)
        cur = fst.Cursor(gr)
        assert list(cur.flatten_strings()) == words
        gr.close()
Esempio n. 3
0
def test_fields():
    with TempStorage() as st:
        f = st.create_file("test")
        gw = fst.GraphWriter(f)
        gw.start_field("f1")
        gw.insert("a")
        gw.insert("aa")
        gw.insert("ab")
        gw.finish_field()
        gw.start_field("f2")
        gw.insert("ba")
        gw.insert("baa")
        gw.insert("bab")
        gw.close()

        gr = fst.GraphReader(st.open_file("test"))
        cur1 = fst.Cursor(gr, gr.root("f1"))
        cur2 = fst.Cursor(gr, gr.root("f2"))
        assert list(cur1.flatten_strings()) == ["a", "aa", "ab"]
        assert list(cur2.flatten_strings()) == ["ba", "baa", "bab"]
        gr.close()
Esempio n. 4
0
def _fst_roundtrip(domain, t):
    with TempStorage() as st:
        f = st.create_file("test")
        gw = fst.GraphWriter(f, vtype=t)
        gw.start_field("_")
        for key, value in domain:
            gw.insert(key, value)
        gw.finish_field()
        gw.close()

        f = st.open_file("test")
        gr = fst.GraphReader(f, vtype=t)
        cur = fst.Cursor(gr)
        assert list(cur.flatten_v()) == domain
        f.close()
Esempio n. 5
0
def test_inactive_raise():
    st = gwrite(enlist("alfa bravo charlie"))
    cur = fst.Cursor(greader(st))
    while cur.is_active():
        cur.next_arc()
    pytest.raises(fst.InactiveCursor, cur.label)
    pytest.raises(fst.InactiveCursor, cur.prefix)
    pytest.raises(fst.InactiveCursor, cur.prefix_bytes)
    pytest.raises(fst.InactiveCursor, list, cur.peek_key())
    pytest.raises(fst.InactiveCursor, cur.peek_key_bytes)
    pytest.raises(fst.InactiveCursor, cur.stopped)
    pytest.raises(fst.InactiveCursor, cur.value)
    pytest.raises(fst.InactiveCursor, cur.accept)
    pytest.raises(fst.InactiveCursor, cur.at_last_arc)
    pytest.raises(fst.InactiveCursor, cur.next_arc)
    pytest.raises(fst.InactiveCursor, cur.follow)
    pytest.raises(fst.InactiveCursor, cur.switch_to, b("a"))
    pytest.raises(fst.InactiveCursor, cur.skip_to, b("a"))
    pytest.raises(fst.InactiveCursor, list, cur.flatten())
    pytest.raises(fst.InactiveCursor, list, cur.flatten_v())
    pytest.raises(fst.InactiveCursor, list, cur.flatten_strings())
    pytest.raises(fst.InactiveCursor, cur.find_path, b("a"))
Esempio n. 6
0
def test_random():
    def randstring():
        length = random.randint(1, 5)
        a = array("B", (random.randint(0, 255) for _ in xrange(length)))
        return array_tobytes(a)

    keys = sorted(randstring() for _ in xrange(100))

    with TempStorage() as st:
        gwrite(keys, st)
        gr = greader(st)
        cur = fst.Cursor(gr)
        s1 = cur.flatten()
        s2 = sorted(set(keys))
        for i, (k1, k2) in enumerate(zip(s1, s2)):
            assert k1 == k2, "%s: %r != %r" % (i, k1, k2)

        sample = list(keys)
        random.shuffle(sample)
        for key in sample:
            cur.reset()
            cur.find_path(key)
            assert cur.prefix_bytes() == key
        gr.close()
Esempio n. 7
0
def test_duplicate_keys():
    st = gwrite(enlist("alfa bravo bravo bravo charlie"))
    cur = fst.Cursor(greader(st))
    assert list(cur.flatten_strings()) == ["alfa", "bravo", "charlie"]