Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #############################################################
- # FILE : check_wordsearch_full.py
- # WRITER : Daniel Kerbel
- # EXERCISE : intro2cs1 ex5 2018-2019
- # DESCRIPTION: Testing wordsearch
- #############################################################
- from wordsearch import *
- mat = [
- list("apple"),
- list("agodo"),
- list("nnert"),
- list("gaTAC"),
- list("micsr"),
- list("PoPoP")
- ]
- def test_find_words_in_matrix():
- words = ["apple", "mic", "nope", "PiT", "", "wut"]
- occurs = find_words_in_matrix(words, mat, EAST+NORTH_EAST)
- assert dict(occurs) == {
- "apple": 1,
- "mic": 1,
- "PiT": 1
- }
- # testing empty inputs/edge cases
- assert find_words_in_matrix(words, mat, "") == []
- assert find_words_in_matrix([], mat, EAST) == []
- assert find_words_in_matrix(words, [], EAST) == []
- assert find_words_in_matrix([], [], EAST) == []
- assert find_words_in_matrix([], [], "") == []
- def test_overlapping_words():
- words = ["pip"]
- matrix = [
- list("pipippip"),
- list("aiaiaiai"),
- list("xxpxpxpx")
- ]
- assert dict(find_words_in_matrix(words, matrix, EAST)) == {
- "pip": 3
- }
- assert dict(find_words_in_matrix(words, matrix, EAST+SOUTH_EAST)) == {
- "pip": 6
- }
- assert dict(find_words_in_matrix(words, matrix, EAST+NORTH_WEST)) == {
- "pip": 6
- }
- assert dict(find_words_in_matrix(words, matrix, EAST+SOUTH_EAST+WEST+NORTH_WEST)) == {
- "pip": 12
- }
- def test_singleletter_matrix():
- matrix = [["h"]]
- words = ["h", "irrelevant"]
- for direction in DIRECTIONS:
- assert dict(find_words_in_matrix(words, matrix, direction)) == {"h": 1}
- def test_singleaxis_matrix():
- matrix = [list("shalom")]
- words = ["shalom", "shal", "lom"]
- assert dict(find_words_in_matrix(words, matrix, EAST)) == {
- "shalom": 1, "shal": 1, "lom": 1
- }
- assert dict(find_words_in_matrix(words, matrix, SOUTH)) == {}
- assert dict(find_words_in_matrix(words, matrix, WEST+SOUTH_WEST + SOUTH_EAST)) == {}
- matrix = [["s"],
- ["h"],
- ["a"],
- ["l"],
- ["o"],
- ["m"]]
- assert dict(find_words_in_matrix(words, matrix, SOUTH)) == {
- "shalom": 1, "shal": 1, "lom": 1
- }
- assert dict(find_words_in_matrix(words, matrix, EAST)) == {}
- assert dict(find_words_in_matrix(words, matrix, WEST+SOUTH_WEST + SOUTH_EAST)) == {}
- def test_diagonals_edgecases():
- # empty matrix
- mat = []
- assert list(main_diagonals_iterator(mat)) ==\
- list(anti_diagonals_iterator(mat)) == []
- # single row matrix
- mat = [list("abcdefg")]
- expected_diags = set("abcdefg")
- assert_equal_char_iterators(expected_diags, main_diagonals_iterator(mat),
- main_diagonals_iterator(mat, invert=True),
- anti_diagonals_iterator(mat),
- anti_diagonals_iterator(mat, invert=True))
- # single column matrix
- mat = [["a"], ["b"], ["c"], ["d"], ["e"], ["f"], ["g"]]
- assert_equal_char_iterators(expected_diags, main_diagonals_iterator(mat),
- main_diagonals_iterator(mat, invert=True),
- anti_diagonals_iterator(mat),
- anti_diagonals_iterator(mat, invert=True))
- def test_main_diagonals():
- expected = { "ageAr", "porC", "pdt", "lo", "e",
- "anTsP", "naco", "giP", "mo", "P"}
- expected_inv = { "rAega", "Crop", "tdp", "ol", "e",
- "PsTna", "ocan", "Pig", "om", "P"}
- assert_equal_char_iterators(expected, main_diagonals_iterator(mat))
- assert_equal_char_iterators(expected_inv, main_diagonals_iterator(mat, invert=True))
- def test_anti_diagonals():
- expected = { "a", "pa", "pgn", "long", "edeam",
- "orTiP", "tAco", "CsP", "ro", "P"}
- expected_inv = { "a", "ap", "ngp", "gnol", "maede",
- "PiTro", "ocAt", "PsC", "or", "P"}
- assert_equal_char_iterators(expected, anti_diagonals_iterator(mat))
- assert_equal_char_iterators(expected_inv, anti_diagonals_iterator(mat, invert=True))
- def test_rows():
- expected = { "apple", "agodo", "nnert", "gaTAC", "micsr", "PoPoP"}
- assert_equal_char_iterators(expected, rows_iterator(mat))
- rev_expected = { "elppa", "odoga", "trenn", "CATag", "rscim", "PoPoP"}
- assert_equal_char_iterators(rev_expected, rows_iterator(mat, invert=True))
- def test_columns():
- expected = { "aangmP", "pgnaio", "poeTcP", "ldrAso", "eotCrP"}
- rev_expected = { "Pmgnaa", "oiangp", "PcTeop", "osArdl", "PrCtoe"}
- assert_equal_char_iterators(expected, columns_iterator(mat))
- assert_equal_char_iterators(rev_expected, columns_iterator(mat, invert=True))
- def filetester(direction):
- if (not os.access("mat.txt", os.R_OK) or
- not os.access("word_list.txt", os.R_OK) or
- not os.access(f"outfile_{direction}.txt", os.R_OK)):
- print("Skipping filetest '", direction, "' due to missing files")
- return
- matrix = read_matrix_file("mat.txt")
- words = read_wordlist_file("word_list.txt")
- my_output = dict(find_words_in_matrix(words, matrix, direction))
- with open(f"outfile_{direction}.txt", 'r') as file:
- words = {}
- for line in file:
- [word, count] = line.rstrip("\n").split(sep=",")
- words[word] = int(count)
- assert my_output == words
- def test_file_r():
- filetester('r')
- def test_file_l():
- filetester('l')
- def test_file_u():
- filetester('u')
- def test_file_d():
- filetester('d')
- def test_file_w():
- filetester('w')
- def test_file_x():
- filetester('x')
- def test_file_y():
- filetester('y')
- def test_file_z():
- filetester('z')
- def test_file_udlrwxyz():
- filetester('udlrwxyz')
- def assert_equal_char_iterators(*iters_args):
- """ Helper function for testing iterator functions, given iterators over
- strings(or character-iterators), ensures all iterators yield the same strings
- without consideration to yielding order.
- :param iters_args: A variadic list of iterators over strings/character-
- iterators
- """
- # convert the arguments tuple into an iterable
- iters = iter(iters_args)
- # we'll always need the first element to compare the rest
- first_iter = next(iters)
- #
- first = {"".join(ch) for ch in first_iter}
- for char_iter in iters:
- current = {"".join(ch) for ch in char_iter}
- assert first == current
- if __name__ == '__main__':
- print("Please run this via 'pytest check_wordsearch.py'")
Advertisement
Add Comment
Please, Sign In to add comment