Source

pyobjc / Modules / objc / unittest.m

The branch 'pyobjc-ancient' does not exist.
Full commit
  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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
 * This file contains some unittests for specific functions in the extension 
 * module. 
 *
 * The PyObjC unittest objc.test.test_ctests executes the tests in this file.
 *
 * All at least for structured types, the basic types have fairly trivial 
 * implementations.
 *
 * This currently only tests a number of problematic structs.
 */
#include "pyobjc-api.h"
#include "pyobjc-unittest.h"

#import <Foundation/Foundation.h>

struct Struct1 {
	int f1;
	double f2;
};

struct Struct2 {
	int f1;
	double f2;
	short s[5];
};

struct Struct3 {
	char ch;
	int  i;
};

struct Struct4 {
	char ch;
	long long i;
};



/* Helper stuff for TestNSInvoke */

static struct Struct2 invokeHelper = { 0, 0, { 0, 0, 0, 0, 0 } };

@interface PyObjCTest_NSInvoke : NSObject
{
}
-(void)methodWithMyStruct: (struct Struct2)val1 andShort:(short)val2;


@end

@implementation PyObjCTest_NSInvoke 
-(void)methodWithMyStruct: (struct Struct2)val1 andShort:(short)val2
{
	(void)val2;
	invokeHelper = val1;
}
@end

#ifndef GNU_RUNTIME


BEGIN_UNITTEST(CheckNSInvoke)
	/* This is not a 'real' unittest, but is used to disable a number of
	 * other tests (in objc.test.test_methods2) when NSInvocation isn't
	 * working correctly (MacOS X at least upto 10.2.6).
	 * [Panther previews also have this problem]
	 *
	 * GNUstep is even worse, this test causes a crash of the interpreter,
	 */
	PyObjCTest_NSInvoke* obj = [[PyObjCTest_NSInvoke alloc] init];
	NSInvocation* inv;
	struct Struct2 v1 = { 1, 2, { 3, 4, 5, 6, 7 } };
	short    v2 = 8;

	[obj methodWithMyStruct: v1 andShort: v2];
	inv = [NSInvocation invocationWithMethodSignature:
		[obj methodSignatureForSelector:@selector(methodWithMyStruct:andShort:)]];
	[inv setTarget: obj];
	[inv setSelector: @selector(methodWithMyStruct:andShort:)];
	[inv setArgument: &v1 atIndex: 2];
	[inv setArgument: &v2 atIndex: 3];

	[inv invoke];
	ASSERT_EQUALS(invokeHelper.f1, v1.f1, "%d");
	ASSERT_EQUALS(invokeHelper.f2, v1.f2, "%g");
	ASSERT_EQUALS(invokeHelper.s[0], v1.s[0], "%d");
	ASSERT_EQUALS(invokeHelper.s[1], v1.s[1], "%d");
	ASSERT_EQUALS(invokeHelper.s[2], v1.s[2], "%d");
	ASSERT_EQUALS(invokeHelper.s[3], v1.s[3], "%d");
	ASSERT_EQUALS(invokeHelper.s[4], v1.s[4], "%d");

END_UNITTEST

#else

BEGIN_UNITTEST(CheckNSInvoke)
	/* Force failure, see comment above */
	ASSERT_EQUALS(0, 1, "%d");
END_UNITTEST

#endif


BEGIN_UNITTEST(StructSize)

	ASSERT_EQUALS(
	    sizeof(struct Struct1), PyObjCRT_SizeOfType(@encode(struct Struct1)),
	    "%d");

	ASSERT_EQUALS(
	    sizeof(struct Struct2), PyObjCRT_SizeOfType(@encode(struct Struct2)),
	    "%d");

	ASSERT_EQUALS(
	    sizeof(struct Struct3), PyObjCRT_SizeOfType(@encode(struct Struct3)),
	    "%d");

	ASSERT_EQUALS(
	    sizeof(struct Struct4), PyObjCRT_SizeOfType(@encode(struct Struct4)),
	    "%d");

END_UNITTEST

