~ubuntu-branches/ubuntu/breezy/gnustep-base/breezy

« back to all changes in this revision

Viewing changes to Testing/nsmethodsignature.m

  • Committer: Bazaar Package Importer
  • Author(s): Eric Heintzmann
  • Date: 2005-04-17 00:14:38 UTC
  • mfrom: (1.2.1 upstream) (4 hoary)
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20050417001438-enf0y07c9tku85z1
Tags: 1.10.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** nsmethodsignature - Program to test NSMethodSignature.
 
2
   Copyright (C) 2004 Free Software Foundation, Inc.
 
3
 
 
4
   Written by:  David Ayers  <d.ayers@inode.at>
 
5
 
 
6
   This file is part of the GNUstep Base Library.
 
7
 
 
8
   This library is free software; you can redistribute it and/or
 
9
   modify it under the terms of the GNU Library General Public
 
10
   License as published by the Free Software Foundation; either
 
11
   version 2 of the License, or (at your option) any later version.
 
12
 
 
13
   This library is distributed in the hope that it will be useful,
 
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
   Library General Public License for more details.
 
17
 
 
18
   You should have received a copy of the GNU Library General Public
 
19
   License along with this library; if not, write to the Free
 
20
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
 
21
*/
 
22
 
 
23
#include <Foundation/NSAutoreleasePool.h>
 
24
#include <Foundation/NSMethodSignature.h>
 
25
#include <Foundation/NSRunLoop.h>
 
26
#include <Foundation/NSConnection.h>
 
27
#include <Foundation/NSException.h>
 
28
#include <Foundation/NSDebug.h>
 
29
 
 
30
#include <GNUstepBase/GSObjCRuntime.h>
 
31
 
 
32
 
 
33
#define SRV_NAME @"nsmethodsignaturetest"
 
34
 
 
35
struct _MyLargeStruct
 
36
{
 
37
  double first;
 
38
  double second;
 
39
};
 
40
typedef struct _MyLargeStruct MyLargeStruct;
 
41
 
 
42
struct _MySmallStruct
 
43
{
 
44
  char first;
 
45
};
 
46
typedef struct _MySmallStruct MySmallStruct;
 
47
 
 
48
/*------------------------------------*/
 
49
@interface MyClass : NSObject
 
50
-(void)void_void;
 
51
-(id)id_void;
 
52
 
 
53
-(char)char_void;
 
54
-(unsigned char)uchar_void;
 
55
-(signed char)schar_void;
 
56
 
 
57
-(short)short_void;
 
58
-(unsigned short)ushort_void;
 
59
-(signed short)sshort_void;
 
60
 
 
61
-(int)int_void;
 
62
-(unsigned int)uint_void;
 
63
-(signed int)sint_void;
 
64
 
 
65
-(long)long_void;
 
66
-(unsigned long)ulong_void;
 
67
-(signed long)slong_void;
 
68
 
 
69
-(float)float_void;
 
70
-(double)double_void;
 
71
 
 
72
-(MyLargeStruct)largeStruct_void;
 
73
-(MySmallStruct)smallStruct_void;
 
74
 
 
75
 
 
76
 
 
77
-(void)void_id:(id)_id;
 
78
 
 
79
-(void)void_char:(char)_char;
 
80
-(void)void_uchar:(unsigned char)_char;
 
81
-(void)void_schar:(signed char)_char;
 
82
 
 
83
-(void)void_short:(short)_short;
 
84
-(void)void_ushort:(unsigned short)_short;
 
85
-(void)void_sshort:(signed short)_short;
 
86
 
 
87
-(void)void_int:(int)_int;
 
88
-(void)void_uint:(unsigned int)_int;
 
89
-(void)void_sint:(signed int)_int;
 
90
 
 
91
-(void)void_long:(long)_long;
 
92
-(void)void_ulong:(unsigned long)_long;
 
