diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 81db0676..5b81cabd 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -68,7 +68,7 @@ bpl-test crossmod_exception [ bpl-test minimal ] [ bpl-test args ] [ bpl-test raw_ctor ] -[ bpl-test numpy ] +[ bpl-test numpy : printer.py numeric_tests.py numarray_tests.py numpy.py numpy.cpp ] [ bpl-test enum ] [ bpl-test exception_translator ] [ bpl-test pearu1 : test_cltree.py cltree.cpp ] diff --git a/test/numarray_tests.py b/test/numarray_tests.py new file mode 100644 index 00000000..be3d9d4e --- /dev/null +++ b/test/numarray_tests.py @@ -0,0 +1,63 @@ +# Copyright David Abrahams 2006. Distributed under the Boost +# Software License, Version 1.0. (See accompanying +# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +import printer + +# So we can coerce portably across Python versions +bool = type(1 == 1) + +''' +>>> from numpy_ext import * +>>> x = new_array() +>>> y = x.copy() +>>> p = _printer() +>>> check = p.check +>>> exercise_numarray(x, p) + +>>> check(str(y)) + +>>> check(y.argmax()); +>>> check(y.argmax(0)); + +>>> check(y.argmin()); +>>> check(y.argmin(0)); + +>>> check(y.argsort()); +>>> check(y.argsort(1)); + +>>> y.byteswap(); +>>> check(y); + +>>> check(y.diagonal()); +>>> check(y.diagonal(1)); +>>> check(y.diagonal(0, 0)); +>>> check(y.diagonal(0, 1, 0)); + +>>> check(y.is_c_array()); + +# coerce because numarray still returns an int and the C++ interface forces +# the return type to bool +>>> check( bool(y.isbyteswapped()) ); + +>>> check(y.trace()); +>>> check(y.trace(1)); +>>> check(y.trace(0, 0)); +>>> check(y.trace(0, 1, 0)); + +>>> check(y.new('D').getshape()); +>>> check(y.new('D').type()); +>>> y.sort(); +>>> check(y); +>>> check(y.type()); + +>>> check(y.factory((1.2, 3.4))); +>>> check(y.factory((1.2, 3.4), "f8")) +>>> check(y.factory((1.2, 3.4), "f8", true)) +>>> check(y.factory((1.2, 3.4), "f8", true, false)) +>>> check(y.factory((1.2, 3.4), "f8", true, false, None)) +>>> check(y.factory((1.2, 3.4), "f8", true, false, None, (1,2,1))) + +>>> p.results +[] +>>> del p +''' diff --git a/test/numeric_tests.py b/test/numeric_tests.py new file mode 100644 index 00000000..569ec19e --- /dev/null +++ b/test/numeric_tests.py @@ -0,0 +1,39 @@ +# Copyright David Abrahams 2006. Distributed under the Boost +# Software License, Version 1.0. (See accompanying +# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +import printer +''' +>>> from numpy_ext import * +>>> x = new_array() +>>> x[1,1] = 0.0 + +>>> try: take_array(3) +... except TypeError: pass +... else: print 'expected a TypeError' + +>>> take_array(x) + +>>> print x +[[1 2 3] + [4 0 6] + [7 8 9]] + +>>> y = x.copy() + + +>>> p = _printer() +>>> check = p.check +>>> exercise(x, p) +>>> y[2,1] = 3 +>>> check(y); + +>>> check(y.astype('D')); + +>>> check(y.copy()); + +>>> check(y.typecode()); + +>>> p.results +[] +>>> del p +''' diff --git a/test/numpy.py b/test/numpy.py index af3e90ed..84e8313d 100644 --- a/test/numpy.py +++ b/test/numpy.py @@ -2,149 +2,18 @@ # Software License, Version 1.0. (See accompanying # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -# Unfortunately the doctest module works differently in Python versions -# 2.2, 2.3, and 2.4. Newer versions evaluate all docstrings, even that -# of objects with names starting with an underscore. To portably disable -# tests based on the availability of Numeric and numarray, the corresponding -# test functions are simply deleted below if necessary. - -# So we can coerce portably across Python versions -bool = type(1 == 1) - -def numeric_tests(): - ''' - >>> from numpy_ext import * - >>> x = new_array() - >>> x[1,1] = 0.0 - - >>> try: take_array(3) - ... except TypeError: pass - ... else: print 'expected a TypeError' - - >>> take_array(x) - - >>> print x - [[1 2 3] - [4 0 6] - [7 8 9]] - - >>> y = x.copy() - - - >>> p = _printer() - >>> check = p.check - >>> exercise(x, p) - >>> y[2,1] = 3 - >>> check(y); - - >>> check(y.astype('D')); - - >>> check(y.copy()); - - >>> check(y.typecode()); - - >>> p.results - [] - >>> del p - ''' - pass - -def _numarray_tests(): - ''' - >>> from numpy_ext import * - >>> x = new_array() - >>> y = x.copy() - >>> p = _printer() - >>> check = p.check - >>> exercise_numarray(x, p) - - >>> check(str(y)) - - >>> check(y.argmax()); - >>> check(y.argmax(0)); - - >>> check(y.argmin()); - >>> check(y.argmin(0)); - - >>> check(y.argsort()); - >>> check(y.argsort(1)); - - >>> y.byteswap(); - >>> check(y); - - >>> check(y.diagonal()); - >>> check(y.diagonal(1)); - >>> check(y.diagonal(0, 0)); - >>> check(y.diagonal(0, 1, 0)); - - >>> check(y.is_c_array()); - - # coerce because numarray still returns an int and the C++ interface forces - # the return type to bool - >>> check( bool(y.isbyteswapped()) ); - - >>> check(y.trace()); - >>> check(y.trace(1)); - >>> check(y.trace(0, 0)); - >>> check(y.trace(0, 1, 0)); - - >>> check(y.new('D').getshape()); - >>> check(y.new('D').type()); - >>> y.sort(); - >>> check(y); - >>> check(y.type()); - - >>> check(y.factory((1.2, 3.4))); - >>> check(y.factory((1.2, 3.4), "f8")) - >>> check(y.factory((1.2, 3.4), "f8", true)) - >>> check(y.factory((1.2, 3.4), "f8", true, false)) - >>> check(y.factory((1.2, 3.4), "f8", true, false, None)) - >>> check(y.factory((1.2, 3.4), "f8", true, false, None, (1,2,1))) - - >>> p.results - [] - >>> del p - ''' - pass - false = 0; true = 1; -class _printer(object): - def __init__(self): - self.results = []; - def __call__(self, *stuff): - for x in stuff: - self.results.append(str(x)) - def check(self, x): - if self.results[0] != str(x): - print ' Expected:\n %s\n but the C++ interface gave:\n %s' % (x, self.results[0]) - del self.results[0] -def _count_failures(test_names = ('numeric_tests',)): - '''Given a sequence of test function names, run all the doctests associated - with each function and return the total number of failures. Works portably - across versions of doctest.''' - - import doctest - if hasattr(doctest, 'DocTestFinder'): - # Newer doctest fails to work with the old idiom, even though it's only - # marked "deprecated." - failures = 0 - for n in test_names: - for t in doctest.DocTestFinder().find(eval(n)): - print 'test:', t - failures += doctest.DocTestRunner().run(t)[0] - return failures - - else: - global __test__ - __test__ = {} - for t in test_names: - __test__[t] = eval(t) - return doctest.testmod(sys.modules.get(__name__))[0] - +import doctest, numeric_tests +def _count_failures(test_modules = (numeric_tests,)): + failures = 0 + for m in test_modules: + failures += doctest.testmod(m)[0] + return failures + def _run(args = None): - import sys + import sys, numarray_tests, numeric_tests if args is not None: sys.argv = args @@ -193,14 +62,13 @@ def _run(args = None): numpy_ext.set_module_and_type('Numeric', 'ArrayType') failures += _count_failures() - - global __test__ + if has_numarray: print 'testing numarray module explicitly' numpy_ext.set_module_and_type('numarray', 'NDArray') # Add the _numarray_tests to the list of things to test in # this case. - failures += _count_failures(('_numarray_tests', 'numeric_tests')) + failures += _count_failures((numarray_tests, numeric_tests)) # see that we can go back to the default numpy_ext.set_module_and_type('', '') diff --git a/test/printer.py b/test/printer.py new file mode 100644 index 00000000..e08f7c45 --- /dev/null +++ b/test/printer.py @@ -0,0 +1,13 @@ +# Copyright David Abrahams 2006. Distributed under the Boost +# Software License, Version 1.0. (See accompanying +# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +class _printer(object): + def __init__(self): + self.results = []; + def __call__(self, *stuff): + for x in stuff: + self.results.append(str(x)) + def check(self, x): + if self.results[0] != str(x): + print ' Expected:\n %s\n but the C++ interface gave:\n %s' % (x, self.results[0]) + del self.results[0]