]> git.saurik.com Git - apple/objc4.git/blob - test/copyProtocolList.m
objc4-781.tar.gz
[apple/objc4.git] / test / copyProtocolList.m
1 // TEST_CONFIG
2
3 #include "test.h"
4 #include <string.h>
5 #include <malloc/malloc.h>
6 #include <objc/objc-runtime.h>
7
8 @protocol Proto1
9 +(id)proto1ClassMethod;
10 -(id)proto1InstanceMethod;
11 @end
12
13 void noNullEntries(Protocol * _Nonnull __unsafe_unretained * _Nullable protolist,
14 unsigned int count)
15 {
16 for (unsigned int i = 0; i != count; ++i) {
17 testassert(protolist[i]);
18 testassert(protocol_getName(protolist[i]));
19 testprintf("Protocol[%d/%d]: %p %s\n", i, count, protolist[i], protocol_getName(protolist[i]));
20 }
21 }
22
23 Protocol* getProtocol(Protocol * _Nonnull __unsafe_unretained * _Nullable protolist,
24 unsigned int count, const char* name) {
25 for (unsigned int i = 0; i != count; ++i) {
26 if (!strcmp(protocol_getName(protolist[i]), name))
27 return protolist[i];
28 }
29 return nil;
30 }
31
32 int main()
33 {
34 Protocol * _Nonnull __unsafe_unretained * _Nullable protolist;
35 unsigned int count;
36
37 count = 100;
38 protolist = objc_copyProtocolList(&count);
39 testassert(protolist);
40 testassert(count != 0);
41 testassert(malloc_size(protolist) >= (count * sizeof(Protocol*)));
42 noNullEntries(protolist, count);
43 testassert(protolist[count] == nil);
44 // Check for a shared cache protocol, ie, the one we know comes from libobjc
45 testassert(getProtocol(protolist, count, "NSObject"));
46 // Test for a protocol we know isn't in the cache
47 testassert(getProtocol(protolist, count, "Proto1") == @protocol(Proto1));
48 // Test for a protocol we know isn't there
49 testassert(!getProtocol(protolist, count, "Proto2"));
50 free(protolist);
51
52 // Now add it
53 Protocol* newproto = objc_allocateProtocol("Proto2");
54 objc_registerProtocol(newproto);
55
56 Protocol * _Nonnull __unsafe_unretained * _Nullable newProtolist;
57 unsigned int newCount;
58
59 newCount = 100;
60 newProtolist = objc_copyProtocolList(&newCount);
61 testassert(newProtolist);
62 testassert(newCount == (count + 1));
63 testassert(getProtocol(newProtolist, newCount, "Proto2"));
64 free(newProtolist);
65
66
67 succeed(__FILE__);
68 return 0;
69 }