2 TEST_CONFIG MEM=arc CC=clang
4 $C{COMPILE_NOLINK_NOMEM} -c $DIR/MRRBase.m
5 $C{COMPILE_NOLINK_NOMEM} -c $DIR/MRRARR.m
6 $C{COMPILE_NOLINK} -c $DIR/ARRBase.m
7 $C{COMPILE_NOLINK} -c $DIR/ARRMRR.m
8 $C{COMPILE} -fobjc-arc $DIR/ARRLayouts.m -x none MRRBase.o MRRARR.o ARRBase.o ARRMRR.o -framework Foundation -o ARRLayouts.out
14 #import <Foundation/Foundation.h>
15 #import <objc/runtime.h>
20 @interface NSObject (Layouts)
21 + (const char *)strongLayout;
22 + (const char *)weakLayout;
25 void printlayout(const char *name, const uint8_t *layout)
27 if (! getenv("VERBOSE")) return;
29 testprintf("%s: ", name);
32 fprintf(stderr, "NULL\n");
37 for (c = layout; *c; c++) {
38 fprintf(stderr, "%02x ", *c);
41 fprintf(stderr, "00\n");
44 @implementation NSObject (Layouts)
46 + (const char *)strongLayout {
47 const uint8_t *layout = class_getIvarLayout(self);
48 printlayout("strong", layout);
49 return (const char *)layout;
52 + (const char *)weakLayout {
53 const uint8_t *weakLayout = class_getWeakIvarLayout(self);
54 printlayout("weak", weakLayout);
55 return (const char *)weakLayout;
58 + (Ivar)instanceVariable:(const char *)name {
59 return class_getInstanceVariable(self, name);
64 int main (int argc __unused, const char * argv[] __unused) {
65 // Under ARR, layout strings are relative to the class' own ivars.
66 testassert(strcmp([ARRBase strongLayout], "\x11\x20") == 0);
67 testassert(strcmp([ARRBase weakLayout], "\x31") == 0);
68 testassert([MRRBase strongLayout] == NULL);
69 testassert([MRRBase weakLayout] == NULL);
70 testassert(strcmp([ARRMRR strongLayout], "\x01") == 0);
71 testassert([ARRMRR weakLayout] == NULL);
72 testassert([MRRARR strongLayout] == NULL);
73 testassert([MRRARR weakLayout] == NULL);
75 // now check consistency between dynamic accessors and KVC, etc.
76 ARRMRR *am = [ARRMRR new];
77 MRRARR *ma = [MRRARR new];
79 NSString *am_description = [[NSString alloc] initWithFormat:@"%s %p", "ARRMRR", am];
80 NSString *ma_description = [[NSString alloc] initWithFormat:@"%s %p", "MRRARR", ma];
83 object_setIvar(am, [ARRMRR instanceVariable:"object"], am_description);
84 testassert(CFGetRetainCount(objc_unretainedPointer(am_description)) == 1);
85 am.pointer = @selector(ARRMRR);
86 object_setIvar(am, [ARRMRR instanceVariable:"delegate"], ma);
87 testassert(CFGetRetainCount(objc_unretainedPointer(ma)) == 1);
90 object_setIvar(ma, [MRRARR instanceVariable:"object"], ma_description);
91 testassert(CFGetRetainCount(objc_unretainedPointer(ma_description)) == 2);
92 ma.pointer = @selector(MRRARR);
94 object_setIvar(ma, [MRRARR instanceVariable:"delegate"], am);
95 testassert(CFGetRetainCount(objc_unretainedPointer(am)) == 1);