2121# Define blinker example
2222BLINKER = [[0 , 1 , 0 ], [0 , 1 , 0 ], [0 , 1 , 0 ]]
2323
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+ }
2448
2549def new_generation (cells : list [list [int ]]) -> list [list [int ]]:
2650 """
@@ -29,44 +53,89 @@ def new_generation(cells: list[list[int]]) -> list[list[int]]:
2953 [[0, 0, 0], [1, 1, 1], [0, 0, 0]]
3054 """
3155 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+
3361 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
3564 # Get the number of live neighbours
65+ flags [2 ] = True
66+ enter_inner_for = True
3667 neighbour_count = 0
37- if i > 0 and j > 0 :
68+ if i > 0 and j > 0 : #ID 3
69+ flags [3 ] = True
3870 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
4076 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
4282 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
4488 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
4694 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
48100 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
50106 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
52112 neighbour_count += cells [i + 1 ][j + 1 ]
113+ else : #ID 18
114+ flags [18 ] = True
53115
54116 # Rules of the game of life (excerpt from Wikipedia):
55117 # 1. Any live cell with two or three live neighbours survives.
56118 # 2. Any dead cell with three live neighbours becomes a live cell.
57119 # 3. All other live cells die in the next generation.
58120 # Similarly, all other dead cells stay dead.
59121 alive = cells [i ][j ] == 1
60- if (
122+ if ( #ID 19
61123 (alive and 2 <= neighbour_count <= 3 )
62124 or not alive
63125 and neighbour_count == 3
64126 ):
127+ flags [19 ] = True
65128 next_generation_row .append (1 )
66- else :
129+ else : #ID 20
130+ flags [20 ] = True
67131 next_generation_row .append (0 )
132+ if not enter_inner_for : #id 21 - if did not enter inner for loop
133+ flags [21 ] = True
68134
69135 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+
70139 return next_generation
71140
72141
@@ -95,3 +164,13 @@ def generate_images(cells: list[list[int]], frames: int) -> list[Image.Image]:
95164if __name__ == "__main__" :
96165 images = generate_images (GLIDER , 16 )
97166 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