-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_grid.py
59 lines (48 loc) · 1.57 KB
/
test_grid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import string as string
import unittest
import unittest.mock as mock
from grid import Grid
import pytest
with mock.patch('random.choice', side_effect=list(string.ascii_lowercase[0:12])):
"""
Initialises the first 9 alphabets as the grid
a b c d
e f g h
i j k l
"""
grid = Grid(4,3)
def test_get_next_pos_direction():
"""
Should return the correct directional characters
considering the grid starting from center
"""
dict = {0: 'j', 1: 'g', 2: 'k', 3: 'c'}
for direction in range(4):
x, y = grid.get_next_pos('fat', {'pos': 1, 'direction': direction, 'x': 1, 'y': 1})
c = grid.data[grid.wid * y + x]
assert c == dict[direction]
def test_search_vertical():
assert grid.search('aei')['success']
assert grid.search('iea')['success']
def test_search_horizontal():
assert grid.search('abcd')['success']
assert grid.search('dcba')['success']
def test_search_diagonal_down():
assert grid.search('afk')['success']
assert grid.search('kfa')['success']
def test_search_diagonal_up():
assert grid.search('ifc')['success']
assert grid.search('cfi')['success']
def test_search_false():
assert grid.search('afkl')['success'] == False
def test_invalid_args():
with pytest.raises(ValueError):
grid = Grid(0,0)
def test_search_in_dictionary_full_strings():
words = ['aei', 'abcd', 'afk', 'ifc']
results = grid.search_in_dictionary(words)
assert len(set(words).intersection(set(results))) == 4
def test_search_in_dictionary_partial_strings():
words = ['ae', 'bc', 'fk', 'cf']
results = grid.search_in_dictionary(words)
assert len(set(words).intersection(set(results))) == 4