]> git.saurik.com Git - apple/objc4.git/blob - test/methodArgs.m
objc4-437.tar.gz
[apple/objc4.git] / test / methodArgs.m
1 #include "test.h"
2 #include <string.h>
3 #include <objc/objc-runtime.h>
4 #include <objc/Protocol.h>
5
6 @interface Super { id isa; } @end
7 @implementation Super
8 +(void)initialize { }
9 +class { return self; }
10 +(id)method:(int)arg { return (id)(intptr_t)arg; }
11 @end
12
13
14 int main()
15 {
16 char buf[128];
17 char *arg;
18 struct objc_method_description *desc;
19 Method m = class_getClassMethod([Super class], sel_registerName("method:"));
20 testassert(m);
21
22 testassert(method_getNumberOfArguments(m) == 3);
23 #if !__OBJC2__
24 testassert(method_getSizeOfArguments(m) == 12);
25 #endif
26
27 arg = method_copyArgumentType(m, 0);
28 testassert(arg);
29 testassert(0 == strcmp(arg, "@"));
30 memset(buf, 1, 128);
31 method_getArgumentType(m, 0, buf, 1+strlen(arg));
32 testassert(0 == strcmp(arg, buf));
33 testassert(buf[1+strlen(arg)] == 1);
34 memset(buf, 1, 128);
35 method_getArgumentType(m, 0, buf, 2);
36 testassert(0 == strncmp(arg, buf, 2));
37 testassert(buf[2] == 1);
38 free(arg);
39
40 arg = method_copyArgumentType(m, 1);
41 testassert(arg);
42 testassert(0 == strcmp(arg, ":"));
43 memset(buf, 1, 128);
44 method_getArgumentType(m, 1, buf, 1+strlen(arg));
45 testassert(0 == strcmp(arg, buf));
46 testassert(buf[1+strlen(arg)] == 1);
47 memset(buf, 1, 128);
48 method_getArgumentType(m, 1, buf, 2);
49 testassert(0 == strncmp(arg, buf, 2));
50 testassert(buf[2] == 1);
51 free(arg);
52
53 arg = method_copyArgumentType(m, 2);
54 testassert(arg);
55 testassert(0 == strcmp(arg, "i"));
56 memset(buf, 1, 128);
57 method_getArgumentType(m, 2, buf, 1+strlen(arg));
58 testassert(0 == strcmp(arg, buf));
59 testassert(buf[1+strlen(arg)] == 1);
60 memset(buf, 1, 128);
61 method_getArgumentType(m, 2, buf, 2);
62 testassert(0 == strncmp(arg, buf, 2));
63 testassert(buf[2] == 1);
64 free(arg);
65
66 arg = method_copyArgumentType(m, 3);
67 testassert(!arg);
68
69 arg = method_copyArgumentType(m, -1);
70 testassert(!arg);
71
72 memset(buf, 1, 128);
73 method_getArgumentType(m, 3, buf, 127);
74 testassert(buf[0] == 0);
75 testassert(buf[1] == 0);
76 testassert(buf[127] == 1);
77
78 memset(buf, 1, 128);
79 method_getArgumentType(m, -1, buf, 127);
80 testassert(buf[0] == 0);
81 testassert(buf[1] == 0);
82 testassert(buf[127] == 1);
83
84 arg = method_copyReturnType(m);
85 testassert(arg);
86 testassert(0 == strcmp(arg, "@"));
87 memset(buf, 1, 128);
88 method_getReturnType(m, buf, 1+strlen(arg));
89 testassert(0 == strcmp(arg, buf));
90 testassert(buf[1+strlen(arg)] == 1);
91 memset(buf, 1, 128);
92 method_getReturnType(m, buf, 2);
93 testassert(0 == strncmp(arg, buf, 2));
94 testassert(buf[2] == 1);
95 free(arg);
96
97 desc = method_getDescription(m);
98 testassert(desc);
99 testassert(desc->name == sel_registerName("method:"));
100 #if __LP64__
101 testassert(0 == strcmp(desc->types, "@20@0:8i16"));
102 #else
103 testassert(0 == strcmp(desc->types, "@12@0:4i8"));
104 #endif
105
106 testassert(0 == method_getNumberOfArguments(NULL));
107 #if !__OBJC2__
108 testassert(0 == method_getSizeOfArguments(NULL));
109 #endif
110 testassert(NULL == method_copyArgumentType(NULL, 10));
111 testassert(NULL == method_copyReturnType(NULL));
112 testassert(NULL == method_getDescription(NULL));
113
114 memset(buf, 1, 128);
115 method_getArgumentType(NULL, 1, buf, 127);
116 testassert(buf[0] == 0);
117 testassert(buf[1] == 0);
118 testassert(buf[127] == 1);
119
120 memset(buf, 1, 128);
121 method_getArgumentType(NULL, 1, buf, 0);
122 testassert(buf[0] == 1);
123 testassert(buf[1] == 1);
124
125 method_getArgumentType(m, 1, NULL, 128);
126 method_getArgumentType(m, 1, NULL, 0);
127 method_getArgumentType(NULL, 1, NULL, 128);
128 method_getArgumentType(NULL, 1, NULL, 0);
129
130 memset(buf, 1, 128);
131 method_getReturnType(NULL, buf, 127);
132 testassert(buf[0] == 0);
133 testassert(buf[1] == 0);
134 testassert(buf[127] == 1);
135
136 memset(buf, 1, 128);
137 method_getReturnType(NULL, buf, 0);
138 testassert(buf[0] == 1);
139 testassert(buf[1] == 1);
140
141 method_getReturnType(m, NULL, 128);
142 method_getReturnType(m, NULL, 0);
143 method_getReturnType(NULL, NULL, 128);
144 method_getReturnType(NULL, NULL, 0);
145
146 succeed(__FILE__);
147 }