]> git.saurik.com Git - apple/objc4.git/blob - test/propertyDesc.m
objc4-781.2.tar.gz
[apple/objc4.git] / test / propertyDesc.m
1 /*
2 TEST_BUILD_OUTPUT
3 .*propertyDesc.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
4 .*propertyDesc.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
5 .*propertyDesc.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
6 .*propertyDesc.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
7 .*propertyDesc.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
8 .*propertyDesc.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
9 .*propertyDesc.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
10 .*propertyDesc.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
11 END
12 */
13
14 #include "test.h"
15 #include "testroot.i"
16 #include <stdint.h>
17 #include <string.h>
18 #include <objc/runtime.h>
19
20 struct objc_property {
21 const char *name;
22 const char *attr;
23 };
24
25 #define checkattrlist(attrs, attrcount, target) \
26 do { \
27 if (target > 0) { \
28 testassert(attrs); \
29 testassert(attrcount == target); \
30 testassert(malloc_size(attrs) >= \
31 (1+target) * sizeof(objc_property_attribute_t)); \
32 testassert(attrs[target].name == NULL); \
33 testassert(attrs[target].value == NULL); \
34 } else { \
35 testassert(!attrs); \
36 testassert(attrcount == 0); \
37 } \
38 } while (0)
39
40 #define checkattr(attrs, i, n, v) \
41 do { \
42 char *attrsstart = (char *)attrs; \
43 char *attrsend = (char *)attrs + malloc_size(attrs); \
44 testassert((char*)(attrs+i+1) <= attrsend); \
45 testassert(attrs[i].name >= attrsstart); \
46 testassert(attrs[i].value >= attrsstart); \
47 testassert(attrs[i].name + strlen(attrs[i].name) + 1 <= attrsend); \
48 testassert(attrs[i].value + strlen(attrs[i].value) + 1 <= attrsend); \
49 if (n) testassert(0 == strcmp(attrs[i].name, n)); \
50 else testassert(attrs[i].name == NULL); \
51 if (v) testassert(0 == strcmp(attrs[i].value, v)); \
52 else testassert(attrs[i].value == NULL); \
53 } while (0)
54
55 int main()
56 {
57 char *value;
58 objc_property_attribute_t *attrs;
59 unsigned int attrcount;
60
61 // STRING TO ATTRIBUTE LIST (property_copyAttributeList)
62
63 struct objc_property prop;
64 prop.name = "test";
65
66 // null property
67 attrcount = 42;
68 attrs = property_copyAttributeList(NULL, &attrcount);
69 testassert(!attrs);
70 testassert(attrcount == 0);
71 attrs = property_copyAttributeList(NULL, NULL);
72 testassert(!attrs);
73
74 // null attributes
75 attrcount = 42;
76 prop.attr = NULL;
77 attrs = property_copyAttributeList(&prop, &attrcount);
78 checkattrlist(attrs, attrcount, 0);
79 attrs = property_copyAttributeList(&prop, NULL);
80 testassert(!attrs);
81
82 // empty attributes
83 attrcount = 42;
84 prop.attr = "";
85 attrs = property_copyAttributeList(&prop, &attrcount);
86 checkattrlist(attrs, attrcount, 0);
87 attrs = property_copyAttributeList(&prop, NULL);
88 testassert(!attrs);
89
90 // commas only
91 attrcount = 42;
92 prop.attr = ",,,";
93 attrs = property_copyAttributeList(&prop, &attrcount);
94 checkattrlist(attrs, attrcount, 0);
95 attrs = property_copyAttributeList(&prop, NULL);
96 testassert(!attrs);
97
98 // long and short names, with and without values
99 attrcount = 42;
100 prop.attr = "?XX,',\"?!?!\"YY,\"''''\"";
101 attrs = property_copyAttributeList(&prop, &attrcount);
102 checkattrlist(attrs, attrcount, 4);
103 checkattr(attrs, 0, "?", "XX");
104 checkattr(attrs, 1, "'", "");
105 checkattr(attrs, 2, "?!?!", "YY");
106 checkattr(attrs, 3, "''''", "");
107 free(attrs);
108
109 // all recognized attributes simultaneously, values with quotes
110 attrcount = 42;
111 prop.attr = "T11,V2222,S333333\",G\"44444444,W,P,D,R,N,C,&";
112 attrs = property_copyAttributeList(&prop, &attrcount);
113 checkattrlist(attrs, attrcount, 11);
114 checkattr(attrs, 0, "T", "11");
115 checkattr(attrs, 1, "V", "2222");
116 checkattr(attrs, 2, "S", "333333\"");
117 checkattr(attrs, 3, "G", "\"44444444");
118 checkattr(attrs, 4, "W", "");
119 checkattr(attrs, 5, "P", "");
120 checkattr(attrs, 6, "D", "");
121 checkattr(attrs, 7, "R", "");
122 checkattr(attrs, 8, "N", "");
123 checkattr(attrs, 9, "C", "");
124 checkattr(attrs,10, "&", "");
125 free(attrs);
126
127 // kitchen sink
128 attrcount = 42;
129 prop.attr = "W,T11,P,?XX,D,V2222,R,',N,S333333\",C,\"?!?!\"YY,&,G\"44444444,\"''''\"";
130 attrs = property_copyAttributeList(&prop, &attrcount);
131 checkattrlist(attrs, attrcount, 15);
132 checkattr(attrs, 0, "W", "");
133 checkattr(attrs, 1, "T", "11");
134 checkattr(attrs, 2, "P", "");
135 checkattr(attrs, 3, "?", "XX");
136 checkattr(attrs, 4, "D", "");
137 checkattr(attrs, 5, "V", "2222");
138 checkattr(attrs, 6, "R", "");
139 checkattr(attrs, 7, "'", "");
140 checkattr(attrs, 8, "N", "");
141 checkattr(attrs, 9, "S", "333333\"");
142 checkattr(attrs,10, "C", "");
143 checkattr(attrs,11, "?!?!", "YY");
144 checkattr(attrs,12, "&", "");
145 checkattr(attrs,13, "G", "\"44444444");
146 checkattr(attrs,14, "''''", "");
147 free(attrs);
148
149 // SEARCH ATTRIBUTE LIST (property_copyAttributeValue)
150
151 // null property, null name, empty name
152 value = property_copyAttributeValue(NULL, NULL);
153 testassert(!value);
154 value = property_copyAttributeValue(NULL, "foo");
155 testassert(!value);
156 value = property_copyAttributeValue(NULL, "");
157 testassert(!value);
158 value = property_copyAttributeValue(&prop, NULL);
159 testassert(!value);
160 value = property_copyAttributeValue(&prop, "");
161 testassert(!value);
162
163 // null attributes, empty attributes
164 prop.attr = NULL;
165 value = property_copyAttributeValue(&prop, "foo");
166 testassert(!value);
167 prop.attr = "";
168 value = property_copyAttributeValue(&prop, "foo");
169 testassert(!value);
170
171 // long and short names, with and without values
172 prop.attr = "?XX,',\"?!?!\"YY,\"''''\"";
173 value = property_copyAttributeValue(&prop, "missing");
174 testassert(!value);
175 value = property_copyAttributeValue(&prop, "X");
176 testassert(!value);
177 value = property_copyAttributeValue(&prop, "\"");
178 testassert(!value);
179 value = property_copyAttributeValue(&prop, "'''");
180 testassert(!value);
181 value = property_copyAttributeValue(&prop, "'''''");
182 testassert(!value);
183
184 value = property_copyAttributeValue(&prop, "?");
185 testassert(0 == strcmp(value, "XX"));
186 testassert(malloc_size(value) >= 1 + strlen(value));
187 free(value);
188 value = property_copyAttributeValue(&prop, "'");
189 testassert(0 == strcmp(value, ""));
190 testassert(malloc_size(value) >= 1 + strlen(value));
191 free(value);
192 value = property_copyAttributeValue(&prop, "?!?!");
193 testassert(0 == strcmp(value, "YY"));
194 testassert(malloc_size(value) >= 1 + strlen(value));
195 free(value);
196 value = property_copyAttributeValue(&prop, "''''");
197 testassert(0 == strcmp(value, ""));
198 testassert(malloc_size(value) >= 1 + strlen(value));
199 free(value);
200
201 // all recognized attributes simultaneously, values with quotes
202 prop.attr = "T11,V2222,S333333\",G\"44444444,W,P,D,R,N,C,&";
203 value = property_copyAttributeValue(&prop, "T");
204 testassert(0 == strcmp(value, "11"));
205 testassert(malloc_size(value) >= 1 + strlen(value));
206 free(value);
207 value = property_copyAttributeValue(&prop, "V");
208 testassert(0 == strcmp(value, "2222"));
209 testassert(malloc_size(value) >= 1 + strlen(value));
210 free(value);
211 value = property_copyAttributeValue(&prop, "S");
212 testassert(0 == strcmp(value, "333333\""));
213 testassert(malloc_size(value) >= 1 + strlen(value));
214 free(value);
215 value = property_copyAttributeValue(&prop, "G");
216 testassert(0 == strcmp(value, "\"44444444"));
217 testassert(malloc_size(value) >= 1 + strlen(value));
218 free(value);
219 value = property_copyAttributeValue(&prop, "W");
220 testassert(0 == strcmp(value, ""));
221 testassert(malloc_size(value) >= 1 + strlen(value));
222 free(value);
223 value = property_copyAttributeValue(&prop, "P");
224 testassert(0 == strcmp(value, ""));
225 testassert(malloc_size(value) >= 1 + strlen(value));
226 free(value);
227 value = property_copyAttributeValue(&prop, "D");
228 testassert(0 == strcmp(value, ""));
229 testassert(malloc_size(value) >= 1 + strlen(value));
230 free(value);
231 value = property_copyAttributeValue(&prop, "R");
232 testassert(0 == strcmp(value, ""));
233 testassert(malloc_size(value) >= 1 + strlen(value));
234 free(value);
235 value = property_copyAttributeValue(&prop, "N");
236 testassert(0 == strcmp(value, ""));
237 testassert(malloc_size(value) >= 1 + strlen(value));
238 free(value);
239 value = property_copyAttributeValue(&prop, "C");
240 testassert(0 == strcmp(value, ""));
241 testassert(malloc_size(value) >= 1 + strlen(value));
242 free(value);
243 value = property_copyAttributeValue(&prop, "&");
244 testassert(0 == strcmp(value, ""));
245 testassert(malloc_size(value) >= 1 + strlen(value));
246 free(value);
247
248 // ATTRIBUTE LIST TO STRING (class_addProperty)
249
250 BOOL ok;
251 objc_property_t prop2;
252
253 // null name
254 ok = class_addProperty([TestRoot class], NULL, (objc_property_attribute_t *)1, 1);
255 testassert(!ok);
256
257 // null description
258 ok = class_addProperty([TestRoot class], "test-null-desc", NULL, 0);
259 testassert(ok);
260 prop2 = class_getProperty([TestRoot class], "test-null-desc");
261 testassert(prop2);
262 testassert(0 == strcmp(property_getAttributes(prop2), ""));
263
264 // empty description
265 ok = class_addProperty([TestRoot class], "test-empty-desc", (objc_property_attribute_t*)1, 0);
266 testassert(ok);
267 prop2 = class_getProperty([TestRoot class], "test-empty-desc");
268 testassert(prop2);
269 testassert(0 == strcmp(property_getAttributes(prop2), ""));
270
271 // long and short names, with and without values
272 objc_property_attribute_t attrs2[] = {
273 { "!", NULL },
274 { "?", "XX" },
275 { "'", "" },
276 { "?!?!", "YY" },
277 { "''''", "" }
278 };
279 ok = class_addProperty([TestRoot class], "test-unrecognized", attrs2, 5);
280 testassert(ok);
281 prop2 = class_getProperty([TestRoot class], "test-unrecognized");
282 testassert(prop2);
283 testassert(0 == strcmp(property_getAttributes(prop2), "?XX,',\"?!?!\"YY,\"''''\""));
284
285 // all recognized attributes simultaneously, values with quotes
286 objc_property_attribute_t attrs3[] = {
287 { "&", "" },
288 { "C", "" },
289 { "N", "" },
290 { "R", "" },
291 { "D", "" },
292 { "P", "" },
293 { "W", "" },
294 { "G", "\"44444444" },
295 { "S", "333333\"" },
296 { "V", "2222" },
297 { "T", "11" },
298 };
299 ok = class_addProperty([TestRoot class], "test-recognized", attrs3, 11);
300 testassert(ok);
301 prop2 = class_getProperty([TestRoot class], "test-recognized");
302 testassert(prop2);
303 testassert(0 == strcmp(property_getAttributes(prop2),
304 "&,C,N,R,D,P,W,G\"44444444,S333333\",V2222,T11"));
305
306 // kitchen sink
307 objc_property_attribute_t attrs4[] = {
308 { "&", "" },
309 { "C", "" },
310 { "N", "" },
311 { "R", "" },
312 { "D", "" },
313 { "P", "" },
314 { "W", "" },
315 { "!", NULL },
316 { "G", "\"44444444" },
317 { "S", "333333\"" },
318 { "V", "2222" },
319 { "T", "11" },
320 { "?", "XX" },
321 { "'", "" },
322 { "?!?!", "YY" },
323 { "''''", "" }
324 };
325 ok = class_addProperty([TestRoot class], "test-sink", attrs4, 16);
326 testassert(ok);
327 prop2 = class_getProperty([TestRoot class], "test-sink");
328 testassert(prop2);
329 testassert(0 == strcmp(property_getAttributes(prop2),
330 "&,C,N,R,D,P,W,G\"44444444,S333333\",V2222,T11,"
331 "?XX,',\"?!?!\"YY,\"''''\""));
332
333 succeed(__FILE__);
334 }