File: testutils.py

package info (click to toggle)
python-numpysane 0.42-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 536 kB
  • sloc: python: 4,953; ansic: 655; makefile: 61
file content (291 lines) | stat: -rw-r--r-- 7,915 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
r'''A simple test harness

These should be trivial, but all the standard ones in python suck. This one
sucks far less.

'''


import sys
import numpy as np
import os
import re
from inspect import currentframe

Nchecks       = 0
NchecksFailed = 0

# no line breaks. Useful for test reporting. Yes, this sets global state, but
# we're running a test harness. This is fine
np.set_printoptions(linewidth=1e10, suppress=True)


def test_location():
    r'''Reports string describing current location in the test

    Skips over the backtrace entries that are in the test harness itself

    '''


    filename_this = os.path.split( __file__ )[1]
    if filename_this.endswith(".pyc"):
        filename_this = filename_this[:-1]

    frame = currentframe().f_back.f_back

    while frame:
        if frame.f_back is None or \
           not frame.f_code.co_filename.endswith(filename_this):
            break
        frame = frame.f_back

    testfile = os.path.split(frame.f_code.co_filename)[1]
    try:
        return "{}:{} {}()".format(testfile, frame.f_lineno, frame.f_code.co_name)
    except:
        return ''


def print_red(x):
    """print the message in red"""
    sys.stdout.write("\x1b[31m" + test_location() + ": " + x + "\x1b[0m\n")


def print_green(x):
    """Print the message in green"""
    sys.stdout.write("\x1b[32m" + test_location() + ": " + x + "\x1b[0m\n")


def confirm_equal(x, xref, msg='', eps=1e-6):
    r'''If x is equal to xref, report test success.

    msg identifies this check. eps sets the RMS equality tolerance. The x,xref
    arguments can be given as many different types. This function tries to do
    the right thing.

    '''

    global Nchecks
    global NchecksFailed
    Nchecks = Nchecks + 1

    # strip all trailing whitespace in each line, in case these are strings
    if isinstance(x, str):
        x = re.sub('[ \t]+(\n|$)', '\\1', x)
    if isinstance(xref, str):
        xref = re.sub('[ \t]+(\n|$)', '\\1', xref)

    # convert data to numpy if possible
    try:
        xref = np.array(xref)
    except:
        pass
    try:
        x = np.array(x)
    except:
        pass

    try:  # flatten array if possible
        x = x.ravel()
        xref = xref.ravel()
    except:
        pass

    try:
        N = x.shape[0]
    except:
        N = 1
    try:
        Nref = xref.shape[0]
    except:
        Nref = 1

    if N != Nref:

        print_red(("FAILED{}: mismatched array sizes: N = {} but Nref = {}. Arrays: \n" +
                   "x = {}\n" +
                   "xref = {}").
                  format((': ' + msg) if msg else '',
                         N, Nref,
                         x, xref))
        NchecksFailed = NchecksFailed + 1
        return False

    if N != 0:
        try:  # I I can subtract, get the error that way
            diff = x - xref

            def norm2sq(x):
                """Return 2 norm"""
                return np.inner(x, x)
            rms = np.sqrt(norm2sq(diff) / N)
            if not np.all(np.isfinite(rms)):
                print_red("FAILED{}: Some comparison results are NaN or Inf. "
                          "rms error = {}. x = {}, xref = {}".format(
                              (': ' + msg) if msg else '', rms, x, xref))
                NchecksFailed = NchecksFailed + 1
                return False
            if rms > eps:
                print_red("FAILED{}: rms error = {}.\nx,xref,err =\n{}".format(
                    (': ' + msg) if msg else '', rms,
                    np.vstack((x, xref, diff)).transpose()))
                NchecksFailed = NchecksFailed + 1
                return False
        except:  # Can't subtract. Do == instead
            if not np.array_equal(x, xref):
                print_red("FAILED{}: x =\n'{}', xref =\n'{}'".format(
                    (': ' + msg) if msg else '', x, xref))
                NchecksFailed = NchecksFailed + 1
                return False
    print_green("OK{}".format((': ' + msg) if msg else ''))
    return True


