Skip to content

Commit 9e61b67

Browse files
committed
TEST: Add tests in test_game_converters.py
1 parent e8c5f77 commit 9e61b67

1 file changed

Lines changed: 51 additions & 1 deletion

File tree

quantecon/game_theory/tests/test_game_converters.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
Tests for game_theory/game_converters.py
33
44
"""
5+
import io
56
import os
67
from tempfile import NamedTemporaryFile
8+
from unittest.mock import patch
79
import numpy as np
810
from numpy.testing import (
911
assert_, assert_array_equal, assert_string_equal, assert_raises
1012
)
1113
from 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
)
1417
from 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

162170
def 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

Comments
 (0)