forked from pepijndevos/futhark-pycffi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
117 lines (88 loc) · 3.34 KB
/
test.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
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
import unittest
import _test
import _test_badconsts
import numpy as np
from numpy.random import random
from numpy.testing import assert_array_equal
from futhark_ffi import Futhark
from futhark_ffi.compat import FutharkCompat
class TestFFI(unittest.TestCase):
def setUp(self):
self.fut = Futhark(_test)
def test_int(self):
self.assertEqual(self.fut.test1(5), 6)
def test_list_input(self):
data = np.arange(10)
self.assertEqual(self.fut.test2(data), np.sum(data))
def test_list_output(self):
data = np.arange(10)
res = self.fut.test3(data)
pyres = self.fut.from_futhark(res)
assert_array_equal(pyres, np.cumsum(data))
def test_multi_output(self):
self.assertEqual(self.fut.test4(4,5), (4+5, 4-5))
def test_2d(self):
data = np.arange(9).reshape(3,3)
res = self.fut.test5(data)
pyres = self.fut.from_futhark(res)
assert_array_equal(pyres, data*2)
res = self.fut.test5(data.T)
pyres = self.fut.from_futhark(res)
assert_array_equal(pyres, (data*2).T)
def test_opaque(self):
res = self.fut.test6(10)
(pos, neg) = self.fut.test7(res)
(pos, neg) = self.fut.from_futhark(pos, neg)
assert_array_equal(pos, np.arange(10))
assert_array_equal(neg, -np.arange(10))
def test_store(self):
stored = self.fut.test10a(42)
intermediate = self.fut.store_testOpaque(stored)
restored = self.fut.restore_testOpaque(intermediate)
self.assertEqual(self.fut.test10b(stored), self.fut.test10b(restored))
with self.assertRaises(ValueError):
self.fut.restore_testOpaque(np.arange(0))
def test_bool(self):
res = self.fut.test8(True)
self.assertEqual(res, False)
def test_error(self):
with self.assertRaises(ValueError):
self.fut.test9(np.arange(4))
def test_issue25(self):
with self.assertRaises(ValueError):
self.fut.from_futhark(self.fut.issue25(np.arange(10,dtype=np.int32)))
class TestCompat(unittest.TestCase):
def setUp(self):
self.fut = FutharkCompat(_test)
def test_int(self):
self.assertEqual(self.fut.test1(5), 6)
def test_list_input(self):
data = np.arange(10)
self.assertEqual(self.fut.test2(data), np.sum(data))
def test_list_output(self):
data = np.arange(10)
res = self.fut.test3(data).get()
assert_array_equal(res, np.cumsum(data))
def test_multi_output(self):
self.assertEqual(self.fut.test4(4,5), (4+5, 4-5))
def test_2d(self):
data = np.arange(9).reshape(3,3)
res = self.fut.test5(data).get()
assert_array_equal(res, data*2)
res = self.fut.test5(data.T).get()
assert_array_equal(res, (data*2).T)
def test_opaque(self):
res = self.fut.test6(10)
(pos, neg) = self.fut.test7(res)
assert_array_equal(pos.get(), np.arange(10))
assert_array_equal(neg.get(), -np.arange(10))
def test_bool(self):
res = self.fut.test8(True)
self.assertEqual(res, False)
def test_error(self):
with self.assertRaises(ValueError):
self.fut.test9(np.arange(4))
def test_bad_consts(self):
self.assertRaises(Exception, Futhark, _test_badconsts)
if __name__ == '__main__':
unittest.main()