run-not-on-stack.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <ccan/foreach/foreach.h>
  2. #include <ccan/tap/tap.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <ccan/foreach/foreach.c>
  7. static int global_i;
  8. static void *allocs[1000];
  9. static unsigned int num_allocs;
  10. static void iterate(unsigned int depth, bool done_global)
  11. {
  12. int *i, expecti;
  13. const char *expectp[] = { "hello", "there" };
  14. const char **p;
  15. int stack_i;
  16. if (depth == 4)
  17. return;
  18. if (!done_global) {
  19. expecti = 0;
  20. foreach_int(global_i, 0, 1) {
  21. ok1(global_i == expecti);
  22. expecti++;
  23. if (global_i == 0)
  24. iterate(depth + 1, true);
  25. }
  26. ok1(expecti == 2);
  27. }
  28. i = allocs[num_allocs++] = malloc(sizeof(*i));
  29. expecti = 0;
  30. foreach_int(*i, 0, 1) {
  31. ok1(*i == expecti);
  32. expecti++;
  33. if (*i == 0)
  34. iterate(depth + 1, done_global);
  35. }
  36. ok1(expecti == 2);
  37. p = allocs[num_allocs++] = malloc(sizeof(*p));
  38. expecti = 0;
  39. foreach_ptr(*p, "hello", "there") {
  40. ok1(strcmp(expectp[expecti], *p) == 0);
  41. expecti++;
  42. if (expecti == 1)
  43. iterate(depth + 1, done_global);
  44. }
  45. ok1(expecti == 2);
  46. ok1(*p == NULL);
  47. expecti = 0;
  48. foreach_int(stack_i, 0, 1) {
  49. ok1(stack_i == expecti);
  50. expecti++;
  51. if (stack_i == 0)
  52. iterate(depth + 1, done_global);
  53. }
  54. ok1(expecti == 2);
  55. }
  56. int main(void)
  57. {
  58. unsigned int i;
  59. plan_tests(861);
  60. iterate(0, false);
  61. ok1(num_allocs < 1000);
  62. for (i = 0; i < num_allocs; i++)
  63. free(allocs[i]);
  64. return exit_status();
  65. }