-
Notifications
You must be signed in to change notification settings - Fork 27
/
test_op_plus.py
86 lines (77 loc) · 3.58 KB
/
test_op_plus.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
# 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(dtypes, shapes[1:]))
@make_test
def test_unary_plus(a, b, c): c[:] = +a
@pytest.mark.parametrize("dtype,shape", itertools.product(dtypes, shapes[1:]))
def test_binary_plus(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)
ao, ah, bo, bh = (ao / 2.).astype(dtype), (ah / 2.).astype(dtype), (bo / 2.).astype(dtype), (bh / 2.).astype(dtype)
ro, rh = fkt(ao, bo, co), hfkt(ah, bh, ch)
assert check(co, ch)
ro, rh = fkt(ao, bo, co), hfkt(ah, bh, ch)
assert check(co, ch)
@pytest.mark.parametrize("dtypea,dtypeb,dtypec", itertools.product(min_dtypes, min_dtypes, min_dtypes))
def test_cross_add_dtype(dtypea, dtypeb, dtypec):
def fkt(a, b, c):
c[:] = a + b
hfkt = hope.jit(fkt)
dtypeao = np.float64 if dtypea == float else dtypea
dtypebo = np.float64 if dtypeb == float else dtypeb
(ao, ah), (bo, bh), (co, ch) = random(dtypeao, [100]), random(dtypebo, [100]), random(dtypec, [100])
ao, ah = (ao / 2.).astype(dtypea), (ah / 2.).astype(dtypea)
bo, bh = (bo / 2.).astype(dtypeb), (bh / 2.).astype(dtypeb)
fkt(ao.astype(dtypea), bo.astype(dtypeb), co), hfkt(ah.astype(dtypea), bh.astype(dtypeb), ch)
assert check(co, ch)
fkt(ao.astype(dtypea), bo.astype(dtypeb), co), hfkt(ah.astype(dtypea), bh.astype(dtypeb), ch)
assert check(co, ch)
@pytest.mark.parametrize("dtype", dtypes)
def test_cross_add_scalar(dtype):
def fkt(a, b, c):
c[:] = a + b
hfkt = hope.jit(fkt)
dtypeo = np.float64 if dtype == float else dtype
(ao, ah), (bo, bh), (co, ch) = random(dtypeo, [100]), random(dtypeo, []), random(dtypeo, [100])
ao, ah = (ao / 2.).astype(dtype), (ah / 2.).astype(dtype)
bo, bh = (bo / 2.).astype(dtype), (bh / 2.).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(dtypes, shapes[1:]))
def test_cross_add_shape(dtype, shape):
def fkt(a, b, c):
c[:] = a + b
hfkt = hope.jit(fkt)
dtypeo = np.float64 if dtype == float else dtype
(ao, ah), (bo, bh), (co, ch) = random(dtypeo, []), random(dtypeo, shape), random(dtypeo, shape)
ao, ah = (ao / 2.).astype(dtype), (ah / 2.).astype(dtype)
bo, bh = (bo / 2.).astype(dtype), (bh / 2.).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)
fkt(bo, ao, co), hfkt(bh, ah, ch)
assert check(co, ch)
fkt(bo, ao, co), hfkt(bh, ah, ch)
assert check(co, ch)
@pytest.mark.parametrize("dtype,shape", itertools.product(dtypes, shapes[1:]))
def test_augmented_plus(dtype, shape):
def fkt(a, c):
c[:] += a
hfkt = hope.jit(fkt)
(ao, ah), (co, ch) = random(dtype, shape), random(dtype, shape)
ao, ah, co, ch = (ao / 4.).astype(dtype), (ah / 4.).astype(dtype), (co / 2.).astype(dtype), (ch / 2.).astype(dtype)
ro, rh = fkt(ao, co), hfkt(ah, ch)
assert check(co, ch)
ro, rh = fkt(ao, co), hfkt(ah, ch)
assert check(co, ch)