aboutsummaryrefslogtreecommitdiff
path: root/diplomacy/tests/test_game.py
blob: 1c2a6fed0811e753d19fa6529f0badd9d61fe1cd (plain)
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
# ==============================================================================
# Copyright (C) 2019 - Philip Paquette
#
#  This program is free software: you can redistribute it and/or modify it under
#  the terms of the GNU Affero General Public License as published by the Free
#  Software Foundation, either version 3 of the License, or (at your option) any
#  later version.
#
#  This program is distributed in the hope that it will be useful, but WITHOUT
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
#  details.
#
#  You should have received a copy of the GNU Affero General Public License along
#  with this program.  If not, see <https://www.gnu.org/licenses/>.
# ==============================================================================
""" Test_game
    - Contains tests for the game object
"""
from copy import deepcopy
from diplomacy.engine.game import Game
from diplomacy.utils.order_results import BOUNCE

def test_is_game_done():
    """ Tests if the game is done """
    game = Game()
    assert not game.is_game_done
    game.phase = 'COMPLETED'
    assert game.is_game_done

def test_create_game():
    """ Test - Creates a game """
    game = Game()
    assert not game.error

def test_get_units():
    """ Tests - get units """
    game = Game()
    game.clear_units()
    game.set_units('FRANCE', ['A PAR', 'A MAR'])
    game.set_units('ENGLAND', ['A PAR', 'A LON'])
    units = game.get_units()
    assert units['AUSTRIA'] == []
    assert units['ENGLAND'] == ['A PAR', 'A LON']
    assert units['FRANCE'] == ['A MAR']
    assert units['GERMANY'] == []
    assert units['ITALY'] == []
    assert units['RUSSIA'] == []
    assert units['TURKEY'] == []

    assert game.get_units('AUSTRIA') == []
    assert game.get_units('ENGLAND') == ['A PAR', 'A LON']
    assert game.get_units('FRANCE') == ['A MAR']
    assert game.get_units('GERMANY') == []
    assert game.get_units('ITALY') == []
    assert game.get_units('RUSSIA') == []
    assert game.get_units('TURKEY') == []

    # Making sure we got a copy, and not a direct game reference
    game.set_units('FRANCE', ['F MAR'])
    units_2 = game.get_units()
    assert units['FRANCE'] == ['A MAR']
    assert units_2['FRANCE'] == ['F MAR']

def test_get_centers():
    """ Test - get centers """
    game = Game()
    centers = game.get_centers()
    assert centers['AUSTRIA'] == ['BUD', 'TRI', 'VIE']
    assert centers['ENGLAND'] == ['EDI', 'LON', 'LVP']
    assert centers['FRANCE'] == ['BRE', 'MAR', 'PAR']
    assert centers['GERMANY'] == ['BER', 'KIE', 'MUN']
    assert centers['ITALY'] == ['NAP', 'ROM', 'VEN']
    assert centers['RUSSIA'] == ['MOS', 'SEV', 'STP', 'WAR']
    assert centers['TURKEY'] == ['ANK', 'CON', 'SMY']

    assert game.get_centers('AUSTRIA') == ['BUD', 'TRI', 'VIE']
    assert game.get_centers('ENGLAND') == ['EDI', 'LON', 'LVP']
    assert game.get_centers('FRANCE') == ['BRE', 'MAR', 'PAR']
    assert game.get_centers('GERMANY') == ['BER', 'KIE', 'MUN']
    assert game.get_centers('ITALY') == ['NAP', 'ROM', 'VEN']
    assert game.get_centers('RUSSIA') == ['MOS', 'SEV', 'STP', 'WAR']
    assert game.get_centers('TURKEY') == ['ANK', 'CON', 'SMY']

    # Making sure we got a copy, and not a direct game reference
    austria = game.get_power('AUSTRIA')
    austria.centers.remove('BUD')
    centers_2 = game.get_centers()
    assert centers['AUSTRIA'] == ['BUD', 'TRI', 'VIE']
    assert centers_2['AUSTRIA'] == ['TRI', 'VIE']

