2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-21 05:02:17 +00:00

Fix lots of bugs in the numeric interface and tests.

Tests:
* Coerce a result to bool to deal with Python's new Bool type
* Better reporting of mismatches in expected and received results
* Remove bogus nullary y.astype() call
* Fix all uses of trace and diagonal so they don't cause errors
* Use appropriate typecodes
* Use doctest detailed API to run just the relevant tests
* Factor out error handling from macro

API:
* Added get_module_name() function to get current numeric module
* new_(x) now returns an array instead of object
* Fixed the signatures of the factory() family of functions
* Updated docs accordingly.


[SVN r35528]
This commit is contained in:
Dave Abrahams
2006-10-09 04:05:25 +00:00
parent 545be29ad3
commit 49d4aac8ec
5 changed files with 267 additions and 210 deletions

View File

@@ -8,6 +8,9 @@
# 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 *
@@ -55,8 +58,8 @@ def _numarray_tests():
>>> check = p.check
>>> exercise_numarray(x, p)
>>> check(y.astype());
>>> check(str(y))
>>> check(y.argmax());
>>> check(y.argmax(0));
@@ -68,30 +71,35 @@ def _numarray_tests():
>>> y.byteswap();
>>> check(y);
>>> check(y.diagonal());
>>> check(y.diagonal(1));
>>> check(y.diagonal(0, 1));
>>> check(y.diagonal(0, 0));
>>> check(y.diagonal(0, 1, 0));
>>> check(y.is_c_array());
>>> check(y.isbyteswapped());
# 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, 1));
>>> check(y.trace(0, 0));
>>> check(y.trace(0, 1, 0));
>>> check(y.new('D'));
>>> 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), "Double"));
>>> check(y.factory((1.2, 3.4), "Double", (1,2,1)));
>>> check(y.factory((1.2, 3.4), "Double", (2,1,1), false));
>>> check(y.factory((1.2, 3.4), "Double", (2,), true, true));
>>> 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
[]
@@ -105,12 +113,12 @@ class _printer(object):
def __init__(self):
self.results = [];
def __call__(self, *stuff):
self.results += [ str(x) for x in stuff ]
for x in stuff:
self.results.append(str(x))
def check(self, x):
if self.results[0] == str(x):
del self.results[0]
else:
print ' Expected:\n %s\n but got:\n %s' % (x, self.results[0])
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 _run(args = None):
import sys
@@ -150,21 +158,29 @@ def _run(args = None):
failures = 0
find = doctest.DocTestFinder().find
run = doctest.DocTestRunner().run
#
# Run tests 4 different ways if both modules are installed, just
# to show that set_module_and_type() is working properly
#
# run all the tests with default module search
print 'testing default extension module'
failures += doctest.testmod(sys.modules.get(__name__))[0]
print 'testing default extension module:', \
numpy_ext.get_module_name() or '[numeric support not installed]'
for test in find(numeric_tests):
failures += run(test)[0]
# test against Numeric if installed
if has_numeric:
print 'testing Numeric module explicitly'
numpy_ext.set_module_and_type('Numeric', 'ArrayType')
failures += doctest.testmod(sys.modules.get(__name__))[0]
for test in find(numeric_tests):
failures += run(test)[0]
global __test__
if has_numarray:
# Add the _numarray_tests to the list of things to test in
@@ -173,13 +189,18 @@ def _run(args = None):
'numeric_tests': numeric_tests }
print 'testing numarray module explicitly'
numpy_ext.set_module_and_type('numarray', 'NDArray')
failures += doctest.testmod(sys.modules.get(__name__))[0]
for test in find(numeric_tests) + find(_numarray_tests):
failures += run(test)[0]
del __test__
# see that we can go back to the default
print 'testing default module again'
numpy_ext.set_module_and_type('', '')
failures += doctest.testmod(sys.modules.get(__name__))[0]
print 'testing default module again:', \
numpy_ext.get_module_name() or '[numeric support not installed]'
for test in find(numeric_tests):
failures += run(test)[0]
return failures