test_magic_domino   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 29

4 Methods

Rating   Name   Duplication   Size   Complexity  
A Tests.setUp() 0 6 3
A Tests.test_Extra() 0 3 2
F Tests.check_data() 0 50 22
A Tests.test_Basics() 0 3 2
1
import itertools
2
import random
3
import unittest
4
5
import pytest
6
7
from magic_domino import magic_domino
8
9
10
class Tests(unittest.TestCase):
11
    extra_tests_count = 3
12
13
    TESTS = {"Basics": [], "Extra": []}
14
15
    def setUp(self):
16
        for n in range(5, 20):
17
            self.TESTS["Basics"].append({"input": (4, n), "answer": (4, n)})
18
19
        for n in random.sample(range(13, 24), self.extra_tests_count):
20
            self.TESTS["Extra"].append({"input": (6, n), "answer": (6, n)})
21
22
    def check_data(self, size, number, user_result):
23
        # check types
24
        check_container_type = lambda o: any(
25
            map(lambda t: isinstance(o, t), (list, tuple))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable o does not seem to be defined.
Loading history...
26
        )
27
        check_cell_type = lambda i: isinstance(i, int)
28
        if not (
29
            check_container_type(user_result)
30
            and all(map(check_container_type, user_result))
31
            and all(map(lambda row: all(map(check_cell_type, row)), user_result))
32
        ):
33
            raise Exception(
34
                "You should return a list/tuple of lists/tuples with integers."
35
            )
36
37
        # check sizes
38
        check_size = lambda o: len(o) == size
39
        if not (check_size(user_result) and all(map(check_size, user_result))):
40
            raise Exception("Wrong size of answer.")
41
42
        # check is it a possible numbers (from 0 to 6 inclusive)
43
        if not all(
44
            map(lambda x: 0 <= x <= 6, itertools.chain.from_iterable(user_result))
45
        ):
46
            raise Exception("Wrong matrix integers (can't be domino tiles)")
47
48
        # check is it a magic square
49
        seq_sum_check = lambda seq: sum(seq) == number
50
        diagonals_indexes = zip(
51
            *map(lambda i: ((i, i), (i, size - i - 1)), range(size))
52
        )
53
        values_from_indexes = lambda inds: itertools.starmap(
54
            lambda x, y: user_result[y][x], inds
55
        )
56
        if not (
57
            all(map(seq_sum_check, user_result))
58
            and all(map(seq_sum_check, zip(*user_result)))  # rows
59
            and all(  # columns
60
                map(seq_sum_check, map(values_from_indexes, diagonals_indexes))
61
            )
62
        ):  # diagonals
63
            raise Exception("It's not a magic square.")
64
65
        # check is it domino square
66
        tiles = set()
67
        for x, y in itertools.product(range(size), range(0, size, 2)):
68
            tile = tuple(sorted((user_result[y][x], user_result[y + 1][x])))
69
            if tile in tiles:
70
                raise Exception("It's not a domino magic square.")
71
            tiles.add(tile)
72
73
    def test_Basics(self):
74
        for i in self.TESTS['Basics']:
75
            self.check_data(i['answer'][0], i['answer'][1], magic_domino(*i['input']))
76
77
    def test_Extra(self):
78
        for i in self.TESTS['Extra']:
79
            self.check_data(i['answer'][0], i['answer'][1], magic_domino(*i['input']))
80
81
82
if __name__ == "__main__":  # pragma: no cover
83
    unittest.main()
84