def test_get_orders():
    """ Test - get orders """
    check_sorted = lambda list_1, list_2: sorted(list_1) == sorted(list_2)
    game = Game()

    # Movement phase
    game.set_orders('FRANCE', ['A PAR H', 'A MAR - BUR'])
    game.set_orders('ENGLAND', ['LON H'])
    orders = game.get_orders()
    assert check_sorted(orders['AUSTRIA'], [])
    assert check_sorted(orders['ENGLAND'], ['F LON H'])
    assert check_sorted(orders['FRANCE'], ['A PAR H', 'A MAR - BUR'])
    assert check_sorted(orders['GERMANY'], [])
    assert check_sorted(orders['ITALY'], [])
    assert check_sorted(orders['RUSSIA'], [])
    assert check_sorted(orders['TURKEY'], [])

    assert check_sorted(game.get_orders('AUSTRIA'), [])
    assert check_sorted(game.get_orders('ENGLAND'), ['F LON H'])
    assert check_sorted(game.get_orders('FRANCE'), ['A PAR H', 'A MAR - BUR'])
    assert check_sorted(game.get_orders('GERMANY'), [])
    assert check_sorted(game.get_orders('ITALY'), [])
    assert check_sorted(game.get_orders('RUSSIA'), [])
    assert check_sorted(game.get_orders('TURKEY'), [])

    # Making sure we got a copy, and not a direct game reference
    france = game.get_power('FRANCE')
    del france.orders['A PAR']
    orders_2 = game.get_orders()
    assert check_sorted(orders['FRANCE'], ['A PAR H', 'A MAR - BUR'])
    assert check_sorted(orders_2['FRANCE'], ['A MAR - BUR'])

    # Moving to W1901A
    game.clear_units('FRANCE')
    game.set_centers('FRANCE', 'SPA')
    game.process()
    game.process()
    assert game.get_current_phase() == 'W1901A'

    # Adjustment phase
    game.set_orders('FRANCE', ['A MAR B', 'F MAR B'])
    game.set_orders('AUSTRIA', 'A PAR H')
    orders = game.get_orders()
    assert check_sorted(orders['AUSTRIA'], [])
    assert check_sorted(orders['ENGLAND'], [])
    assert check_sorted(orders['FRANCE'], ['A MAR B'])
    assert check_sorted(orders['GERMANY'], [])
    assert check_sorted(orders['ITALY'], [])
    assert check_sorted(orders['RUSSIA'], [])
    assert check_sorted(orders['TURKEY'], [])

    assert check_sorted(game.get_orders('AUSTRIA'), [])
    assert check_sorted(game.get_orders('ENGLAND'), [])
    assert check_sorted(game.get_orders('FRANCE'), ['A MAR B'])
    assert check_sorted(game.get_orders('GERMANY'), [])
    assert check_sorted(game.get_orders('ITALY'), [])
    assert check_sorted(game.get_orders('RUSSIA'), [])
    assert check_sorted(game.get_orders('TURKEY'), [])

