test_boundscheck.py 9.25 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
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
292
import numpy as np

from numba.core.compiler import compile_isolated, DEFAULT_FLAGS
from numba.cuda.testing import SerialMixin
from numba import typeof, cuda, njit
from numba.core.types import float64
from numba.tests.support import MemoryLeakMixin, override_env_config
from numba.core import config
import unittest

BOUNDSCHECK_FLAGS = DEFAULT_FLAGS.copy()
BOUNDSCHECK_FLAGS.boundscheck = True


def basic_array_access(a):
    return a[10]


def slice_array_access(a):
    # The first index (slice) is not bounds checked
    return a[10:, 10]


def fancy_array_access(x):
    a = np.array([1, 2, 3])
    return x[a]


def fancy_array_modify(x):
    a = np.array([1, 2, 3])
    x[a] = 0
    return x


class TestBoundsCheckNoError(MemoryLeakMixin, unittest.TestCase):
    def setUp(self):
        self.old_boundscheck = config.BOUNDSCHECK
        config.BOUNDSCHECK = None

    def test_basic_array_boundscheck(self):
        a = np.arange(5)
        # Check the numpy behavior to make sure the test is correct
        with self.assertRaises(IndexError):
            # TODO: When we raise the same error message as numpy, test that
            # they are the same
            basic_array_access(a)

        at = typeof(a)
        c_noboundscheck = compile_isolated(basic_array_access, [at],
                                           flags=DEFAULT_FLAGS)
        noboundscheck = c_noboundscheck.entry_point
        # Check that the default flag doesn't raise
        noboundscheck(a)
        # boundscheck(a) is tested in TestBoundsCheckError below

    def test_slice_array_boundscheck(self):
        a = np.ones((5, 5))
        b = np.ones((5, 20))
        with self.assertRaises(IndexError):
            # TODO: When we raise the same error message as numpy, test that
            # they are the same
            slice_array_access(a)
        # Out of bounds on a slice doesn't raise
        slice_array_access(b)

        at = typeof(a)
        rt = float64[:]
        c_noboundscheck = compile_isolated(slice_array_access, [at],
                                           return_type=rt,
                                           flags=DEFAULT_FLAGS)
        noboundscheck = c_noboundscheck.entry_point
        c_boundscheck = compile_isolated(slice_array_access, [at],
                                         return_type=rt,
                                         flags=BOUNDSCHECK_FLAGS)
        boundscheck = c_boundscheck.entry_point
        # Check that the default flag doesn't raise
        noboundscheck(a)
        noboundscheck(b)
        # boundscheck(a) is tested in TestBoundsCheckError below

        # Doesn't raise
        boundscheck(b)

    def test_fancy_indexing_boundscheck(self):
        a = np.arange(3)
        b = np.arange(4)

        # Check the numpy behavior to ensure the test is correct.
        with self.assertRaises(IndexError):
            # TODO: When we raise the same error message as numpy, test that
            # they are the same
            fancy_array_access(a)
        fancy_array_access(b)

        at = typeof(a)
        rt = at.dtype[:]
        c_noboundscheck = compile_isolated(fancy_array_access, [at],
                                           return_type=rt,
                                           flags=DEFAULT_FLAGS)
        noboundscheck = c_noboundscheck.entry_point
        c_boundscheck = compile_isolated(fancy_array_access, [at],
                                         return_type=rt,
                                         flags=BOUNDSCHECK_FLAGS)
        boundscheck = c_boundscheck.entry_point
        # Check that the default flag doesn't raise
        noboundscheck(a)
        noboundscheck(b)
        # boundscheck(a) is tested in TestBoundsCheckError below

        # Doesn't raise
        boundscheck(b)

    def tearDown(self):
        config.BOUNDSCHECK = self.old_boundscheck


class TestNoCudaBoundsCheck(SerialMixin, unittest.TestCase):
    def setUp(self):
        self.old_boundscheck = config.BOUNDSCHECK
        config.BOUNDSCHECK = None

    @unittest.skipIf(not cuda.is_available(), "NO CUDA")
    def test_no_cuda_boundscheck(self):
        with self.assertRaises(NotImplementedError):
            @cuda.jit(boundscheck=True)
            def func():
                pass

        # Make sure we aren't raising "not supported" error if we aren't
        # requesting bounds checking anyway. Related pull request: #5257
        @cuda.jit(boundscheck=False)
        def func3():
            pass

        with override_env_config('NUMBA_BOUNDSCHECK', '1'):
            @cuda.jit
            def func2(x, a):
                a[1] = x[1]

            a = np.ones((1,))
            x = np.zeros((1,))
            # Out of bounds but doesn't raise (it does raise in the simulator,
            # so skip there)
            if not config.ENABLE_CUDASIM:
                func2[1, 1](x, a)

    def tearDown(self):
        config.BOUNDSCHECK = self.old_boundscheck


