44import axelrod
55from axelrod import Actions
66
7+ from hypothesis import given , example
8+ from hypothesis .strategies import integers , floats , random_module , assume
9+
710C , D = Actions .C , Actions .D
811
912
1013class TestMatch (unittest .TestCase ):
1114
12- def test_init (self ):
15+ @given (turns = integers (min_value = 1 , max_value = 200 ),
16+ prob_end = floats (min_value = 0 , max_value = 1 ))
17+ @example (turns = 5 , prob_end = None )
18+ def test_init (self , turns , prob_end ):
1319 p1 , p2 = axelrod .Cooperator (), axelrod .Cooperator ()
14- match = axelrod .Match ((p1 , p2 ), 5 )
20+ match = axelrod .Match ((p1 , p2 ), turns , prob_end = prob_end )
21+ self .assertEqual (match .result , [])
22+ self .assertEqual (match ._player1 , p1 )
23+ self .assertEqual (match ._player2 , p2 )
24+ self .assertEqual (
25+ match ._classes , (axelrod .Cooperator , axelrod .Cooperator ))
26+ self .assertEqual (match ._turns , turns )
27+ self .assertEqual (match ._prob_end , prob_end )
28+ self .assertEqual (match ._cache , {})
29+ self .assertEqual (match ._cache_mutable , True )
30+ self .assertEqual (match ._noise , 0 )
31+
32+ # Checking that prob_end has default None
33+ match = axelrod .Match ((p1 , p2 ), turns )
1534 self .assertEqual (match .result , [])
1635 self .assertEqual (match ._player1 , p1 )
1736 self .assertEqual (match ._player2 , p2 )
1837 self .assertEqual (
1938 match ._classes , (axelrod .Cooperator , axelrod .Cooperator ))
20- self .assertEqual (match ._turns , 5 )
39+ self .assertEqual (match ._turns , turns )
40+ self .assertEqual (match ._prob_end , None )
2141 self .assertEqual (match ._cache , {})
2242 self .assertEqual (match ._cache_mutable , True )
2343 self .assertEqual (match ._noise , 0 )
2444
25- def test_stochastic (self ):
45+ @given (p = floats (min_value = 0 , max_value = 1 ),
46+ rm = random_module ())
47+ def test_stochastic (self , p , rm ):
48+
49+ assume (0 < p < 1 )
50+
2651 p1 , p2 = axelrod .Cooperator (), axelrod .Cooperator ()
2752 match = axelrod .Match ((p1 , p2 ), 5 )
2853 self .assertFalse (match ._stochastic )
2954
30- match = axelrod .Match ((p1 , p2 ), 5 , noise = 0.2 )
55+ match = axelrod .Match ((p1 , p2 ), 5 , noise = p )
56+ self .assertTrue (match ._stochastic )
57+
58+ match = axelrod .Match ((p1 , p2 ), 5 , prob_end = p )
3159 self .assertTrue (match ._stochastic )
3260
3361 p1 = axelrod .Random ()
3462 match = axelrod .Match ((p1 , p2 ), 5 )
3563 self .assertTrue (match ._stochastic )
3664
37- def test_cache_update_required (self ):
65+ @given (p = floats (min_value = 0 , max_value = 1 ),
66+ rm = random_module ())
67+ def test_cache_update_required (self , p , rm ):
68+
69+ assume (0 < p < 1 )
70+
3871 p1 , p2 = axelrod .Cooperator (), axelrod .Cooperator ()
39- match = axelrod .Match ((p1 , p2 ), 5 , noise = 0.2 )
72+ match = axelrod .Match ((p1 , p2 ), 5 , noise = p )
73+ self .assertFalse (match ._cache_update_required )
74+
75+ match = axelrod .Match ((p1 , p2 ), 5 , prob_end = p )
4076 self .assertFalse (match ._cache_update_required )
4177
4278 match = axelrod .Match ((p1 , p2 ), 5 , cache_mutable = False )
@@ -64,6 +100,30 @@ def test_play(self):
64100 match = axelrod .Match (players , 3 , cache )
65101 self .assertEqual (match .play (), expected_result )
66102
103+ @given (turns = integers (min_value = 1 , max_value = 200 ),
104+ prob_end = floats (min_value = 0 , max_value = 1 ),
105+ rm = random_module ())
106+ def test_prob_end_play (self , turns , prob_end , rm ):
107+
108+ players = (axelrod .Cooperator (), axelrod .Defector ())
109+ match = axelrod .Match (players , turns , prob_end = prob_end )
110+ self .assertTrue (0 <= len (match .play ()))
111+
112+ # If game has no ending the length will be turns
113+ match = axelrod .Match (players , turns , prob_end = 0 )
114+ self .assertEqual (len (match .play ()), turns )
115+
116+ # If game has 1 prob of ending it lasts only one turn
117+ match = axelrod .Match (players , turns , prob_end = 1 )
118+ self .assertEqual (len (match .play ()), 1 )
119+
120+ @given (prob_end = floats (min_value = 0.25 , max_value = 0.75 ),
121+ rm = random_module ())
122+ def test_prob_end_play_with_no_turns (self , prob_end , rm ):
123+ players = (axelrod .Cooperator (), axelrod .Defector ())
124+ match = axelrod .Match (players , float ("inf" ), prob_end = prob_end )
125+ self .assertTrue (0 <= len (match .play ()))
126+
67127 def test_sparklines (self ):
68128 players = (axelrod .Cooperator (), axelrod .Alternator ())
69129 match = axelrod .Match (players , 4 )
@@ -72,4 +132,3 @@ def test_sparklines(self):
72132 self .assertEqual (match .sparklines (), expected_sparklines )
73133 expected_sparklines = u'XXXX\n XYXY'
74134 self .assertEqual (match .sparklines ('X' , 'Y' ), expected_sparklines )
75-
0 commit comments