Skip to content

Commit 188e5ba

Browse files
QBatistammcky
authored andcommitted
Test lemke howson exceptions (#323)
* TEST: Add tests for lemke_howson.py * FIX: Add `raise TypeError` when init_pivot type is not int
1 parent 7808f02 commit 188e5ba

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

quantecon/game_theory/lemke_howson.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Lemke-Howson algorithm.
66
77
"""
8+
import numbers
89
import numpy as np
910
from numba import jit
1011
from .utilities import NashResult
@@ -128,11 +129,14 @@ def lemke_howson(g, init_pivot=0, max_iter=10**6, capping=None,
128129
nums_actions = g.nums_actions
129130
total_num = sum(nums_actions)
130131

132+
msg = '`init_pivot` must be an integer k' + \
133+
'such that 0 <= k < {0}'.format(total_num)
134+
135+
if not isinstance(init_pivot, numbers.Integral):
136+
raise TypeError(msg)
137+
131138
if not (0 <= init_pivot < total_num):
132-
raise ValueError(
133-
'`init_pivot` must be an integer k such that 0 <= k < {0}'
134-
.format(total_num)
135-
)
139+
raise ValueError(msg)
136140

137141
if capping is None:
138142
capping = max_iter

quantecon/game_theory/tests/test_lemke_howson.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"""
77
import numpy as np
88
from numpy.testing import assert_allclose
9-
from nose.tools import eq_
9+
from nose.tools import eq_, raises
1010
from quantecon.game_theory import Player, NormalFormGame, lemke_howson
1111

1212

@@ -110,6 +110,32 @@ def test_lemke_howson_capping():
110110
eq_(res.init, init_pivot-1)
111111

112112

113+
@raises(TypeError)
114+
def test_lemke_howson_invalid_g():
115+
bimatrix = [[(3, 3), (3, 2)],
116+
[(2, 2), (5, 6)],
117+
[(0, 3), (6, 1)]]
118+
lemke_howson(bimatrix)
119+
120+
121+
@raises(ValueError)
122+
def test_lemke_howson_invalid_init_pivot_integer():
123+
bimatrix = [[(3, 3), (3, 2)],
124+
[(2, 2), (5, 6)],
125+
[(0, 3), (6, 1)]]
126+
g = NormalFormGame(bimatrix)
127+
lemke_howson(g, -1)
128+
129+
130+
@raises(TypeError)
131+
def test_lemke_howson_invalid_init_pivot_float():
132+
bimatrix = [[(3, 3), (3, 2)],
133+
[(2, 2), (5, 6)],
134+
[(0, 3), (6, 1)]]
135+
g = NormalFormGame(bimatrix)
136+
lemke_howson(g, 1.0)
137+
138+
113139
if __name__ == '__main__':
114140
import sys
115141
import nose

0 commit comments

Comments
 (0)