run.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "typesafe_cb/typesafe_cb.h"
  2. #include <string.h>
  3. #include "tap/tap.h"
  4. static char dummy = 0;
  5. /* The example usage. */
  6. static void _set_some_value(void *val)
  7. {
  8. ok1(val == &dummy);
  9. }
  10. #define set_some_value(expr) \
  11. _set_some_value(cast_if_type((expr), unsigned long, void *))
  12. static void _callback_onearg(void (*fn)(void *arg), void *arg)
  13. {
  14. fn(arg);
  15. }
  16. static void _callback_preargs(void (*fn)(int a, int b, void *arg), void *arg)
  17. {
  18. fn(1, 2, arg);
  19. }
  20. static void _callback_postargs(void (*fn)(void *arg, int a, int b), void *arg)
  21. {
  22. fn(arg, 1, 2);
  23. }
  24. #define callback_onearg(cb, arg) \
  25. _callback_onearg(typesafe_cb(void, (cb), (arg)), (arg))
  26. #define callback_preargs(cb, arg) \
  27. _callback_preargs(typesafe_cb_preargs(void, (cb), (arg), int, int), (arg))
  28. #define callback_postargs(cb, arg) \
  29. _callback_postargs(typesafe_cb_postargs(void, (cb), (arg), int, int), (arg))
  30. static void my_callback_onearg(char *p)
  31. {
  32. ok1(strcmp(p, "hello world") == 0);
  33. }
  34. static void my_callback_onearg_const(const char *p)
  35. {
  36. ok1(strcmp(p, "hello world") == 0);
  37. }
  38. static void my_callback_onearg_volatile(volatile char *p)
  39. {
  40. ok1(strcmp((char *)p, "hello world") == 0);
  41. }
  42. static void my_callback_preargs(int a, int b, char *p)
  43. {
  44. ok1(a == 1);
  45. ok1(b == 2);
  46. ok1(strcmp(p, "hello world") == 0);
  47. }
  48. static void my_callback_preargs_const(int a, int b, const char *p)
  49. {
  50. ok1(a == 1);
  51. ok1(b == 2);
  52. ok1(strcmp(p, "hello world") == 0);
  53. }
  54. static void my_callback_preargs_volatile(int a, int b, volatile char *p)
  55. {
  56. ok1(a == 1);
  57. ok1(b == 2);
  58. ok1(strcmp((char *)p, "hello world") == 0);
  59. }
  60. static void my_callback_postargs(char *p, int a, int b)
  61. {
  62. ok1(a == 1);
  63. ok1(b == 2);
  64. ok1(strcmp(p, "hello world") == 0);
  65. }
  66. static void my_callback_postargs_const(const char *p, int a, int b)
  67. {
  68. ok1(a == 1);
  69. ok1(b == 2);
  70. ok1(strcmp(p, "hello world") == 0);
  71. }
  72. static void my_callback_postargs_volatile(volatile char *p, int a, int b)
  73. {
  74. ok1(a == 1);
  75. ok1(b == 2);
  76. ok1(strcmp((char *)p, "hello world") == 0);
  77. }
  78. /* This is simply a compile test; we promised cast_if_type can be in a
  79. * static initializer. */
  80. struct callback_onearg
  81. {
  82. void (*fn)(void *arg);
  83. void *arg;
  84. };
  85. struct callback_onearg cb_onearg
  86. = { typesafe_cb(void, my_callback_onearg, "hello world"), "hello world" };
  87. struct callback_preargs
  88. {
  89. void (*fn)(int a, int b, void *arg);
  90. void *arg;
  91. };
  92. struct callback_preargs cb_preargs
  93. = { typesafe_cb_preargs(void, my_callback_preargs, "hi", int, int), "hi" };
  94. struct callback_postargs
  95. {
  96. void (*fn)(void *arg, int a, int b);
  97. void *arg;
  98. };
  99. struct callback_postargs cb_postargs
  100. = { typesafe_cb_postargs(void, my_callback_postargs, "hi", int, int), "hi" };
  101. int main(int argc, char *argv[])
  102. {
  103. void *p = &dummy;
  104. unsigned long l = (unsigned long)p;
  105. plan_tests(2 + 3 + 9 + 9);
  106. set_some_value(p);
  107. set_some_value(l);
  108. callback_onearg(my_callback_onearg, "hello world");
  109. callback_onearg(my_callback_onearg_const, "hello world");
  110. callback_onearg(my_callback_onearg_volatile, "hello world");
  111. callback_preargs(my_callback_preargs, "hello world");
  112. callback_preargs(my_callback_preargs_const, "hello world");
  113. callback_preargs(my_callback_preargs_volatile, "hello world");
  114. callback_postargs(my_callback_postargs, "hello world");
  115. callback_postargs(my_callback_postargs_const, "hello world");
  116. callback_postargs(my_callback_postargs_volatile, "hello world");
  117. return exit_status();
  118. }