1 /*
2 * Copyright (c) 2020 Tobias Svehagen
3 * Copyright (c) 2020 Grinn
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <ztest.h>
9
10 #include <net/socket.h>
11
12 #ifdef CONFIG_POSIX_API
13 #include <sys/eventfd.h>
14 #else
15 #include <posix/sys/eventfd.h>
16 #endif
17
test_eventfd(void)18 static void test_eventfd(void)
19 {
20 int fd = eventfd(0, 0);
21
22 zassert_true(fd >= 0, "fd == %d", fd);
23
24 zsock_close(fd);
25 }
26
test_eventfd_write_then_read(void)27 static void test_eventfd_write_then_read(void)
28 {
29 eventfd_t val;
30 int fd, ret;
31
32 fd = eventfd(0, 0);
33 zassert_true(fd >= 0, "fd == %d", fd);
34
35 ret = eventfd_write(fd, 3);
36 zassert_true(ret == 0, "write ret %d", ret);
37
38 ret = eventfd_write(fd, 2);
39 zassert_true(ret == 0, "write ret %d", ret);
40
41 ret = eventfd_read(fd, &val);
42 zassert_true(ret == 0, "read ret %d", ret);
43 zassert_true(val == 5, "val == %d", val);
44
45 zsock_close(fd);
46
47 /* Test EFD_SEMAPHORE */
48
49 fd = eventfd(0, EFD_SEMAPHORE);
50 zassert_true(fd >= 0, "fd == %d", fd);
51
52 ret = eventfd_write(fd, 3);
53 zassert_true(ret == 0, "write ret %d", ret);
54
55 ret = eventfd_write(fd, 2);
56 zassert_true(ret == 0, "write ret %d", ret);
57
58 ret = eventfd_read(fd, &val);
59 zassert_true(ret == 0, "read ret %d", ret);
60 zassert_true(val == 1, "val == %d", val);
61
62 zsock_close(fd);
63 }
64
test_main(void)65 void test_main(void)
66 {
67 ztest_test_suite(test_eventfd_basic,
68 ztest_unit_test(test_eventfd),
69 ztest_unit_test(test_eventfd_write_then_read)
70 );
71 ztest_run_test_suite(test_eventfd_basic);
72 }
73