def test_get_orders_no_check():
    """ Test - get orders NO_CHECK """
    check_sorted = lambda list_1, list_2: sorted(list_1) == sorted(list_2)
    game = Game()
    game.add_rule('NO_CHECK')

    # Movement phase
    game.set_orders('FRANCE', ['A PAR H', 'A MAR - BUR'])
    game.set_orders('ENGLAND', ['LON H'])
    orders = game.get_orders()
    assert check_sorted(orders['AUSTRIA'], [])
    assert check_sorted(orders['ENGLAND'], ['LON H'])                       # Should not be fixed
    assert check_sorted(orders['FRANCE'], ['A PAR H', 'A MAR - BUR'])
    assert check_sorted(orders['GERMANY'], [])
    assert check_sorted(orders['ITALY'], [])
    assert check_sorted(orders['RUSSIA'], [])
    assert check_sorted(orders['TURKEY'], [])

    assert check_sorted(game.get_orders('AUSTRIA'), [])
    assert check_sorted(game.get_orders('ENGLAND'), ['LON H'])              # Should not be fixed
    assert check_sorted(game.get_orders('FRANCE'), ['A PAR H', 'A MAR - BUR'])
    assert check_sorted(game.get_orders('GERMANY'), [])
    assert check_sorted(game.get_orders('ITALY'), [])
    assert check_sorted(game.get_orders('RUSSIA'), [])
    assert check_sorted(game.get_orders('TURKEY'), [])

    # Making sure we got a copy, and not a direct game reference
    france = game.get_power('FRANCE')
    france.orders = {order_ix: order_value for order_ix, order_value in france.orders.items()
                     if not order_value.startswith('A PAR')}
    orders_2 = game.get_orders()
    assert check_sorted(orders['FRANCE'], ['A PAR H', 'A MAR - BUR'])
    assert check_sorted(orders_2['FRANCE'], ['A MAR - BUR'])

    # Moving to W1901A
    game.clear_units('FRANCE')
    game.set_centers('FRANCE', 'SPA')
    game.process()
    game.process()
    assert game.get_current_phase() == 'W1901A'

    # Adjustment phase
    game.set_orders('FRANCE', ['A MAR B', 'F MAR B'])
    game.set_orders('AUSTRIA', 'A PAR H')
    orders = game.get_orders()
    assert check_sorted(orders['AUSTRIA'], [])                  # 'A PAR H' is VOID
    assert check_sorted(orders['ENGLAND'], [])
    assert check_sorted(orders['FRANCE'], ['A MAR B'])          # 'F MAR B' is VOID
    assert check_sorted(orders['GERMANY'], [])
    assert check_sorted(orders['ITALY'], [])
    assert check_sorted(orders['RUSSIA'], [])
    assert check_sorted(orders['TURKEY'], [])

    assert check_sorted(game.get_orders('AUSTRIA'), [])
    assert check_sorted(game.get_orders('ENGLAND'), [])
    assert check_sorted(game.get_orders('FRANCE'), ['A MAR B'])
    assert check_sorted(game.get_orders('GERMANY'), [])
    assert check_sorted(game.get_orders('ITALY'), [])
    assert check_sorted(game.get_orders('RUSSIA'), [])
    assert check_sorted(game.get_orders('TURKEY'), [])

def test_get_order_status():
    """ Tests - get order status """
    game = Game()
    game.clear_units()
    game.set_units('ITALY', 'A VEN')
    game.set_units('AUSTRIA', 'A VIE')
    game.set_orders('ITALY', 'A VEN - TYR')
    game.set_orders('AUSTRIA', 'A VIE - TYR')
    game.process()
    results = game.get_order_status()
    assert BOUNCE in results['ITALY']['A VEN']
    assert BOUNCE in results['AUSTRIA']['A VIE']
    assert BOUNCE in game.get_order_status(unit='A VEN')
    assert BOUNCE in game.get_order_status(unit='A VIE')
    assert BOUNCE in game.get_order_status('ITALY')['A VEN']
    assert BOUNCE in game.get_order_status('AUSTRIA')['A VIE']

def test_set_units():
    """ Test - Sets units """
    game = Game()
    game.clear_units()
    game.set_units('FRANCE', ['A PAR', 'A MAR', '*A GAS'], reset=False)
    game.set_units('ENGLAND', ['A PAR', 'A LON'])
    assert game.get_power('AUSTRIA').units == []
    assert game.get_power('ENGLAND').units == ['A PAR', 'A LON']
    assert game.get_power('FRANCE').units == ['A MAR']
    assert 'A GAS' in game.get_power('FRANCE').retreats
    assert game.get_power('GERMANY').units == []
    assert game.get_power('ITALY').units == []
    assert game.get_power('RUSSIA').units == []
    assert game.get_power('TURKEY').units == []

    # Adding F PIC to England without resetting
    game.set_units('ENGLAND', ['F PIC'], reset=False)
    assert game.get_power('ENGLAND').units == ['A PAR', 'A LON', 'F PIC']

    # Adding F PIC to England with resetting
    game.set_units('ENGLAND', ['F PIC'], reset=True)
    assert game.get_power('ENGLAND').units == ['F PIC']

    # Adding F PAR (Illegal unit) to England without resetting
    game.set_units('ENGLAND', ['F PAR'], reset=False)
    assert game.get_power('ENGLAND').units == ['F PIC']

