[12] | 1 | # Copyright David Abrahams 2004. Distributed under the Boost |
---|
| 2 | # Software License, Version 1.0. (See accompanying |
---|
| 3 | # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
| 4 | |
---|
| 5 | # Unfortunately the doctest module works differently in Python versions |
---|
| 6 | # 2.2, 2.3, and 2.4. Newer versions evaluate all docstrings, even that |
---|
| 7 | # of objects with names starting with an underscore. To portably disable |
---|
| 8 | # tests based on the availability of Numeric and numarray, the corresponding |
---|
| 9 | # test functions are simply deleted below if necessary. |
---|
| 10 | |
---|
| 11 | def numeric_tests(): |
---|
| 12 | ''' |
---|
| 13 | >>> from numpy_ext import * |
---|
| 14 | >>> x = new_array() |
---|
| 15 | >>> x[1,1] = 0.0 |
---|
| 16 | |
---|
| 17 | >>> try: take_array(3) |
---|
| 18 | ... except TypeError: pass |
---|
| 19 | ... else: print 'expected a TypeError' |
---|
| 20 | |
---|
| 21 | >>> take_array(x) |
---|
| 22 | |
---|
| 23 | >>> print x |
---|
| 24 | [[1 2 3] |
---|
| 25 | [4 0 6] |
---|
| 26 | [7 8 9]] |
---|
| 27 | |
---|
| 28 | >>> y = x.copy() |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | >>> p = _printer() |
---|
| 32 | >>> check = p.check |
---|
| 33 | >>> exercise(x, p) |
---|
| 34 | >>> y[2,1] = 3 |
---|
| 35 | >>> check(y); |
---|
| 36 | |
---|
| 37 | >>> check(y.astype('D')); |
---|
| 38 | |
---|
| 39 | >>> check(y.copy()); |
---|
| 40 | |
---|
| 41 | >>> check(y.typecode()); |
---|
| 42 | |
---|
| 43 | >>> p.results |
---|
| 44 | [] |
---|
| 45 | >>> del p |
---|
| 46 | ''' |
---|
| 47 | pass |
---|
| 48 | |
---|
| 49 | def _numarray_tests(): |
---|
| 50 | ''' |
---|
| 51 | >>> from numpy_ext import * |
---|
| 52 | >>> x = new_array() |
---|
| 53 | >>> y = x.copy() |
---|
| 54 | >>> p = _printer() |
---|
| 55 | >>> check = p.check |
---|
| 56 | >>> exercise_numarray(x, p) |
---|
| 57 | |
---|
| 58 | >>> check(y.astype()); |
---|
| 59 | |
---|
| 60 | >>> check(y.argmax()); |
---|
| 61 | >>> check(y.argmax(0)); |
---|
| 62 | |
---|
| 63 | >>> check(y.argmin()); |
---|
| 64 | >>> check(y.argmin(0)); |
---|
| 65 | |
---|
| 66 | >>> check(y.argsort()); |
---|
| 67 | >>> check(y.argsort(1)); |
---|
| 68 | |
---|
| 69 | >>> y.byteswap(); |
---|
| 70 | >>> check(y); |
---|
| 71 | |
---|
| 72 | >>> check(y.diagonal()); |
---|
| 73 | >>> check(y.diagonal(1)); |
---|
| 74 | >>> check(y.diagonal(0, 1)); |
---|
| 75 | >>> check(y.diagonal(0, 1, 0)); |
---|
| 76 | |
---|
| 77 | >>> check(y.is_c_array()); |
---|
| 78 | >>> check(y.isbyteswapped()); |
---|
| 79 | |
---|
| 80 | >>> check(y.trace()); |
---|
| 81 | >>> check(y.trace(1)); |
---|
| 82 | >>> check(y.trace(0, 1)); |
---|
| 83 | >>> check(y.trace(0, 1, 0)); |
---|
| 84 | |
---|
| 85 | >>> check(y.new('D')); |
---|
| 86 | >>> y.sort(); |
---|
| 87 | >>> check(y); |
---|
| 88 | >>> check(y.type()); |
---|
| 89 | |
---|
| 90 | >>> check(y.array((1.2, 3.4))); |
---|
| 91 | >>> check(y.array((1.2, 3.4), "Double")); |
---|
| 92 | >>> check(y.array((1.2, 3.4), "Double", (1,2,1))); |
---|
| 93 | >>> check(y.array((1.2, 3.4), "Double", (2,1,1), false)); |
---|
| 94 | >>> check(y.array((1.2, 3.4), "Double", (2,), true, true)); |
---|
| 95 | |
---|
| 96 | >>> p.results |
---|
| 97 | [] |
---|
| 98 | >>> del p |
---|
| 99 | ''' |
---|
| 100 | pass |
---|
| 101 | |
---|
| 102 | false = 0; |
---|
| 103 | true = 1; |
---|
| 104 | class _printer(object): |
---|
| 105 | def __init__(self): |
---|
| 106 | self.results = []; |
---|
| 107 | def __call__(self, *stuff): |
---|
| 108 | self.results += [ str(x) for x in stuff ] |
---|
| 109 | def check(self, x): |
---|
| 110 | if self.results[0] == str(x): |
---|
| 111 | del self.results[0] |
---|
| 112 | else: |
---|
| 113 | print ' Expected:\n %s\n but got:\n %s' % (x, self.results[0]) |
---|
| 114 | |
---|
| 115 | def _run(args = None): |
---|
| 116 | import sys |
---|
| 117 | import doctest |
---|
| 118 | |
---|
| 119 | if args is not None: |
---|
| 120 | sys.argv = args |
---|
| 121 | |
---|
| 122 | # See which of the numeric modules are installed |
---|
| 123 | has_numeric = 0 |
---|
| 124 | try: |
---|
| 125 | import Numeric |
---|
| 126 | m = Numeric |
---|
| 127 | has_numeric = 1 |
---|
| 128 | except ImportError: |
---|
| 129 | global numeric_tests |
---|
| 130 | numeric_tests = None |
---|
| 131 | |
---|
| 132 | has_numarray = 0 |
---|
| 133 | try: |
---|
| 134 | import numarray |
---|
| 135 | m = numarray |
---|
| 136 | has_numarray = 1 |
---|
| 137 | except ImportError: |
---|
| 138 | global _numarray_tests |
---|
| 139 | _numarray_tests = None |
---|
| 140 | |
---|
| 141 | # Bail if neither one is installed |
---|
| 142 | if not (has_numeric or has_numarray): |
---|
| 143 | return 0 |
---|
| 144 | |
---|
| 145 | # test the info routine outside the doctest. See numpy.cpp for an |
---|
| 146 | # explanation |
---|
| 147 | import numpy_ext |
---|
| 148 | if (has_numarray): |
---|
| 149 | numpy_ext.info(m.array((1,2,3))) |
---|
| 150 | |
---|
| 151 | failures = 0 |
---|
| 152 | |
---|
| 153 | # |
---|
| 154 | # Run tests 4 different ways if both modules are installed, just |
---|
| 155 | # to show that set_module_and_type() is working properly |
---|
| 156 | # |
---|
| 157 | |
---|
| 158 | # run all the tests with default module search |
---|
| 159 | print 'testing default extension module' |
---|
| 160 | failures += doctest.testmod(sys.modules.get(__name__))[0] |
---|
| 161 | |
---|
| 162 | # test against Numeric if installed |
---|
| 163 | if has_numeric: |
---|
| 164 | print 'testing Numeric module explicitly' |
---|
| 165 | numpy_ext.set_module_and_type('Numeric', 'ArrayType') |
---|
| 166 | failures += doctest.testmod(sys.modules.get(__name__))[0] |
---|
| 167 | |
---|
| 168 | global __test__ |
---|
| 169 | if has_numarray: |
---|
| 170 | # Add the _numarray_tests to the list of things to test in |
---|
| 171 | # this case. |
---|
| 172 | __test__ = { 'numarray_tests':_numarray_tests, |
---|
| 173 | 'numeric_tests': numeric_tests } |
---|
| 174 | print 'testing numarray module explicitly' |
---|
| 175 | numpy_ext.set_module_and_type('numarray', 'NDArray') |
---|
| 176 | failures += doctest.testmod(sys.modules.get(__name__))[0] |
---|
| 177 | del __test__ |
---|
| 178 | |
---|
| 179 | # see that we can go back to the default |
---|
| 180 | print 'testing default module again' |
---|
| 181 | numpy_ext.set_module_and_type('', '') |
---|
| 182 | failures += doctest.testmod(sys.modules.get(__name__))[0] |
---|
| 183 | |
---|
| 184 | return failures |
---|
| 185 | |
---|
| 186 | if __name__ == '__main__': |
---|
| 187 | print "running..." |
---|
| 188 | import sys |
---|
| 189 | status = _run() |
---|
| 190 | if (status == 0): print "Done." |
---|
| 191 | sys.exit(status) |
---|