From 52eaa1cc80f92eb6db8d72de5851afc913828e1b Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Sun, 14 May 2017 23:04:13 +0900 Subject: [PATCH 1/3] TEST: Add tests for compute_fp.py --- quantecon/tests/test_compute_fp.py | 51 ++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/quantecon/tests/test_compute_fp.py b/quantecon/tests/test_compute_fp.py index 4fe880b0d..2d904f5c0 100644 --- a/quantecon/tests/test_compute_fp.py +++ b/quantecon/tests/test_compute_fp.py @@ -15,6 +15,7 @@ from __future__ import division import unittest import numpy as np +from nose.tools import ok_, raises from quantecon import compute_fixed_point @@ -91,3 +92,53 @@ def test_imitation_game_method(self): abs(self.T(fp_computed, mu=mu) - fp_computed).max() <= error_tol ) + + +class TestComputeFPContraction(): + def setUp(self): + self.coeff = 0.5 + self.methods = ['iteration', 'imitation_game'] + + def f(self, x): + return self.coeff * x + + def test_num_iter_one(self): + init = 1. + error_tol = self.coeff + + for method in self.methods: + fp_computed = compute_fixed_point(self.f, init, + error_tol=error_tol, + method=method) + ok_(fp_computed <= error_tol * 2) + + def test_num_iter_large(self): + init = 1. + buff_size = 2**8 # buff_size in 'imitation_game' + max_iter = buff_size + 2 + error_tol = self.coeff**max_iter + + for method in self.methods: + fp_computed = compute_fixed_point(self.f, init, + error_tol=error_tol, + max_iter=max_iter, method=method, + print_skip=max_iter) + ok_(fp_computed <= error_tol * 2) + + def test_2d_input(self): + error_tol = self.coeff**4 + + for method in self.methods: + init = np.array([[-1, 0.5], [-1/3, 0.1]]) + fp_computed = compute_fixed_point(self.f, init, + error_tol=error_tol, + method=method) + ok_((fp_computed <= error_tol * 2).all()) + + +@raises(ValueError) +def test_raises_value_error_nonpositive_max_iter(): + f = lambda x: 0.5*x + init = 1. + max_iter = 0 + fp = compute_fixed_point(f, init, max_iter=max_iter) From 0cb8910ed4a658f7c9423c9cdcabe9ce0272b4a4 Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Sun, 14 May 2017 23:06:21 +0900 Subject: [PATCH 2/3] TEST: Add `show_missing = True` to coveragerc http://stackoverflow.com/questions/37733194/python-nosetests-with-coverage-no-longer-shows-missing-lines --- .coveragerc | 1 + 1 file changed, 1 insertion(+) diff --git a/.coveragerc b/.coveragerc index d497ce902..975e4cdf3 100644 --- a/.coveragerc +++ b/.coveragerc @@ -9,6 +9,7 @@ omit = */unittest2/* [report] +show_missing = True # Regexes for lines to exclude from consideration exclude_lines = # Have to re-enable the standard pragma From 0b705e0168afa86dbdb56dd17772a1e47d7b9b4e Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Sun, 14 May 2017 23:23:01 +0900 Subject: [PATCH 3/3] TEST: Add `# pragma: no cover` --- quantecon/compute_fp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quantecon/compute_fp.py b/quantecon/compute_fp.py index 866885d32..9b6b41404 100644 --- a/quantecon/compute_fp.py +++ b/quantecon/compute_fp.py @@ -364,7 +364,7 @@ def _square_sum(a): return _square_sum_array -def _square_sum_array(a): +def _square_sum_array(a): # pragma: no cover sum_ = 0 for x in a.flat: sum_ += x**2