fdarray.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include <api/fd/array.h>
  2. #include "util/debug.h"
  3. #include "tests/tests.h"
  4. static void fdarray__init_revents(struct fdarray *fda, short revents)
  5. {
  6. int fd;
  7. fda->nr = fda->nr_alloc;
  8. for (fd = 0; fd < fda->nr; ++fd) {
  9. fda->entries[fd].fd = fda->nr - fd;
  10. fda->entries[fd].revents = revents;
  11. }
  12. }
  13. static int fdarray__fprintf_prefix(struct fdarray *fda, const char *prefix, FILE *fp)
  14. {
  15. int printed = 0;
  16. if (!verbose)
  17. return 0;
  18. printed += fprintf(fp, "\n%s: ", prefix);
  19. return printed + fdarray__fprintf(fda, fp);
  20. }
  21. int test__fdarray__filter(void)
  22. {
  23. int nr_fds, expected_fd[2], fd, err = TEST_FAIL;
  24. struct fdarray *fda = fdarray__new(5, 5);
  25. if (fda == NULL) {
  26. pr_debug("\nfdarray__new() failed!");
  27. goto out;
  28. }
  29. fdarray__init_revents(fda, POLLIN);
  30. nr_fds = fdarray__filter(fda, POLLHUP, NULL);
  31. if (nr_fds != fda->nr_alloc) {
  32. pr_debug("\nfdarray__filter()=%d != %d shouldn't have filtered anything",
  33. nr_fds, fda->nr_alloc);
  34. goto out_delete;
  35. }
  36. fdarray__init_revents(fda, POLLHUP);
  37. nr_fds = fdarray__filter(fda, POLLHUP, NULL);
  38. if (nr_fds != 0) {
  39. pr_debug("\nfdarray__filter()=%d != %d, should have filtered all fds",
  40. nr_fds, fda->nr_alloc);
  41. goto out_delete;
  42. }
  43. fdarray__init_revents(fda, POLLHUP);
  44. fda->entries[2].revents = POLLIN;
  45. expected_fd[0] = fda->entries[2].fd;
  46. pr_debug("\nfiltering all but fda->entries[2]:");
  47. fdarray__fprintf_prefix(fda, "before", stderr);
  48. nr_fds = fdarray__filter(fda, POLLHUP, NULL);
  49. fdarray__fprintf_prefix(fda, " after", stderr);
  50. if (nr_fds != 1) {
  51. pr_debug("\nfdarray__filter()=%d != 1, should have left just one event", nr_fds);
  52. goto out_delete;
  53. }
  54. if (fda->entries[0].fd != expected_fd[0]) {
  55. pr_debug("\nfda->entries[0].fd=%d != %d\n",
  56. fda->entries[0].fd, expected_fd[0]);
  57. goto out_delete;
  58. }
  59. fdarray__init_revents(fda, POLLHUP);
  60. fda->entries[0].revents = POLLIN;
  61. expected_fd[0] = fda->entries[0].fd;
  62. fda->entries[3].revents = POLLIN;
  63. expected_fd[1] = fda->entries[3].fd;
  64. pr_debug("\nfiltering all but (fda->entries[0], fda->entries[3]):");
  65. fdarray__fprintf_prefix(fda, "before", stderr);
  66. nr_fds = fdarray__filter(fda, POLLHUP, NULL);
  67. fdarray__fprintf_prefix(fda, " after", stderr);
  68. if (nr_fds != 2) {
  69. pr_debug("\nfdarray__filter()=%d != 2, should have left just two events",
  70. nr_fds);
  71. goto out_delete;
  72. }
  73. for (fd = 0; fd < 2; ++fd) {
  74. if (fda->entries[fd].fd != expected_fd[fd]) {
  75. pr_debug("\nfda->entries[%d].fd=%d != %d\n", fd,
  76. fda->entries[fd].fd, expected_fd[fd]);
  77. goto out_delete;
  78. }
  79. }
  80. pr_debug("\n");
  81. err = 0;
  82. out_delete:
  83. fdarray__delete(fda);
  84. out:
  85. return err;
  86. }
  87. int test__fdarray__add(void)
  88. {
  89. int err = TEST_FAIL;
  90. struct fdarray *fda = fdarray__new(2, 2);
  91. if (fda == NULL) {
  92. pr_debug("\nfdarray__new() failed!");
  93. goto out;
  94. }
  95. #define FDA_CHECK(_idx, _fd, _revents) \
  96. if (fda->entries[_idx].fd != _fd) { \
  97. pr_debug("\n%d: fda->entries[%d](%d) != %d!", \
  98. __LINE__, _idx, fda->entries[1].fd, _fd); \
  99. goto out_delete; \
  100. } \
  101. if (fda->entries[_idx].events != (_revents)) { \
  102. pr_debug("\n%d: fda->entries[%d].revents(%d) != %d!", \
  103. __LINE__, _idx, fda->entries[_idx].fd, _revents); \
  104. goto out_delete; \
  105. }
  106. #define FDA_ADD(_idx, _fd, _revents, _nr) \
  107. if (fdarray__add(fda, _fd, _revents) < 0) { \
  108. pr_debug("\n%d: fdarray__add(fda, %d, %d) failed!", \
  109. __LINE__,_fd, _revents); \
  110. goto out_delete; \
  111. } \
  112. if (fda->nr != _nr) { \
  113. pr_debug("\n%d: fdarray__add(fda, %d, %d)=%d != %d", \
  114. __LINE__,_fd, _revents, fda->nr, _nr); \
  115. goto out_delete; \
  116. } \
  117. FDA_CHECK(_idx, _fd, _revents)
  118. FDA_ADD(0, 1, POLLIN, 1);
  119. FDA_ADD(1, 2, POLLERR, 2);
  120. fdarray__fprintf_prefix(fda, "before growing array", stderr);
  121. FDA_ADD(2, 35, POLLHUP, 3);
  122. if (fda->entries == NULL) {
  123. pr_debug("\nfdarray__add(fda, 35, POLLHUP) should have allocated fda->pollfd!");
  124. goto out_delete;
  125. }
  126. fdarray__fprintf_prefix(fda, "after 3rd add", stderr);
  127. FDA_ADD(3, 88, POLLIN | POLLOUT, 4);
  128. fdarray__fprintf_prefix(fda, "after 4th add", stderr);
  129. FDA_CHECK(0, 1, POLLIN);
  130. FDA_CHECK(1, 2, POLLERR);
  131. FDA_CHECK(2, 35, POLLHUP);
  132. FDA_CHECK(3, 88, POLLIN | POLLOUT);
  133. #undef FDA_ADD
  134. #undef FDA_CHECK
  135. pr_debug("\n");
  136. err = 0;
  137. out_delete:
  138. fdarray__delete(fda);
  139. out:
  140. return err;
  141. }