Skip to content

Commit 11565f4

Browse files
oyamadmmcky
authored andcommitted
TEST: Add tests for compute_fp.py (QuantEcon#309)
* TEST: Add tests for compute_fp.py * TEST: Add `show_missing = True` to coveragerc http://stackoverflow.com/questions/37733194/python-nosetests-with-coverage-no-longer-shows-missing-lines * TEST: Add `# pragma: no cover`
1 parent 5fe79e3 commit 11565f4

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

.coveragerc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ omit =
99
*/unittest2/*
1010

1111
[report]
12+
show_missing = True
1213
# Regexes for lines to exclude from consideration
1314
exclude_lines =
1415
# Have to re-enable the standard pragma

quantecon/compute_fp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ def _square_sum(a):
364364
return _square_sum_array
365365

366366

367-
def _square_sum_array(a):
367+
def _square_sum_array(a): # pragma: no cover
368368
sum_ = 0
369369
for x in a.flat:
370370
sum_ += x**2

quantecon/tests/test_compute_fp.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from __future__ import division
1616
import unittest
1717
import numpy as np
18+
from nose.tools import ok_, raises
1819
from quantecon import compute_fixed_point
1920

2021

@@ -91,3 +92,53 @@ def test_imitation_game_method(self):
9192
abs(self.T(fp_computed, mu=mu) - fp_computed).max() <=
9293
error_tol
9394
)
95+
96+
97+
class TestComputeFPContraction():
98+
def setUp(self):
99+
self.coeff = 0.5
100+
self.methods = ['iteration', 'imitation_game']
101+
102+
def f(self, x):
103+
return self.coeff * x
104+
105+
def test_num_iter_one(self):
106+
init = 1.
107+
error_tol = self.coeff
108+
109+
for method in self.methods:
110+
fp_computed = compute_fixed_point(self.f, init,
111+
error_tol=error_tol,
112+
method=method)
113+
ok_(fp_computed <= error_tol * 2)
114+
115+
def test_num_iter_large(self):
116+
init = 1.
117+
buff_size = 2**8 # buff_size in 'imitation_game'
118+
max_iter = buff_size + 2
119+
error_tol = self.coeff**max_iter
120+
121+
for method in self.methods:
122+
fp_computed = compute_fixed_point(self.f, init,
123+
error_tol=error_tol,
124+
max_iter=max_iter, method=method,
125+
print_skip=max_iter)
126+
ok_(fp_computed <= error_tol * 2)
127+
128+
def test_2d_input(self):
129+
error_tol = self.coeff**4
130+
131+
for method in self.methods:
132+
init = np.array([[-1, 0.5], [-1/3, 0.1]])
133+
fp_computed = compute_fixed_point(self.f, init,
134+
error_tol=error_tol,
135+
method=method)
136+
ok_((fp_computed <= error_tol * 2).all())
137+
138+
139+
@raises(ValueError)
140+
def test_raises_value_error_nonpositive_max_iter():
141+
f = lambda x: 0.5*x
142+
init = 1.
143+
max_iter = 0
144+
fp = compute_fixed_point(f, init, max_iter=max_iter)

0 commit comments

Comments
 (0)