def test_set_centers():
    """ Tests - Sets centers """
    game = Game()
    game.clear_centers()
    game.set_centers('FRANCE', ['PAR', 'MAR', 'GAS'])       # GAS is not a valid SC loc
    game.set_centers('ENGLAND', ['PAR', 'LON'])
    assert game.get_power('AUSTRIA').centers == []
    assert game.get_power('ENGLAND').centers == ['PAR', 'LON']
    assert game.get_power('FRANCE').centers == ['MAR']
    assert game.get_power('GERMANY').centers == []
    assert game.get_power('ITALY').centers == []
    assert game.get_power('RUSSIA').centers == []
    assert game.get_power('TURKEY').centers == []

    # Adding BUD to England without resetting
    game.set_centers('ENGLAND', 'BUD', reset=False)
    assert game.get_power('ENGLAND').centers == ['PAR', 'LON', 'BUD']

    # Adding BUD to England with resetting
    game.set_centers('ENGLAND', ['BUD'], reset=True)
    assert game.get_power('ENGLAND').centers == ['BUD']

    # Adding UKR to England (illegal SC)
    game.set_centers('ENGLAND', 'UKR', reset=False)
    assert game.get_power('ENGLAND').centers == ['BUD']

def test_set_orders():
    """ Test - Sets orders """
    game = Game()
    game.clear_units()
    game.set_units('ITALY', 'A VEN')
    game.set_units('AUSTRIA', 'A VIE')
    game.set_units('FRANCE', 'A PAR')
    game.set_orders('ITALY', 'A VEN - TYR')
    game.set_orders('AUSTRIA', 'A VIE - TYR')

    game.set_orders('FRANCE', ['', '', 'A PAR - GAS', '', '', ''])
    game.set_orders('RUSSIA', '')
    game.set_orders('GERMANY', [])
    assert game.get_orders('FRANCE') == ['A PAR - GAS']
    assert not game.get_orders('RUSSIA')
    assert not game.get_orders('GERMANY')

    game.process()
    results = game.get_order_status()
    assert BOUNCE in results['ITALY']['A VEN']
    assert BOUNCE in results['AUSTRIA']['A VIE']