93
-(void)void_slong:(signed long)_long;
 
94
 
 
95
-(void)void_float:(float)_float;
 
96
-(void)void_double:(double)_double;
 
97
 
 
98
-(void)void_largeStruct:(MyLargeStruct)_str;
 
99
-(void)void_smallStruct:(MySmallStruct)_str;
 
100
 
 
101
-(void)void_float:(float)_float double:(double)_double;
 
102
-(void)void_double:(double)_double float:(float)_float;
 
103
 
 
104
-(MyLargeStruct)largeStruct_id:(id)_id
 
105
                          char:(char)_char
 
106
                         short:(short)_short
 
107
                           int:(int)_int
 
108
                          long:(long)_long
 
109
                         float:(float)_float
 
110
                        double:(double)_double
 
111
                   largeStruct:(MyLargeStruct)_lstr
 
112
                   smallStruct:(MySmallStruct)_sstr;
 
113
-(MySmallStruct)smallStruct_id:(id)_id
 
114
                         uchar:(unsigned char)_uchar
 
115
                        ushort:(unsigned short)_ushort
 
116
                          uint:(unsigned int)_uint
 
117
                         ulong:(unsigned long)_ulong
 
118
                         float:(float)_float
 
119
                        double:(double)_double
 
120
                   largeStruct:(MyLargeStruct)_lstr
 
121
                   smallStruct:(MySmallStruct)_sstr;
 
122
 
 
123
-(const char *)runtimeSignatureForSelector:(SEL)selector;
 
124
-(const char *)mframeSignatureForSelector:(SEL)selector;
 
125
@end
 
126
 
 
127
@implementation MyClass
 
128
-(void)void_void {}
 
129
-(id)id_void { return 0; }
 
130
 
 
131
-(char)char_void { return 0; }
 
132
-(unsigned char)uchar_void { return 0; }
 
133
-(signed char)schar_void { return 0; }
 
134
 
 
135
-(short)short_void { return 0; }
 
136
-(unsigned short)ushort_void { return 0; }
 
137
-(signed short)sshort_void { return 0; }
 
138
 
 
139
-(int)int_void { return 0; }
 
140
-(unsigned int)uint_void { return 0; }
 
141
-(signed int)sint_void { return 0; }
 
142
 
 
143
-(long)long_void { return 0; }
 
144
-(unsigned long)ulong_void { return 0; }
 
145
-(signed long)slong_void { return 0; }
 
146
 
 
147
-(float)float_void { return 0; }
 
148
-(double)double_void { return 0; }
 
149
 
 
150
-(MyLargeStruct)largeStruct_void { MyLargeStruct str; return str; }
 
151
-(MySmallStruct)smallStruct_void { MySmallStruct str; return str; }
 
152
 
 
153
 
 
154
 
 
155
-(void)void_id:(id)_id {}
 
156
 
 
157
-(void)void_char:(char)_char {}
 
158
-(void)void_uchar:(unsigned char)_char {}
 
159
-(void)void_schar:(signed char)_char {}
 
160
 
 
161
-(void)void_short:(short)_short {}
 
162
-(void)void_ushort:(unsigned short)_short {}
 
163
-(void)void_sshort:(signed short)_short {}
 
164
 
 
165
-(void)void_int:(int)_int {}
 
166
-(void)void_uint:(unsigned int)_int {}
 
167
-(void)void_sint:(signed int)_int {}
 
168
 
 
169
-(void)void_long:(long)_long {}
 
170
-(void)void_ulong:(unsigned long)_long {}
 
171
-(void)void_slong:(signed long)_long {}
 
172
 
 
173
-(void)void_float:(float)_float {}
 
174
-(void)void_double:(double)_double {}
 
175
 
 
176
-(void)void_largeStruct:(MyLargeStruct)_str {}
 
177
-(void)void_smallStruct:(MySmallStruct)_str {}
 
