NMDanny

Untitled

Nov 21st, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.46 KB | None | 0 0
  1. #############################################################
  2. # FILE : check_wordsearch.py
  3. # WRITER : Daniel Kerbel , danielkerbel
  4. # EXERCISE : intro2cs1 ex5 2018-2019
  5. # DESCRIPTION: Testing wordsearch
  6. #############################################################
  7.  
  8. WEST = "l"
  9. EAST = "r"
  10. NORTH = "u"
  11. SOUTH = "d"
  12. NORTH_EAST = "w"
  13. NORTH_WEST = "x"
  14. SOUTH_EAST = "y"
  15. SOUTH_WEST = "z"
  16.  
  17. DIRECTIONS = {EAST, WEST, NORTH, SOUTH,
  18.               NORTH_EAST, NORTH_WEST, SOUTH_EAST, SOUTH_WEST}
  19.  
  20. from wordsearch import *
  21.  
  22. mat = [
  23.     list("apple"),
  24.     list("agodo"),
  25.     list("nnert"),
  26.     list("gaTAC"),
  27.     list("micsr"),
  28.     list("PoPoP")
  29. ]
  30.  
  31. """
  32. a,p,p,l,e
  33. a,g,o,d,o
  34. n,n,e,r,t
  35. g,a,T,A,C
  36. m,i,c,s,r
  37. P,o,P,o,P
  38. """
  39.  
  40.  
  41.  
  42.  
  43.  
  44. def test_find_words_in_matrix():
  45.     words = ["apple", "mic", "nope", "PiT", "", "wut"]
  46.     occurs = find_words_in_matrix(words, mat, EAST+NORTH_EAST)
  47.     assert dict(occurs) == {
  48.         "apple": 1,
  49.         "mic": 1,
  50.         "PiT": 1
  51.     }
  52.     # testing empty inputs
  53.     assert find_words_in_matrix(words, mat, "") == []
  54.     assert find_words_in_matrix([], mat, EAST) == []
  55.     assert find_words_in_matrix(words, [], EAST) == []
  56.     assert find_words_in_matrix([], [], EAST) == []
  57.     assert find_words_in_matrix([], [], "") == []
  58.  
  59.  
  60. def test_tricky_words():
  61.     words = ["pip"]
  62.     matrix = [
  63.         list("pipippip"),
  64.         list("aiaiaiai"),
  65.         list("xxpxpxpx")
  66.     ]
  67.     assert dict(find_words_in_matrix(words, matrix, EAST)) == {
  68.         "pip": 3
  69.     }
  70.     assert dict(find_words_in_matrix(words, matrix, EAST+SOUTH_EAST)) == {
  71.         "pip": 6
  72.     }
  73.     assert dict(find_words_in_matrix(words, matrix, EAST+NORTH_WEST)) == {
  74.         "pip": 6
  75.     }
  76.     assert dict(find_words_in_matrix(words, matrix, EAST+SOUTH_EAST+WEST+NORTH_WEST)) == {
  77.         "pip": 12
  78.     }
  79.  
  80.  
  81. def test_singleletter_matrix():
  82.     matrix = [["h"]]
  83.     words = ["h", "irrelevant"]
  84.     for direction in DIRECTIONS:
  85.         assert dict(find_words_in_matrix(words, matrix, direction)) == {"h": 1}
  86.  
  87.  
  88. def test_singleaxis_matrix():
  89.     matrix = [list("shalom")]
  90.     words = ["shalom", "shal", "lom"]
  91.     assert dict(find_words_in_matrix(words, matrix, EAST)) == {
  92.         "shalom": 1, "shal": 1, "lom": 1
  93.     }
  94.     assert dict(find_words_in_matrix(words, matrix, SOUTH)) == {}
  95.     assert dict(find_words_in_matrix(words, matrix, WEST+SOUTH_WEST + SOUTH_EAST)) == {}
  96.  
  97.     matrix = [["s"],
  98.               ["h"],
  99.               ["a"],
  100.               ["l"],
  101.               ["o"],
  102.               ["m"]]
  103.     assert dict(find_words_in_matrix(words, matrix, SOUTH)) == {
  104.         "shalom": 1, "shal": 1, "lom": 1
  105.     }
  106.     assert dict(find_words_in_matrix(words, matrix, EAST)) == {}
  107.     assert dict(find_words_in_matrix(words, matrix, WEST+SOUTH_WEST + SOUTH_EAST)) == {}
  108.  
  109.  
  110.  
  111. def test_antidiagonals_2():
  112.     matrix = [
  113.         list("ABCDEF"),
  114.         list("GHIJKL"),
  115.         list("MNOPQR"),
  116.         list("STUVWX")
  117.     ]
  118.  
  119.  
  120. def test_main_diagonal_range():
  121.     assert list(main_diagonal_iterator(mat, 0, 0)) == list("ageAr")
  122.     assert list(main_diagonal_iterator(mat, 0, 0, invert=True)) == list("rAega")
  123.     assert list(main_diagonal_iterator(mat, 0, 2)) == list("pdt")
  124.     assert list(main_diagonal_iterator(mat, 0, 2, invert=True)) == list("tdp")
  125.     assert list(main_diagonal_iterator(mat, 1, 2)) == list("orC")
  126.     assert list(main_diagonal_iterator(mat, 1, 2, invert=True)) == list("Cro")
  127.     assert list(main_diagonal_iterator(mat, 5, 0)) == ["P"]
  128.     assert list(main_diagonal_iterator(mat, 5, 0, invert=True)) == ["P"]
  129.     assert list(main_diagonal_iterator(mat, 4, 3)) == list("sP")
  130.     assert list(main_diagonal_iterator(mat, 4, 3, invert=True)) == list("Ps")
  131.  
  132.  
  133. def test_antidiagonal_range():
  134.     assert list(antidiagonal_iterator(mat, 0, 0)) == ["a"]
  135.     assert list(antidiagonal_iterator(mat, 0, 0, invert=True)) == ["a"]
  136.     assert list(antidiagonal_iterator(mat, 0, 4)) == list("edeam")
  137.     assert list(antidiagonal_iterator(mat, 0, 4, invert=True)) == list("maede")
  138.     assert list(antidiagonal_iterator(mat, 1, 3)) == list("deam")
  139.     assert list(antidiagonal_iterator(mat, 1, 3, invert=True)) == list("maed")
  140.     assert list(antidiagonal_iterator(mat, 1, 4)) == list("orTiP")
  141.     assert list(antidiagonal_iterator(mat, 1, 4, invert=True)) == list("PiTro")
  142.     assert list(antidiagonal_iterator(mat, 5, 4)) == ["P"]
  143.     assert list(antidiagonal_iterator(mat, 5, 4, invert=True)) == ["P"]
  144.  
  145. def test_main_diagonals():
  146.     diagonals = { "".join(diagonal) for diagonal in main_diagonals_iterator(mat)}
  147.     expected = { "ageAr", "porC", "pdt", "lo", "e",
  148.                  "anTsP", "naco", "giP", "mo", "P" }
  149.     assert expected == diagonals
  150.  
  151.  
  152. def test_anti_diagonals():
  153.     diagonals = { "".join(diagonal) for diagonal in anti_diagonals_iterator(mat)}
  154.     expected = { "a", "pa", "pgn", "long", "edeam",
  155.                  "orTiP", "tAco", "CsP", "ro", "P"}
  156.     assert expected == diagonals
  157.  
  158.  
  159. def test_rows():
  160.     rows_set = { "".join(row) for row in rows_iterator(mat)}
  161.     expected = { "apple", "agodo", "nnert", "gaTAC", "micsr", "PoPoP"}
  162.     assert rows_set == expected
  163.  
  164.  
  165. """
  166. a,p,p,l,e
  167. a,g,o,d,o
  168. n,n,e,r,t
  169. g,a,T,A,C
  170. m,i,c,s,r
  171. P,o,P,o,P
  172. """
  173.  
  174. def test_columns():
  175.     cols_set = { "".join(col) for col in columns_iterator(mat)}
  176.     expected = { "aangmP", "pgnaio", "poeTcP", "ldrAso", "eotCrP"}
  177.     revexpected = { "Pmgnaa", "oiangp", "PcTeop", "osArdl", "PrCtoe"}
  178.     assert cols_set == expected
  179.     rev_col_set = { "".join(col) for col in columns_iterator(mat, invert=True)}
  180.     assert rev_col_set == revexpected
  181.  
  182.  
  183. def filetester(direction):
  184.     matrix = read_matrix_file("mat.txt")
  185.     words = read_wordlist_file("word_list.txt")
  186.     my_output = dict(find_words_in_matrix(words, matrix, direction))
  187.     with open(f"outfile_{direction}.txt", 'r') as file:
  188.         words = {}
  189.         for line in file:
  190.             [word, count] = line.rstrip("\n").split(sep=",")
  191.             words[word] = int(count)
  192.         assert my_output == words
  193.  
  194.  
  195. def test_file_r():
  196.     filetester('r')
  197.  
  198.  
  199. def test_file_l():
  200.     filetester('l')
  201.  
  202.  
  203. def test_file_u():
  204.     filetester('u')
  205.  
  206.  
  207. def test_file_d():
  208.     filetester('d')
  209.  
  210.  
  211. def test_file_w():
  212.     filetester('w')
  213.  
  214.  
  215. def test_file_x():
  216.     filetester('x')
  217.  
  218.  
  219. def test_file_y():
  220.     filetester('y')
  221.  
  222.  
  223. def test_file_z():
  224.     filetester('z')
  225.  
  226.  
  227. def test_file_udlrwxyz():
  228.     filetester('udlrwxyz')
  229.  
  230.  
  231. if __name__ == '__main__':
  232.     print("Please run this via 'pytest check_wordsearch.py'")
Advertisement
Add Comment
Please, Sign In to add comment