def test_set_orders_replace():
    """ Test - Sets orders with replace=True """
    check_sorted = lambda list_1, list_2: sorted(list_1) == sorted(list_2)

    # Regular Movement Phase
    game = Game()
    game.clear_units()
    game.set_units('ITALY', ['A VEN', 'A PAR'])
    game.set_units('AUSTRIA', 'A VIE')
    game.set_orders('ITALY', ['A VEN - TYR', 'A PAR H'])
    game.set_orders('AUSTRIA', 'A VIE - TYR')
    game.set_orders('ITALY', 'A PAR - GAS')
    orders = game.get_orders()
    assert check_sorted(orders['ITALY'], ['A VEN - TYR', 'A PAR - GAS'])
    assert check_sorted(orders['AUSTRIA'], ['A VIE - TYR'])

    # NO_CHECK Movement Phase
    game = Game()
    game.add_rule('NO_CHECK')
    game.clear_units()
    game.set_units('ITALY', ['A VEN', 'A PAR'])
    game.set_units('AUSTRIA', 'A VIE')
    game.set_orders('ITALY', ['A VEN - TYR', 'A PAR H'])
    game.set_orders('AUSTRIA', 'A VIE - TYR')
    game.set_orders('ITALY', 'A PAR - GAS')
    orders = game.get_orders()
    assert check_sorted(orders['ITALY'], ['A VEN - TYR', 'A PAR - GAS'])
    assert check_sorted(orders['AUSTRIA'], ['A VIE - TYR'])

    # Regular Retreat Phase
    game = Game()
    game.clear_units()
    game.set_units('ITALY', ['A BRE', 'A PAR'])
    game.set_units('AUSTRIA', 'A GAS')
    game.set_orders('ITALY', ['A BRE - GAS', 'A PAR S A BRE - GAS'])
    game.set_orders('AUSTRIA', 'A GAS H')
    game.process()
    game.set_orders('AUSTRIA', 'A GAS R MAR')
    game.set_orders('AUSTRIA', 'A GAS R SPA')
    orders = game.get_orders()
    assert check_sorted(orders['AUSTRIA'], ['A GAS R SPA'])

    # NO_CHECK Retreat Phase
    game = Game()
    game.add_rule('NO_CHECK')
    game.clear_units()
    game.set_units('ITALY', ['A BRE', 'A PAR'])
    game.set_units('AUSTRIA', 'A GAS')
    game.set_orders('ITALY', ['A BRE - GAS', 'A PAR S A BRE - GAS'])
    game.set_orders('AUSTRIA', 'A GAS H')
    game.process()
    game.set_orders('AUSTRIA', 'A GAS R MAR')
    game.set_orders('AUSTRIA', 'A GAS R SPA')
    orders = game.get_orders()
    assert check_sorted(orders['AUSTRIA'], ['A GAS R SPA'])

    # Regular Adjustment Phase
    game = Game()
    game.clear_units()
    game.clear_centers()
    game.set_units('FRANCE', ['A BRE', 'A PAR'])
    game.set_units('AUSTRIA', 'A GAS')
    game.set_orders('FRANCE', 'A PAR - PIC')
    game.process()
    game.set_orders('FRANCE', ['A PIC - BEL', 'A BRE - PAR'])
    game.process()
    game.set_orders('FRANCE', 'A BRE B')
    game.set_orders('FRANCE', 'F BRE B')
    orders = game.get_orders()
    assert check_sorted(orders['FRANCE'], ['F BRE B'])

    # NO_CHECK Adjustment Phase
    game = Game()
    game.add_rule('NO_CHECK')
    game.clear_units()
    game.clear_centers()
    game.set_units('FRANCE', ['A BRE', 'A PAR'])
    game.set_units('AUSTRIA', 'A GAS')
    game.set_orders('FRANCE', 'A PAR - PIC')
    game.process()
    game.set_orders('FRANCE', ['A PIC - BEL', 'A BRE - PAR'])
    game.process()
    game.set_orders('FRANCE', 'A BRE B')
    game.set_orders('FRANCE', 'F BRE B')
    orders = game.get_orders()
    assert check_sorted(orders['FRANCE'], ['F BRE B'])

