protocol_copyMethodList.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. .*protocol_copyMethodList.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
  7. .*protocol_copyMethodList.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
  8. .*protocol_copyMethodList.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
  9. .*protocol_copyMethodList.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
  10. END
  11. */
  12. #include "test.h"
  13. #include <malloc/malloc.h>
  14. #include <objc/objc-runtime.h>
  15. @protocol SuperMethods
  16. +(void)SuperMethodClass;
  17. +(void)SuperMethodClass2;
  18. -(void)SuperMethodInstance;
  19. -(void)SuperMethodInstance2;
  20. @end
  21. @protocol SubMethods
  22. +(void)SubMethodClass;
  23. +(void)SubMethodClass2;
  24. -(void)SubMethodInstance;
  25. -(void)SubMethodInstance2;
  26. @end
  27. @protocol SuperOptionalMethods
  28. @optional
  29. +(void)SuperOptMethodClass;
  30. +(void)SuperOptMethodClass2;
  31. -(void)SuperOptMethodInstance;
  32. -(void)SuperOptMethodInstance2;
  33. @end
  34. @protocol SubOptionalMethods <SuperOptionalMethods>
  35. @optional
  36. +(void)SubOptMethodClass;
  37. +(void)SubOptMethodClass2;
  38. -(void)SubOptMethodInstance;
  39. -(void)SubOptMethodInstance2;
  40. @end
  41. @protocol NoMethods @end
  42. static int isNamed(struct objc_method_description m, const char *name)
  43. {
  44. return (m.name == sel_registerName(name));
  45. }
  46. int main()
  47. {
  48. struct objc_method_description *methods;
  49. unsigned int count;
  50. Protocol *proto;
  51. proto = @protocol(SubMethods);
  52. testassert(proto);
  53. // Check required methods
  54. count = 999;
  55. methods = protocol_copyMethodDescriptionList(proto, YES, YES, &count);
  56. testassert(methods);
  57. testassert(count == 2);
  58. testassert((isNamed(methods[0], "SubMethodInstance") &&
  59. isNamed(methods[1], "SubMethodInstance2"))
  60. ||
  61. (isNamed(methods[1], "SubMethodInstance") &&
  62. isNamed(methods[0], "SubMethodInstance2")));
  63. free(methods);
  64. count = 999;
  65. methods = protocol_copyMethodDescriptionList(proto, YES, NO, &count);
  66. testassert(methods);
  67. testassert(count == 2);
  68. testassert((isNamed(methods[0], "SubMethodClass") &&
  69. isNamed(methods[1], "SubMethodClass2"))
  70. ||
  71. (isNamed(methods[1], "SubMethodClass") &&
  72. isNamed(methods[0], "SubMethodClass2")));
  73. free(methods);
  74. // Check lack of optional methods
  75. count = 999;
  76. methods = protocol_copyMethodDescriptionList(proto, NO, YES, &count);
  77. testassert(!methods);
  78. testassert(count == 0);
  79. count = 999;
  80. methods = protocol_copyMethodDescriptionList(proto, NO, NO, &count);
  81. testassert(!methods);
  82. testassert(count == 0);
  83. proto = @protocol(SubOptionalMethods);
  84. testassert(proto);
  85. // Check optional methods
  86. count = 999;
  87. methods = protocol_copyMethodDescriptionList(proto, NO, YES, &count);
  88. testassert(methods);
  89. testassert(count == 2);
  90. testassert((isNamed(methods[0], "SubOptMethodInstance") &&
  91. isNamed(methods[1], "SubOptMethodInstance2"))
  92. ||
  93. (isNamed(methods[1], "SubOptMethodInstance") &&
  94. isNamed(methods[0], "SubOptMethodInstance2")));
  95. free(methods);
  96. count = 999;
  97. methods = protocol_copyMethodDescriptionList(proto, NO, NO, &count);
  98. testassert(methods);
  99. testassert(count == 2);
  100. testassert((isNamed(methods[0], "SubOptMethodClass") &&
  101. isNamed(methods[1], "SubOptMethodClass2"))
  102. ||
  103. (isNamed(methods[1], "SubOptMethodClass") &&
  104. isNamed(methods[0], "SubOptMethodClass2")));
  105. free(methods);
  106. // Check lack of required methods
  107. count = 999;
  108. methods = protocol_copyMethodDescriptionList(proto, YES, YES, &count);
  109. testassert(!methods);
  110. testassert(count == 0);
  111. count = 999;
  112. methods = protocol_copyMethodDescriptionList(proto, YES, NO, &count);
  113. testassert(!methods);
  114. testassert(count == 0);
  115. // Check NULL protocol parameter
  116. count = 999;
  117. methods = protocol_copyMethodDescriptionList(NULL, YES, YES, &count);
  118. testassert(!methods);
  119. testassert(count == 0);
  120. count = 999;
  121. methods = protocol_copyMethodDescriptionList(NULL, YES, NO, &count);
  122. testassert(!methods);
  123. testassert(count == 0);
  124. count = 999;
  125. methods = protocol_copyMethodDescriptionList(NULL, NO, YES, &count);
  126. testassert(!methods);
  127. testassert(count == 0);
  128. count = 999;
  129. methods = protocol_copyMethodDescriptionList(NULL, NO, NO, &count);
  130. testassert(!methods);
  131. testassert(count == 0);
  132. succeed(__FILE__);
  133. }