forked from christianguenter2/ABAP_conways_game_of_life
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzcl_cg_conways_game_of_life.clas.testclasses.abap
325 lines (206 loc) · 7.25 KB
/
zcl_cg_conways_game_of_life.clas.testclasses.abap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
*"* use this source file for your ABAP unit test classes
CLASS test_conway DEFINITION DEFERRED.
CLASS zcl_cg_conways_game_of_life DEFINITION LOCAL FRIENDS test_conway.
CLASS test_conway DEFINITION FINAL FOR TESTING
DURATION SHORT
RISK LEVEL HARMLESS.
PRIVATE SECTION.
TYPES: tty_values TYPE STANDARD TABLE OF char1
WITH NON-UNIQUE EMPTY KEY.
CONSTANTS: BEGIN OF co_random_seed,
_1_ TYPE i VALUE 1234,
_2_ TYPE i VALUE 789,
END OF co_random_seed.
DATA: board TYPE REF TO zcl_cg_conways_game_of_life,
row_count TYPE i,
given_rows TYPE i.
METHODS:
init_board FOR TESTING RAISING cx_static_check,
fill_randomly FOR TESTING RAISING cx_static_check,
less_than_two_neighbours FOR TESTING RAISING cx_static_check,
two_or_three_neighbours FOR TESTING RAISING cx_static_check,
two_neighbours FOR TESTING RAISING cx_static_check,
more_than_three_neighbours FOR TESTING RAISING cx_static_check,
exactly_three_neighbours FOR TESTING RAISING cx_static_check,
turn_with_two_active_cells FOR TESTING RAISING cx_static_check,
two_consecutive_turns FOR TESTING RAISING cx_static_check,
turn_count FOR TESTING RAISING cx_static_check,
cells_alive FOR TESTING RAISING cx_static_check,
large_board FOR TESTING RAISING cx_static_check,
_given_board_is
IMPORTING
given_row TYPE string,
_when_turn_is_executed,
_when_mult_turns_are_executed
IMPORTING
turns_to_execute TYPE i,
_then_cells_alive_should_be
IMPORTING
i_exp_cells_alive TYPE i,
_then_board_should_be
IMPORTING
exp_val TYPE string,
_split_row_string
IMPORTING
i_row_string TYPE string
RETURNING
VALUE(rt_values) TYPE tty_values.
ENDCLASS.
CLASS test_conway IMPLEMENTATION.
METHOD _when_turn_is_executed.
board->turn( ).
ENDMETHOD.
METHOD _when_mult_turns_are_executed.
board->multiple_turns( turns_to_execute ).
ENDMETHOD.
METHOD _then_cells_alive_should_be.
cl_abap_unit_assert=>assert_equals( act = board->get_alive_cells_count( )
exp = i_exp_cells_alive ).
ENDMETHOD.
METHOD _given_board_is.
given_rows = given_rows + 1.
DATA(values) = _split_row_string( given_row ).
IF board IS NOT BOUND.
board = NEW zcl_cg_conways_game_of_life( size = lines( values ) ).
ENDIF.
LOOP AT values ASSIGNING FIELD-SYMBOL(<value>).
DATA(col) = sy-tabix.
CHECK <value> = abap_true.
board->activate_cell( i_col = col
i_row = given_rows ).
ENDLOOP.
ENDMETHOD.
METHOD _then_board_should_be.
IF row_count >= given_rows.
CLEAR: row_count.
ENDIF.
row_count = row_count + 1.
DATA(values) = _split_row_string( exp_val ).
LOOP AT values ASSIGNING FIELD-SYMBOL(<value>).
DATA(col) = sy-tabix.
cl_abap_unit_assert=>assert_equals( act = board->get_cell( i_col = col
i_row = row_count )
exp = <value>
msg = |Col { col }, Row { row_count }| ).
ENDLOOP.
ENDMETHOD.
METHOD _split_row_string.
SPLIT i_row_string AT `|` INTO TABLE rt_values.
DELETE rt_values INDEX 1.
ENDMETHOD.
METHOD init_board.
_given_board_is(: `| | | |` ),
`| | | |` ),
`| | | |` ).
_then_board_should_be(: `| | | |` ),
`| | | |` ),
`| | | |` ).
ENDMETHOD.
METHOD fill_randomly.
_given_board_is(: `| | | |` ),
`| | | |` ),
`| | | |` ).
board->fill_randomly( i_seed = co_random_seed-_1_ ).
_then_board_should_be(: `| |X|X|` ),
`| | |X|` ),
`|X|X|X|` ).
ENDMETHOD.
METHOD less_than_two_neighbours.
_given_board_is(: `| | | |` ),
`| |X| |` ),
`| | | |` ).
_when_turn_is_executed( ).
_then_board_should_be(: `| | | |` ),
`| | | |` ),
`| | | |` ).
ENDMETHOD.
METHOD two_or_three_neighbours.
_given_board_is(: `| | | |` ),
`|X|X| |` ),
`| |X|X|` ).
_when_turn_is_executed( ).
_then_board_should_be(: `| | | |` ),
`|X|X|X|` ),
`|X|X|X|` ).
ENDMETHOD.
METHOD two_neighbours.
_given_board_is(: `| | | |` ),
`|X|X| |` ),
`| | |X|` ).
_when_turn_is_executed( ).
_then_board_should_be(: `| | | |` ),
`| |X| |` ),
`| |X| |` ).
ENDMETHOD.
METHOD more_than_three_neighbours.
_given_board_is(: `| | |X|` ),
`|X|X| |` ),
`| |X|X|` ).
_when_turn_is_executed( ).
_then_board_should_be(: `| |X| |` ),
`|X| | |` ),
`|X|X|X|` ).
ENDMETHOD.
METHOD exactly_three_neighbours.
_given_board_is(: `| | | |` ),
`| |X| |` ),
`| |X|X|` ).
_when_turn_is_executed( ).
_then_board_should_be(: `| | | |` ),
`| |X|X|` ),
`| |X|X|` ).
ENDMETHOD.
METHOD turn_with_two_active_cells.
_given_board_is(: `| | | |` ),
`| | | |` ),
`| |X|X|` ).
_when_turn_is_executed( ).
_then_board_should_be(: `| | | |` ),
`| | | |` ),
`| | | |` ).
ENDMETHOD.
METHOD two_consecutive_turns.
_given_board_is(: `| |X|X|` ),
`| | | |` ),
`| | |X|` ).
_when_mult_turns_are_executed( turns_to_execute = 2 ).
_then_board_should_be(: `| | | |` ),
`| | | |` ),
`| | | |` ).
ENDMETHOD.
METHOD turn_count.
_given_board_is( `| |` ).
_when_mult_turns_are_executed( turns_to_execute = 17 ).
cl_abap_unit_assert=>assert_equals( act = board->get_turns( )
exp = 17 ).
ENDMETHOD.
METHOD cells_alive.
_given_board_is(: `| |X|X|` ),
`|X| | |` ),
`|X| |X|` ).
_then_cells_alive_should_be( 5 ).
_when_turn_is_executed( ).
_then_cells_alive_should_be( 4 ).
_when_turn_is_executed( ).
_then_cells_alive_should_be( 4 ).
ENDMETHOD.
METHOD large_board.
_given_board_is(: `| | | | | |` ),
`| | | | | |` ),
`| | | | | |` ),
`| | | | | |` ),
`| | | | | |` ).
board->fill_randomly( i_seed = co_random_seed-_2_ ).
_then_board_should_be(: `| | |X|X| |` ),
`| |X| | | |` ),
`| |X| | |X|` ),
`| |X|X|X|X|` ),
`|X|X| | |X|` ).
_when_turn_is_executed( ).
_then_board_should_be(: `| | |X| | |` ),
`| |X| |X| |` ),
`|X|X| | |X|` ),
`| | | | |X|` ),
`|X|X| | |X|` ).
ENDMETHOD.
ENDCLASS.