-
Notifications
You must be signed in to change notification settings - Fork 27
/
test_operators.py
87 lines (78 loc) · 3.85 KB
/
test_operators.py
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
# Copyright (C) 2014 ETH Zurich, Institute for Astronomy
"""
Test operators for `hope` module.
"""
from __future__ import print_function, division, absolute_import, unicode_literals
import numpy as np
import hope, itertools, pytest, sys, sysconfig, os, shutil
from test.utilities import random, check, make_test, JENKINS, min_dtypes, dtypes, shapes, setup_module, setup_method, teardown_module
@pytest.mark.parametrize("dtype,shape", itertools.product([dtype for dtype in dtypes if issubclass(dtype, np.integer) or dtype == int], shapes[1:]))
def test_binary_mod(dtype, shape):
if JENKINS and dtype == np.int8:
pytest.skip("Fails on debian: dtype={0!s}".format(dtype))
def fkt(a, b, c):
c[:] = a % b
hfkt = hope.jit(fkt)
(ao, ah), (bo, bh), (co, ch) = random(dtype, shape), random(dtype, shape), random(dtype, shape)
if np.count_nonzero(bo == 0) > 0: bo[bo == 0] += 1
if np.count_nonzero(bh == 0) > 0: bh[bh == 0] += 1
fkt(ao, bo, co), hfkt(ah, bh, ch)
assert check(co, ch)
fkt(ao, bo, co), hfkt(ah, bh, ch)
assert check(co, ch)
@pytest.mark.parametrize("dtype,shape", itertools.product([dtype for dtype in dtypes if issubclass(dtype, np.integer) or dtype == int], shapes[1:]))
def test_binary_lshifts(dtype, shape):
def fkt(a, b, c):
c[:] = a << b
hfkt = hope.jit(fkt)
(ao, ah), (bo, bh), (co, ch) = random(dtype, shape), random(dtype, shape), random(dtype, shape)
bo, bh = (bo % (np.dtype(dtype).itemsize * 8)).astype(dtype), (bh % (np.dtype(dtype).itemsize * 8)).astype(dtype)
fkt(ao, bo, co), hfkt(ah, bh, ch)
assert check(co, ch)
fkt(ao, bo, co), hfkt(ah, bh, ch)
assert check(co, ch)
@pytest.mark.parametrize("dtype,shape", itertools.product([dtype for dtype in dtypes if issubclass(dtype, np.integer) or dtype == int], shapes[1:]))
def test_binary_rshift(dtype, shape):
def fkt(a, b, c):
c[:] = a >> b
hfkt = hope.jit(fkt)
(ao, ah), (bo, bh), (co, ch) = random(dtype, shape), random(dtype, shape), random(dtype, shape)
bo, bh = (bo % (np.dtype(dtype).itemsize * 8)).astype(dtype), (bh % (np.dtype(dtype).itemsize * 8)).astype(dtype)
fkt(ao, bo, co), hfkt(ah, bh, ch)
assert check(co, ch)
fkt(ao, bo, co), hfkt(ah, bh, ch)
assert check(co, ch)
@pytest.mark.parametrize("dtype,shape", itertools.product([dtype for dtype in dtypes if issubclass(dtype, np.integer) or dtype == int], shapes[1:]))
def test_augmented_mod(dtype, shape):
def fkt(a, c):
c[:] %= a
hfkt = hope.jit(fkt)
(ao, ah), (co, ch) = random(dtype, shape), random(dtype, shape)
if np.count_nonzero(ao == 0) > 0: ao[ao == 0] += 1
if np.count_nonzero(ah == 0) > 0: ah[ah == 0] += 1
fkt(ao, co), hfkt(ah, ch)
assert check(co, ch)
fkt(ao, co), hfkt(ah, ch)
assert check(co, ch)
@pytest.mark.parametrize("dtype,shape", itertools.product([dtype for dtype in dtypes if issubclass(dtype, np.integer) or dtype == int], shapes[1:]))
def test_augmented_lshifts(dtype, shape):
def fkt(a, c):
c[:] <<= a
hfkt = hope.jit(fkt)
(ao, ah), (co, ch) = random(dtype, [10]), random(dtype, [10])
ao, ah = (ao % (np.dtype(dtype).itemsize * 8)).astype(dtype), (ah % (np.dtype(dtype).itemsize * 8)).astype(dtype)
fkt(ao, co), hfkt(ah, ch)
assert check(co, ch)
fkt(ao, co), hfkt(ah, ch)
assert check(co, ch)
@pytest.mark.parametrize("dtype,shape", itertools.product([dtype for dtype in dtypes if issubclass(dtype, np.integer) or dtype == int], shapes[1:]))
def test_augmented_rshift(dtype, shape):
def fkt(a, c):
c[:] >>= a
hfkt = hope.jit(fkt)
(ao, ah), (co, ch) = random(dtype, [10]), random(dtype, [10])
ao, ah = (ao % (np.dtype(dtype).itemsize * 8)).astype(dtype), (ah % (np.dtype(dtype).itemsize * 8)).astype(dtype)
fkt(ao, co), hfkt(ah, ch)
assert check(co, ch)
fkt(ao, co), hfkt(ah, ch)
assert check(co, ch)