BEGIN_UNITTEST(StructAlign)

	ASSERT_EQUALS(
	    __alignof__(struct Struct1), 
	    PyObjCRT_AlignOfType(@encode(struct Struct1)),
	    "%d");

	ASSERT_EQUALS(
	    __alignof__(struct Struct2), 
	    PyObjCRT_AlignOfType(@encode(struct Struct2)),
	    "%d");

	ASSERT_EQUALS(
	    __alignof__(struct Struct3), 
	    PyObjCRT_AlignOfType(@encode(struct Struct3)),
	    "%d");

	ASSERT_EQUALS(
	    __alignof__(struct Struct4), 
	    PyObjCRT_AlignOfType(@encode(struct Struct4)),
	    "%d");

END_UNITTEST


BEGIN_UNITTEST(FillStruct1)

	PyObject* input;
	struct Struct1 output;
	int r;

	input = PyTuple_New(2);
	FAIL_IF(input == NULL);

	PyTuple_SET_ITEM(input, 0, PyInt_FromLong(1));
	PyTuple_SET_ITEM(input, 1, PyFloat_FromDouble(2));
	
	r = PyObjC_PythonToObjC(@encode(struct Struct1), input, &output);
	FAIL_IF(r < 0);

	Py_DECREF(input);

	ASSERT_EQUALS(output.f1, 1, "%d");
	ASSERT_EQUALS(output.f2, 2.0, "%g");

END_UNITTEST

BEGIN_UNITTEST(FillStruct2)

	PyObject* input;
	PyObject* v;
	struct Struct2 output;
	int r;

	input = PyTuple_New(3);
	FAIL_IF(input == NULL);

	v = PyTuple_New(5);
	PyTuple_SET_ITEM(v, 0, PyInt_FromLong(10));
	PyTuple_SET_ITEM(v, 1, PyInt_FromLong(11));
	PyTuple_SET_ITEM(v, 2, PyInt_FromLong(12));
	PyTuple_SET_ITEM(v, 3, PyInt_FromLong(13));
	PyTuple_SET_ITEM(v, 4, PyInt_FromLong(14));

	PyTuple_SET_ITEM(input, 0, PyInt_FromLong(1));
	PyTuple_SET_ITEM(input, 1, PyFloat_FromDouble(2));
	PyTuple_SET_ITEM(input, 2, v);
	
	r = PyObjC_PythonToObjC(@encode(struct Struct2), input, &output);
	FAIL_IF(r < 0);

	Py_DECREF(input);

	ASSERT_EQUALS(output.f1, 1,    "%d");
	ASSERT_EQUALS(output.f2, 2.0,  "%g");
	ASSERT_EQUALS(output.s[0], 10, "%d");
	ASSERT_EQUALS(output.s[1], 11, "%d");
	ASSERT_EQUALS(output.s[2], 12, "%d");
	ASSERT_EQUALS(output.s[3], 13, "%d");
	ASSERT_EQUALS(output.s[4], 14, "%d");

END_UNITTEST

BEGIN_UNITTEST(FillStruct3)

	PyObject* input;
	struct Struct3 output;
	int r;

	input = PyTuple_New(2);
	FAIL_IF(input == NULL);

	PyTuple_SET_ITEM(input, 0,  PyString_FromStringAndSize("\001", 1));
	PyTuple_SET_ITEM(input, 1, PyInt_FromLong(2));
	
	r = PyObjC_PythonToObjC(@encode(struct Struct3), input, &output);
	FAIL_IF(r < 0);

	Py_DECREF(input);

	ASSERT_EQUALS(output.ch, '\001',    "%d");
	ASSERT_EQUALS(output.i, 2,  "%d");

END_UNITTEST

BEGIN_UNITTEST(FillStruct4)

	PyObject* input;
	struct Struct4 output;
	int r;

	input = PyTuple_New(2);
	FAIL_IF(input == NULL);

	PyTuple_SET_ITEM(input, 0,  PyString_FromStringAndSize("\001", 1));
	PyTuple_SET_ITEM(input, 1, PyInt_FromLong(500000));
	
	r = PyObjC_PythonToObjC(@encode(struct Struct4), input, &output);
	FAIL_IF(r < 0);

	Py_DECREF(input);

	ASSERT_EQUALS(output.ch, '\001',    "%d");
	ASSERT_EQUALS(output.i, 500000,  "%ll");

END_UNITTEST

