NMDanny

Untitled

Nov 21st, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.68 KB | None | 0 0
  1. #############################################################
  2. # FILE : check_wordsearch_full.py
  3. # WRITER : Daniel Kerbel
  4. # EXERCISE : intro2cs1 ex5 2018-2019
  5. # DESCRIPTION: Testing wordsearch
  6. #############################################################
  7.  
  8.  
  9. from wordsearch import *
  10.  
  11. mat = [
  12.     list("apple"),
  13.     list("agodo"),
  14.     list("nnert"),
  15.     list("gaTAC"),
  16.     list("micsr"),
  17.     list("PoPoP")
  18. ]
  19.  
  20.  
  21. def test_find_words_in_matrix():
  22.     words = ["apple", "mic", "nope", "PiT", "", "wut"]
  23.     occurs = find_words_in_matrix(words, mat, EAST+NORTH_EAST)
  24.     assert dict(occurs) == {
  25.         "apple": 1,
  26.         "mic": 1,
  27.         "PiT": 1
  28.     }
  29.     # testing empty inputs/edge cases
  30.     assert find_words_in_matrix(words, mat, "") == []
  31.     assert find_words_in_matrix([], mat, EAST) == []
  32.     assert find_words_in_matrix(words, [], EAST) == []
  33.     assert find_words_in_matrix([], [], EAST) == []
  34.     assert find_words_in_matrix([], [], "") == []
  35.  
  36.  
  37. def test_overlapping_words():
  38.     words = ["pip"]
  39.     matrix = [
  40.         list("pipippip"),
  41.         list("aiaiaiai"),
  42.         list("xxpxpxpx")
  43.     ]
  44.     assert dict(find_words_in_matrix(words, matrix, EAST)) == {
  45.         "pip": 3
  46.     }
  47.     assert dict(find_words_in_matrix(words, matrix, EAST+SOUTH_EAST)) == {
  48.         "pip": 6
  49.     }
  50.     assert dict(find_words_in_matrix(words, matrix, EAST+NORTH_WEST)) == {
  51.         "pip": 6
  52.     }
  53.     assert dict(find_words_in_matrix(words, matrix, EAST+SOUTH_EAST+WEST+NORTH_WEST)) == {
  54.         "pip": 12
  55.     }
  56.  
  57.  
  58. def test_singleletter_matrix():
  59.     matrix = [["h"]]
  60.     words = ["h", "irrelevant"]
  61.     for direction in DIRECTIONS:
  62.         assert dict(find_words_in_matrix(words, matrix, direction)) == {"h": 1}
  63.  
  64.  
  65. def test_singleaxis_matrix():
  66.     matrix = [list("shalom")]
  67.     words = ["shalom", "shal", "lom"]
  68.     assert dict(find_words_in_matrix(words, matrix, EAST)) == {
  69.         "shalom": 1, "shal": 1, "lom": 1
  70.     }
  71.     assert dict(find_words_in_matrix(words, matrix, SOUTH)) == {}
  72.     assert dict(find_words_in_matrix(words, matrix, WEST+SOUTH_WEST + SOUTH_EAST)) == {}
  73.  
  74.     matrix = [["s"],
  75.               ["h"],
  76.               ["a"],
  77.               ["l"],
  78.               ["o"],
  79.               ["m"]]
  80.     assert dict(find_words_in_matrix(words, matrix, SOUTH)) == {
  81.         "shalom": 1, "shal": 1, "lom": 1
  82.     }
  83.     assert dict(find_words_in_matrix(words, matrix, EAST)) == {}
  84.     assert dict(find_words_in_matrix(words, matrix, WEST+SOUTH_WEST + SOUTH_EAST)) == {}
  85.  
  86.  
  87. def test_diagonals_edgecases():
  88.     # empty matrix
  89.     mat = []
  90.     assert list(main_diagonals_iterator(mat)) ==\
  91.            list(anti_diagonals_iterator(mat)) == []
  92.  
  93.     # single row matrix
  94.     mat = [list("abcdefg")]
  95.     expected_diags = set("abcdefg")
  96.     assert_equal_char_iterators(expected_diags, main_diagonals_iterator(mat),
  97.                            main_diagonals_iterator(mat, invert=True),
  98.                            anti_diagonals_iterator(mat),
  99.                            anti_diagonals_iterator(mat, invert=True))
  100.  
  101.     # single column matrix
  102.     mat = [["a"], ["b"], ["c"], ["d"], ["e"], ["f"], ["g"]]
  103.     assert_equal_char_iterators(expected_diags, main_diagonals_iterator(mat),
  104.                            main_diagonals_iterator(mat, invert=True),
  105.                            anti_diagonals_iterator(mat),
  106.                            anti_diagonals_iterator(mat, invert=True))
  107.  
  108.  
  109.  
  110.  
  111. def test_main_diagonals():
  112.     expected = { "ageAr", "porC", "pdt", "lo", "e",
  113.                  "anTsP", "naco", "giP", "mo", "P"}
  114.     expected_inv = { "rAega", "Crop", "tdp", "ol", "e",
  115.                      "PsTna", "ocan", "Pig", "om", "P"}
  116.     assert_equal_char_iterators(expected, main_diagonals_iterator(mat))
  117.     assert_equal_char_iterators(expected_inv, main_diagonals_iterator(mat, invert=True))
  118.  
  119.  
  120. def test_anti_diagonals():
  121.     expected = { "a", "pa", "pgn", "long", "edeam",
  122.                  "orTiP", "tAco", "CsP", "ro", "P"}
  123.     expected_inv = { "a", "ap", "ngp", "gnol", "maede",
  124.                      "PiTro", "ocAt", "PsC", "or", "P"}
  125.     assert_equal_char_iterators(expected, anti_diagonals_iterator(mat))
  126.     assert_equal_char_iterators(expected_inv, anti_diagonals_iterator(mat, invert=True))
  127.  
  128.  
  129. def test_rows():
  130.     expected = { "apple", "agodo", "nnert", "gaTAC", "micsr", "PoPoP"}
  131.     assert_equal_char_iterators(expected, rows_iterator(mat))
  132.     rev_expected = { "elppa", "odoga", "trenn", "CATag", "rscim", "PoPoP"}
  133.     assert_equal_char_iterators(rev_expected, rows_iterator(mat, invert=True))
  134.  
  135.  
  136. def test_columns():
  137.     expected = { "aangmP", "pgnaio", "poeTcP", "ldrAso", "eotCrP"}
  138.     rev_expected = { "Pmgnaa", "oiangp", "PcTeop", "osArdl", "PrCtoe"}
  139.     assert_equal_char_iterators(expected, columns_iterator(mat))
  140.     assert_equal_char_iterators(rev_expected, columns_iterator(mat, invert=True))
  141.  
  142.  
  143. def filetester(direction):
  144.     if (not os.access("mat.txt", os.R_OK) or
  145.         not os.access("word_list.txt", os.R_OK) or
  146.         not os.access(f"outfile_{direction}.txt", os.R_OK)):
  147.             print("Skipping filetest '", direction, "' due to missing files")
  148.             return
  149.  
  150.     matrix = read_matrix_file("mat.txt")
  151.     words = read_wordlist_file("word_list.txt")
  152.     my_output = dict(find_words_in_matrix(words, matrix, direction))
  153.     with open(f"outfile_{direction}.txt", 'r') as file:
  154.         words = {}
  155.         for line in file:
  156.             [word, count] = line.rstrip("\n").split(sep=",")
  157.             words[word] = int(count)
  158.         assert my_output == words
  159.  
  160.  
  161. def test_file_r():
  162.     filetester('r')
  163.  
  164.  
  165. def test_file_l():
  166.     filetester('l')
  167.  
  168.  
  169. def test_file_u():
  170.     filetester('u')
  171.  
  172.  
  173. def test_file_d():
  174.     filetester('d')
  175.  
  176.  
  177. def test_file_w():
  178.     filetester('w')
  179.  
  180.  
  181. def test_file_x():
  182.     filetester('x')
  183.  
  184.  
  185. def test_file_y():
  186.     filetester('y')
  187.  
  188.  
  189. def test_file_z():
  190.     filetester('z')
  191.  
  192.  
  193. def test_file_udlrwxyz():
  194.     filetester('udlrwxyz')
  195.  
  196.  
  197. def assert_equal_char_iterators(*iters_args):
  198.     """ Helper function for testing iterator functions, given iterators over
  199.     strings(or character-iterators), ensures all iterators yield the same strings
  200.     without consideration to yielding order.
  201.  
  202.     :param iters_args: A variadic list of iterators over strings/character-
  203.     iterators
  204.     """
  205.     # convert the arguments tuple into an iterable
  206.     iters = iter(iters_args)
  207.     # we'll always need the first element to compare the rest
  208.     first_iter = next(iters)
  209.     #
  210.     first = {"".join(ch) for ch in first_iter}
  211.     for char_iter in iters:
  212.         current = {"".join(ch) for ch in char_iter}
  213.         assert first == current
  214.  
  215.  
  216. if __name__ == '__main__':
  217.     print("Please run this via 'pytest check_wordsearch.py'")
Advertisement
Add Comment
Please, Sign In to add comment