forked from robince/pymex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_mxmodule.py
443 lines (408 loc) · 12.7 KB
/
test_mxmodule.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
from nose.tools import *
from nose.plugins.skip import SkipTest
import mx
# mxmodule: creating (non-wrapped) mxArray objects of various sizes.
def bare_cell(dims):
return mx.create_cell_array(dims, wrap=False)
def test_bare_cell():
'''
Testing bare cell creation
'''
size = 3
yield bare_cell, ()
for i in range(size): yield bare_cell, (i,)
for i in range(size):
for j in range(size):
yield bare_cell, (i,j)
for i in range(size):
for j in range(size):
for k in range(size):
yield bare_cell, (i,j,k)
@raises(ValueError)
def test_negative_cell():
'''
Test that cells of negative size not allowed
'''
bare_cell((-1,))
def bare_array(dims, mxclass=mx.DOUBLE):
return mx.create_numeric_array(dims, mxclass, wrap=False)
def test_bare_numerics():
'''
Testing bare numeric creation
'''
size = 3
mxclasses = [mx.DOUBLE, mx.SINGLE,
mx.INT8, mx.UINT8,
mx.INT16, mx.UINT16,
mx.INT32, mx.UINT32,
mx.INT64, mx.UINT64,
mx.CHAR, mx.LOGICAL]
for c in mxclasses:
yield bare_array, (), c
for i in range(size): yield bare_array, (i,), c
for i in range(size):
for j in range(size):
yield bare_array, (i,j), c
for i in range(size):
for j in range(size):
for k in range(size):
yield bare_array, (i,j,k), c
@raises(ValueError)
def test_negative_array():
'''
Test that arrays of negative size not allowed
'''
bare_array((-1,))
def bare_struct(dims):
return mx.create_struct_array(dims, wrap=False)
def test_bare_struct():
'''
Testing bare struct creation
'''
size = 3
yield bare_struct, ()
for i in range(size): yield bare_struct, (i,)
for i in range(size):
for j in range(size):
yield bare_struct, (i,j)
for i in range(size):
for j in range(size):
for k in range(size):
yield bare_struct, (i,j,k)
@raises(ValueError)
def test_negative_struct():
'''
Test that structs of negative size not allowed
'''
bare_struct((-1,))
def bare_char(dims):
return mx.create_char_array(dims, wrap=False)
def test_bare_char():
'''
Testing bare char creation
'''
size = 3
yield bare_char, ()
for i in range(size): yield bare_char, (i,)
for i in range(size):
for j in range(size):
yield bare_char, (i,j)
for i in range(size):
for j in range(size):
for k in range(size):
yield bare_char, (i,j,k)
@raises(ValueError)
def test_negative_char():
'''
Test that strings of negative size not allowed
'''
bare_char((-1,))
def bare_string(text):
return mx.create_string(text, wrap=False)
loremipsum = '''
Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diam nonummy nibh euismod tincidunt ut laoreet dolore
magna aliquam erat volutpat. Ut wisi enim ad minim veniam,
quis nostrud exerci tation ullamcorper suscipit lobortis
nisl ut aliquip ex ea commodo consequat. Duis autem vel
eum iriure dolor in hendrerit in vulputate velit esse
molestie consequat, vel illum dolore eu feugiat nulla
facilisis at vero eros et accumsan et iusto odio dignissim
qui blandit praesent luptatum zzril delenit augue duis
dolore te feugait nulla facilisi.
'''
def test_bare_string():
'''
Testing bare string creation
'''
for i in range(0,len(loremipsum),16):
yield bare_string, loremipsum[0:i]
def test_bare_funchandle():
'''
Test creation of bare function handle
'''
return mx.create_function_handle(name="sin", wrap=False)
def test_bare_closure():
'''
Test creation of bare anonymous function handle
'''
return mx.create_function_handle(closure="@(x)x+1", wrap=False)
@raises(ValueError)
def test_bare_func_notboth():
'''
Function handle may not be instantiated with both
a name and a closure string.
'''
return mx.create_function_handle(name="sin",
closure="@(x)x+1",
wrap=False)
@raises(mx.MATLABError)
def test_bare_func_badsyntax():
'''
Closure must be valid MATLAB syntax.
'''
return mx.create_function_handle(closure="@(x)$$$", wrap=False)
@raises(ValueError)
def test_bare_func_notafunc():
'''
Closure must evaluate to a function handle
'''
return mx.create_function_handle(closure="42", wrap=False)
def test_wrap_bare_cell():
'''
Test that the wrapping function seems to work
'''
return mx.wrap_pycobject(bare_cell(()))
@raises(ValueError)
def test_wrap_wrongthing():
'''
Wrapping things that don't belong raises error
'''
return mx.wrap_pycobject(42)
############################################################
# Tests for member methods of mx.Array
# (NOT its subclasses - those go in mltypes/tests.py
############################################################
mxclasses = {'double' : mx.DOUBLE,
'single' : mx.SINGLE,
'int8' : mx.INT8,
'uint8' : mx.UINT8,
'int16' : mx.INT16,
'uint16' : mx.UINT16,
'int32' : mx.INT32,
'uint32' : mx.UINT32,
'int64' : mx.INT64,
'uint64' : mx.UINT64,
'char' : mx.CHAR,
'logical' : mx.LOGICAL,
'struct' : mx.STRUCT,
'cell' : mx.CELL,
'function_handle': mx.FUNCTION,
'unknown' : mx.UNKNOWN,
}
class Test_mxArray(object):
def setUp(self):
cobj = bare_array(())
self.obj = mx.Array(mxpointer=cobj)
def tearDown(self):
del self.obj
def test_mxclass(self):
classname = self.obj._get_class_name()
classid = self.obj._get_class_id()
if classname in mxclasses:
eq_(classid, mxclasses[classname])
elif classid in mxclasses.values():
raise TypeError, ("Class %s apparently has class ID %d, which "
"belongs to a builtin class") % (classname,
classid)
else: pass # user object, dynamically assigned ID
def test_str(self):
str(self.obj)
def test_repr(self):
repr(self.obj)
def test_bool(self):
bool(self.obj)
class Test_Struct(Test_mxArray):
def setUp(self):
cobj = bare_struct((1,2))
self.obj = mx.Array(mxpointer=cobj)
def test_setfield(self):
'''
_set_field works in normal case
'''
self.obj._set_field(fieldname='foo',
value='bar',
index=0)
def test_getfield(self):
'''
_get_field works in normal case
'''
self.test_setfield()
val = self.obj._get_field(fieldname='foo',
index=0)
eq_(val, 'bar')
def test_getempty(self):
'''
_get_field returns [] for uninitialized field
'''
self.test_setfield()
val = self.obj._get_field(fieldname='foo',
index=1)
eq_(val._get_number_of_elements(),0)
@raises(KeyError)
def test_missingfield(self):
'''
_get_field raises KeyError for non-existent fields
'''
val = self.obj._get_field(fieldname='foo',
index=0)
@raises(IndexError)
def test_getbadindex(self):
'''
_get_field checks upper index bound
'''
self.test_setfield()
val = self.obj._get_field(fieldname='foo',
index=2)
@raises(IndexError)
def test_getnegindex(self):
'''
_get_field checks lower index bound
'''
self.test_setfield()
val = self.obj._get_field(fieldname='foo',
index=-1)
@raises(IndexError)
def test_setbadindex(self):
'''
_set_field checks upper index bound
'''
self.obj._set_field(fieldname='foo',
value='bar',
index=2)
@raises(IndexError)
def test_setnegindex(self):
'''
_set_field checks lower index bound
'''
self.obj._set_field(fieldname='foo',
value='bar',
index=-1)
@raises(TypeError)
def test_setfieldnotstring(self):
'''
_set_field rejects non-string fieldnames
'''
self.obj._set_field(fieldname=42,
value='bar',
index=0)
@raises(TypeError)
def test_getfieldnotstring(self):
'''
_get_field rejects non-string fieldnames
'''
self.test_setfield()
self.obj._get_field(fieldname=42,
index=0)
def test_getfields(self):
'''
_get_fields lists the correct set of fieldnames
'''
infields = set(['foo', 'bar', 'spam', 'eggs'])
for f in infields:
self.obj._set_field(fieldname=f,
value="contents of %s"%f,
index=0)
outfields = set(self.obj._get_fields())
eq_(infields, outfields)
def test_emptyfields(self):
'''
_get_fields acts appropriately for struct with no fields
'''
eq_(len(self.obj._get_fields()), 0)
class Test_Cell(Test_mxArray):
def setUp(self):
cobj = bare_cell((1,2))
self.obj = mx.Array(mxpointer=cobj)
def test_setcell(self):
'''
_set_cell works in standard case
'''
self.obj._set_cell(index=0, value='foo')
def test_getcell(self):
'''
_get_cell works in standard case
'''
self.test_setcell()
eq_(self.obj._get_cell(index=0), 'foo')
def test_getempty(self):
'''
_get_cell returns [] for uninitialized items
'''
eq_(self.obj._get_cell(index=0)._get_number_of_elements(), 0)
@raises(IndexError)
def test_getbadindex(self):
'''
_get_cell checks index upper bounds
'''
self.obj._get_cell(index=2)
@raises(IndexError)
def test_getnegindex(self):
'''
_get_cell checks index lower bounds
'''
self.obj._get_cell(index=-1)
@raises(IndexError)
def test_setbadinex(self):
'''
_set_cell checks index upper bounds
'''
self.obj._set_cell(index=2,value='foo')
@raises(IndexError)
def test_setnegindex(self):
'''
_set_cell checks index lower bounds
'''
self.obj._set_cell(index=-1,value='foo')
class Test_EmptyCell(Test_mxArray):
'''
Test bounds checking for 0-element cell array
'''
def setUp(self):
cobj = bare_cell(())
self.obj = mx.Array(mxpointer=cobj)
@raises(IndexError)
def test_getbadindex(self):
'''
_get_cell checks index upper bounds
'''
self.obj._get_cell(index=0)
@raises(IndexError)
def test_getnegindex(self):
'''
_get_cell checks index lower bounds
'''
self.obj._get_cell(index=-1)
@raises(IndexError)
def test_setbadinex(self):
'''
_set_cell checks index upper bounds
'''
self.obj._set_cell(index=0,value='foo')
@raises(IndexError)
def test_setnegindex(self):
'''
_set_cell checks index lower bounds
'''
self.obj._set_cell(index=-1,value='foo')
class Test_CVPartition(Test_mxArray):
'''
Test get/set property code
We should probably find a simple value class to
test with instead of this, since we can't actually
set a cvpartition's properties.
'''
def setUp(self):
from matlab import cvpartition
cobj = cvpartition(100, 'k', 10, wrap=False)
self.obj = mx.Array(mxpointer=cobj)
def test_getprop(self):
'''
_get_property works in normal case
'''
N = self.obj._get_property('N')
eq_(N, 100)
@raises(KeyError)
def test_badprop(self):
'''
_get_property raises KeyError for missing property
'''
N = self.obj._get_property('foo')
@raises(mx.MATLABError)
def test_setprop(self):
'''
_set_property doesn't work here.
No way to test this in a general fashion just now.
'''
raise SkipTest, "No way to determine property set failure"
self.obj._set_property('N', 42)