# This is a separate test because the jitted functions that raise exceptions
# have memory leaks.
class TestBoundsCheckError(unittest.TestCase):
    def setUp(self):
        self.old_boundscheck = config.BOUNDSCHECK
        config.BOUNDSCHECK = None

    def test_basic_array_boundscheck(self):
        a = np.arange(5)
        # Check the numpy behavior to make sure the test is correct
        with self.assertRaises(IndexError):
            # TODO: When we raise the same error message as numpy, test that
            # they are the same
            basic_array_access(a)

        at = typeof(a)
        c_boundscheck = compile_isolated(basic_array_access, [at],
                                         flags=BOUNDSCHECK_FLAGS)
        boundscheck = c_boundscheck.entry_point

        with self.assertRaises(IndexError):
            boundscheck(a)

    def test_slice_array_boundscheck(self):
        a = np.ones((5, 5))
        b = np.ones((5, 20))
        with self.assertRaises(IndexError):
            # TODO: When we raise the same error message as numpy, test that
            # they are the same
            slice_array_access(a)
        # Out of bounds on a slice doesn't raise
        slice_array_access(b)

        at = typeof(a)
        rt = float64[:]
        c_boundscheck = compile_isolated(slice_array_access, [at],
                                         return_type=rt,
                                         flags=BOUNDSCHECK_FLAGS)
        boundscheck = c_boundscheck.entry_point
        with self.assertRaises(IndexError):
            boundscheck(a)

    def test_fancy_indexing_boundscheck(self):
        a = np.arange(3)
        b = np.arange(4)

        # Check the numpy behavior to ensure the test is correct.
        with self.assertRaises(IndexError):
            # TODO: When we raise the same error message as numpy, test that
            # they are the same
            fancy_array_access(a)
        fancy_array_access(b)

        at = typeof(a)
        rt = at.dtype[:]
        c_boundscheck = compile_isolated(fancy_array_access, [at],
                                         return_type=rt,
                                         flags=BOUNDSCHECK_FLAGS)
        boundscheck = c_boundscheck.entry_point
        with self.assertRaises(IndexError):
            boundscheck(a)

    def test_fancy_indexing_with_modification_boundscheck(self):
        a = np.arange(3)
        b = np.arange(4)

        # Check the numpy behavior to ensure the test is correct.
        with self.assertRaises(IndexError):
            # TODO: When we raise the same error message as numpy, test that
            # they are the same
            fancy_array_modify(a)
        fancy_array_modify(b)

        at = typeof(a)
        rt = at.dtype[:]
        c_boundscheck = compile_isolated(fancy_array_modify, [at],
                                         return_type=rt,
                                         flags=BOUNDSCHECK_FLAGS)
        boundscheck = c_boundscheck.entry_point
        with self.assertRaises(IndexError):
            boundscheck(a)

    def tearDown(self):
        config.BOUNDSCHECK = self.old_boundscheck


class TestBoundsEnvironmentVariable(unittest.TestCase):
    def setUp(self):
        self.old_boundscheck = config.BOUNDSCHECK
        config.BOUNDSCHECK = None

        @njit
        def default(x):
            return x[1]

        @njit(boundscheck=False)
        def off(x):
            return x[1]

        @njit(boundscheck=True)
        def on(x):
            return x[1]

        self.default = default
        self.off = off
        self.on = on

    def test_boundscheck_unset(self):
        with override_env_config('NUMBA_BOUNDSCHECK', ''):
            a = np.array([1])

            # Doesn't raise
            self.default(a)
            self.off(a)

            with self.assertRaises(IndexError):
                self.on(a)

    def test_boundscheck_enabled(self):
        with override_env_config('NUMBA_BOUNDSCHECK', '1'):
            a = np.array([1])

            with self.assertRaises(IndexError):
                self.default(a)
                self.off(a)
                self.on(a)

    def test_boundscheck_disabled(self):
        with override_env_config('NUMBA_BOUNDSCHECK', '0'):
            a = np.array([1])

            # Doesn't raise
            self.default(a)
            self.off(a)
            self.on(a)

    def tearDown(self):
        config.BOUNDSCHECK = self.old_boundscheck


if __name__ == '__main__':
    unittest.main()