178
 
 
179
-(void)void_float:(float)_float double:(double)_double {}
 
180
-(void)void_double:(double)_double float:(float)_float {}
 
181
 
 
182
 
 
183
-(MyLargeStruct)largeStruct_id:(id)_id
 
184
                          char:(char)_char
 
185
                         short:(short)_short
 
186
                           int:(int)_int
 
187
                          long:(long)_long
 
188
                         float:(float)_float
 
189
                        double:(double)_double
 
190
                   largeStruct:(MyLargeStruct)_lstr
 
191
                   smallStruct:(MySmallStruct)_sstr { return _lstr; }
 
192
 
 
193
-(MySmallStruct)smallStruct_id:(id)_id
 
194
                         uchar:(unsigned char)_uchar
 
195
                        ushort:(unsigned short)_ushort
 
196
                          uint:(unsigned int)_uint
 
197
                         ulong:(unsigned long)_ulong
 
198
                         float:(float)_float
 
199
                        double:(double)_double
 
200
                   largeStruct:(MyLargeStruct)_lstr
 
201
                   smallStruct:(MySmallStruct)_sstr { return _sstr; }
 
202
 
 
203
-(const char *)runtimeSignatureForSelector:(SEL)selector
 
204
{
 
205
  GSMethod meth = GSGetMethod(isa, selector, YES, YES);
 
206
  return meth->method_types;
 
207
}
 
208
 
 
209
-(const char *)mframeSignatureForSelector:(SEL)selector
 
210
{
 
211
  const char *types = [self runtimeSignatureForSelector: selector];
 
212
  NSMethodSignature *sig = [NSMethodSignature signatureWithObjCTypes: types];
 
213
  return [sig methodType];
 
214
}
 
215
@end
 
216
 
 
217
/*------------------------------------*/
 
218
 
 
219
int failed = 0;
 
220
 
 
221
void
 
222
test_mframe_build_signature(void)
 
223
{
 
224
  const char *mf_types;
 
225
  void *it = 0;
 
226
  GSMethod meth;
 
227
  GSMethodList list;
 
228
  Class cls = [MyClass class];
 
229
  NSMethodSignature *sig;
 
230
  unsigned int i;
 
231
 
 
232
  for (it = 0, list = class_nextMethodList(cls, &it);
 
233
       list != 0;
 
234
       list = class_nextMethodList(cls, &it))
 
235
    {
 
236
      id pool = [NSAutoreleasePool new];
 
237
 
 
238
      for (i = 0; i < list->method_count; i++)
 
239
        {
 
240
          meth = &list->method_list[i];
 
241
          sig = [NSMethodSignature signatureWithObjCTypes: meth->method_types];
 
242
          mf_types = [sig methodType];
 
243
          if (strcmp(meth->method_types, mf_types))
 
244
            {
 
245
              NSLog(@"sel: %s\nrts:%s\nmfs:%s",
 
246
                    GSNameFromSelector(meth->method_name),
 
247
                    meth->method_types, mf_types);
 
248
              failed = 1;
 
249
            }
 
250
        }
 
251
 
 
252
      [pool release];
 
253
    }
 
254
 
 
255
}
 
256
 
 
257
/*
 
258
   This test is useful if the nsmethodsignatureserver is running which
 
259
   was compiled with either a different GNUstep-base version or a different
 
260
   version of gcc.  It the server isn't found the test is skipped.
 
261
*/
 
262
void
 
263
test_compare_server_signature(void)
 