def confirm(x, msg=''):
    r'''If x is true, report test success.

    msg identifies this check'''

    global Nchecks
    global NchecksFailed
    Nchecks = Nchecks + 1

    if not x:
        print_red("FAILED{}".format((': ' + msg) if msg else ''))
        NchecksFailed = NchecksFailed + 1
        return False
    print_green("OK{}".format((': ' + msg) if msg else ''))
    return True


def confirm_is(x, xref, msg=''):
    r'''If x is xref, report test success.

    msg identifies this check

    '''

    global Nchecks
    global NchecksFailed
    Nchecks = Nchecks + 1

    if x is xref:
        print_green("OK{}".format((': ' + msg) if msg else ''))
        return True

    print_red("FAILED{}".format((': ' + msg) if msg else ''))
    NchecksFailed = NchecksFailed + 1
    return False


def confirm_raises(f, msg=''):
    r'''If f() raises an exception, report test success.

    msg identifies this check'''

    global Nchecks
    global NchecksFailed
    Nchecks = Nchecks + 1

    try:
        f()
        print_red("FAILED{}".format((': ' + msg) if msg else ''))
        NchecksFailed = NchecksFailed + 1
        return False
    except:
        print_green("OK{}".format((': ' + msg) if msg else ''))
        return True


def confirm_does_not_raise(f, msg=''):
    r'''If f() raises an exception, report test failure.

    msg identifies this check'''

    global Nchecks
    global NchecksFailed
    Nchecks = Nchecks + 1

    try:
        f()
        print_green("OK{}".format((': ' + msg) if msg else ''))
        return True
    except Exception as e:
        print_red("FAILED{}. Exception: {}".format((': ' + msg) if msg else '', e))
        NchecksFailed = NchecksFailed + 1
        return False


def finish():
    r'''Finalize the executed tests.

    Prints the test summary. Exits successfully iff all the tests passed.

    '''
    if not Nchecks and not NchecksFailed:
        print_red("No tests defined")
        sys.exit(0)

    if NchecksFailed:
        print_red("Some tests failed: {} out of {}".format(NchecksFailed, Nchecks))
        sys.exit(1)

    print_green("All tests passed: {} total".format(Nchecks))
    sys.exit(0)




# numpysane-specific tests. Keep these in this file to make sure test-harness
# line numbers are not reported

def assertValueShape(value_ref, s, f, *args, **kwargs):
    r'''Makes sure a given call produces a given value and shape.

    It is redundant to specify both, but it makes it clear I'm asking for
    what I think I'm asking. The value check can be skipped by passing None.

    '''
    try:
        res = f(*args, **kwargs)
    except Exception as e:
        print_red("FAILED: Exception \"{}\" calling \"{}\"".format(e,f))
        global NchecksFailed
        NchecksFailed += 1
        return

    if 'out' in kwargs:
        confirm(res is kwargs['out'], msg='returning same matrix as the given "out"')
    if s is not None:
        try:
            shape = res.shape
        except:
            shape = ()
        confirm_equal(shape, s, msg='shape matches')
    if value_ref is not None:
        confirm_equal(value_ref, res, msg='value matches')
    if 'dtype' in kwargs:
        confirm_equal(np.dtype(res.dtype), np.dtype(kwargs['dtype']), msg='matching dtype')


def assertResult_inoutplace( ref, func, *args, **kwargs ):
    r'''makes sure func(a,b) == ref.

    Tests both a pre-allocated array and a slice-at-a-time allocate/copy
    mode

    Only one test-specific kwarg is known: 'out_inplace_dtype'. The rest are
    passed down to the test function

    '''

    out_inplace_dtype = kwargs.get('out_inplace_dtype', None)
    try: del kwargs['out_inplace_dtype']
    except: pass

    assertValueShape( ref, ref.shape, func, *args, **kwargs )

    output = np.empty(ref.shape, dtype=out_inplace_dtype)
    assertValueShape( ref, ref.shape, func, *args, out=output, **kwargs)
    confirm_equal(ref, output)