Skip to content

Commit

Permalink
Test that no slow functions creep in where they don't belong.
Browse files Browse the repository at this point in the history
  • Loading branch information
kwgoodman committed Jan 11, 2011
1 parent c34c3eb commit e8aab67
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.rst
Expand Up @@ -309,6 +309,6 @@ After you have installed Bottleneck, run the suite of unit tests::
>>> import bottleneck as bn
>>> bn.test()
<snip>
Ran 17 tests in 54.756s
Ran 32 tests in 55.756s
OK
<nose.result.TextTestResult run=17 errors=0 failures=0>
<nose.result.TextTestResult run=32 errors=0 failures=0>
108 changes: 108 additions & 0 deletions bottleneck/tests/fast_test.py
@@ -0,0 +1,108 @@
"Test that no slow functions creep in where they don't belong."

import numpy as np
import bottleneck as bn


def arrayaxis(dtypes=bn.dtypes):
"Iterator that yield arrays and axis to use for unit testing."
ss = {}
ss[1] = {'size': 4, 'shapes': [(4,)]}
ss[2] = {'size': 6, 'shapes': [(2,3)]}
ss[3] = {'size': 24, 'shapes': [(2,3,4)]}
for ndim in ss:
size = ss[ndim]['size']
shapes = ss[ndim]['shapes']
for dtype in dtypes:
a = np.arange(size, dtype=dtype)
if not issubclass(a.dtype.type, np.inexact):
for shape in shapes:
a = a.reshape(shape)
for axis in range(-a.ndim, a.ndim) + [None]:
yield a.copy(), axis

def fast_checker(selector, mode='func'):
for arr, axis in arrayaxis():
if mode == 'func':
func, a = selector(arr, axis)
elif mode == 'move':
if axis is not None:
func, a = selector(arr, 1, axis)
else:
func = np.sum
elif mode == 'group':
if axis is not None:
label = range(arr.shape[axis])
func, a, label_dict, order = selector(arr, label, None, axis)
else:
func = np.sum
else:
raise ValueError("`mode` value not recognized.")
if 'slow' in func.__name__:
raise AssertionError("slow version of func unexpectedly called.")

# Functions -----------------------------------------------------------------

def test_median_selector():
"Test median_selector."
fast_checker(bn.func.median_selector)

def test_nanmedian_selector():
"Test nanmedian_selector."
fast_checker(bn.func.nanmedian_selector)

def test_nanmin_selector():
"Test nanmin_selector."
fast_checker(bn.func.nanmin_selector)

def test_nanmax_selector():
"Test nanmax_selector."
fast_checker(bn.func.nanmax_selector)

def test_nanmean_selector():
"Test nanmean_selector."
fast_checker(bn.func.nanmean_selector)

def test_nanstd_selector():
"Test nanstd_selector."
fast_checker(bn.func.nanstd_selector)

def test_nanargmin_selector():
"Test nanargmin_selector."
fast_checker(bn.func.nanargmin_selector)

def test_nanargmax_selector():
"Test nanargmax_selector."
fast_checker(bn.func.nanargmax_selector)

def test_nanvar_selector():
"Test nanvar_selector."
fast_checker(bn.func.nanvar_selector)

# Moving functions ----------------------------------------------------------

def test_move_nanmean_selector():
"Test move_nanmean_selector."
fast_checker(bn.move.move_nanmean_selector, mode='move')

def test_move_min_selector():
"Test move_min_selector."
fast_checker(bn.move.move_min_selector, mode='move')

def test_move_max_selector():
"Test move_max_selector."
fast_checker(bn.move.move_max_selector, mode='move')

def test_move_nanmin_selector():
"Test move_nanmin_selector."
fast_checker(bn.move.move_nanmin_selector, mode='move')

def test_move_nanmixn_selector():
"Test move_nanmax_selector."
fast_checker(bn.move.move_nanmax_selector, mode='move')

# Group functions -----------------------------------------------------------

def test_group_nanmean_selector():
"Test group_nanmean_selector."
fast_checker(bn.group.group_nanmean_selector, mode='group')

0 comments on commit e8aab67

Please sign in to comment.