264
{
 
265
  id objct = [MyClass new];
 
266
  id proxy = [NSConnection rootProxyForConnectionWithRegisteredName: SRV_NAME
 
267
                           host: @"*"];
 
268
  if (proxy)
 
269
    {
 
270
      const char *rmtSig;
 
271
      const char *lclSig;
 
272
 
 
273
#define TEST_SEL(SELNAME) { \
 
274
      lclSig = [objct runtimeSignatureForSelector: @selector(SELNAME)]; \
 
275
      rmtSig = [proxy runtimeSignatureForSelector: @selector(SELNAME)]; \
 
276
      if (!GSSelectorTypesMatch(lclSig, rmtSig)) \
 
277
        NSLog(@"runtime: sel:%s\nlcl:%s\nrmt:%s", \
 
278
              GSNameFromSelector(@selector(SELNAME)), \
 
279
              lclSig, rmtSig); \
 
280
      lclSig = [objct mframeSignatureForSelector: @selector(SELNAME)]; \
 
281
      rmtSig = [proxy mframeSignatureForSelector: @selector(SELNAME)]; \
 
282
      if (!GSSelectorTypesMatch(lclSig, rmtSig)) \
 
283
        NSLog(@"mframe : sel:%s\nlcl:%s\nrmt:%s", \
 
284
              GSNameFromSelector(@selector(SELNAME)), \
 
285
              lclSig, rmtSig); \
 
286
      }
 
287
 
 
288
      TEST_SEL(void_void);
 
289
      TEST_SEL(id_void);
 
290
      TEST_SEL(char_void);
 
291
      TEST_SEL(uchar_void);
 
292
      TEST_SEL(schar_void);
 
293
      TEST_SEL(short_void);
 
294
      TEST_SEL(ushort_void);
 
295
      TEST_SEL(sshort_void);
 
296
      TEST_SEL(int_void);
 
297
      TEST_SEL(uint_void);
 
298
      TEST_SEL(sint_void);
 
299
      TEST_SEL(long_void);
 
300
      TEST_SEL(ulong_void);
 
301
      TEST_SEL(slong_void);
 
302
      TEST_SEL(float_void);
 
303
      TEST_SEL(double_void);
 
304
      TEST_SEL(largeStruct_void);
 
305
      TEST_SEL(smallStruct_void);
 
306
 
 
307
      TEST_SEL(void_id:);
 
308
      TEST_SEL(void_char:);
 
309
      TEST_SEL(void_uchar:);
 
310
      TEST_SEL(void_schar:);
 
311
      TEST_SEL(void_short:);
 
312
      TEST_SEL(void_ushort:);
 
313
      TEST_SEL(void_sshort:);
 
314
      TEST_SEL(void_int:);
 
315
      TEST_SEL(void_uint:);
 
316
      TEST_SEL(void_sint:);
 
317
      TEST_SEL(void_long:);
 
318
      TEST_SEL(void_ulong:);
 
319
      TEST_SEL(void_slong:);
 
320
      TEST_SEL(void_float:);
 
321
      TEST_SEL(void_double:);
 
322
      TEST_SEL(void_largeStruct:);
 
323
      TEST_SEL(void_smallStruct:);
 
324
      TEST_SEL(void_float:double:);
 
325
      TEST_SEL(void_double:float:);
 
326
      TEST_SEL(largeStruct_id:char:short:int:long:float:double:largeStruct:smallStruct:);
 
327
      TEST_SEL(smallStruct_id:uchar:ushort:uint:ulong:float:double:largeStruct:smallStruct:);
 
328
 
 
329
    }
 
330
  else
 
331
    {
 
332
      NSLog(@"Skipping test_compare_server_signature: proxy not found.");
 
333
    }
 
334
}
 
335
 
 
336
void
 
337
test_GSSelectorTypesMatch(void)
 
