]> git.saurik.com Git - apple/objc4.git/blob - test/taggedPointersAllClasses.m
objc4-756.2.tar.gz
[apple/objc4.git] / test / taggedPointersAllClasses.m
1 // TEST_CONFIG
2
3 #include "test.h"
4 #include "testroot.i"
5 #include <objc/runtime.h>
6 #include <objc/objc-internal.h>
7
8 #if OBJC_HAVE_TAGGED_POINTERS
9
10 @interface TagSuperclass: TestRoot
11
12 - (void)test;
13
14 @end
15
16 @implementation TagSuperclass
17
18 - (void)test {}
19
20 @end
21
22 int main()
23 {
24 Class classes[OBJC_TAG_Last52BitPayload + 1] = {};
25
26 __block uintptr_t expectedPayload;
27 __block uintptr_t sawPayload;
28 __block int sawTag;
29
30 for (int i = 0; i <= OBJC_TAG_Last52BitPayload; i++) {
31 objc_tag_index_t tag = (objc_tag_index_t)i;
32 if (i > OBJC_TAG_Last60BitPayload && i < OBJC_TAG_First52BitPayload)
33 continue;
34 if (_objc_getClassForTag(tag) != nil)
35 continue;
36
37 char *name;
38 asprintf(&name, "Tag%d", i);
39 classes[i] = objc_allocateClassPair([TagSuperclass class], name, 0);
40 free(name);
41
42 IMP testIMP = imp_implementationWithBlock(^(void *self) {
43 testassert(i == _objc_getTaggedPointerTag(self));
44 testassert(expectedPayload == _objc_getTaggedPointerValue(self));
45 sawPayload = _objc_getTaggedPointerValue(self);
46 sawTag = i;
47 });
48 class_addMethod(classes[i], @selector(test), testIMP, "v@@");
49
50 objc_registerClassPair(classes[i]);
51 _objc_registerTaggedPointerClass(tag, classes[i]);
52 }
53
54 for (int i = 0; i <= OBJC_TAG_Last52BitPayload; i++) {
55 objc_tag_index_t tag = (objc_tag_index_t)i;
56 if (classes[i] == nil)
57 continue;
58
59 for (int byte = 0; byte <= 0xff; byte++) {
60 uintptr_t payload;
61 memset(&payload, byte, sizeof(payload));
62
63 if (i <= OBJC_TAG_Last60BitPayload)
64 payload >>= _OBJC_TAG_PAYLOAD_RSHIFT;
65 else
66 payload >>= _OBJC_TAG_EXT_PAYLOAD_RSHIFT;
67
68 expectedPayload = payload;
69 id obj = (__bridge id)_objc_makeTaggedPointer(tag, payload);
70 [obj test];
71 testassert(sawPayload == payload);
72 testassert(sawTag == i);
73 }
74 }
75
76 succeed(__FILE__);
77 }
78
79 #else
80
81 int main()
82 {
83 succeed(__FILE__);
84 }
85
86 #endif