test_disposable_teleports.Tests.setUp()   A
last analyzed

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 random
2
import unittest
3
4
from disposable_teleports import checkio
5
6
7 View Code Duplication
def check_solution(func, teleports_str):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
8
    route = func(teleports_str)
9
    teleports_map = [
10
        tuple(sorted([int(x), int(y)])) for x, y in teleports_str.split(",")
11
    ]
12
    if route[0] != '1' or route[-1] != '1':
13
        print("The path must start and end at 1")
14
        return False
15
    ch_route = route[0]
16
    for i in range(len(route) - 1):
17
        teleport = tuple(sorted([int(route[i]), int(route[i + 1])]))
18
        if not (teleport in teleports_map):
19
            print("No way from {0} to {1}".format(route[i], route[i + 1]))
20
            return False
21
        teleports_map.remove(teleport)
22
        ch_route += route[i + 1]
23
    for s in range(1, 9):
24
        if not str(s) in ch_route:
25
            print("You forgot about {0}".format(s))
26
            return False
27
    return True
28
29
30
class Tests(unittest.TestCase):
31
    TESTS = {
32
        "Basics": [
33
            {"input": "12,23,34,45,56,67,78,81", "answer": "12,23,34,45,56,67,78,81"},
34
            {
35
                "input": "12,28,87,71,13,14,34,35,45,46,63,65",
36
                "answer": "12,28,87,71,13,14,34,35,45,46,63,65",
37
            },
38
            {
39
                "input": "12,15,16,23,24,28,83,85,86,87,71,74,56",
40
                "answer": "12,15,16,23,24,28,83,85,86,87,71,74,56",
41
            },
42
            {
43
                "input": "13,14,23,25,34,35,47,56,58,76,68",
44
                "answer": "13,14,23,25,34,35,47,56,58,76,68",
45
            },
46
            {
47
                "input": "12,13,14,15,16,17,18,82,83,84,85,86,87",
48
                "answer": "12,13,14,15,16,17,18,82,83,84,85,86,87",
49
            },
50
            {
51
                "input": "12,17,87,86,85,82,65,43,35,46",
52
                "answer": "12,17,87,86,85,82,65,43,35,46",
53
            },
54
            {
55
                "input": "13,14,34,32,35,52,37,38,78,16,26",
56
                "answer": "13,14,34,32,35,52,37,38,78,16,26",
57
            },
58
            {
59
                "input": "12,23,36,68,85,57,74,41,38,62,25",
60
                "answer": "12,23,36,68,85,57,74,41,38,62,25",
61
            },
62
            {
63
                "input": "13,37,78,82,26,64,45,51,75,27,34,36,25,35,17,48,47",
64
                "answer": "13,37,78,82,26,64,45,51,75,27,34,36,25,35,17,48,47",
65
            },
66
            {
67
                "input": "18,84,47,76,62,23,35,51,87,65,38,41,75",
68
                "answer": "18,84,47,76,62,23,35,51,87,65,38,41,75",
69
            },
70
            {
71
                "input": "18,84,43,36,62,25,57,71,16,56,85",
72
                "answer": "18,84,43,36,62,25,57,71,16,56,85",
73
            },
74
            {
75
                "input": "16,64,45,53,38,87,72,21,74,52,15,73",
76
                "answer": "16,64,45,53,38,87,72,21,74,52,15,73",
77
            },
78
        ],
79
        "Extra": [],
80
    }
81
82
    def generateMap(self, N):
83
        """int -> str
84
        return random route from 1 to 1 through all N points
85
        Constrains: N > 1
86
        Return: String contain route.
87
        """
88
        route = "1"
89
        stations = [str(i) for i in range(2, N + 1)]
90
        for _ in range(N - 1):
91
            route += stations.pop(random.randrange(0, len(stations)))
92
        route += "1"
93
        teleports = [str(route[i]) + str(route[i + 1]) for i in range(len(route) - 1)]
94
        for _ in range(9):
95
            rand_teleport = str(random.randrange(1, N + 1)) + str(
96
                random.randrange(1, N + 1)
97
            )
98
            if (
99
                rand_teleport[0] != rand_teleport[1]
100
                and rand_teleport not in teleports
101
                and rand_teleport[::-1] not in teleports
102
            ):
103
                teleports.append(rand_teleport)
104
        return ','.join(teleports)
105
106
    def setUp(self):
107
        for _ in range(4):
108
            r = self.generateMap(8)
109
            self.TESTS["Extra"].append({"input": r, "answer": r})
110
111
    def test_Basics(self):
112
        for i in self.TESTS['Basics']:
113
            assert check_solution(checkio, i['input'])
114
115
    def test_Extra(self):
116
        for i in self.TESTS['Extra']:
117
            assert check_solution(checkio, i['input'])
118
119
120
if __name__ == "__main__":  # pragma: no cover
121
    unittest.main()
122