338
{
 
339
  const char *pairs[][2] = { {"@@::", "@12@0:4:8"},
 
340
                             {"@@::", "@12@+0:+4:+8"},
 
341
                             {"@@::", "@12@-0:-4:-8"},
 
342
                             {"@12@0:4:8", "@@::"},
 
343
                             {"@12@+0:+4:+8", "@@::"},
 
344
                             {"@12@-0:-4:-8", "@@::"},
 
345
 
 
346
                             {"@12@0:4:8", "@12@+0:+4:+8"},
 
347
                             {"@12@0:4:8", "@12@-0:-4:-8"},
 
348
                             {"@12@+0:+4:+8", "@12@0:4:8"},
 
349
                             {"@12@-0:-4:-8", "@12@0:4:8"},
 
350
 
 
351
                             {"@12@0:4:8", "@16@+4:+8:+12"},
 
352
                             {"@12@0:4:8", "@16@-4:-8:-12"},
 
353
                             {"@12@+0:+4:+8", "@16@4:8:12"},
 
354
                             {"@12@-0:-4:-8", "@16@4:8:12"},
 
355
 
 
356
                             {"{_MyLargeStruct2={_MyLargeStruct=dd}dd}@:",
 
357
                              "{??={??=dd}dd}16@0:4"},
 
358
 
 
359
                             {"{_MyLargeStruct=dd}56@+8:+12@+16c+23s+26i+28l24f28d32{_MyLargeStruct=dd}40{_MySmallStruct=c}44",
 
360
                              "{_MyLargeStruct=dd}46@+8:+12@+16c+17s+16i+20l+24f+28d24{_MyLargeStruct=dd}32{_MySmallStruct=c}45"},
 
361
                             {"{_MyLargeStruct=dd}56@+8:+12@+16c+23s+26i+28l24f28d32{_MyLargeStruct=dd}40{_MySmallStruct=c}44",
 
362
                              "{??=dd}46@+8:+12@+16c+17s+16i+20l+24f+28d24{??=dd}32{??=c}45"},
 
363
                             {0, 0} };
 
364
  unsigned int i = 0;
 
365
 
 
366
  while (pairs[i][0])
 
367
    {
 
368
      if (GSSelectorTypesMatch(pairs[i][0], pairs[i][1]) == NO)
 
369
        {
 
370
          NSLog(@"pair %d does not match:\n%s\n%s",
 
371
                i, pairs[i][0], pairs[i][1]);
 
372
          failed = 1;
 
373
        }
 
374
      i++;
 
375
    }
 
376
}
 
377
 
 
378
void
 
379
run_server(void)
 
380
{
 
381
  id obj = [MyClass new];
 
382
  NSConnection *conn = [NSConnection defaultConnection];
 
383
 
 
384
  [conn setRootObject: obj];
 
385
  if ([conn registerName: SRV_NAME] == NO)
 
386
    {
 
387
      NSLog(@"Failed to register name: " SRV_NAME );
 
388
      abort();
 
389
    }
 
390
  [[NSRunLoop currentRunLoop] run];
 
391
}
 
392
 
 
393
int
 
394
main(int argc, char *argv[])
 
395
{
 
396
  NSAutoreleasePool *pool;
 
397
  pool = [[NSAutoreleasePool alloc] init];
 
398
 
 
399
  if ([[[[NSProcessInfo processInfo] arguments] lastObject] isEqual: @"srv"])
 
400
    {
 
401
      run_server();
 
402
      abort();
 
403
    }
 
404
 
 
405
  NS_DURING
 
406
    {
 
407
      test_mframe_build_signature();
 
408
      test_compare_server_signature();
 
409
      test_GSSelectorTypesMatch();
 
410
      if (failed)
 
411
        [NSException raise: NSInternalInconsistencyException
 
412
                     format: @"discrepancies between gcc/mframe signatures"];
 
413
 
 
414
      NSLog(@"MethodSignature Test Succeeded.");
 
415
    }
 
416
  NS_HANDLER
 
417
    {
 
418
      NSLog(@"MethodSignature Test Failed:");
 
419
      NSLog(@"%@ %@ %@",
 
420
            [localException name],
 
421
            [localException reason],
 
422
            [localException userInfo]);
 
423
    }
 
424
  NS_ENDHANDLER
 
425
 
 
426
  [pool release];
 
427
 
 
428
  exit(0);
 
429
}
 
430