def test_set_orders_no_replace():
    """ Test - Sets orders with replace=False """
    check_sorted = lambda list_1, list_2: sorted(list_1) == sorted(list_2)

    # Regular Movement Phase
    game = Game()
    game.clear_units()
    game.set_units('ITALY', ['A VEN', 'A PAR'])
    game.set_units('AUSTRIA', 'A VIE')
    game.set_orders('ITALY', ['A VEN - TYR', 'A PAR H'], replace=False)
    game.set_orders('AUSTRIA', 'A VIE - TYR', replace=False)
    game.set_orders('ITALY', 'A PAR - GAS', replace=False)
    orders = game.get_orders()
    assert check_sorted(orders['ITALY'], ['A VEN - TYR', 'A PAR H'])
    assert check_sorted(orders['AUSTRIA'], ['A VIE - TYR'])

    # NO_CHECK Movement Phase
    game = Game()
    game.add_rule('NO_CHECK')
    game.clear_units()
    game.set_units('ITALY', ['A VEN', 'A PAR'])
    game.set_units('AUSTRIA', 'A VIE')
    game.set_orders('ITALY', ['A VEN - TYR', 'A PAR H'], replace=False)
    game.set_orders('AUSTRIA', 'A VIE - TYR', replace=False)
    game.set_orders('ITALY', 'A PAR - GAS', replace=False)
    orders = game.get_orders()
    assert check_sorted(orders['ITALY'], ['A VEN - TYR', 'A PAR H', 'A PAR - GAS'])
    assert check_sorted(orders['AUSTRIA'], ['A VIE - TYR'])

    # Regular Retreat Phase
    game = Game()
    game.clear_units()
    game.set_units('ITALY', ['A BRE', 'A PAR'])
    game.set_units('AUSTRIA', 'A GAS')
    game.set_orders('ITALY', ['A BRE - GAS', 'A PAR S A BRE - GAS'], replace=False)
    game.set_orders('AUSTRIA', 'A GAS H', replace=False)
    game.process()
    game.set_orders('AUSTRIA', 'A GAS R MAR', replace=False)
    game.set_orders('AUSTRIA', 'A GAS R SPA', replace=False)
    orders = game.get_orders()
    assert check_sorted(orders['AUSTRIA'], ['A GAS R MAR'])

    # NO_CHECK Retreat Phase
    game = Game()
    game.add_rule('NO_CHECK')
    game.clear_units()
    game.set_units('ITALY', ['A BRE', 'A PAR'])
    game.set_units('AUSTRIA', 'A GAS')
    game.set_orders('ITALY', ['A BRE - GAS', 'A PAR S A BRE - GAS'], replace=False)
    game.set_orders('AUSTRIA', 'A GAS H', replace=False)
    game.process()
    game.set_orders('AUSTRIA', 'A GAS R MAR', replace=False)
    game.set_orders('AUSTRIA', 'A GAS R SPA', replace=False)
    orders = game.get_orders()
    assert check_sorted(orders['AUSTRIA'], ['A GAS R MAR'])

    # Regular Adjustment Phase
    game = Game()
    game.clear_units()
    game.clear_centers()
    game.set_units('FRANCE', ['A BRE', 'A PAR'])
    game.set_units('AUSTRIA', 'A GAS')
    game.set_orders('FRANCE', 'A PAR - PIC', replace=False)
    game.process()
    game.set_orders('FRANCE', ['A PIC - BEL', 'A BRE - PAR'], replace=False)
    game.process()
    game.set_orders('FRANCE', 'A BRE B', replace=False)
    game.set_orders('FRANCE', 'F BRE B', replace=False)
    orders = game.get_orders()
    assert check_sorted(orders['FRANCE'], ['A BRE B'])

    # NO_CHECK Adjustment Phase
    game = Game()
    game.add_rule('NO_CHECK')
    game.clear_units()
    game.clear_centers()
    game.set_units('FRANCE', ['A BRE', 'A PAR'])
    game.set_units('AUSTRIA', 'A GAS')
    game.set_orders('FRANCE', 'A PAR - PIC', replace=False)
    game.process()
    game.set_orders('FRANCE', ['A PIC - BEL', 'A BRE - PAR'], replace=False)
    game.process()
    game.set_orders('FRANCE', 'A BRE B', replace=False)
    game.set_orders('FRANCE', 'F BRE B', replace=False)
    orders = game.get_orders()
    assert check_sorted(orders['FRANCE'], ['A BRE B'])

def test_clear_units():
    """ Tests - Clear units """
    game = Game()
    game.clear_units()
    for power in game.powers.values():
        assert not power.units
    assert not game.error

def test_clear_centers():
    """ Tests - Clear centers """
    game = Game()
    game.clear_centers()
    for power in game.powers.values():
        assert not power.centers
    assert not game.error

