1 // SPDX-License-Identifier: GPL-2.0
2 #include <api/fd/array.h>
4 #include "util/debug.h"
5 #include "tests/tests.h"
7 static void fdarray__init_revents(struct fdarray
*fda
, short revents
)
11 fda
->nr
= fda
->nr_alloc
;
13 for (fd
= 0; fd
< fda
->nr
; ++fd
) {
14 fda
->entries
[fd
].fd
= fda
->nr
- fd
;
15 fda
->entries
[fd
].events
= revents
;
16 fda
->entries
[fd
].revents
= revents
;
20 static int fdarray__fprintf_prefix(struct fdarray
*fda
, const char *prefix
, FILE *fp
)
27 printed
+= fprintf(fp
, "\n%s: ", prefix
);
28 return printed
+ fdarray__fprintf(fda
, fp
);
31 int test__fdarray__filter(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
33 int nr_fds
, err
= TEST_FAIL
;
34 struct fdarray
*fda
= fdarray__new(5, 5);
37 pr_debug("\nfdarray__new() failed!");
41 fdarray__init_revents(fda
, POLLIN
);
42 nr_fds
= fdarray__filter(fda
, POLLHUP
, NULL
, NULL
);
43 if (nr_fds
!= fda
->nr_alloc
) {
44 pr_debug("\nfdarray__filter()=%d != %d shouldn't have filtered anything",
45 nr_fds
, fda
->nr_alloc
);
49 fdarray__init_revents(fda
, POLLHUP
);
50 nr_fds
= fdarray__filter(fda
, POLLHUP
, NULL
, NULL
);
52 pr_debug("\nfdarray__filter()=%d != %d, should have filtered all fds",
53 nr_fds
, fda
->nr_alloc
);
57 fdarray__init_revents(fda
, POLLHUP
);
58 fda
->entries
[2].revents
= POLLIN
;
60 pr_debug("\nfiltering all but fda->entries[2]:");
61 fdarray__fprintf_prefix(fda
, "before", stderr
);
62 nr_fds
= fdarray__filter(fda
, POLLHUP
, NULL
, NULL
);
63 fdarray__fprintf_prefix(fda
, " after", stderr
);
65 pr_debug("\nfdarray__filter()=%d != 1, should have left just one event", nr_fds
);
69 fdarray__init_revents(fda
, POLLHUP
);
70 fda
->entries
[0].revents
= POLLIN
;
71 fda
->entries
[3].revents
= POLLIN
;
73 pr_debug("\nfiltering all but (fda->entries[0], fda->entries[3]):");
74 fdarray__fprintf_prefix(fda
, "before", stderr
);
75 nr_fds
= fdarray__filter(fda
, POLLHUP
, NULL
, NULL
);
76 fdarray__fprintf_prefix(fda
, " after", stderr
);
78 pr_debug("\nfdarray__filter()=%d != 2, should have left just two events",
92 int test__fdarray__add(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
95 struct fdarray
*fda
= fdarray__new(2, 2);
98 pr_debug("\nfdarray__new() failed!");
102 #define FDA_CHECK(_idx, _fd, _revents) \
103 if (fda->entries[_idx].fd != _fd) { \
104 pr_debug("\n%d: fda->entries[%d](%d) != %d!", \
105 __LINE__, _idx, fda->entries[1].fd, _fd); \
108 if (fda->entries[_idx].events != (_revents)) { \
109 pr_debug("\n%d: fda->entries[%d].revents(%d) != %d!", \
110 __LINE__, _idx, fda->entries[_idx].fd, _revents); \
114 #define FDA_ADD(_idx, _fd, _revents, _nr) \
115 if (fdarray__add(fda, _fd, _revents, fdarray_flag__default) < 0) { \
116 pr_debug("\n%d: fdarray__add(fda, %d, %d) failed!", \
117 __LINE__,_fd, _revents); \
120 if (fda->nr != _nr) { \
121 pr_debug("\n%d: fdarray__add(fda, %d, %d)=%d != %d", \
122 __LINE__,_fd, _revents, fda->nr, _nr); \
125 FDA_CHECK(_idx, _fd, _revents)
127 FDA_ADD(0, 1, POLLIN
, 1);
128 FDA_ADD(1, 2, POLLERR
, 2);
130 fdarray__fprintf_prefix(fda
, "before growing array", stderr
);
132 FDA_ADD(2, 35, POLLHUP
, 3);
134 if (fda
->entries
== NULL
) {
135 pr_debug("\nfdarray__add(fda, 35, POLLHUP) should have allocated fda->pollfd!");
139 fdarray__fprintf_prefix(fda
, "after 3rd add", stderr
);
141 FDA_ADD(3, 88, POLLIN
| POLLOUT
, 4);
143 fdarray__fprintf_prefix(fda
, "after 4th add", stderr
);
145 FDA_CHECK(0, 1, POLLIN
);
146 FDA_CHECK(1, 2, POLLERR
);
147 FDA_CHECK(2, 35, POLLHUP
);
148 FDA_CHECK(3, 88, POLLIN
| POLLOUT
);
157 fdarray__delete(fda
);