1 /* Method encoding tests for stand-alone @protocol declarations. */
2 /* Contributed by Ziemowit Laski <zlaski@apple.com>. */
5 #include <objc/Protocol.h>
9 #define ProtoBool _Bool
12 #ifndef __NEXT_RUNTIME__
13 #include <objc/objc-api.h>
16 extern int sscanf(const char *str, const char *format, ...);
17 extern void abort(void);
18 #define CHECK_IF(expr) if(!(expr)) abort()
23 typedef enum Enum Enum;
24 typedef signed char ObjCBool; /* as used by the NeXT runtime */
27 union __XXAngle { unsigned int alpha, beta; };
28 typedef struct { float x, y; union __XXAngle a; } XXPoint;
29 typedef struct { double width, height; } XXSize;
30 typedef struct _XXRect { XXPoint origin; XXSize size; struct _XXRect *next; } XXRect;
31 - (void) char:(signed char)c float:(float)f double:(double)d unsigned:(unsigned)u short:(short)s long:(long)l;
32 - (void *)setRect:(XXRect)r withBool:(ProtoBool)b withInt:(int)i;
33 + (Enum *)getEnum:(XXPoint *)pt enum:(enum Enum)e bool:(ObjCBool)b;
34 + (ProtoBool **)getBool:(ObjCBool **)b;
37 Protocol *proto = @protocol(Proto);
38 struct objc_method_description *meth;
39 unsigned totsize, offs0, offs1, offs2, offs3, offs4, offs5, offs6, offs7;
41 static void scan_initial(const char *pattern) {
42 totsize = offs0 = offs1 = offs2 = offs3 = offs4 = offs5 = offs6 = offs7 = (unsigned)-1;
43 sscanf(meth->types, pattern, &totsize, &offs0, &offs1, &offs2, &offs3,
44 &offs4, &offs5, &offs6, &offs7);
45 CHECK_IF(!offs0 && offs1 == sizeof(id) && offs2 == offs1 + sizeof(SEL) && totsize >= offs2);
51 meth = [proto descriptionForInstanceMethod: @selector(char:float:double:unsigned:short:long:)];
52 if (sizeof (long) == 8)
53 string = "v%u@%u:%uc%uf%ud%uI%us%uq%u";
55 string = "v%u@%u:%uc%uf%ud%uI%us%ul%u";
57 CHECK_IF(offs3 == offs2 + sizeof(int) && offs4 == offs3 + sizeof(float));
58 CHECK_IF(offs5 == offs4 + sizeof(double) && offs6 == offs5 + sizeof(unsigned));
59 CHECK_IF(offs7 == offs6 + sizeof(int) && totsize == offs7 + sizeof(long));
60 meth = [proto descriptionForInstanceMethod: @selector(setRect:withBool:withInt:)];
61 scan_initial("^v%u@%u:%u{_XXRect={?=ff(__XXAngle=II)}{?=dd}^{_XXRect}}%uB%ui%u");
62 CHECK_IF(offs3 == offs2 + sizeof(XXRect) && offs4 == offs3 + sizeof(int));
63 CHECK_IF(totsize == offs4 + sizeof(int));
64 meth = [proto descriptionForClassMethod: @selector(getEnum:enum:bool:)];
65 scan_initial("^i%u@%u:%u^{?=ff(__XXAngle=II)}%ui%uc%u");
66 CHECK_IF(offs3 == offs2 + sizeof(XXPoint *) && offs4 == offs3 + sizeof(enum Enum));
67 CHECK_IF(totsize == offs4 + sizeof(int)); /* 'ObjCBool' is really 'char' */
68 meth = [proto descriptionForClassMethod: @selector(getBool:)];
69 scan_initial("^^B%u@%u:%u^*%u");
70 CHECK_IF(totsize == offs2 + sizeof(ObjCBool **));