21
21
# Define blinker example
22
22
BLINKER = [[0 , 1 , 0 ], [0 , 1 , 0 ], [0 , 1 , 0 ]]
23
23
24
+ flags = {
25
+ 1 : False ,
26
+ 2 : False ,
27
+ 3 : False ,
28
+ 4 : False ,
29
+ 5 : False ,
30
+ 6 : False ,
31
+ 7 : False ,
32
+ 8 : False ,
33
+ 9 : False ,
34
+ 10 : False ,
35
+ 11 : False ,
36
+ 12 : False ,
37
+ 13 : False ,
38
+ 14 : False ,
39
+ 15 : False ,
40
+ 16 : False ,
41
+ 17 : False ,
42
+ 18 : False ,
43
+ 19 : False ,
44
+ 20 : False ,
45
+ 21 : False ,
46
+ 22 : False ,
47
+ }
24
48
25
49
def new_generation (cells : list [list [int ]]) -> list [list [int ]]:
26
50
"""
@@ -29,44 +53,89 @@ def new_generation(cells: list[list[int]]) -> list[list[int]]:
29
53
[[0, 0, 0], [1, 1, 1], [0, 0, 0]]
30
54
"""
31
55
next_generation = []
32
- for i in range (len (cells )):
56
+ enter_outer_for = False
57
+ for i in range (len (cells )): #ID 1
58
+ flags [1 ] = True
59
+ enter_outer_for = True
60
+
33
61
next_generation_row = []
34
- for j in range (len (cells [i ])):
62
+ enter_inner_for = False
63
+ for j in range (len (cells [i ])): #ID 2
35
64
# Get the number of live neighbours
65
+ flags [2 ] = True
66
+ enter_inner_for = True
36
67
neighbour_count = 0
37
- if i > 0 and j > 0 :
68
+ if i > 0 and j > 0 : #ID 3
69
+ flags [3 ] = True
38
70
neighbour_count += cells [i - 1 ][j - 1 ]
39
- if i > 0 :
71
+ else : #ID 4
72
+ flags [4 ] = True
73
+
74
+ if i > 0 : #ID 5
75
+ flags [5 ] = True
40
76
neighbour_count += cells [i - 1 ][j ]
41
- if i > 0 and j < len (cells [i ]) - 1 :
77
+ else : #ID 6
78
+ flags [6 ] = True
79
+
80
+ if i > 0 and j < len (cells [i ]) - 1 : #ID 7
81
+ flags [7 ] = True
42
82
neighbour_count += cells [i - 1 ][j + 1 ]
43
- if j > 0 :
83
+ else : #ID 8
84
+ flags [8 ] = True
85
+
86
+ if j > 0 : #ID 9
87
+ flags [9 ] = True
44
88
neighbour_count += cells [i ][j - 1 ]
45
- if j < len (cells [i ]) - 1 :
89
+ else : #ID 10
90
+ flags [10 ] = True
91
+
92
+ if j < len (cells [i ]) - 1 : #ID 11
93
+ flags [11 ] = True
46
94
neighbour_count += cells [i ][j + 1 ]
47
- if i < len (cells ) - 1 and j > 0 :
95
+ else : #ID 12
96
+ flags [12 ] = True
97
+
98
+ if i < len (cells ) - 1 and j > 0 : #ID 13
99
+ flags [13 ] = True
48
100
neighbour_count += cells [i + 1 ][j - 1 ]
49
- if i < len (cells ) - 1 :
101
+ else : #ID 14
102
+ flags [14 ] = True
103
+
104
+ if i < len (cells ) - 1 : #ID 15
105
+ flags [15 ] = True
50
106
neighbour_count += cells [i + 1 ][j ]
51
- if i < len (cells ) - 1 and j < len (cells [i ]) - 1 :
107
+ else : #ID 16
108
+ flags [16 ] = True
109
+
110
+ if i < len (cells ) - 1 and j < len (cells [i ]) - 1 : #ID 17
111
+ flags [17 ] = True
52
112
neighbour_count += cells [i + 1 ][j + 1 ]
113
+ else : #ID 18
114
+ flags [18 ] = True
53
115
54
116
# Rules of the game of life (excerpt from Wikipedia):
55
117
# 1. Any live cell with two or three live neighbours survives.
56
118
# 2. Any dead cell with three live neighbours becomes a live cell.
57
119
# 3. All other live cells die in the next generation.
58
120
# Similarly, all other dead cells stay dead.
59
121
alive = cells [i ][j ] == 1
60
- if (
122
+ if ( #ID 19
61
123
(alive and 2 <= neighbour_count <= 3 )
62
124
or not alive
63
125
and neighbour_count == 3
64
126
):
127
+ flags [19 ] = True
65
128
next_generation_row .append (1 )
66
- else :
129
+ else : #ID 20
130
+ flags [20 ] = True
67
131
next_generation_row .append (0 )
132
+ if not enter_inner_for : #id 21 - if did not enter inner for loop
133
+ flags [21 ] = True
68
134
69
135
next_generation .append (next_generation_row )
136
+ if not enter_outer_for : #ID 22 - if did not enter outer for loop
137
+ flags [22 ] = True
138
+
70
139
return next_generation
71
140
72
141
@@ -95,3 +164,13 @@ def generate_images(cells: list[list[int]], frames: int) -> list[Image.Image]:
95
164
if __name__ == "__main__" :
96
165
images = generate_images (GLIDER , 16 )
97
166
images [0 ].save ("out.gif" , save_all = True , append_images = images [1 :])
167
+
168
+ import doctest
169
+ doctest .testmod ()
170
+
171
+ print (flags )
172
+ sum = 0
173
+ for i in range (len (flags )):
174
+ if flags [i + 1 ] == True :
175
+ sum += 1
176
+ print ("Branch coverage: " + "{:.2f}" .format (sum / len (flags )))
0 commit comments