def test_clear_orders():
    """ Test - Clear orders"""
    game = Game()
    game.clear_units()
    game.set_units('ITALY', 'A VEN')
    game.set_units('AUSTRIA', 'A VIE')
    game.set_orders('ITALY', 'A VEN - TYR')
    game.set_orders('AUSTRIA', 'A VIE - TYR')
    game.clear_orders()
    game.process()
    results = game.get_order_status()
    assert results['ITALY']['A VEN'] == []
    assert results['AUSTRIA']['A VIE'] == []

def test_get_current_phase():
    """ Tests - get current phase """
    game = Game()
    assert game.get_current_phase() == 'S1901M'

def test_set_current_phase():
    """ Tests - set current phase"""
    game = Game()
    power = game.get_power('FRANCE')
    power.units.remove('A PAR')
    game.set_current_phase('W1901A')
    game.clear_cache()
    assert game.get_current_phase() == 'W1901A'
    assert game.phase_type == 'A'
    assert 'A PAR B' in game.get_all_possible_orders()['PAR']

def test_process_game():
    """ Tests - Process game """
    game = Game()
    game.clear_units()
    game.set_units('ITALY', 'A VEN')
    game.set_units('AUSTRIA', 'A VIE')
    game.set_orders('ITALY', 'A VEN - TYR')
    game.set_orders('AUSTRIA', 'A VIE - TYR')
    game.process()
    results = game.get_order_status()
    assert BOUNCE in results['ITALY']['A VEN']
    assert BOUNCE in results['AUSTRIA']['A VIE']

def test_deepcopy():
    """ Tests - deepcopy """
    game = Game()
    game2 = deepcopy(game)
    assert game != game2
    assert game.get_hash() == game2.get_hash()

def test_automatic_draw():
    """ Tests - draw """
    game = Game()
    assert game.map.first_year == 1901

    # fast forward 99 years with no winter
    for year in range(1, 100):
        game.process()
        game.process()
        assert int(game.get_current_phase()[1:5]) == game.map.first_year + year
    assert game.is_game_done is False

    # forward 2000 year. after this year should draw
    game.process()
    game.process()
    assert game.is_game_done is True
    assert list(sorted(game.outcome)) == list(
        sorted(['W2000A', 'AUSTRIA', 'ENGLAND', 'FRANCE', 'GERMANY', 'ITALY', 'RUSSIA', 'TURKEY']))

