fdarray.c 4.3 KB

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