1"""
2XXX: Should verify if test_methods2.py implements all tests in this file.
3
4Test lowlevel message passing details (Python -> ObjC)
5
6
7NOTE: 'long long' return-value test for calls from Objective-C are disabled,
8  they crash the interpreter.
9
10Done:
11- Return simple values, but need to add limit-cases (exactly INT_MAX et.al.)
12- Return structs, but need to add more complex cases
13- Pass in basic types
14- Pass in struct types, but need to add more complex cases
15- Pass by reference of basic types (in, out, inout)
16  [Need to add python side of case 'id']
17
18Todo:
19- Pass by reference of struct types (in, out, inout)
20- more than 1 argument
21- More argument conversions
22
23Also Todo: Passing from Objective-C to python
24- Using NSInvocation '[obj forwardInvocation:inv]'
25- Using normal calls
26- Done: Return simple values, return structs, pass in basic types (c part)
27
28NOTES:
29- Always use makeCFloat when testing the value of a C float against a
30  precomputed (literal) value. Just comparing against a Python float won't work
31  as that is a C double which has a different representation from C floats
32  resulting in spurious test failures.
33- See also testbndl.m, the implementation there must be synchronized
34  with this file.
35"""
36from __future__ import unicode_literals
37from PyObjCTools.TestSupport import *
38import objc
39import sys
40import struct
41
42# Can't set the right signatures in plain Objective-C.
43for method, argmeta in [
44        ( b"passInOutChar:", { 2: dict(type_modifier=b'N')}),
45        ( b"passOutChar:", { 2: dict(type_modifier=b'o')}),
46        ( b"passInChar:", { 2: dict(type_modifier=b'n')}),
47        ( b"passInOutUChar:", { 2: dict(type_modifier=b'N', type='^C')}),
48        ( b"passOutUChar:", { 2: dict(type_modifier=b'o', type='^C')}),
49        ( b"passInUChar:", { 2: dict(type_modifier=b'n', type='^C')}),
50        ( b"passInOutShort:", { 2: dict(type_modifier=b'N')}),
51        ( b"passOutShort:", { 2: dict(type_modifier=b'o')}),
52        ( b"passInShort:", { 2: dict(type_modifier=b'n')}),
53        ( b"passInOutUShort:", { 2: dict(type_modifier=b'N')}),
54        ( b"passOutUShort:", { 2: dict(type_modifier=b'o')}),
55        ( b"passInUShort:", { 2: dict(type_modifier=b'n')}),
56        ( b"passInOutInt:", { 2: dict(type_modifier=b'N')}),
57        ( b"passOutInt:", { 2: dict(type_modifier=b'o')}),
58        ( b"passInInt:", { 2: dict(type_modifier=b'n')}),
59        ( b"passInOutUInt:", { 2: dict(type_modifier=b'N')}),
60        ( b"passOutUInt:", { 2: dict(type_modifier=b'o')}),
61        ( b"passInUInt:", { 2: dict(type_modifier=b'n')}),
62        ( b"passInOutLong:", { 2: dict(type_modifier=b'N')}),
63        ( b"passOutLong:", { 2: dict(type_modifier=b'o')}),
64        ( b"passInLong:", { 2: dict(type_modifier=b'n')}),
65        ( b"passInOutULong:", { 2: dict(type_modifier=b'N')}),
66        ( b"passOutULong:", { 2: dict(type_modifier=b'o')}),
67        ( b"passInULong:", { 2: dict(type_modifier=b'n')}),
68        ( b"passInOutLongLong:", { 2: dict(type_modifier=b'N')}),
69        ( b"passOutLongLong:", { 2: dict(type_modifier=b'o')}),
70        ( b"passInLongLong:", { 2: dict(type_modifier=b'n')}),
71        ( b"passInOutULongLong:", { 2: dict(type_modifier=b'N')}),
72        ( b"passOutULongLong:", { 2: dict(type_modifier=b'o')}),
73        ( b"passInULongLong:", { 2: dict(type_modifier=b'n')}),
74        ( b"passInOutFloat:", { 2: dict(type_modifier=b'N')}),
75        ( b"passOutFloat:", { 2: dict(type_modifier=b'o')}),
76        ( b"passInFloat:", { 2: dict(type_modifier=b'n')}),
77        ( b"passInOutDouble:", { 2: dict(type_modifier=b'N')}),
78        ( b"passOutDouble:", { 2: dict(type_modifier=b'o')}),
79        ( b"passInDouble:", { 2: dict(type_modifier=b'n')}),
80        ( b"passInOutCharp:", { 2: dict(type_modifier=b'N')}),
81        ( b"passOutCharp:", { 2: dict(type_modifier=b'o')}),
82        ( b"passInCharp:", { 2: dict(type_modifier=b'n')}),
83        ( b"passInOutID:", { 2: dict(type_modifier=b'N')}),
84        ( b"passOutID:", { 2: dict(type_modifier=b'o')}),
85        ( b"passInID:", { 2: dict(type_modifier=b'n')}),
86    ]:
87
88    objc.registerMetaDataForSelector(b"OC_TestClass1", method,
89                    dict(arguments=argmeta))
90
91
92from PyObjCTest.testbndl import *
93
94def makeCFloat(value):
95    """
96    C floats and doubles have a different representation, this function returns
97    the result of converting a python float (== C double) to a C float and back.
98    """
99    if sys.version_info[0] == 2:
100        struct_fmt = b'f'
101    else:
102        struct_fmt = 'f'
103
104    return struct.unpack(struct_fmt,struct.pack(struct_fmt, value))[0]
105
106
107class PyOCTestSimpleReturns(TestCase):
108    #
109    # Test returns of simple types from instance and classs methods
110    #
111    def testClsLongLong(self):
112        OC_TestClass1.clsReset()
113        self.assertEqual(OC_TestClass1.longlongClsFunc(), -(1 << 60))
114        self.assertEqual(OC_TestClass1.longlongClsFunc(), -42)
115        self.assertEqual(OC_TestClass1.longlongClsFunc(), 0)
116        self.assertEqual(OC_TestClass1.longlongClsFunc(), 42)
117        self.assertEqual(OC_TestClass1.longlongClsFunc(), (1 << 60))
118
119    def testClsULongLong(self):
120        OC_TestClass1.clsReset()
121        self.assertEqual(OC_TestClass1.ulonglongClsFunc(), 0)
122        self.assertEqual(OC_TestClass1.ulonglongClsFunc(), 42)
123        self.assertEqual(OC_TestClass1.ulonglongClsFunc(), (1 << 63))
124
125    def testClsLong(self):
126        OC_TestClass1.clsReset()
127        self.assertEqual(OC_TestClass1.longClsFunc(), -(1 << 30))
128        self.assertEqual(OC_TestClass1.longClsFunc(), -42)
129        self.assertEqual(OC_TestClass1.longClsFunc(), 0)
130        self.assertEqual(OC_TestClass1.longClsFunc(), 42)
131        self.assertEqual(OC_TestClass1.longClsFunc(), (1 << 30))
132
133    def testClsULong(self):
134        OC_TestClass1.clsReset()
135        self.assertEqual(OC_TestClass1.ulongClsFunc(), 0)
136        self.assertEqual(OC_TestClass1.ulongClsFunc(), 42)
137        self.assertEqual(OC_TestClass1.ulongClsFunc(), (1 << 30))
138
139    def testClsInt(self):
140        OC_TestClass1.clsReset()
141        self.assertEqual(OC_TestClass1.intClsFunc(), -(1 << 30))
142        self.assertEqual(OC_TestClass1.intClsFunc(), -42)
143        self.assertEqual(OC_TestClass1.intClsFunc(), 0)
144        self.assertEqual(OC_TestClass1.intClsFunc(), 42)
145        self.assertEqual(OC_TestClass1.intClsFunc(), (1 << 30))
146
147    def testClsUInt(self):
148        OC_TestClass1.clsReset()
149        self.assertEqual(OC_TestClass1.uintClsFunc(), 0)
150        self.assertEqual(OC_TestClass1.uintClsFunc(), 42)
151        self.assertEqual(OC_TestClass1.uintClsFunc(), (1 << 30))
152
153    def testClsShort(self):
154        OC_TestClass1.clsReset()
155        self.assertEqual(OC_TestClass1.shortClsFunc(), -(1 << 14))
156        self.assertEqual(OC_TestClass1.shortClsFunc(), -42)
157        self.assertEqual(OC_TestClass1.shortClsFunc(), 0)
158        self.assertEqual(OC_TestClass1.shortClsFunc(), 42)
159        self.assertEqual(OC_TestClass1.shortClsFunc(), (1 << 14))
160
161    def testClsUShort(self):
162        OC_TestClass1.clsReset()
163        self.assertEqual(OC_TestClass1.ushortClsFunc(), 0)
164        self.assertEqual(OC_TestClass1.ushortClsFunc(), 42)
165        self.assertEqual(OC_TestClass1.ushortClsFunc(), (1 << 14))
166
167    def testClsChar(self):
168        # Fails with libffi due to the way we treat 'char' at the moment
169        OC_TestClass1.clsReset()
170        self.assertEqual(OC_TestClass1.charClsFunc(), -128)
171        self.assertEqual(OC_TestClass1.charClsFunc(), 0)
172        self.assertEqual(OC_TestClass1.charClsFunc(), 127)
173
174    def testClsUChar(self):
175        OC_TestClass1.clsReset()
176        self.assertEqual(OC_TestClass1.ucharClsFunc(), 0)
177        self.assertEqual(OC_TestClass1.ucharClsFunc(), 128)
178        self.assertEqual(OC_TestClass1.ucharClsFunc(), 255)
179
180    def testClsFloat(self):
181        # Fails, possibly rounding error
182        OC_TestClass1.clsReset()
183        self.assertEqual(OC_TestClass1.floatClsFunc(), makeCFloat(0.128))
184        self.assertEqual(OC_TestClass1.floatClsFunc(), makeCFloat(1.0))
185        self.assertEqual(OC_TestClass1.floatClsFunc(), makeCFloat(42.0))
186        self.assertEqual(OC_TestClass1.floatClsFunc(), makeCFloat(1e10))
187
188    def testClsDouble(self):
189        OC_TestClass1.clsReset()
190        self.assertEqual(OC_TestClass1.doubleClsFunc(), 0.128)
191        self.assertEqual(OC_TestClass1.doubleClsFunc(), 1.0)
192        self.assertEqual(OC_TestClass1.doubleClsFunc(), 42.0)
193        self.assertEqual(OC_TestClass1.doubleClsFunc(), 1e10)
194
195    def testClsCharp(self):
196        OC_TestClass1.clsReset()
197        self.assertEqual(OC_TestClass1.charpClsFunc(), b'hello')
198        self.assertEqual(OC_TestClass1.charpClsFunc(), b'world')
199        self.assertEqual(OC_TestClass1.charpClsFunc(), b'foobar')
200
201    def testClsID(self):
202        OC_TestClass1.clsReset()
203        self.assertEqual(len(OC_TestClass1.idClsFunc()), 0)
204        self.assertEqual(type(OC_TestClass1.idClsFunc()).__name__, 'NSHost')
205        self.assertIn(str(OC_TestClass1.idClsFunc()), ('{}', '{\n}'))
206        self.assertEqual(OC_TestClass1.idClsFunc(), None)
207
208    # testCls* ends here
209
210    def testLongLong(self):
211        obj = OC_TestClass1.new()
212        obj.reset()
213        self.assertEqual(obj.longlongFunc(), -(1 << 60))
214        self.assertEqual(obj.longlongFunc(), -42)
215        self.assertEqual(obj.longlongFunc(), 0)
216        self.assertEqual(obj.longlongFunc(), 42)
217        self.assertEqual(obj.longlongFunc(), (1 << 60))
218
219    def testULongLong(self):
220        obj = OC_TestClass1.new()
221        obj.reset()
222        self.assertEqual(obj.ulonglongFunc(), 0)
223        self.assertEqual(obj.ulonglongFunc(), 42)
224        self.assertEqual(obj.ulonglongFunc(), (1 << 63))
225
226    def testLong(self):
227        obj = OC_TestClass1.new()
228        obj.reset()
229        self.assertEqual(obj.longFunc(), -(1 << 30))
230        self.assertEqual(obj.longFunc(), -42)
231        self.assertEqual(obj.longFunc(), 0)
232        self.assertEqual(obj.longFunc(), 42)
233        self.assertEqual(obj.longFunc(), (1 << 30))
234
235    def testULong(self):
236        obj = OC_TestClass1.new()
237        obj.reset()
238        self.assertEqual(obj.ulongFunc(), 0)
239        self.assertEqual(obj.ulongFunc(), 42)
240        self.assertEqual(obj.ulongFunc(), (1 << 30))
241
242    def testInt(self):
243        obj = OC_TestClass1.new()
244        obj.reset()
245        self.assertEqual(obj.intFunc(), -(1 << 30))
246        self.assertEqual(obj.intFunc(), -42)
247        self.assertEqual(obj.intFunc(), 0)
248        self.assertEqual(obj.intFunc(), 42)
249        self.assertEqual(obj.intFunc(), (1 << 30))
250
251    def testUInt(self):
252        obj = OC_TestClass1.new()
253        obj.reset()
254        self.assertEqual(obj.uintFunc(), 0)
255        self.assertEqual(obj.uintFunc(), 42)
256        self.assertEqual(obj.uintFunc(), (1 << 30))
257
258    def testShort(self):
259        obj = OC_TestClass1.new()
260        obj.reset()
261        self.assertEqual(obj.shortFunc(), -(1 << 14))
262        self.assertEqual(obj.shortFunc(), -42)
263        self.assertEqual(obj.shortFunc(), 0)
264        self.assertEqual(obj.shortFunc(), 42)
265        self.assertEqual(obj.shortFunc(), (1 << 14))
266
267    def testUShort(self):
268        obj = OC_TestClass1.new()
269        obj.reset()
270        self.assertEqual(obj.ushortFunc(), 0)
271        self.assertEqual(obj.ushortFunc(), 42)
272        self.assertEqual(obj.ushortFunc(), (1 << 14))
273
274    def testChar(self):
275        obj = OC_TestClass1.new()
276        # Fails with libffi due to the way we treat 'char' at the moment
277        obj.reset()
278        self.assertEqual(obj.charFunc(), -128)
279        self.assertEqual(obj.charFunc(), 0)
280        self.assertEqual(obj.charFunc(), 127)
281
282    def testUChar(self):
283        obj = OC_TestClass1.new()
284        obj.reset()
285        self.assertEqual(obj.ucharFunc(), 0)
286        self.assertEqual(obj.ucharFunc(), 128)
287        self.assertEqual(obj.ucharFunc(), 255)
288
289    def testFloat(self):
290        # Fails, possibly rounding error
291        obj = OC_TestClass1.new()
292        obj.reset()
293        self.assertEqual(obj.floatFunc(), makeCFloat(0.128))
294        self.assertEqual(obj.floatFunc(), makeCFloat(1.0))
295        self.assertEqual(obj.floatFunc(), makeCFloat(42.0))
296        self.assertEqual(obj.floatFunc(), makeCFloat(1e10))
297
298    def testDouble(self):
299        obj = OC_TestClass1.new()
300        obj.reset()
301        self.assertEqual(obj.doubleFunc(), 0.128)
302        self.assertEqual(obj.doubleFunc(), 1.0)
303        self.assertEqual(obj.doubleFunc(), 42.0)
304        self.assertEqual(obj.doubleFunc(), 1e10)
305
306    def testCharp(self):
307        obj = OC_TestClass1.new()
308        obj.reset()
309        self.assertEqual(obj.charpFunc(), b'hello')
310        self.assertEqual(obj.charpFunc(), b'world')
311        self.assertEqual(obj.charpFunc(), b'foobar')
312
313    def testID(self):
314        obj = OC_TestClass1.new()
315        obj.reset()
316        self.assertEqual(len(obj.idFunc()), 0)
317        self.assertEqual(type(obj.idFunc()).__name__, 'NSHost')
318        self.assertIn(str(obj.idFunc()), ('{}', '{\n}'))
319        self.assertEqual(obj.idFunc(), None)
320
321    def testStruct1(self):
322        obj = OC_TestClass1.new()
323
324        self.assertEqual(obj.dummyFunc(), (-1, 1))
325
326    def testStruct2(self):
327        obj = OC_TestClass1.new()
328
329        self.assertEqual(obj.dummy2Func(), ((1,2,3,4),))
330
331
332class PyOCTestSimpleArguments(TestCase):
333    #
334    # Test argument passing of single basic values.
335    #
336    def setUp(self):
337        self.obj = OC_TestClass1.new()
338
339    def testLongLong(self):
340        self.assertEqual(self.obj.longlongArg_(0), 0)
341        self.assertEqual(self.obj.longlongArg_(10), 5)
342        self.assertEqual(self.obj.longlongArg_(10), 5)
343        self.assertEqual(self.obj.longlongArg_(1 << 60), (1 << 59))
344        self.assertEqual(self.obj.longlongArg_(-10), -5)
345        self.assertEqual(self.obj.longlongArg_(-10), -5)
346        self.assertEqual(self.obj.longlongArg_(-1 << 60), -(1 << 59))
347
348        # TODO: Out of range values
349
350    def testULongLong(self):
351        self.assertEqual(self.obj.ulonglongArg_(0), 0)
352        self.assertEqual(self.obj.ulonglongArg_(10), 5)
353        self.assertEqual(self.obj.ulonglongArg_(10), 5)
354        self.assertEqual(self.obj.ulonglongArg_(1 << 60), (1 << 59))
355
356        # TODO: Out of range values
357
358    def testLong(self):
359        self.assertEqual(self.obj.longArg_(0), 0)
360        self.assertEqual(self.obj.longArg_(10), 5)
361        self.assertEqual(self.obj.longArg_(10), 5)
362        self.assertEqual(self.obj.longArg_(1 << 30), (1 << 29))
363        self.assertEqual(self.obj.longArg_(-10), -5)
364        self.assertEqual(self.obj.longArg_(-10), -5)
365        self.assertEqual(self.obj.longArg_(-1 << 30), -(1 << 29))
366
367        # TODO: Out of range values
368
369    def testULong(self):
370        self.assertEqual(self.obj.ulongArg_(0), 0)
371        self.assertEqual(self.obj.ulongArg_(10), 5)
372        self.assertEqual(self.obj.ulongArg_(10), 5)
373        self.assertEqual(self.obj.ulongArg_(1 << 30), (1 << 29))
374
375        # TODO: Out of range values
376
377    def testInt(self):
378        self.assertEqual(self.obj.intArg_(0), 0)
379        self.assertEqual(self.obj.intArg_(10), 5)
380        self.assertEqual(self.obj.intArg_(10), 5)
381        self.assertEqual(self.obj.intArg_(1 << 30), (1 << 29))
382        self.assertEqual(self.obj.intArg_(-10), -5)
383        self.assertEqual(self.obj.intArg_(-10), -5)
384        self.assertEqual(self.obj.intArg_(-1 << 30), -(1 << 29))
385
386        self.assertEqual(self.obj.intArg_(10.0), 5)
387
388        self.assertRaises(ValueError, self.obj.intArg_, sys.maxsize+1)
389        self.assertRaises(ValueError, self.obj.intArg_, -sys.maxsize-2)
390        self.assertRaises(ValueError, self.obj.intArg_, -float(sys.maxsize)-2)
391
392    def testUInt(self):
393        self.assertEqual(self.obj.uintArg_(0), 0)
394        self.assertEqual(self.obj.uintArg_(10), 5)
395        self.assertEqual(self.obj.uintArg_(10), 5)
396        self.assertEqual(self.obj.uintArg_(1 << 30), (1 << 29))
397
398        self.assertEqual(self.obj.uintArg_(10.0), 5)
399
400        self.assertRaises(ValueError, self.obj.uintArg_, -5)
401        self.assertRaises(ValueError, self.obj.uintArg_, -5)
402        self.assertRaises(ValueError, self.obj.uintArg_, 1+2*(sys.maxsize+1))
403
404
405    def testShort(self):
406        self.assertEqual(self.obj.shortArg_(0), 0)
407        self.assertEqual(self.obj.shortArg_(10), 5)
408        self.assertEqual(self.obj.shortArg_(10), 5)
409        self.assertEqual(self.obj.shortArg_(1 << 14), (1 << 13))
410        self.assertEqual(self.obj.shortArg_(-10), -5)
411        self.assertEqual(self.obj.shortArg_(-10), -5)
412        self.assertEqual(self.obj.shortArg_(-(1 << 14)), -(1 << 13))
413
414        self.assertEqual(self.obj.shortArg_(10.0), 5)
415
416        # Out of range arguments, assumes a short is 16 bits
417        self.assertRaises(ValueError, self.obj.shortArg_, -(1<<16)-1)
418        self.assertRaises(ValueError, self.obj.shortArg_, 1<<16)
419        self.assertRaises(ValueError, self.obj.shortArg_, -(1<<16)-1)
420        self.assertRaises(ValueError, self.obj.shortArg_, 1<<16)
421
422    def testUShort(self):
423        self.assertEqual(self.obj.ushortArg_(0), 0)
424        self.assertEqual(self.obj.ushortArg_(10), 5)
425        self.assertEqual(self.obj.ushortArg_(10), 5)
426        self.assertEqual(self.obj.ushortArg_(1 << 14), (1 << 13))
427
428        self.assertEqual(self.obj.ushortArg_(10.0), 5)
429
430        # Out of range arguments, assumes a short is 16 bits
431        self.assertRaises(ValueError, self.obj.ushortArg_, -5)
432        self.assertRaises(ValueError, self.obj.ushortArg_, -(1<<16)-1)
433        self.assertRaises(ValueError, self.obj.ushortArg_, 1<<16)
434        self.assertRaises(ValueError, self.obj.ushortArg_, -5)
435        self.assertRaises(ValueError, self.obj.ushortArg_, -(1<<16)-1)
436        self.assertRaises(ValueError, self.obj.ushortArg_, 1<<16)
437
438
439    def testChar(self):
440        self.assertEqual(self.obj.charArg_(0), (0))
441        self.assertEqual(self.obj.charArg_(10), (5))
442        self.assertEqual(self.obj.charArg_(10), (5))
443        self.assertEqual(self.obj.charArg_(1 << 6), (1 << 5))
444        self.assertEqual(self.obj.charArg_(b'\x00'), 0x00)
445        self.assertEqual(self.obj.charArg_(b'\x10'), int(ord(b'\x10')/2))
446
447        # TODO: Out of range arguments
448
449    def testUChar(self):
450        self.assertEqual(self.obj.ucharArg_(0), 0)
451        self.assertEqual(self.obj.ucharArg_(10), 5)
452        self.assertEqual(self.obj.ucharArg_(10), 5)
453        self.assertEqual(self.obj.ucharArg_(1 << 7), (1 << 6))
454        self.assertEqual(self.obj.ucharArg_(b'\x00'), 0)
455        self.assertEqual(self.obj.ucharArg_(b'\x10'), int(ord(b'\x10')/2))
456
457        self.assertEqual(self.obj.ucharArg_(10.0), 5)
458
459        # Out of range arguments
460        self.assertRaises(ValueError, self.obj.ucharArg_, -5)
461        self.assertRaises(ValueError, self.obj.ucharArg_, -256)
462        self.assertRaises(ValueError, self.obj.ucharArg_, 256)
463        self.assertRaises(ValueError, self.obj.ucharArg_, -5)
464        self.assertRaises(ValueError, self.obj.ucharArg_, -256)
465        self.assertRaises(ValueError, self.obj.ucharArg_, 256)
466
467    def testCharp(self):
468        self.assertEqual(self.obj.charpArg_(b"hello world"), b'dlrow olleh')
469
470        self.assertRaises(ValueError, self.obj.charpArg_, 256)
471        self.assertRaises(ValueError, self.obj.charpArg_, "hello world")
472
473    def testIDPython(self):
474        # Test a Python object as the argument
475        s = self.obj.idArg_("hello")
476        self.assertEqual(len(s), 1)
477        self.assertEqual(s[0], "hello")
478
479    def testIDOC(self):
480        # Test an Objective-C object as the argument
481
482        # NSHost gives problems on GNUstep, better check those problems
483        # seperately in the Foundation test suite.
484        #c = objc.lookUpClass("NSHost")
485        #o = c.hostWithAddress_('127.0.0.1')
486
487        c = objc.lookUpClass("NSScanner")
488        o = c.scannerWithString_("hello world")
489        s = self.obj.idArg_(o)
490        self.assertEqual(len(s), 1)
491        self.assertIs(s[0], o)
492
493    def testStruct1(self):
494        self.assertEqual(self.obj.dummyArg_((-1, 1)), (-2, 2))
495
496        self.assertRaises(TypeError, self.obj.dummyArg_, 256)
497        self.assertRaises(ValueError, self.obj.dummyArg_, (-1,))
498        self.assertRaises(ValueError, self.obj.dummyArg_, (-1,1,2))
499
500    def testStruct2(self):
501        self.assertEqual(self.obj.dummy2Arg_(((1,2,3,4),)), ((8,6,4,2),))
502
503        self.assertRaises(ValueError, self.obj.dummy2Arg_, ((8,6,4,2),1))
504        self.assertRaises(ValueError, self.obj.dummy2Arg_, ((8,6,4,),))
505        self.assertRaises(ValueError, self.obj.dummy2Arg_, ((8,6,4,2,1),))
506
507
508class PyOCTestByReferenceArguments(TestCase):
509    #
510    # Test argument passing of single basic values by reference.
511    #
512    def setUp(self):
513        self.obj = OC_TestClass1.new()
514
515    def testCharIn(self):
516        self.assertEqual(self.obj.passInChar_(b'\x10'), 0x19)
517
518    def testCharOut(self):
519        self.obj.reset()
520        self.assertEqual(self.obj.passOutChar_(None), -128)
521        self.assertEqual(self.obj.passOutChar_(None), 0)
522        self.assertEqual(self.obj.passOutChar_(None), 127)
523
524    def testCharInOut(self):
525        self.assertEqual(self.obj.passInOutChar_(b'\x10'), 0x3a)
526
527    def testUCharIn(self):
528        self.assertEqual(self.obj.passInUChar_(10), 19)
529
530    def testUCharOut(self):
531        self.obj.reset()
532        self.assertEqual(self.obj.passOutUChar_(None), 0)
533        self.assertEqual(self.obj.passOutUChar_(None), 128)
534        self.assertEqual(self.obj.passOutUChar_(None), 255)
535
536    def testUCharInOut(self):
537        self.assertEqual(self.obj.passInOutUChar_(10), 52)
538
539    def testShortIn(self):
540        self.assertEqual(self.obj.passInShort_(10), 19)
541        self.assertEqual(self.obj.passInShort_(-100), -91)
542        self.assertEqual(self.obj.passInShort_(-100.0), -91)
543
544    def testShortOut(self):
545        self.obj.reset()
546        self.assertEqual(self.obj.passOutShort_(None), -(1 << 14))
547        self.assertEqual(self.obj.passOutShort_(None), -42)
548        self.assertEqual(self.obj.passOutShort_(None), 0)
549        self.assertEqual(self.obj.passOutShort_(None), 42)
550        self.assertEqual(self.obj.passOutShort_(None), 1 << 14)
551
552    def testShortInOut(self):
553        self.assertEqual(self.obj.passInOutShort_(10), 52)
554        self.assertEqual(self.obj.passInOutShort_(-100), -58)
555        self.assertEqual(self.obj.passInOutShort_(-100.0), -58)
556
557    def testUShortIn(self):
558        self.assertEqual(self.obj.passInUShort_(10), 19)
559
560    def testUShortOut(self):
561        self.obj.reset()
562        self.assertEqual(self.obj.passOutUShort_(None), 0)
563        self.assertEqual(self.obj.passOutUShort_(None), 42)
564        self.assertEqual(self.obj.passOutUShort_(None), (1 << 14))
565
566    def testUShortInOut(self):
567        self.assertEqual(self.obj.passInOutUShort_(10), 52)
568
569    def testIntIn(self):
570        self.assertEqual(self.obj.passInInt_(10), 19)
571        self.assertEqual(self.obj.passInInt_(-100), -91)
572
573    def testIntOut(self):
574        self.obj.reset()
575        self.assertEqual(self.obj.passOutInt_(None), -(1 << 30))
576        self.assertEqual(self.obj.passOutInt_(None), -42)
577        self.assertEqual(self.obj.passOutInt_(None), 0)
578        self.assertEqual(self.obj.passOutInt_(None), 42)
579        self.assertEqual(self.obj.passOutInt_(None), (1 << 30))
580
581    def testIntInOut(self):
582        self.assertEqual(self.obj.passInOutInt_(10), 52)
583        self.assertEqual(self.obj.passInOutInt_(-100), -58)
584        self.assertEqual(self.obj.passInOutInt_(-100.0), -58)
585
586    def testUIntIn(self):
587        self.assertEqual(self.obj.passInUInt_(10), 19)
588
589    def testUIntOut(self):
590        self.obj.reset()
591        self.assertEqual(self.obj.passOutUInt_(None), 0)
592        self.assertEqual(self.obj.passOutUInt_(None), 42)
593        self.assertEqual(self.obj.passOutUInt_(None), (1 << 30))
594
595    def testUIntInOut(self):
596        self.assertEqual(self.obj.passInOutUInt_(10), 52)
597        self.assertEqual(self.obj.passInOutUInt_(10.0), 52)
598
599    def testLongIn(self):
600        self.assertEqual(self.obj.passInLong_(10), 19)
601        self.assertEqual(self.obj.passInLong_(-100), -91)
602
603    def testLongOut(self):
604        self.obj.reset()
605        self.assertEqual(self.obj.passOutLong_(None), -(1 << 30))
606        self.assertEqual(self.obj.passOutLong_(None), -42)
607        self.assertEqual(self.obj.passOutLong_(None), 0)
608        self.assertEqual(self.obj.passOutLong_(None), 42)
609        self.assertEqual(self.obj.passOutLong_(None), (1 << 30))
610
611    def testLongInOut(self):
612        self.assertEqual(self.obj.passInOutLong_(10), 52)
613        self.assertEqual(self.obj.passInOutLong_(-100), -58)
614        self.assertEqual(self.obj.passInOutLong_(-100.0), -58)
615
616    def testULongIn(self):
617        self.assertEqual(self.obj.passInULong_(10), 19)
618
619    def testULongOut(self):
620        self.obj.reset()
621        self.assertEqual(self.obj.passOutULong_(None), 0)
622        self.assertEqual(self.obj.passOutULong_(None), 42)
623        self.assertEqual(self.obj.passOutULong_(None), (1 << 30))
624
625    def testULongInOut(self):
626        self.assertEqual(self.obj.passInOutULong_(10), 52)
627        self.assertEqual(self.obj.passInOutULong_(10.0), 52)
628
629    def testLongLongIn(self):
630        self.assertEqual(self.obj.passInLongLong_(10), 19)
631        self.assertEqual(self.obj.passInLongLong_(-100), -91)
632
633    def testLongLongOut(self):
634        self.obj.reset()
635        self.assertEqual(self.obj.passOutLongLong_(None), -(1 << 60))
636        self.assertEqual(self.obj.passOutLongLong_(None), -42)
637        self.assertEqual(self.obj.passOutLongLong_(None), 0)
638        self.assertEqual(self.obj.passOutLongLong_(None), 42)
639        self.assertEqual(self.obj.passOutLongLong_(None), (1 << 60))
640
641    def testLongLongInOut(self):
642        self.assertEqual(self.obj.passInOutLongLong_(10), 52)
643        self.assertEqual(self.obj.passInOutLongLong_(-100), -58)
644        self.assertEqual(self.obj.passInOutLongLong_(-100.0), -58)
645
646    def testULongLongIn(self):
647        self.assertEqual(self.obj.passInULongLong_(10), 19)
648
649    def testULongLongOut(self):
650        self.obj.reset()
651        self.assertEqual(self.obj.passOutULongLong_(None), 0)
652        self.assertEqual(self.obj.passOutULongLong_(None), 42)
653        self.assertEqual(self.obj.passOutULongLong_(None), (1 << 63))
654
655    def testULongLongInOut(self):
656        self.assertEqual(self.obj.passInOutULongLong_(10), 52)
657        self.assertEqual(self.obj.passInOutULongLong_(10.0), 52)
658
659    def testFloatIn(self):
660        self.assertEqual(self.obj.passInFloat_(10), makeCFloat(90.0))
661        self.assertEqual(self.obj.passInFloat_(0.11), makeCFloat(0.99))
662
663    def testFloatOut(self):
664        self.obj.reset()
665        self.assertEqual(self.obj.passOutFloat_(None), makeCFloat(0.128))
666        self.assertEqual(self.obj.passOutFloat_(None), makeCFloat(1.0))
667        self.assertEqual(self.obj.passOutFloat_(None), makeCFloat(42.0))
668        self.assertEqual(self.obj.passOutFloat_(None), makeCFloat(1e10))
669
670    def testFloatInOut(self):
671        self.assertEqual(self.obj.passInOutFloat_(10), makeCFloat(420))
672        self.assertEqual(self.obj.passInOutFloat_(10.0), makeCFloat(420))
673        self.assertEqual(self.obj.passInOutFloat_(0.01), makeCFloat(0.42))
674
675    def testDoubleIn(self):
676        self.assertEqual(self.obj.passInDouble_(10), 90.0)
677        self.assertEqual(self.obj.passInDouble_(0.11), 0.99)
678
679    def testDoubleOut(self):
680        self.obj.reset()
681        self.assertEqual(self.obj.passOutDouble_(None), 0.128)
682        self.assertEqual(self.obj.passOutDouble_(None), 1.0)
683        self.assertEqual(self.obj.passOutDouble_(None), 42.0)
684        self.assertEqual(self.obj.passOutDouble_(None), 1e10)
685
686    def testDoubleInOut(self):
687        self.assertEqual(self.obj.passInOutDouble_(10), 420)
688        self.assertEqual(self.obj.passInOutDouble_(10.0), 420)
689        self.assertEqual(self.obj.passInOutDouble_(0.01), 0.42)
690
691    def testCharpIn(self):
692        self.assertEqual(self.obj.passInCharp_(b"hello"), b"hheelllloo")
693        self.assertEqual(self.obj.passInCharp_(b"abcde"), b"aabbccddee")
694
695    def testCharpOut(self):
696        self.obj.reset()
697        self.assertEqual(self.obj.passOutCharp_(None), b"hello")
698        self.assertEqual(self.obj.passOutCharp_(None), b"world")
699        self.assertEqual(self.obj.passOutCharp_(None), b"foobar")
700
701    def testCharpInOut(self):
702        self.assertEqual(self.obj.passInOutCharp_(b"hello"), b"hheelllloo")
703        self.assertEqual(self.obj.passInOutCharp_(b"abcdef"), b"aabbccddeeff")
704
705    # TODO: structs (including Objective-C part)
706
707
708
709#
710# Below this point only TestCases that test calling Python from Objective-C
711#
712# Notes:
713# - There is both a pure python and an Python-ObjC hybrid class
714#   (MyPyClass and MyOCClass). The former is not used in the first few tests,
715#   we need to decide what functionality we'll support for these (e.g. do
716#   we support selector objects, or only normal methods?)
717# - Every test below should exists in two variants:
718#   * 'invoke' - Calls back from ObjC to Python using an NSInvocation
719#                and the forwardInvocation method
720#   * 'call'   - Calls back from ObjC to Python using a normal ObjC method call
721
722# Values for
723# - 8-bit char
724# - 16-bit short
725# - 32-bit int/long
726# - 64-bit long long
727# Values are: max-negative, -42, 0, 42, max-positive, out-of-range, bad-type, None
728#
729CHAR_NUMBERS=[ -128, -42, 0, 42, 127, b'a', 128, "hello", None ]
730UCHAR_NUMBERS=[ 0, 42, 255, b'a', 256, "hello", None ]
731SHORT_NUMBERS=[ -32768, -42, 0, 32767, 32768, "hello", None ]
732USHORT_NUMBERS=[ 0, 42, 65535, 65536, "hello", None ]
733INT_NUMBERS=[ -2147483648, -42, 0, 2147483647, 2147483648, "hello", None ]
734UINT_NUMBERS=[ 0, 42, 4294967295, 4294967296, "hello", None ]
735LONG_NUMBERS=[ -2147483648, -42, 0, 2147483647, sys.maxsize+1, "hello", None ]
736ULONG_NUMBERS=[ 0, 42, 4294967295, 2*(sys.maxsize+1), "hello", None ]
737LONGLONG_NUMBERS=[ -9223372036854775808, -42, 0, 42, 9223372036854775807, 9223372036854775808, "hello", None ]
738ULONGLONG_NUMBERS=[0, 42, 18446744073709551615, 18446744073709551616, "hello", None ]
739
740FLOAT_NUMBERS = [ makeCFloat(0.1), makeCFloat(100.0) ]
741DOUBLE_NUMBERS = [ 1.5, 3.5, 1e10, 1.99e10 ]
742OBJECTS = [ "hello", 1.0, range(4), lambda x: 10 ]
743DUMMY_OBJECTS = [ (1, 1), (-10, -10), (-4, -5), (0, 0), (10, 20) ]
744DUMMY2_OBJECTS = [ ((1, 2, 3, 4),), ((-9, -8, -7, -6),)]
745POINTS=[ (1.0, 2.0), (1e10, 2e10), (-0.5, 0.5) ]
746
747class MyPyClass:
748    def __init__(self):
749        self.idx = 0
750
751    def reset(self):
752        self.idx = 0
753
754    def idFunc(self):
755        i = self.idx
756        self.idx += 1
757        return OBJECTS[i]
758
759class MyOCClass (objc.lookUpClass('NSObject')):
760    def __init__(self):
761        self.idx = 0
762
763    def reset(self):
764        self.idx = 0
765
766    def charFunc(self):
767        i = self.idx
768        self.idx += 1
769        return CHAR_NUMBERS[i]
770    charFunc = objc.selector(charFunc, signature=OC_TestClass1.charFunc.signature)
771
772    def ucharFunc(self):
773        i = self.idx
774        self.idx += 1
775        return UCHAR_NUMBERS[i]
776    ucharFunc = objc.selector(ucharFunc, signature=OC_TestClass1.ucharFunc.signature)
777
778    def shortFunc(self):
779        i = self.idx
780        self.idx += 1
781        return SHORT_NUMBERS[i]
782    shortFunc = objc.selector(shortFunc, signature=OC_TestClass1.shortFunc.signature)
783
784    def ushortFunc(self):
785        i = self.idx
786        self.idx += 1
787        return USHORT_NUMBERS[i]
788    ushortFunc = objc.selector(ushortFunc, signature=OC_TestClass1.ushortFunc.signature)
789
790    def intFunc(self):
791        i = self.idx
792        self.idx += 1
793        return INT_NUMBERS[i]
794    intFunc = objc.selector(intFunc, signature=OC_TestClass1.intFunc.signature)
795
796    def uintFunc(self):
797        i = self.idx
798        self.idx += 1
799        return UINT_NUMBERS[i]
800    uintFunc = objc.selector(uintFunc, signature=OC_TestClass1.uintFunc.signature)
801
802    def longFunc(self):
803        i = self.idx
804        self.idx += 1
805        return LONG_NUMBERS[i]
806    longFunc = objc.selector(longFunc, signature=OC_TestClass1.longFunc.signature)
807
808    def ulongFunc(self):
809        i = self.idx
810        self.idx += 1
811        return ULONG_NUMBERS[i]
812    ulongFunc = objc.selector(ulongFunc, signature=OC_TestClass1.ulongFunc.signature)
813
814    def longlongFunc(self):
815        i = self.idx
816        self.idx += 1
817        return LONGLONG_NUMBERS[i]
818    longlongFunc = objc.selector(longlongFunc, signature=OC_TestClass1.longlongFunc.signature)
819
820    def ulonglongFunc(self):
821        i = self.idx
822        self.idx += 1
823        return ULONGLONG_NUMBERS[i]
824    ulonglongFunc = objc.selector(ulonglongFunc, signature=OC_TestClass1.ulonglongFunc.signature)
825
826    def floatFunc(self):
827        i = self.idx
828        self.idx += 1
829        return FLOAT_NUMBERS[i]
830    floatFunc = objc.selector(floatFunc, signature=OC_TestClass1.floatFunc.signature)
831
832    def doubleFunc(self):
833        i = self.idx
834        self.idx += 1
835        return DOUBLE_NUMBERS[i]
836    doubleFunc = objc.selector(doubleFunc, signature=OC_TestClass1.doubleFunc.signature)
837
838    def idFunc(self):
839        i = self.idx
840        self.idx += 1
841        return OBJECTS[i]
842
843    def dummyFunc(self):
844        i = self.idx
845        self.idx += 1
846        return DUMMY_OBJECTS[i]
847    dummyFunc = objc.selector(dummyFunc, signature=OC_TestClass1.dummyFunc.signature)
848
849    def dummy2Func(self):
850        i = self.idx
851        self.idx += 1
852        return DUMMY2_OBJECTS[i]
853    dummy2Func = objc.selector(dummy2Func, signature=OC_TestClass1.dummy2Func.signature)
854
855    def nspointFunc(self):
856        i = self.idx
857        self.idx += 1
858        return POINTS[i]
859    nspointFunc = objc.selector(nspointFunc, signature=OC_TestClass1.nspointFunc.signature)
860
861    def charArg_(self, arg):
862        return arg * 2
863    charArg_ = objc.selector(charArg_, signature=OC_TestClass1.charArg_.signature)
864    def ucharArg_(self, arg):
865        return arg * 2
866    ucharArg_ = objc.selector(ucharArg_, signature=OC_TestClass1.ucharArg_.signature)
867
868    def shortArg_(self, arg):
869        return arg * 2
870    shortArg_ = objc.selector(shortArg_, signature=OC_TestClass1.shortArg_.signature)
871
872    def ushortArg_(self, arg):
873        return arg * 2
874    ushortArg_ = objc.selector(ushortArg_, signature=OC_TestClass1.ushortArg_.signature)
875
876    def intArg_(self, arg):
877        return arg * 2
878    intArg_ = objc.selector(intArg_, signature=OC_TestClass1.intArg_.signature)
879
880    def uintArg_(self, arg):
881        return arg * 2
882    uintArg_ = objc.selector(uintArg_, signature=OC_TestClass1.uintArg_.signature)
883
884    def longArg_(self, arg):
885        return arg * 2
886    longArg_ = objc.selector(longArg_, signature=OC_TestClass1.longArg_.signature)
887
888    def ulongArg_(self, arg):
889        return arg * 2
890    ulongArg_ = objc.selector(ulongArg_, signature=OC_TestClass1.ulongArg_.signature)
891
892    def longlongArg_(self, arg):
893        return arg * 2
894    longlongArg_ = objc.selector(longlongArg_, signature=OC_TestClass1.longlongArg_.signature)
895
896    def ulonglongArg_(self, arg):
897        return arg * 2
898    ulonglongArg_ = objc.selector(ulonglongArg_, signature=OC_TestClass1.ulonglongArg_.signature)
899
900    def floatArg_(self, arg):
901        return arg * 2
902    floatArg_ = objc.selector(floatArg_, signature=OC_TestClass1.floatArg_.signature)
903
904    def doubleArg_(self, arg):
905        return arg * 2
906    doubleArg_ = objc.selector(doubleArg_, signature=OC_TestClass1.doubleArg_.signature)
907
908
909class OCPyTestSimpleCalls(TestCase):
910    #
911    # Test argument passing of single basic values by reference.
912    #
913    def setUp(self):
914        self.pyobj = MyPyClass()
915        self.ocobj = MyOCClass.new()
916        self.obj = OC_TestClass2.new()
917
918    def testCChar(self):
919        self.pyobj.reset()
920        self.ocobj.reset()
921
922        for o in CHAR_NUMBERS[:-3]:
923            if isinstance(o, (bytes, str)):
924                o = ord(o)
925            self.assertEqual(self.obj.callInstanceCharFuncOf_(self.ocobj), o)
926
927        self.assertRaises(ValueError, self.obj.callInstanceCharFuncOf_, self.ocobj)
928        self.assertRaises(ValueError, self.obj.callInstanceCharFuncOf_, self.ocobj)
929        self.assertRaises(ValueError, self.obj.callInstanceCharFuncOf_, self.ocobj)
930
931    def testIChar(self):
932        self.pyobj.reset()
933        self.ocobj.reset()
934
935        for o in CHAR_NUMBERS[:-3]:
936            if isinstance(o, (str, bytes)):
937                o = ord(o)
938            self.assertEqual(self.obj.invokeInstanceCharFuncOf_(self.ocobj), o)
939
940        self.assertRaises(ValueError, self.obj.invokeInstanceCharFuncOf_, self.ocobj)
941        self.assertRaises(ValueError, self.obj.invokeInstanceCharFuncOf_, self.ocobj)
942        self.assertRaises(ValueError, self.obj.invokeInstanceCharFuncOf_, self.ocobj)
943
944    def testCUChar(self):
945        self.pyobj.reset()
946        self.ocobj.reset()
947
948        for o in UCHAR_NUMBERS[:-3]:
949            if isinstance(o, (str, bytes)):
950                o = ord(o)
951            self.assertEqual(self.obj.callInstanceUnsignedCharFuncOf_(self.ocobj), o)
952
953        self.assertRaises(ValueError, self.obj.callInstanceUnsignedCharFuncOf_, self.ocobj)
954        self.assertRaises(ValueError, self.obj.callInstanceUnsignedCharFuncOf_, self.ocobj)
955        self.assertRaises(ValueError, self.obj.callInstanceUnsignedCharFuncOf_, self.ocobj)
956
957    def testIUChar(self):
958        self.pyobj.reset()
959        self.ocobj.reset()
960
961        for o in UCHAR_NUMBERS[:-3]:
962            if isinstance(o, (bytes, str)):
963                o = ord(o)
964            self.assertEqual(self.obj.invokeInstanceUnsignedCharFuncOf_(self.ocobj), o)
965
966        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedCharFuncOf_, self.ocobj)
967        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedCharFuncOf_, self.ocobj)
968        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedCharFuncOf_, self.ocobj)
969
970    def testCShort(self):
971        self.pyobj.reset()
972        self.ocobj.reset()
973
974        for o in SHORT_NUMBERS[:-3]:
975            self.assertEqual(self.obj.callInstanceShortFuncOf_(self.ocobj), o)
976
977        self.assertRaises(ValueError, self.obj.callInstanceShortFuncOf_, self.ocobj)
978        self.assertRaises(ValueError, self.obj.callInstanceShortFuncOf_, self.ocobj)
979        self.assertRaises(ValueError, self.obj.callInstanceShortFuncOf_, self.ocobj)
980
981    def testIShort(self):
982        self.pyobj.reset()
983        self.ocobj.reset()
984
985        for o in SHORT_NUMBERS[:-3]:
986            self.assertEqual(self.obj.invokeInstanceShortFuncOf_(self.ocobj), o)
987
988        self.assertRaises(ValueError, self.obj.invokeInstanceShortFuncOf_, self.ocobj)
989        self.assertRaises(ValueError, self.obj.invokeInstanceShortFuncOf_, self.ocobj)
990        self.assertRaises(ValueError, self.obj.invokeInstanceShortFuncOf_, self.ocobj)
991
992    def testCUShort(self):
993        self.pyobj.reset()
994        self.ocobj.reset()
995
996        for o in USHORT_NUMBERS[:-3]:
997            self.assertEqual(self.obj.callInstanceUnsignedShortFuncOf_(self.ocobj), o)
998
999        self.assertRaises(ValueError, self.obj.callInstanceUnsignedShortFuncOf_, self.ocobj)
1000        self.assertRaises(ValueError, self.obj.callInstanceUnsignedShortFuncOf_, self.ocobj)
1001        self.assertRaises(ValueError, self.obj.callInstanceUnsignedShortFuncOf_, self.ocobj)
1002
1003    def testIUShort(self):
1004        self.pyobj.reset()
1005        self.ocobj.reset()
1006
1007        for o in USHORT_NUMBERS[:-3]:
1008            self.assertEqual(self.obj.invokeInstanceUnsignedShortFuncOf_(self.ocobj), o)
1009
1010        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedShortFuncOf_, self.ocobj)
1011        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedShortFuncOf_, self.ocobj)
1012        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedShortFuncOf_, self.ocobj)
1013
1014    def testCInt(self):
1015        self.pyobj.reset()
1016        self.ocobj.reset()
1017
1018        for o in INT_NUMBERS[:-3]:
1019            self.assertEqual(self.obj.callInstanceIntFuncOf_(self.ocobj), o)
1020
1021        self.assertRaises(ValueError, self.obj.callInstanceIntFuncOf_, self.ocobj)
1022        self.assertRaises(ValueError, self.obj.callInstanceIntFuncOf_, self.ocobj)
1023        self.assertRaises(ValueError, self.obj.callInstanceIntFuncOf_, self.ocobj)
1024
1025    def testIInt(self):
1026        self.pyobj.reset()
1027        self.ocobj.reset()
1028
1029        for o in INT_NUMBERS[:-3]:
1030            self.assertEqual(self.obj.invokeInstanceIntFuncOf_(self.ocobj), o)
1031
1032        self.assertRaises(ValueError, self.obj.invokeInstanceIntFuncOf_, self.ocobj)
1033        self.assertRaises(ValueError, self.obj.invokeInstanceIntFuncOf_, self.ocobj)
1034        self.assertRaises(ValueError, self.obj.invokeInstanceIntFuncOf_, self.ocobj)
1035
1036    def testCUInt(self):
1037        self.pyobj.reset()
1038        self.ocobj.reset()
1039
1040        for o in UINT_NUMBERS[:-3]:
1041            self.assertEqual(self.obj.callInstanceUnsignedIntFuncOf_(self.ocobj), o)
1042
1043        self.assertRaises(ValueError, self.obj.callInstanceUnsignedIntFuncOf_, self.ocobj)
1044        self.assertRaises(ValueError, self.obj.callInstanceUnsignedIntFuncOf_, self.ocobj)
1045        self.assertRaises(ValueError, self.obj.callInstanceUnsignedIntFuncOf_, self.ocobj)
1046
1047    def testIUInt(self):
1048        self.pyobj.reset()
1049        self.ocobj.reset()
1050
1051        for o in UINT_NUMBERS[:-3]:
1052            self.assertEqual(self.obj.invokeInstanceUnsignedIntFuncOf_(self.ocobj), o)
1053
1054        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedIntFuncOf_, self.ocobj)
1055        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedIntFuncOf_, self.ocobj)
1056        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedIntFuncOf_, self.ocobj)
1057
1058    def testCLong(self):
1059        self.pyobj.reset()
1060        self.ocobj.reset()
1061
1062        for o in LONG_NUMBERS[:-3]:
1063            self.assertEqual(self.obj.callInstanceLongFuncOf_(self.ocobj), o)
1064
1065        self.assertRaises(ValueError, self.obj.callInstanceLongFuncOf_, self.ocobj)
1066        self.assertRaises(ValueError, self.obj.callInstanceLongFuncOf_, self.ocobj)
1067        self.assertRaises(ValueError, self.obj.callInstanceLongFuncOf_, self.ocobj)
1068
1069    def testILong(self):
1070        self.pyobj.reset()
1071        self.ocobj.reset()
1072
1073        for o in LONG_NUMBERS[:-3]:
1074            self.assertEqual(self.obj.invokeInstanceLongFuncOf_(self.ocobj), o)
1075
1076        self.assertRaises(ValueError, self.obj.invokeInstanceLongFuncOf_, self.ocobj)
1077        self.assertRaises(ValueError, self.obj.invokeInstanceLongFuncOf_, self.ocobj)
1078        self.assertRaises(ValueError, self.obj.invokeInstanceLongFuncOf_, self.ocobj)
1079
1080    def testCULong(self):
1081        self.pyobj.reset()
1082        self.ocobj.reset()
1083
1084        for o in ULONG_NUMBERS[:-3]:
1085            self.assertEqual(self.obj.callInstanceUnsignedLongFuncOf_(self.ocobj), o)
1086
1087        self.assertRaises(ValueError, self.obj.callInstanceUnsignedLongFuncOf_, self.ocobj)
1088        self.assertRaises(ValueError, self.obj.callInstanceUnsignedLongFuncOf_, self.ocobj)
1089        self.assertRaises(ValueError, self.obj.callInstanceUnsignedLongFuncOf_, self.ocobj)
1090
1091    def testIULong(self):
1092        self.pyobj.reset()
1093        self.ocobj.reset()
1094
1095        for o in ULONG_NUMBERS[:-3]:
1096            self.assertEqual(self.obj.invokeInstanceUnsignedLongFuncOf_(self.ocobj), o)
1097
1098        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedLongFuncOf_, self.ocobj)
1099        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedLongFuncOf_, self.ocobj)
1100        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedLongFuncOf_, self.ocobj)
1101
1102    def testCLongLong(self):
1103        self.pyobj.reset()
1104        self.ocobj.reset()
1105
1106        for o in LONGLONG_NUMBERS[:-3]:
1107            self.assertEqual(self.obj.callInstanceLongLongFuncOf_(self.ocobj), o)
1108
1109        self.assertRaises(ValueError, self.obj.callInstanceLongLongFuncOf_, self.ocobj)
1110        self.assertRaises(ValueError, self.obj.callInstanceLongLongFuncOf_, self.ocobj)
1111        self.assertRaises(ValueError, self.obj.callInstanceLongLongFuncOf_, self.ocobj)
1112
1113    def testILongLong(self):
1114        self.pyobj.reset()
1115        self.ocobj.reset()
1116
1117        for o in LONGLONG_NUMBERS[:-3]:
1118            self.assertEqual(self.obj.invokeInstanceLongLongFuncOf_(self.ocobj), o)
1119
1120        self.assertRaises(ValueError, self.obj.invokeInstanceLongLongFuncOf_, self.ocobj)
1121        self.assertRaises(ValueError, self.obj.invokeInstanceLongLongFuncOf_, self.ocobj)
1122        self.assertRaises(ValueError, self.obj.invokeInstanceLongLongFuncOf_, self.ocobj)
1123
1124    def testCULongLong(self):
1125        self.pyobj.reset()
1126        self.ocobj.reset()
1127
1128        for o in ULONGLONG_NUMBERS[:-3]:
1129            self.assertEqual(self.obj.callInstanceUnsignedLongLongFuncOf_(self.ocobj), o)
1130
1131        self.assertRaises(ValueError, self.obj.callInstanceUnsignedLongLongFuncOf_, self.ocobj)
1132        self.assertRaises(ValueError, self.obj.callInstanceUnsignedLongLongFuncOf_, self.ocobj)
1133        self.assertRaises(ValueError, self.obj.callInstanceUnsignedLongLongFuncOf_, self.ocobj)
1134
1135    def testIULongLong(self):
1136        self.pyobj.reset()
1137        self.ocobj.reset()
1138
1139        for o in ULONGLONG_NUMBERS[:-3]:
1140            self.assertEqual(self.obj.invokeInstanceUnsignedLongLongFuncOf_(self.ocobj), o)
1141
1142        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedLongLongFuncOf_, self.ocobj)
1143        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedLongLongFuncOf_, self.ocobj)
1144        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedLongLongFuncOf_, self.ocobj)
1145
1146    def testCFloat(self):
1147        self.pyobj.reset()
1148        self.ocobj.reset()
1149
1150        for o in FLOAT_NUMBERS:
1151            self.assertEqual(self.obj.callInstanceFloatFuncOf_(self.ocobj), o)
1152
1153    def testIFloat(self):
1154        self.pyobj.reset()
1155        self.ocobj.reset()
1156
1157        for o in FLOAT_NUMBERS:
1158            self.assertEqual(self.obj.invokeInstanceFloatFuncOf_(self.ocobj), o)
1159
1160    def testCDouble(self):
1161        self.pyobj.reset()
1162        self.ocobj.reset()
1163
1164        for o in DOUBLE_NUMBERS:
1165            self.assertEqual(self.obj.callInstanceDoubleFuncOf_(self.ocobj), o)
1166
1167    def testIDouble(self):
1168        self.pyobj.reset()
1169        self.ocobj.reset()
1170
1171        for o in DOUBLE_NUMBERS:
1172            self.assertEqual(self.obj.invokeInstanceDoubleFuncOf_(self.ocobj), o)
1173
1174    def testCId(self):
1175        self.pyobj.reset()
1176        self.ocobj.reset()
1177
1178        for o in OBJECTS:
1179            self.assertEqual(self.obj.callInstanceIdFuncOf_(self.ocobj), o)
1180
1181    def testIId(self):
1182        self.pyobj.reset()
1183        self.ocobj.reset()
1184
1185        for o in OBJECTS:
1186            self.assertEqual(self.obj.invokeInstanceIdFuncOf_(self.ocobj), o)
1187
1188    def testCId2(self):
1189        self.pyobj.reset()
1190        self.ocobj.reset()
1191
1192        for o in OBJECTS:
1193            self.assertEqual(self.obj.callInstanceIdFuncOf_(self.pyobj), o)
1194
1195    def testIId2(self):
1196        self.pyobj.reset()
1197        self.ocobj.reset()
1198
1199        for o in OBJECTS:
1200            self.assertEqual(self.obj.invokeInstanceIdFuncOf_(self.pyobj), o)
1201
1202    def testCStruct1(self):
1203        self.pyobj.reset()
1204        self.ocobj.reset()
1205
1206        for o in DUMMY_OBJECTS:
1207            self.assertEqual(self.obj.callInstanceDummyFuncOf_(self.ocobj), o)
1208
1209    def testIStruct1(self):
1210        self.pyobj.reset()
1211        self.ocobj.reset()
1212
1213        for o in DUMMY_OBJECTS:
1214            self.assertEqual(self.obj.invokeInstanceDummyFuncOf_(self.ocobj), o)
1215
1216    def testCStruct2(self):
1217        self.pyobj.reset()
1218        self.ocobj.reset()
1219
1220        for o in DUMMY2_OBJECTS:
1221            self.assertEqual(self.obj.callInstanceDummy2FuncOf_(self.ocobj), o)
1222
1223    def testIStruct2(self):
1224        self.pyobj.reset()
1225        self.ocobj.reset()
1226
1227        for o in DUMMY2_OBJECTS:
1228            self.assertEqual(self.obj.invokeInstanceDummy2FuncOf_(self.ocobj), o)
1229
1230    def testCNSPoint(self):
1231        self.pyobj.reset()
1232        self.ocobj.reset()
1233
1234        for o in POINTS:
1235            self.assertEqual(self.obj.callInstanceNSPointFuncOf_(self.ocobj), o)
1236
1237    def testINSPoint(self):
1238        self.pyobj.reset()
1239        self.ocobj.reset()
1240
1241        for o in POINTS:
1242            self.assertEqual(self.obj.invokeInstanceNSPointFuncOf_(self.ocobj), o)
1243
1244class OCPyTestSimpleArguments(TestCase):
1245    #
1246    # Test argument passing of single basic values.
1247    #
1248    # TODO: Copy and adapt rest of OCPyTestSimpleArguments
1249    #
1250
1251    def setUp(self):
1252        self.ocobj = MyOCClass.new()
1253        self.obj = OC_TestClass2.new()
1254
1255    def testCLongLong(self):
1256        self.assertEqual(self.obj.callInstanceLongLongArg_on_(0, self.ocobj), 0)
1257        self.assertEqual(self.obj.callInstanceLongLongArg_on_(10, self.ocobj), 20)
1258        self.assertEqual(self.obj.callInstanceLongLongArg_on_(1 << 60, self.ocobj), 1 << 61)
1259        self.assertEqual(self.obj.callInstanceLongLongArg_on_(-10, self.ocobj), -20)
1260        self.assertEqual(self.obj.callInstanceLongLongArg_on_(-(1 << 60), self.ocobj), -(1 << 61))
1261
1262    def testILongLong(self):
1263        self.assertEqual(self.obj.invokeInstanceLongLongArg_on_(0, self.ocobj), 0)
1264        self.assertEqual(self.obj.invokeInstanceLongLongArg_on_(10, self.ocobj), 20)
1265        self.assertEqual(self.obj.invokeInstanceLongLongArg_on_(1 << 60, self.ocobj), 1 << 61)
1266        self.assertEqual(self.obj.invokeInstanceLongLongArg_on_(-10, self.ocobj), -20)
1267        self.assertEqual(self.obj.invokeInstanceLongLongArg_on_(-(1 << 60), self.ocobj), -(1 << 61))
1268
1269if __name__ == '__main__':
1270    main()
1271