Passed
Push — master ( e05f01...5c6057 )
by Ken M.
02:06
created

test_magic_domino.Tests.test_Basics()   A

Complexity

Conditions 2

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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