BEGIN_UNITTEST(ExtractStruct1) 

	struct Struct1 input;
	PyObject* output;

	input.f1 = 1;
	input.f2 = 2;

	output = PyObjC_ObjCToPython(@encode(struct Struct1), &input);
	FAIL_IF(output == NULL);

	ASSERT_ISINSTANCE(output, Tuple);	
	ASSERT_EQUALS(PyTuple_GET_SIZE(output), 2, "%d");
	ASSERT_ISINSTANCE(PyTuple_GET_ITEM(output, 0), Int);
	ASSERT_ISINSTANCE(PyTuple_GET_ITEM(output, 1), Float);
	ASSERT_EQUALS(PyInt_AsLong(PyTuple_GET_ITEM(output, 0)), 1, "%d");
	ASSERT_EQUALS(PyFloat_AsDouble(PyTuple_GET_ITEM(output, 1)), 2.0, "%g");

END_UNITTEST

BEGIN_UNITTEST(ExtractStruct2) 

	struct Struct2 input;
	PyObject* output;
	PyObject* tup;
	PyObject* v;

	input.f1 = 1;
	input.f2 = 2;
	input.s[0] = 3;
	input.s[1] = 4;
	input.s[2] = 5;
	input.s[3] = 6;
	input.s[4] = 7;

	output = PyObjC_ObjCToPython(@encode(struct Struct2), &input);
	FAIL_IF(output == NULL);

	ASSERT_ISINSTANCE(output, Tuple);	
	ASSERT_EQUALS(PyTuple_GET_SIZE(output), 3, "%d");
	ASSERT_ISINSTANCE(PyTuple_GET_ITEM(output, 0), Int);
	ASSERT_ISINSTANCE(PyTuple_GET_ITEM(output, 1), Float);
	ASSERT_ISINSTANCE(PyTuple_GET_ITEM(output, 2), Tuple);
	ASSERT_EQUALS(PyInt_AsLong(PyTuple_GET_ITEM(output, 0)), 1, "%d");
	ASSERT_EQUALS(PyFloat_AsDouble(PyTuple_GET_ITEM(output, 1)), 2.0, "%g");

	tup = PyTuple_GET_ITEM(output, 2);
	ASSERT_EQUALS(PyTuple_GET_SIZE(tup), 5, "%d");

	v = PyTuple_GET_ITEM(tup, 0);
	ASSERT_ISINSTANCE(v, Int);
	ASSERT_EQUALS(PyInt_AsLong(v), 3, "%d");

	v = PyTuple_GET_ITEM(tup, 1);
	ASSERT_ISINSTANCE(v, Int);
	ASSERT_EQUALS(PyInt_AsLong(v), 4, "%d");

	v = PyTuple_GET_ITEM(tup, 2);
	ASSERT_ISINSTANCE(v, Int);
	ASSERT_EQUALS(PyInt_AsLong(v), 5, "%d");

	v = PyTuple_GET_ITEM(tup, 3);
	ASSERT_ISINSTANCE(v, Int);
	ASSERT_EQUALS(PyInt_AsLong(v), 6, "%d");

	v = PyTuple_GET_ITEM(tup, 4);
	ASSERT_ISINSTANCE(v, Int);
	ASSERT_EQUALS(PyInt_AsLong(v), 7, "%d");

END_UNITTEST

BEGIN_UNITTEST(ExtractStruct3) 

	struct Struct3 input;
	PyObject* output;

	input.ch = 1;
	input.i = 2;

	output = PyObjC_ObjCToPython(@encode(struct Struct3), &input);
	FAIL_IF(output == NULL);

	ASSERT_ISINSTANCE(output, Tuple);	
	ASSERT_EQUALS(PyTuple_GET_SIZE(output), 2, "%d");
	ASSERT_ISINSTANCE(PyTuple_GET_ITEM(output, 0), Int);
	ASSERT_ISINSTANCE(PyTuple_GET_ITEM(output, 1), Int);
	ASSERT_EQUALS(PyInt_AsLong(PyTuple_GET_ITEM(output, 0)), 1, "%d");
	ASSERT_EQUALS(PyInt_AsLong(PyTuple_GET_ITEM(output, 1)), 2, "%d");

END_UNITTEST

