blob: bc5982f42dc3a173d81e5100172578ac4bdb280b [file] [log] [blame]
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -03001#include <api/fd/array.h>
Arnaldo Carvalho de Melo16b91d52016-07-07 11:35:04 -03002#include <poll.h>
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -03003#include "util/debug.h"
4#include "tests/tests.h"
5
6static void fdarray__init_revents(struct fdarray *fda, short revents)
7{
8 int fd;
9
10 fda->nr = fda->nr_alloc;
11
12 for (fd = 0; fd < fda->nr; ++fd) {
13 fda->entries[fd].fd = fda->nr - fd;
14 fda->entries[fd].revents = revents;
15 }
16}
17
18static int fdarray__fprintf_prefix(struct fdarray *fda, const char *prefix, FILE *fp)
19{
20 int printed = 0;
21
Namhyung Kimbb963e12017-02-17 17:17:38 +090022 if (verbose <= 0)
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -030023 return 0;
24
25 printed += fprintf(fp, "\n%s: ", prefix);
26 return printed + fdarray__fprintf(fda, fp);
27}
28
Arnaldo Carvalho de Melo721a1f52015-11-19 12:01:48 -030029int test__fdarray__filter(int subtest __maybe_unused)
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -030030{
31 int nr_fds, expected_fd[2], fd, err = TEST_FAIL;
32 struct fdarray *fda = fdarray__new(5, 5);
33
34 if (fda == NULL) {
35 pr_debug("\nfdarray__new() failed!");
36 goto out;
37 }
38
39 fdarray__init_revents(fda, POLLIN);
Wang Nan258e4bf2016-05-25 13:44:57 +000040 nr_fds = fdarray__filter(fda, POLLHUP, NULL, NULL);
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -030041 if (nr_fds != fda->nr_alloc) {
42 pr_debug("\nfdarray__filter()=%d != %d shouldn't have filtered anything",
43 nr_fds, fda->nr_alloc);
44 goto out_delete;
45 }
46
47 fdarray__init_revents(fda, POLLHUP);
Wang Nan258e4bf2016-05-25 13:44:57 +000048 nr_fds = fdarray__filter(fda, POLLHUP, NULL, NULL);
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -030049 if (nr_fds != 0) {
50 pr_debug("\nfdarray__filter()=%d != %d, should have filtered all fds",
51 nr_fds, fda->nr_alloc);
52 goto out_delete;
53 }
54
55 fdarray__init_revents(fda, POLLHUP);
56 fda->entries[2].revents = POLLIN;
57 expected_fd[0] = fda->entries[2].fd;
58
59 pr_debug("\nfiltering all but fda->entries[2]:");
60 fdarray__fprintf_prefix(fda, "before", stderr);
Wang Nan258e4bf2016-05-25 13:44:57 +000061 nr_fds = fdarray__filter(fda, POLLHUP, NULL, NULL);
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -030062 fdarray__fprintf_prefix(fda, " after", stderr);
63 if (nr_fds != 1) {
64 pr_debug("\nfdarray__filter()=%d != 1, should have left just one event", nr_fds);
65 goto out_delete;
66 }
67
68 if (fda->entries[0].fd != expected_fd[0]) {
69 pr_debug("\nfda->entries[0].fd=%d != %d\n",
70 fda->entries[0].fd, expected_fd[0]);
71 goto out_delete;
72 }
73
74 fdarray__init_revents(fda, POLLHUP);
75 fda->entries[0].revents = POLLIN;
76 expected_fd[0] = fda->entries[0].fd;
77 fda->entries[3].revents = POLLIN;
78 expected_fd[1] = fda->entries[3].fd;
79
80 pr_debug("\nfiltering all but (fda->entries[0], fda->entries[3]):");
81 fdarray__fprintf_prefix(fda, "before", stderr);
Wang Nan258e4bf2016-05-25 13:44:57 +000082 nr_fds = fdarray__filter(fda, POLLHUP, NULL, NULL);
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -030083 fdarray__fprintf_prefix(fda, " after", stderr);
84 if (nr_fds != 2) {
85 pr_debug("\nfdarray__filter()=%d != 2, should have left just two events",
86 nr_fds);
87 goto out_delete;
88 }
89
90 for (fd = 0; fd < 2; ++fd) {
91 if (fda->entries[fd].fd != expected_fd[fd]) {
92 pr_debug("\nfda->entries[%d].fd=%d != %d\n", fd,
93 fda->entries[fd].fd, expected_fd[fd]);
94 goto out_delete;
95 }
96 }
97
98 pr_debug("\n");
99
100 err = 0;
101out_delete:
102 fdarray__delete(fda);
103out:
104 return err;
105}
106
Arnaldo Carvalho de Melo721a1f52015-11-19 12:01:48 -0300107int test__fdarray__add(int subtest __maybe_unused)
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -0300108{
109 int err = TEST_FAIL;
110 struct fdarray *fda = fdarray__new(2, 2);
111
112 if (fda == NULL) {
113 pr_debug("\nfdarray__new() failed!");
114 goto out;
115 }
116
117#define FDA_CHECK(_idx, _fd, _revents) \
118 if (fda->entries[_idx].fd != _fd) { \
119 pr_debug("\n%d: fda->entries[%d](%d) != %d!", \
120 __LINE__, _idx, fda->entries[1].fd, _fd); \
121 goto out_delete; \
122 } \
123 if (fda->entries[_idx].events != (_revents)) { \
124 pr_debug("\n%d: fda->entries[%d].revents(%d) != %d!", \
125 __LINE__, _idx, fda->entries[_idx].fd, _revents); \
126 goto out_delete; \
127 }
128
129#define FDA_ADD(_idx, _fd, _revents, _nr) \
130 if (fdarray__add(fda, _fd, _revents) < 0) { \
131 pr_debug("\n%d: fdarray__add(fda, %d, %d) failed!", \
132 __LINE__,_fd, _revents); \
133 goto out_delete; \
134 } \
135 if (fda->nr != _nr) { \
136 pr_debug("\n%d: fdarray__add(fda, %d, %d)=%d != %d", \
137 __LINE__,_fd, _revents, fda->nr, _nr); \
138 goto out_delete; \
139 } \
140 FDA_CHECK(_idx, _fd, _revents)
141
142 FDA_ADD(0, 1, POLLIN, 1);
143 FDA_ADD(1, 2, POLLERR, 2);
144
145 fdarray__fprintf_prefix(fda, "before growing array", stderr);
146
147 FDA_ADD(2, 35, POLLHUP, 3);
148
149 if (fda->entries == NULL) {
150 pr_debug("\nfdarray__add(fda, 35, POLLHUP) should have allocated fda->pollfd!");
151 goto out_delete;
152 }
153
154 fdarray__fprintf_prefix(fda, "after 3rd add", stderr);
155
156 FDA_ADD(3, 88, POLLIN | POLLOUT, 4);
157
158 fdarray__fprintf_prefix(fda, "after 4th add", stderr);
159
160 FDA_CHECK(0, 1, POLLIN);
161 FDA_CHECK(1, 2, POLLERR);
162 FDA_CHECK(2, 35, POLLHUP);
163 FDA_CHECK(3, 88, POLLIN | POLLOUT);
164
165#undef FDA_ADD
166#undef FDA_CHECK
167
168 pr_debug("\n");
169
170 err = 0;
171out_delete:
172 fdarray__delete(fda);
173out:
174 return err;
175}