def test_histories():
    """ Test order_history, state_history, message_history and messages. """
    from diplomacy.server.server_game import ServerGame
    from diplomacy.utils.sorted_dict import SortedDict
    from diplomacy.utils import strings
    game = ServerGame(status=strings.ACTIVE)
    assert game.solitaire
    assert not game.n_controls
    assert game.is_game_active

    assert isinstance(game.messages, SortedDict)
    assert isinstance(game.message_history, SortedDict)
    assert isinstance(game.order_history, SortedDict)
    assert isinstance(game.state_history, SortedDict)
    assert not game.messages
    assert not game.message_history
    assert not game.order_history
    assert not game.state_history
    game.new_system_message('FRANCE', 'Hello France!')
    game.new_system_message('GLOBAL', 'Hello World!')
    game.set_orders('FRANCE', ['A PAR H'])
    assert len(game.messages) == 2
    previous_phase = game.get_current_phase()
    game.process()
    current_phase = game.get_current_phase()
    assert previous_phase != current_phase, (previous_phase, current_phase)
    assert not game.messages
    assert len(game.message_history) == 1
    assert len(game.order_history) == 1
    assert len(game.state_history) == 1
    game.set_orders('AUSTRIA', ['A BUD - GAL'])
    game.set_orders('FRANCE', ['A PAR H'])
    game.new_system_message('GLOBAL', 'New world.')
    assert len(game.messages) == 1
    game.process()
    assert not game.messages
    assert len(game.message_history) == 2
    assert len(game.order_history) == 2
    assert len(game.state_history) == 2
    assert all((p1 == p2 == p3) for (p1, p2, p3) in zip(game.message_history.keys(),
                                                        game.order_history.keys(),
                                                        game.state_history.keys()))

    assert game.map.compare_phases(str(game.state_history.first_key()), str(game.state_history.last_key())) < 0

    messages_phase_1 = list(game.message_history.first_value().values())
    messages_phase_2 = list(game.message_history.last_value().values())
    orders_phase_1 = game.order_history.first_value()
    orders_phase_2 = game.order_history.last_value()
    assert len(messages_phase_1) == 2
    assert len(messages_phase_2) == 1
    assert messages_phase_1[0].message == 'Hello France!'
    assert messages_phase_1[1].message == 'Hello World!'
    assert messages_phase_2[0].message == 'New world.'
    assert orders_phase_1['FRANCE'] == ['A PAR H']
    assert orders_phase_2['FRANCE'] == ['A PAR H']
    assert orders_phase_2['AUSTRIA'] == ['A BUD - GAL']

    assert all('messages' not in state for state in game.state_history.values())

    game_to_json = game.to_dict()
    game_copy = ServerGame.from_dict(game_to_json)
    assert list(game.state_history.keys()) == list(game_copy.state_history.keys())
    assert list(game.message_history.keys()) == list(game_copy.message_history.keys())
    assert list(game.order_history.keys()) == list(game_copy.order_history.keys())
    # Check histories in game copy.
    messages_phase_1 = list(game_copy.message_history.first_value().values())
    messages_phase_2 = list(game_copy.message_history.last_value().values())
    orders_phase_1 = game_copy.order_history.first_value()
    orders_phase_2 = game_copy.order_history.last_value()
    assert len(messages_phase_1) == 2
    assert len(messages_phase_2) == 1
    assert messages_phase_1[0].message == 'Hello France!'
    assert messages_phase_1[1].message == 'Hello World!'
    assert messages_phase_2[0].message == 'New world.'
    assert orders_phase_1['FRANCE'] == ['A PAR H']
    assert orders_phase_2['FRANCE'] == ['A PAR H']
    assert orders_phase_2['AUSTRIA'] == ['A BUD - GAL']

def test_result_history():
    """ Test result history. """
    short_phase_name = 'S1901M'
    game = Game()
    game.set_orders('FRANCE', ['A PAR - BUR', 'A MAR - BUR'])
    assert game.current_short_phase == short_phase_name
    game.process()
    assert game.current_short_phase == 'F1901M'
    phase_data = game.get_phase_from_history(short_phase_name)
    assert BOUNCE in phase_data.results['A PAR']
    assert BOUNCE in phase_data.results['A MAR']

def test_unit_owner():
    """ Test Unit Owner Resolver making sure the cached results are correct """
    game = Game()
    print(game.get_units('RUSSIA'))

    assert game._unit_owner('F STP/SC', coast_required=1) is game.get_power('RUSSIA')                                   # pylint: disable=protected-access
    assert game._unit_owner('F STP/SC', coast_required=0) is game.get_power('RUSSIA')                                   # pylint: disable=protected-access

    assert game._unit_owner('F STP', coast_required=1) is None                                                          # pylint: disable=protected-access
    assert game._unit_owner('F STP', coast_required=0) is game.get_power('RUSSIA')                                      # pylint: disable=protected-access

    assert game._unit_owner('A WAR', coast_required=0) is game.get_power('RUSSIA')                                      # pylint: disable=protected-access
    assert game._unit_owner('A WAR', coast_required=1) is game.get_power('RUSSIA')                                      # pylint: disable=protected-access

    assert game._unit_owner('F SEV', coast_required=0) is game.get_power('RUSSIA')                                      # pylint: disable=protected-access
    assert game._unit_owner('F SEV', coast_required=1) is game.get_power('RUSSIA')                                      # pylint: disable=protected-access