]> git.saurik.com Git - apple/objc4.git/blob - test/ARRLayouts.m
objc4-680.tar.gz
[apple/objc4.git] / test / ARRLayouts.m
1 /*
2 TEST_CONFIG MEM=arc CC=clang
3 TEST_BUILD
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
9 END
10 */
11
12 #include "test.h"
13 #import <stdio.h>
14 #import <Foundation/Foundation.h>
15 #import <objc/runtime.h>
16
17 #import "ARRMRR.h"
18 #import "MRRARR.h"
19
20 @interface NSObject (Layouts)
21 + (const char *)strongLayout;
22 + (const char *)weakLayout;
23 @end
24
25 void printlayout(const char *name, const uint8_t *layout)
26 {
27 if (! getenv("VERBOSE")) return;
28
29 testprintf("%s: ", name);
30
31 if (!layout) {
32 fprintf(stderr, "NULL\n");
33 return;
34 }
35
36 const uint8_t *c;
37 for (c = layout; *c; c++) {
38 fprintf(stderr, "%02x ", *c);
39 }
40
41 fprintf(stderr, "00\n");
42 }
43
44 @implementation NSObject (Layouts)
45
46 + (const char *)strongLayout {
47 const uint8_t *layout = class_getIvarLayout(self);
48 printlayout("strong", layout);
49 return (const char *)layout;
50 }
51
52 + (const char *)weakLayout {
53 const uint8_t *weakLayout = class_getWeakIvarLayout(self);
54 printlayout("weak", weakLayout);
55 return (const char *)weakLayout;
56 }
57
58 + (Ivar)instanceVariable:(const char *)name {
59 return class_getInstanceVariable(self, name);
60 }
61
62 @end
63
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);
74
75 // now check consistency between dynamic accessors and KVC, etc.
76 ARRMRR *am = [ARRMRR new];
77 MRRARR *ma = [MRRARR new];
78
79 NSString *am_description = [[NSString alloc] initWithFormat:@"%s %p", "ARRMRR", am];
80 NSString *ma_description = [[NSString alloc] initWithFormat:@"%s %p", "MRRARR", ma];
81
82 am.number = M_PI;
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);
88
89 ma.number = M_E;
90 object_setIvar(ma, [MRRARR instanceVariable:"object"], ma_description);
91 testassert(CFGetRetainCount(objc_unretainedPointer(ma_description)) == 2);
92 ma.pointer = @selector(MRRARR);
93 ma.delegate = am;
94 object_setIvar(ma, [MRRARR instanceVariable:"delegate"], am);
95 testassert(CFGetRetainCount(objc_unretainedPointer(am)) == 1);
96
97 succeed(__FILE__);
98 return 0;
99 }