CS2613/tests/Q3/test_prefix.py
2022-11-23 10:09:24 -04:00

42 lines
1.3 KiB
Python

from prefix import with_prefix
words=["apple","baby","abba"]
a_words=["apple", "abba"]
def test_simple():
assert list(with_prefix(["a"],words)) == [a_words]
def test_order():
assert list(with_prefix(["b","a"],words)) == [["baby"], a_words]
def test_multi():
assert list(with_prefix(["bb","ab"],words)) == [[],["abba"]]
# Commented out because the solution I am submitting is not using regex
#def test_regex1():
# assert list(with_prefix(["[a-z]b"], words)) == [ ["abba"] ]
#def test_regex2():
# assert list(with_prefix([".*a$"], words)) == [ ["abba"] ]
def test_gen():
gen = with_prefix(["b"],words)
assert next(gen) == ["baby"]
def test_gen2(): #Testing out of order prefixes, with generator syntax
gen = with_prefix(["b", "a"], words)
assert next(gen) == ["baby"]
assert next(gen) == ["apple", "abba"]
def test_gen3(): #Testing out returning the same number of elements as words, out of order
gen = with_prefix(["bab", "abb", "app"], words)
assert next(gen) == ["baby"]
assert next(gen) == ["abba"]
assert next(gen) == ["apple"]
words2 = ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "z"]
def test_gen4(): #Testing a long word and one letter word
gen = with_prefix(["a", "z"], words2)
assert next(gen) == ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"]
assert next(gen) == ["z"]