BEGIN_UNITTEST(ExtractStruct4) 

	struct Struct4 input;
	PyObject* output;

	input.ch = 1;
	input.i = 500000;

	output = PyObjC_ObjCToPython(@encode(struct Struct4), &input);
	FAIL_IF(output == NULL);

	ASSERT_ISINSTANCE(output, Tuple);	
	ASSERT_EQUALS(PyTuple_GET_SIZE(output), 2, "%d");
	ASSERT_ISINSTANCE(PyTuple_GET_ITEM(output, 0), Int);
	ASSERT_ISINSTANCE(PyTuple_GET_ITEM(output, 1), Long);
	ASSERT_EQUALS(PyInt_AsLong(PyTuple_GET_ITEM(output, 0)), 1, "%d");
	ASSERT_EQUALS(PyInt_AsLong(PyTuple_GET_ITEM(output, 1)), 500000, "%d");

END_UNITTEST

#ifdef _C_BOOL

BEGIN_UNITTEST(TestSizeOfBool)

	/* Code in libffi_support.m depends on this equality. */
	ASSERT_EQUALS(sizeof(bool), sizeof(int), "%d");

END_UNITTEST

#endif

BEGIN_UNITTEST(TestTypeCode)
	
	/* These are manually defined on some platforms */
	ASSERT_EQUALS(@encode(long long)[0], 'q', "%c");
	ASSERT_EQUALS(@encode(unsigned long long)[0], 'Q', "%c");

#if defined(MACOSX) &&  MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_1
	ASSERT_EQUALS(@encode(bool)[0], 'B', "%#x");
#endif

END_UNITTEST

BEGIN_UNITTEST(TestSimplifySignature)
	/* Make sure PyObjCRT_SimplifySignature works */
	char b[1024];

	PyObjCRT_SimplifySignature("@1234@0:{_NSPoint=ff}02i22", b, sizeof(b));
	ASSERT_STREQUALS("@@:{_NSPoint=ff}i", b);

	PyObjCRT_SimplifySignature("@@:{_NSPoint=ff}i", b, sizeof(b));
	ASSERT_STREQUALS("@@:{_NSPoint=ff}i", b);
END_UNITTEST

BEGIN_UNITTEST(TestArrayCoding)
	/*
  	 * According to the docs on Panther, valueForKey: on an array should
	 * return [ o.valueForKey_(key) for o in anArray ]. 
	 * On MacOS X 10.2 it doesn't.
	 *
         * This test was added to make sure PyObjC is not at fault :-), the
	 * test is also used to avoid giving false positives in the unittests
 	 * for key-value coding.
         */

	NSMutableDictionary* d;
	NSMutableArray* a;
	NSObject* v;
	int haveException;

	NSAutoreleasePool* p;

	p = [[NSAutoreleasePool alloc] init];

	d = [NSMutableDictionary dictionary];

	[d setObject:@"foo" forKey:@"keyM"];

	a = [NSMutableArray arrayWithObjects: d, nil];

	NS_DURING
		v = [a valueForKey:@"keyM"];	
		haveException = 0;
	NS_HANDLER
		v = nil;
		haveException = 1;
	NS_ENDHANDLER

	[p release];

	ASSERT(!haveException);
END_UNITTEST

static PyMethodDef unittest_methods[] = {
	TESTDEF(CheckNSInvoke),

	TESTDEF(StructSize),	
	TESTDEF(StructAlign),	
	TESTDEF(FillStruct1),	
	TESTDEF(FillStruct2),	
	TESTDEF(FillStruct3),	
	TESTDEF(FillStruct4),	
	TESTDEF(ExtractStruct1),	
	TESTDEF(ExtractStruct2),	
	TESTDEF(ExtractStruct3),	
	TESTDEF(ExtractStruct4),	
#ifdef _C_BOOL
	TESTDEF(TestSizeOfBool),	
#endif
	TESTDEF(TestTypeCode),	
	TESTDEF(TestSimplifySignature),	
	TESTDEF(TestArrayCoding),
	{ 0, 0, 0, 0 }
};

void initctests(void);
void initctests(void)
{
	PyObject* m;

	m = Py_InitModule4("ctests", unittest_methods, NULL,
			                        NULL, PYTHON_API_VERSION);


	PyObjC_ImportAPI(m);
}