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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
"""
check_var_in -- tests whether a variable is passed in correctly
and also if the passed in variable can be reassigned
check_var_local -- tests wheter a variable is passed in , modified,
and returned correctly in the local_dict dictionary
argument
check_return -- test whether a variable is passed in, modified, and
then returned as a function return value correctly
"""
import unittest
import time
import sys
sys.path.append('..')
import ext_tools
class test_string_specification(unittest.TestCase):
def check_type_match_string(self):
s = ext_tools.string_specification()
assert( s.type_match('string') )
def check_type_match_int(self):
s = ext_tools.string_specification()
assert(not s.type_match(5))
def check_type_match_float(self):
s = ext_tools.string_specification()
assert(not s.type_match(5.))
def check_type_match_complex(self):
s = ext_tools.string_specification()
assert(not s.type_match(5.+1j))
def check_var_in(self):
mod = ext_tools.ext_module('string_var_in')
a = 'string'
var_specs = ext_tools.assign_variable_types(['a'],locals())
code = 'a=Py::String("hello");'
test = ext_tools.ext_function('test',var_specs,code)
mod.add_function(test)
mod.compile()
import string_var_in
b='bub'
string_var_in.test(b)
try:
b = 1.
string_var_in.test(b)
except TypeError:
pass
try:
b = 1
string_var_in.test(b)
except TypeError:
pass
def check_var_local(self):
mod = ext_tools.ext_module('string_var_local')
a = 'string'
var_specs = ext_tools.assign_variable_types(['a'],locals())
code = 'a=Py::String("hello");'
test = ext_tools.ext_function('test',var_specs,code)
mod.add_function(test)
mod.compile()
import string_var_local
b='bub'
q={}
string_var_local.test(b,q)
assert(q['a'] == 'hello')
def check_return(self):
mod = ext_tools.ext_module('string_return')
a = 'string'
var_specs = ext_tools.assign_variable_types(['a'],locals())
code = """
a= Py::String("hello");
return_val = Py::new_reference_to(a);
"""
test = ext_tools.ext_function('test',var_specs,code)
mod.add_function(test)
mod.compile()
import string_return
b='bub'
c = string_return.test(b)
assert( c == 'hello')
class test_list_specification(unittest.TestCase):
def check_type_match_bad(self):
s = ext_tools.list_specification()
objs = [{},(),'',1,1.,1+1j]
for i in objs:
assert( not s.type_match(i) )
def check_type_match_good(self):
s = ext_tools.list_specification()
assert(s.type_match([]))
def check_var_in(self):
mod = ext_tools.ext_module('list_var_in')
a = [1]
var_specs = ext_tools.assign_variable_types(['a'],locals())
code = 'a=Py::List();'
test = ext_tools.ext_function('test',var_specs,code)
mod.add_function(test)
mod.compile()
import list_var_in
b=[1,2]
list_var_in.test(b)
try:
b = 1.
list_var_in.test(b)
except TypeError:
pass
try:
b = 'string'
list_var_in.test(b)
except TypeError:
pass
def check_var_local(self):
mod = ext_tools.ext_module('list_var_local')
a = []
var_specs = ext_tools.assign_variable_types(['a'],locals())
code = """
a=Py::List();
a.append(Py::String("hello"));
"""
test = ext_tools.ext_function('test',var_specs,code)
mod.add_function(test)
mod.compile()
import list_var_local
a=[1,2]
q={}
list_var_local.test(a,q)
assert(q['a'] == ['hello'])
def check_return(self):
mod = ext_tools.ext_module('list_return')
a = [1]
var_specs = ext_tools.assign_variable_types(['a'],locals())
code = """
a=Py::List();
a.append(Py::String("hello"));
return_val = Py::new_reference_to(a);
"""
test = ext_tools.ext_function('test',var_specs,code)
mod.add_function(test)
mod.compile()
import list_return
b=[1,2]
c = list_return.test(b)
assert( c == ['hello'])
def check_speed(self):
mod = ext_tools.ext_module('list_speed')
a = range(1e6);b=1 # b to force availability of py_to_scalar<int>
var_specs = ext_tools.assign_variable_types(['a','b'],locals())
code = """
Py::Int v = Py::Int();
int vv, sum = 0;
for(int i = 0; i < a.length(); i++)
{
v = a[i];
vv = (int)v;
if (vv % 2)
sum += vv;
else
sum -= vv;
}
return_val = Py::new_reference_to(Py::Int(sum));
"""
with_cxx = ext_tools.ext_function('with_cxx',var_specs,code)
mod.add_function(with_cxx)
code = """
int vv, sum = 0;
PyObject *a_ptr = a.ptr(), *v;
for(int i = 0; i < a.length(); i++)
{
v = PyList_GetItem(a_ptr,i);
//didn't set error here -- just speed test
vv = py_to_scalar<int>(v,"list item");
if (vv % 2)
sum += vv;
else
sum -= vv;
}
return_val = Py::new_reference_to(Py::Int(sum));
"""
no_checking = ext_tools.ext_function('no_checking',var_specs,code)
mod.add_function(no_checking)
mod.compile()
import list_speed
import time
t1 = time.time()
sum1 = list_speed.with_cxx(a,b)
t2 = time.time()
print 'cxx:', t2 - t1
t1 = time.time()
sum2 = list_speed.no_checking(a,b)
t2 = time.time()
print 'C, no checking:', t2 - t1
sum3 = 0
t1 = time.time()
for i in a:
if i % 2:
sum3 += i
else:
sum3 -= i
t2 = time.time()
print 'python:', t2 - t1
assert( sum1 == sum2 and sum1 == sum3)
class test_tuple_specification(unittest.TestCase):
def check_type_match_bad(self):
s = ext_tools.tuple_specification()
objs = [{},[],'',1,1.,1+1j]
for i in objs:
assert( not s.type_match(i) )
def check_type_match_good(self):
s = ext_tools.tuple_specification()
assert(s.type_match((1,)))
def check_var_in(self):
mod = ext_tools.ext_module('tuple_var_in')
a = (1,)
var_specs = ext_tools.assign_variable_types(['a'],locals())
code = 'a=Py::Tuple();'
test = ext_tools.ext_function('test',var_specs,code)
mod.add_function(test)
mod.compile()
import tuple_var_in
b=(1,2)
tuple_var_in.test(b)
try:
b = 1.
tuple_var_in.test(b)
except TypeError:
pass
try:
b = 'string'
tuple_var_in.test(b)
except TypeError:
pass
def check_var_local(self):
mod = ext_tools.ext_module('tuple_var_local')
a = (1,)
var_specs = ext_tools.assign_variable_types(['a'],locals())
code = """
a=Py::Tuple(2);
a[0] = Py::String("hello");
"""
test = ext_tools.ext_function('test',var_specs,code)
mod.add_function(test)
mod.compile()
import tuple_var_local
a=(1,2)
q={}
tuple_var_local.test(a,q)
assert(q['a'] == ('hello',None))
def check_return(self):
mod = ext_tools.ext_module('tuple_return')
a = (1,)
var_specs = ext_tools.assign_variable_types(['a'],locals())
code = """
a=Py::Tuple(2);
a[0] = Py::String("hello");
return_val = Py::new_reference_to(a);
"""
test = ext_tools.ext_function('test',var_specs,code)
mod.add_function(test)
mod.compile()
import tuple_return
b=(1,2)
c = tuple_return.test(b)
assert( c == ('hello',None))
class test_dict_specification(unittest.TestCase):
def check_type_match_bad(self):
s = ext_tools.dict_specification()
objs = [[],(),'',1,1.,1+1j]
for i in objs:
assert( not s.type_match(i) )
def check_type_match_good(self):
s = ext_tools.dict_specification()
assert(s.type_match({}))
def check_var_in(self):
mod = ext_tools.ext_module('dict_var_in')
a = {'z':1}
var_specs = ext_tools.assign_variable_types(['a'],locals())
code = 'a=Py::Dict();' # This just checks to make sure the type is correct
test = ext_tools.ext_function('test',var_specs,code)
mod.add_function(test)
mod.compile()
import dict_var_in
b={'y':2}
dict_var_in.test(b)
try:
b = 1.
dict_var_in.test(b)
except TypeError:
pass
try:
b = 'string'
dict_var_in.test(b)
except TypeError:
pass
def check_var_local(self):
mod = ext_tools.ext_module('dict_var_local')
a = {'z':1}
var_specs = ext_tools.assign_variable_types(['a'],locals())
code = """
a=Py::Dict();
a[Py::String("hello")] = Py::Int(5);
"""
test = ext_tools.ext_function('test',var_specs,code)
mod.add_function(test)
mod.compile()
import dict_var_local
a = {'z':2}
q={}
dict_var_local.test(a,q)
assert(q['a']['hello'] == 5)
def check_return(self):
mod = ext_tools.ext_module('dict_return')
a = {'z':1}
var_specs = ext_tools.assign_variable_types(['a'],locals())
code = """
a=Py::Dict();
a[Py::String("hello")] = Py::Int(5);
return_val = Py::new_reference_to(a);
"""
test = ext_tools.ext_function('test',var_specs,code)
mod.add_function(test)
mod.compile()
import dict_return
b = {'z':2}
c = dict_return.test(b)
assert( c['hello'] == 5)
def test_suite():
suites = []
#suites.append( unittest.makeSuite(test_string_specification,'check_'))
suites.append( unittest.makeSuite(test_list_specification,'check_'))
#suites.append( unittest.makeSuite(test_tuple_specification,'check_'))
#suites.append( unittest.makeSuite(test_dict_specification,'check_'))
total_suite = unittest.TestSuite(suites)
return total_suite
def test():
all_tests = test_suite()
runner = unittest.TextTestRunner()
runner.run(all_tests)
return runner
if __name__ == "__main__":
test()
|