Passed
Push — master ( 3eb980...a5aa16 )
by Ken M.
01:11
created

test_bats_bunker   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 88
dl 0
loc 129
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A Tests.test_Extra() 0 3 2
A Tests.test_Basics() 0 3 2
A Tests.test_path_coordinates() 0 10 1
A Tests.test_numbers() 0 5 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A almost_equal() 0 3 1
1
import unittest
2
3
from bats_bunker import checkio, numbers, path_coordinates
4
5
6
def almost_equal(checked, correct, significant_digits=2):
7
    precision = 0.1 ** significant_digits
8
    return correct - precision < checked < correct + precision
9
10
11
class Tests(unittest.TestCase):
12
    TESTS = {
13
        "Basics": [
14
            {
15
                "input": ["B--", "---", "--A"],
16
                "answer": 2.83,
17
                "explanation": [[0, 0, 0], [2, 2, 2.83]],
18
            },
19
            {
20
                "input": ["B-B", "BW-", "-BA"],
21
                "answer": 4,
22
                "explanation": [[0, 0, 0], [0, 2, 2], [2, 2, 4]],
23
            },
24
            {
25
                "input": ["BWB--B", "-W-WW-", "B-BWAB"],
26
                "answer": 12,
27
                "explanation": [
28
                    [0, 0, 0],
29
                    [2, 0, 2],
30
                    [2, 2, 4],
31
                    [0, 2, 6],
32
                    [0, 5, 9],
33
                    [2, 5, 11],
34
                    [2, 4, 12],
35
                ],
36
            },
37
            {
38
                "input": ["B---B-", "-WWW-B", "-WA--B", "-W-B--", "-WWW-B", "B-BWB-"],
39
                "answer": 9.24,
40
                "explanation": [[0, 0, 0], [0, 4, 4], [2, 5, 6.24], [2, 2, 9.24]],
41
            },
42
        ],
43
        "Extra": [
44
            {
45
                "input": ["BW-", "--A", "B--"],
46
                "answer": 4.24,
47
                "explanation": [[0, 0, 0], [2, 0, 2], [1, 2, 4.24]],
48
            },
49
            {"input": ["A--", "---", "---"], "answer": 0, "explanation": []},
50
            {
51
                "input": ["B-B-B", "-WWW-", "BWA-B", "-WWW-", "B-B-B"],
52
                "answer": 8,
53
                "explanation": [[0, 0, 0], [0, 4, 4], [2, 4, 6], [2, 2, 8]],
54
            },
55
            {
56
                "input": ["BWA-B-", "-W----", "-WW-B-", "-W---B", "--B---", "B-B---"],
57
                "answer": 12.83,
58
                "explanation": [[0, 0, 0], [5, 0, 5], [2, 4, 10.07], [0, 2, 12.83]],
59
            },
60
            {
61
                "input": [
62
                    "B-B--B-",
63
                    "-W-W-W-",
64
                    "--B---B",
65
                    "BW-W-W-",
66
                    "----A--",
67
                    "BW-W-W-",
68
                    "-B--B-B",
69
                ],
70
                "answer": 16,
71
                "explanation": [
72
                    [0, 0, 0],
73
                    [0, 2, 2],
74
                    [2, 2, 4],
75
                    [2, 6, 8],
76
                    [6, 6, 12],
77
                    [6, 4, 14],
78
                    [4, 4, 16],
79
                ],
80
            },
81
            {
82
                "input": ["BBBBB", "BBBBB", "BBBBB", "BBBBB", "BBBBA"],
83
                "answer": 5.66,
84
                "explanation": [[0, 0, 0], [4, 4, 5.66]],
85
            },
86
            {
87
                "input": ["B-----", "-BBB--", "-WWBW-", "-----A", "B-W-B-"],
88
                "answer": 7.81,
89
                "explanation": [
90
                    [0, 0, 0],
91
                    [1, 3, 3.16],
92
                    [2, 3, 4.16],
93
                    [4, 4, 6.40],
94
                    [3, 5, 7.81],
95
                ],
96
            },
97
        ],
98
    }
99
100
    def test_numbers(self):
101
        assert list(numbers(0, 5)) == [0, 1, 2, 3, 4, 5]
102
        assert list(numbers(5, 0)) == [0, -1, -2, -3, -4, -5]
103
        assert list(numbers(-5, -10)) == [0, -1, -2, -3, -4, -5]
104
        assert list(numbers(-10, -5)) == [0, 1, 2, 3, 4, 5]
105
106
    def test_path_coordinates(self):
107
        assert set(path_coordinates((0, 0), (1, 1))) == set(
108
            [(0, 0), (0, 1), (1, 0), (1, 1)]
109
        )
110
        assert set(path_coordinates((2, 2), (0, 0))) == set(
111
            [(2, 2), (2, 1), (1, 2), (1, 1), (1, 0), (0, 1), (0, 0)]
112
        )
113
        assert set(path_coordinates((0, 0), (0, 2))) == set([(0, 0), (0, 1), (0, 2)])
114
        assert set(path_coordinates((0, 0), (1, 2))) == set(
115
            [(0, 0), (0, 1), (1, 1), (1, 2)]
116
        )
117
118
    def test_Basics(self):
119
        for i in self.TESTS['Basics']:
120
            assert almost_equal(checkio(i['input']), i['answer'])
121
122
    def test_Extra(self):
123
        for i in self.TESTS['Extra']:
124
            assert almost_equal(checkio(i['input']), i['answer'])
125
126
127
if __name__ == "__main__":  # pragma: no cover
128
    unittest.main()
129