From f1a61f2c991fea0b8d3ae2dc575a1cea917adb98 Mon Sep 17 00:00:00 2001 From: Erik Bernhardsson Date: Tue, 31 Oct 2017 09:14:35 -0400 Subject: [PATCH 1/2] use pytest --- test/test_irr.py | 72 ---------------------------------------------- test_irr.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 72 deletions(-) delete mode 100644 test/test_irr.py create mode 100644 test_irr.py diff --git a/test/test_irr.py b/test/test_irr.py deleted file mode 100644 index 6d82cc6..0000000 --- a/test/test_irr.py +++ /dev/null @@ -1,72 +0,0 @@ -import unittest, random, math, numpy, time, functools -import irr - -class _Base(unittest.TestCase): - def test(self): - for test in range(1000): - d, r = self.case() - self.assertAlmostEqual(irr.irr(d), r) - - -class SimpleBondTest(_Base): - def case(self): - r = math.exp(random.gauss(0, 1)) - 1 - x = random.gauss(0, 1) - d = [x / (1 + r), -x] - return d, r - - -class SlightlyLongerBondTest(_Base): - def case(self, n=10): - r = math.exp(random.gauss(0, 1)) - 1 - x = random.gauss(0, 1) - d = [x] + [0.0] * (n-2) + [-x * (1+r)**(n-1)] - return d, r - - -class MoreNonzeroTest(_Base): - def case(self, n=10): - r = math.exp(random.gauss(0, 1)) - 1 - d = [random.random() for i in range(n-1)] - d.append(-sum([x * (1+r)**(n-i-1) for i, x in enumerate(d)])) - return d, r - - -class PerformanceTest(unittest.TestCase): - def test(self): - us_times = [] - np_times = [] - ns = [10, 20, 50, 100] - for n in ns: - k = 100 - sums = [0.0, 0.0] - for j in range(k): - r = math.exp(random.gauss(0, 1.0 / n)) - 1 - x = random.gauss(0, 1) - d = [x] + [0.0] * (n-2) + [-x * (1+r)**(n-1)] - - results = [] - for i, f in enumerate([irr.irr, numpy.irr]): - t0 = time.time() - results.append(f(d)) - sums[i] += time.time() - t0 - - if not numpy.isnan(results[1]): - self.assertAlmostEqual(results[0], results[1]) - for times, sum in zip([us_times, np_times], sums): - times.append(sum/k) - - try: - from matplotlib import pyplot - import seaborn - except ImportError: - return - - pyplot.plot(ns, us_times, label='Our library') - pyplot.plot(ns, np_times, label='Numpy') - pyplot.xlabel('n') - pyplot.ylabel('time(s)') - pyplot.yscale('log') - pyplot.savefig('plot.png') - - diff --git a/test_irr.py b/test_irr.py new file mode 100644 index 0000000..69d749c --- /dev/null +++ b/test_irr.py @@ -0,0 +1,74 @@ +import pytest, random, math, numpy, time, functools +import irr + + +def run_many(case): + @functools.wraps(case) + def wrapped(): + for test in range(1000): + d, r = case() + assert irr.irr(d) == pytest.approx(r) + return wrapped + + +@run_many +def test_simple_bond(): + r = math.exp(random.gauss(0, 1)) - 1 + x = random.gauss(0, 1) + d = [x / (1 + r), -x] + return d, r + + +@run_many +def test_slightly_longer_bond(n=10): + r = math.exp(random.gauss(0, 1)) - 1 + x = random.gauss(0, 1) + d = [x] + [0.0] * (n-2) + [-x * (1+r)**(n-1)] + return d, r + + +@run_many +def test_more_nonzero(n=10): + r = math.exp(random.gauss(0, 1)) - 1 + d = [random.random() for i in range(n-1)] + d.append(-sum([x * (1+r)**(n-i-1) for i, x in enumerate(d)])) + return d, r + + +def test_performance(): + us_times = [] + np_times = [] + ns = [10, 20, 50, 100] + for n in ns: + k = 100 + sums = [0.0, 0.0] + for j in range(k): + r = math.exp(random.gauss(0, 1.0 / n)) - 1 + x = random.gauss(0, 1) + d = [x] + [0.0] * (n-2) + [-x * (1+r)**(n-1)] + + results = [] + for i, f in enumerate([irr.irr, numpy.irr]): + t0 = time.time() + results.append(f(d)) + sums[i] += time.time() - t0 + + if not numpy.isnan(results[1]): + assert results[0] == pytest.approx(results[1]) + for times, sum in zip([us_times, np_times], sums): + times.append(sum/k) + + try: + from matplotlib import pyplot + import seaborn + except ImportError: + return + + pyplot.plot(ns, us_times, label='Our library') + pyplot.plot(ns, np_times, label='Numpy') + pyplot.xlabel('n') + pyplot.ylabel('time(s)') + pyplot.yscale('log') + pyplot.savefig('plot.png') + + From 8efdc14cd15c71c797170c0b7c4235eae4701b9b Mon Sep 17 00:00:00 2001 From: Erik Bernhardsson Date: Tue, 31 Oct 2017 09:18:59 -0400 Subject: [PATCH 2/2] drop 3.2 and 3.3, add 3.6 --- .travis.yml | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4d2283f..34f4f1d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,11 +2,9 @@ language: python python: - "2.6" - "2.7" - - "3.2" - - "3.3" - "3.4" - "3.5" - - "3.5-dev" # 3.5 development branch - - "nightly" # currently points to 3.6-dev -install: "pip install nose numpy" -script: nosetests + - "3.6" + - "nightly" +install: "pip install pytest numpy" +script: pytest