"cpp_onnx/include/webrtc_vad.h" did not exist on "5f46ad1c74b34434534eb8c2b81e4e403f1eff3a"
cfunc_cache_usecases.py 1.57 KB
Newer Older
dugupeiwen's avatar
dugupeiwen committed
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
"""
This file will be copied to a temporary directory in order to
exercise caching compiled C callbacks.

See test_cfunc.py.
"""

import sys

from numba import cfunc, jit
from numba.tests.support import TestCase, captured_stderr


Z = 1

add_sig = "float64(float64, float64)"

div_sig = "float64(int64, int64)"


@cfunc(add_sig, cache=True, nopython=True)
def add_usecase(x, y):
    return x + y + Z

@cfunc(add_sig, nopython=True)
def add_nocache_usecase(x, y):
    return x + y + Z

@cfunc(div_sig, cache=True, nopython=True)
def div_usecase(a, b):
    return a / b


@jit(nopython=True)
def inner(x, y):
    return x + y + Z

@cfunc(add_sig, cache=True, nopython=True)
def outer(x, y):
    return inner(-y, x)


class _TestModule(TestCase):
    """
    Tests for functionality of this module's cfuncs.
    Note this does not define any "test_*" method, instead check_module()
    should be called by hand.
    """

    def check_module(self, mod):
        f = mod.add_usecase
        self.assertPreciseEqual(f.ctypes(2.0, 3.0), 6.0)
        f = mod.add_nocache_usecase
        self.assertPreciseEqual(f.ctypes(2.0, 3.0), 6.0)
        f = mod.outer
        self.assertPreciseEqual(f.ctypes(5.0, 2.0), 4.0)

        f = mod.div_usecase
        with captured_stderr() as err:
            self.assertPreciseEqual(f.ctypes(7, 2), 3.5)
        self.assertEqual(err.getvalue(), "")
        with captured_stderr() as err:
            f.ctypes(7, 0)
        err = err.getvalue()
        self.assertIn("ZeroDivisionError", err)


def self_test():
    mod = sys.modules[__name__]
    _TestModule().check_module(mod)