Passed
Push — master ( 8c8daf...fcd5d8 )
by Ken M.
01:48
created

test_magic_domino.Tests.test_Extra()   A

Complexity

Conditions 2

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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