methodArgs.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. TEST_CFLAGS -Wno-deprecated-declarations
  3. TEST_BUILD_OUTPUT
  4. .*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')?
  5. .*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')?
  6. .*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')?
  7. .*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')?
  8. .*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')?
  9. .*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
  10. .*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
  11. .*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
  12. .*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
  13. .*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
  14. .*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
  15. .*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
  16. .*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
  17. .*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
  18. .*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
  19. .*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
  20. END
  21. */
  22. #include "test.h"
  23. #include "testroot.i"
  24. #include <string.h>
  25. #include <objc/objc-runtime.h>
  26. @interface Super : TestRoot @end
  27. @implementation Super
  28. +(id)method:(int)__unused arg :(void(^)(void)) __unused arg2 {
  29. return 0;
  30. }
  31. @end
  32. int main()
  33. {
  34. char buf[128];
  35. char *arg;
  36. struct objc_method_description *desc;
  37. Method m = class_getClassMethod([Super class], sel_registerName("method::"));
  38. testassert(m);
  39. testassert(method_getNumberOfArguments(m) == 4);
  40. arg = method_copyArgumentType(m, 0);
  41. testassert(arg);
  42. testassert(0 == strcmp(arg, "@"));
  43. memset(buf, 1, 128);
  44. method_getArgumentType(m, 0, buf, 1+strlen(arg));
  45. testassert(0 == strcmp(arg, buf));
  46. testassert(buf[1+strlen(arg)] == 1);
  47. memset(buf, 1, 128);
  48. method_getArgumentType(m, 0, buf, 2);
  49. testassert(0 == strncmp(arg, buf, 2));
  50. testassert(buf[2] == 1);
  51. free(arg);
  52. arg = method_copyArgumentType(m, 1);
  53. testassert(arg);
  54. testassert(0 == strcmp(arg, ":"));
  55. memset(buf, 1, 128);
  56. method_getArgumentType(m, 1, buf, 1+strlen(arg));
  57. testassert(0 == strcmp(arg, buf));
  58. testassert(buf[1+strlen(arg)] == 1);
  59. memset(buf, 1, 128);
  60. method_getArgumentType(m, 1, buf, 2);
  61. testassert(0 == strncmp(arg, buf, 2));
  62. testassert(buf[2] == 1);
  63. free(arg);
  64. arg = method_copyArgumentType(m, 2);
  65. testassert(arg);
  66. testassert(0 == strcmp(arg, "i"));
  67. memset(buf, 1, 128);
  68. method_getArgumentType(m, 2, buf, 1+strlen(arg));
  69. testassert(0 == strcmp(arg, buf));
  70. testassert(buf[1+strlen(arg)] == 1);
  71. memset(buf, 1, 128);
  72. method_getArgumentType(m, 2, buf, 2);
  73. testassert(0 == strncmp(arg, buf, 2));
  74. testassert(buf[2] == 1);
  75. free(arg);
  76. arg = method_copyArgumentType(m, 3);
  77. testassert(arg);
  78. testassert(0 == strcmp(arg, "@?"));
  79. memset(buf, 1, 128);
  80. method_getArgumentType(m, 3, buf, 1+strlen(arg));
  81. testassert(0 == strcmp(arg, buf));
  82. testassert(buf[1+strlen(arg)] == 1);
  83. memset(buf, 1, 128);
  84. method_getArgumentType(m, 3, buf, 2);
  85. testassert(0 == strncmp(arg, buf, 2));
  86. testassert(buf[2] == 1);
  87. memset(buf, 1, 128);
  88. method_getArgumentType(m, 3, buf, 3);
  89. testassert(0 == strncmp(arg, buf, 3));
  90. testassert(buf[3] == 1);
  91. free(arg);
  92. arg = method_copyArgumentType(m, 4);
  93. testassert(!arg);
  94. arg = method_copyArgumentType(m, -1);
  95. testassert(!arg);
  96. memset(buf, 1, 128);
  97. method_getArgumentType(m, 4, buf, 127);
  98. testassert(buf[0] == 0);
  99. testassert(buf[1] == 0);
  100. testassert(buf[127] == 1);
  101. memset(buf, 1, 128);
  102. method_getArgumentType(m, -1, buf, 127);
  103. testassert(buf[0] == 0);
  104. testassert(buf[1] == 0);
  105. testassert(buf[127] == 1);
  106. arg = method_copyReturnType(m);
  107. testassert(arg);
  108. testassert(0 == strcmp(arg, "@"));
  109. memset(buf, 1, 128);
  110. method_getReturnType(m, buf, 1+strlen(arg));
  111. testassert(0 == strcmp(arg, buf));
  112. testassert(buf[1+strlen(arg)] == 1);
  113. memset(buf, 1, 128);
  114. method_getReturnType(m, buf, 2);
  115. testassert(0 == strncmp(arg, buf, 2));
  116. testassert(buf[2] == 1);
  117. free(arg);
  118. desc = method_getDescription(m);
  119. testassert(desc);
  120. testassert(desc->name == sel_registerName("method::"));
  121. #if __LP64__
  122. testassert(0 == strcmp(desc->types, "@28@0:8i16@?20"));
  123. #else
  124. testassert(0 == strcmp(desc->types, "@16@0:4i8@?12"));
  125. #endif
  126. testassert(0 == method_getNumberOfArguments(NULL));
  127. testassert(NULL == method_copyArgumentType(NULL, 10));
  128. testassert(NULL == method_copyReturnType(NULL));
  129. testassert(NULL == method_getDescription(NULL));
  130. memset(buf, 1, 128);
  131. method_getArgumentType(NULL, 1, buf, 127);
  132. testassert(buf[0] == 0);
  133. testassert(buf[1] == 0);
  134. testassert(buf[127] == 1);
  135. memset(buf, 1, 128);
  136. method_getArgumentType(NULL, 1, buf, 0);
  137. testassert(buf[0] == 1);
  138. testassert(buf[1] == 1);
  139. method_getArgumentType(m, 1, NULL, 128);
  140. method_getArgumentType(m, 1, NULL, 0);
  141. method_getArgumentType(NULL, 1, NULL, 128);
  142. method_getArgumentType(NULL, 1, NULL, 0);
  143. memset(buf, 1, 128);
  144. method_getReturnType(NULL, buf, 127);
  145. testassert(buf[0] == 0);
  146. testassert(buf[1] == 0);
  147. testassert(buf[127] == 1);
  148. memset(buf, 1, 128);
  149. method_getReturnType(NULL, buf, 0);
  150. testassert(buf[0] == 1);
  151. testassert(buf[1] == 1);
  152. method_getReturnType(m, NULL, 128);
  153. method_getReturnType(m, NULL, 0);
  154. method_getReturnType(NULL, NULL, 128);
  155. method_getReturnType(NULL, NULL, 0);
  156. succeed(__FILE__);
  157. }