22Tests for game_theory/game_converters.py
33
44"""
5+ import io
56import os
67from tempfile import NamedTemporaryFile
8+ from unittest .mock import patch
79import numpy as np
810from numpy .testing import (
911 assert_ , assert_array_equal , assert_string_equal , assert_raises
1012)
1113from quantecon .game_theory import (
12- Player , NormalFormGame , random_game , GAMWriter , to_gam
14+ Player , NormalFormGame , random_game ,
15+ GAMWriter , to_gam , from_gam_string , from_gam_url
1316)
1417from quantecon .game_theory .game_converters import GAMPayoffVector
1518
@@ -158,6 +161,11 @@ def test_to_gam(self):
158161
159162 os .remove (temp_path )
160163
164+ def test_from_gam_string (self ):
165+ g2 = from_gam_string (self .s_desired )
166+ assert_array_equal (g2 .payoff_profile_array ,
167+ self .g .payoff_profile_array )
168+
161169
162170def test_gam_writer_many_actions ():
163171 n0 , n1 = 40 , 60
@@ -181,3 +189,45 @@ def test_gam_writer_many_actions():
181189
182190 expected = N * np .prod (nums_actions )
183191 assert_ (len (payoff_tokens ) == expected )
192+
193+
194+ # GAMReader/from_gam #
195+
196+ def test_from_gam_string ():
197+ s = """\
198+ 2
199+ 3 2
200+
201+ 3 2 0 3 5 6 3 2 3 2 6 1"""
202+
203+ g = from_gam_string (s )
204+
205+ expected = NormalFormGame ([
206+ [(3 , 3 ), (3 , 2 )],
207+ [(2 , 2 ), (5 , 6 )],
208+ [(0 , 3 ), (6 , 1 )],
209+ ])
210+
211+ assert_array_equal (g .payoff_profile_array , expected .payoff_profile_array )
212+
213+
214+ class _FakeResponse (io .BytesIO ):
215+ def __enter__ (self ): return self
216+ def __exit__ (self , exc_type , exc , tb ): self .close ()
217+
218+
219+ def test_from_gam_url ():
220+ s = """\
221+ 2
222+ 3 2
223+
224+ 3 2 0 3 5 6 3 2 3 2 6 1"""
225+
226+ def fake_urlopen (url ):
227+ return _FakeResponse (s .encode ("utf-8" ))
228+
229+ with patch ("urllib.request.urlopen" , fake_urlopen ):
230+ g_url = from_gam_url ("http://example.com/game.gam" )
231+
232+ g_str = from_gam_string (s )
233+ assert_array_equal (g_url .payoff_profile_array , g_str .payoff_profile_array )
0 commit comments