1 #include <api/fd/array.h>
2 #include "util/debug.h"
3 #include "tests/tests.h"
5 static void fdarray__init_revents(struct fdarray
*fda
, short revents
)
9 fda
->nr
= fda
->nr_alloc
;
11 for (fd
= 0; fd
< fda
->nr
; ++fd
) {
12 fda
->entries
[fd
].fd
= fda
->nr
- fd
;
13 fda
->entries
[fd
].revents
= revents
;
17 static int fdarray__fprintf_prefix(struct fdarray
*fda
, const char *prefix
, FILE *fp
)
24 printed
+= fprintf(fp
, "\n%s: ", prefix
);
25 return printed
+ fdarray__fprintf(fda
, fp
);
28 int test__fdarray__filter(void)
30 int nr_fds
, expected_fd
[2], fd
, err
= TEST_FAIL
;
31 struct fdarray
*fda
= fdarray__new(5, 5);
34 pr_debug("\nfdarray__new() failed!");
38 fdarray__init_revents(fda
, POLLIN
);
39 nr_fds
= fdarray__filter(fda
, POLLHUP
, NULL
);
40 if (nr_fds
!= fda
->nr_alloc
) {
41 pr_debug("\nfdarray__filter()=%d != %d shouldn't have filtered anything",
42 nr_fds
, fda
->nr_alloc
);
46 fdarray__init_revents(fda
, POLLHUP
);
47 nr_fds
= fdarray__filter(fda
, POLLHUP
, NULL
);
49 pr_debug("\nfdarray__filter()=%d != %d, should have filtered all fds",
50 nr_fds
, fda
->nr_alloc
);
54 fdarray__init_revents(fda
, POLLHUP
);
55 fda
->entries
[2].revents
= POLLIN
;
56 expected_fd
[0] = fda
->entries
[2].fd
;
58 pr_debug("\nfiltering all but fda->entries[2]:");
59 fdarray__fprintf_prefix(fda
, "before", stderr
);
60 nr_fds
= fdarray__filter(fda
, POLLHUP
, NULL
);
61 fdarray__fprintf_prefix(fda
, " after", stderr
);
63 pr_debug("\nfdarray__filter()=%d != 1, should have left just one event", nr_fds
);
67 if (fda
->entries
[0].fd
!= expected_fd
[0]) {
68 pr_debug("\nfda->entries[0].fd=%d != %d\n",
69 fda
->entries
[0].fd
, expected_fd
[0]);
73 fdarray__init_revents(fda
, POLLHUP
);
74 fda
->entries
[0].revents
= POLLIN
;
75 expected_fd
[0] = fda
->entries
[0].fd
;
76 fda
->entries
[3].revents
= POLLIN
;
77 expected_fd
[1] = fda
->entries
[3].fd
;
79 pr_debug("\nfiltering all but (fda->entries[0], fda->entries[3]):");
80 fdarray__fprintf_prefix(fda
, "before", stderr
);
81 nr_fds
= fdarray__filter(fda
, POLLHUP
, NULL
);
82 fdarray__fprintf_prefix(fda
, " after", stderr
);
84 pr_debug("\nfdarray__filter()=%d != 2, should have left just two events",
89 for (fd
= 0; fd
< 2; ++fd
) {
90 if (fda
->entries
[fd
].fd
!= expected_fd
[fd
]) {
91 pr_debug("\nfda->entries[%d].fd=%d != %d\n", fd
,
92 fda
->entries
[fd
].fd
, expected_fd
[fd
]);
101 fdarray__delete(fda
);
106 int test__fdarray__add(void)
109 struct fdarray
*fda
= fdarray__new(2, 2);
112 pr_debug("\nfdarray__new() failed!");
116 #define FDA_CHECK(_idx, _fd, _revents) \
117 if (fda->entries[_idx].fd != _fd) { \
118 pr_debug("\n%d: fda->entries[%d](%d) != %d!", \
119 __LINE__, _idx, fda->entries[1].fd, _fd); \
122 if (fda->entries[_idx].events != (_revents)) { \
123 pr_debug("\n%d: fda->entries[%d].revents(%d) != %d!", \
124 __LINE__, _idx, fda->entries[_idx].fd, _revents); \
128 #define FDA_ADD(_idx, _fd, _revents, _nr) \
129 if (fdarray__add(fda, _fd, _revents) < 0) { \
130 pr_debug("\n%d: fdarray__add(fda, %d, %d) failed!", \
131 __LINE__,_fd, _revents); \
134 if (fda->nr != _nr) { \
135 pr_debug("\n%d: fdarray__add(fda, %d, %d)=%d != %d", \
136 __LINE__,_fd, _revents, fda->nr, _nr); \
139 FDA_CHECK(_idx, _fd, _revents)
141 FDA_ADD(0, 1, POLLIN
, 1);
142 FDA_ADD(1, 2, POLLERR
, 2);
144 fdarray__fprintf_prefix(fda
, "before growing array", stderr
);
146 FDA_ADD(2, 35, POLLHUP
, 3);
148 if (fda
->entries
== NULL
) {
149 pr_debug("\nfdarray__add(fda, 35, POLLHUP) should have allocated fda->pollfd!");
153 fdarray__fprintf_prefix(fda
, "after 3rd add", stderr
);
155 FDA_ADD(3, 88, POLLIN
| POLLOUT
, 4);
157 fdarray__fprintf_prefix(fda
, "after 4th add", stderr
);
159 FDA_CHECK(0, 1, POLLIN
);
160 FDA_CHECK(1, 2, POLLERR
);
161 FDA_CHECK(2, 35, POLLHUP
);
162 FDA_CHECK(3, 88, POLLIN
| POLLOUT
);
171 fdarray__delete(fda
);