5 #include <malloc/malloc.h>
6 #include <objc/objc-runtime.h>
9 +(id)proto1ClassMethod;
10 -(id)proto1InstanceMethod;
13 void noNullEntries(Protocol * _Nonnull __unsafe_unretained * _Nullable protolist,
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]));
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))
34 Protocol * _Nonnull __unsafe_unretained * _Nullable protolist;
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"));
53 Protocol* newproto = objc_allocateProtocol("Proto2");
54 objc_registerProtocol(newproto);
56 Protocol * _Nonnull __unsafe_unretained * _Nullable newProtolist;
57 unsigned int newCount;
60 newProtolist = objc_copyProtocolList(&newCount);
61 testassert(newProtolist);
62 testassert(newCount == (count + 1));
63 testassert(getProtocol(newProtolist, newCount, "Proto2"));