]> git.saurik.com Git - apple/objc4.git/blob - test/protocolSmall.m
objc4-818.2.tar.gz
[apple/objc4.git] / test / protocolSmall.m
1 // TEST_CFLAGS -framework Foundation
2 // need Foundation to get NSObject compatibility additions for class Protocol
3 // because ARC calls [protocol retain]
4 /*
5 TEST_BUILD_OUTPUT
6 .*protocolSmall.m:\d+:\d+: warning: cannot find protocol definition for 'SmallProto'
7 .*protocolSmall.m:\d+:\d+: note: protocol 'SmallProto' has no definition
8 END
9 */
10
11 #include "test.h"
12 #include "testroot.i"
13 #include <objc/runtime.h>
14
15 struct MethodListOneEntry {
16 uint32_t entSizeAndFlags;
17 uint32_t count;
18 SEL name;
19 const char *types;
20 void *imp;
21 };
22
23 struct SmallProtoStructure {
24 Class isa;
25 const char *mangledName;
26 struct protocol_list_t *protocols;
27 void *instanceMethods;
28 void *classMethods;
29 void *optionalInstanceMethods;
30 void *optionalClassMethods;
31 void *instanceProperties;
32 uint32_t size; // sizeof(protocol_t)
33 uint32_t flags;
34 };
35
36 struct MethodListOneEntry SmallProtoMethodList = {
37 .entSizeAndFlags = 3 * sizeof(void *),
38 .count = 1,
39 .name = NULL,
40 .types = "v@:",
41 .imp = NULL,
42 };
43
44 struct SmallProtoStructure SmallProtoData
45 __asm__("__OBJC_PROTOCOL_$_SmallProto")
46 = {
47 .mangledName = "SmallProto",
48 .instanceMethods = &SmallProtoMethodList,
49 .size = sizeof(struct SmallProtoStructure),
50 };
51
52 void *SmallProtoListEntry
53 __attribute__((section("__DATA,__objc_protolist,coalesced,no_dead_strip")))
54 = &SmallProtoData;
55
56 @protocol SmallProto;
57 @protocol NormalProto
58 - (void)protoMethod;
59 @end
60
61 @interface C: TestRoot <SmallProto, NormalProto> @end
62 @implementation C
63 - (void)protoMethod {}
64 @end
65
66 int main()
67 {
68 // Fix up the method list selector by hand, getting the compiler to generate a
69 // proper selref as a compile-time constant is a pain.
70 SmallProtoMethodList.name = @selector(protoMethod);
71 unsigned protoCount;
72
73 Protocol * __unsafe_unretained *protos = class_copyProtocolList([C class], &protoCount);
74 for (unsigned i = 0; i < protoCount; i++) {
75 testprintf("Checking index %u protocol %p\n", i, protos[i]);
76 const char *name = protocol_getName(protos[i]);
77 testprintf("Name is %s\n", name);
78 testassert(strcmp(name, "SmallProto") == 0 || strcmp(name, "NormalProto") == 0);
79
80 objc_property_t *classProperties = protocol_copyPropertyList2(protos[i], NULL, YES, NO);
81 testassert(classProperties == NULL);
82
83 struct objc_method_description desc = protocol_getMethodDescription(protos[i], @selector(protoMethod), YES, YES);
84 testprintf("Protocol protoMethod name is %s types are %s\n", desc.name, desc.types);
85 testassert(desc.name == @selector(protoMethod));
86 testassert(desc.types[0] == 'v');
87 }
88 free(protos);
89
90 succeed(__FILE__);
91 }