Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use pytest #2

Merged
merged 2 commits into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
72 changes: 0 additions & 72 deletions test/test_irr.py

This file was deleted.

74 changes: 74 additions & 0 deletions test_irr.py
Original file line number Diff line number Diff line change
@@ -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')