Skip to content

Commit 7f0c674

Browse files
committed
tests: posix: add POSIX_DEVICE_IO testsuite
Add a testsuite for the POSIX_DEVICE_IO Option Group. Signed-off-by: Chris Friedt <[email protected]>
1 parent 8ea9c9f commit 7f0c674

File tree

4 files changed

+184
-0
lines changed

4 files changed

+184
-0
lines changed

tests/posix/device_io/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(posix_c_lib_ext)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
9+
target_sources(app PRIVATE ${app_sources})
10+
11+
target_include_directories(app PRIVATE ${ZEPHYR_BASE}/lib/posix/options/getopt)
12+
target_compile_options(app PRIVATE -U_POSIX_C_SOURCE -D_POSIX_C_SOURCE=200809L)

tests/posix/device_io/prj.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CONFIG_POSIX_API=y
2+
CONFIG_ZTEST=y
3+
4+
CONFIG_POSIX_AEP_CHOICE_BASE=y
5+
CONFIG_POSIX_DEVICE_IO=y

tests/posix/device_io/src/main.c

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright (c) 2025 Tenstorrent AI ULC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stdio.h>
8+
9+
#include <zephyr/ztest.h>
10+
11+
#include <zephyr/posix/fcntl.h>
12+
#include <zephyr/posix/poll.h>
13+
#include <zephyr/posix/sys/select.h>
14+
#include <zephyr/posix/unistd.h>
15+
16+
#ifndef STDIN_FILENO
17+
#define STDIN_FILENO 0
18+
#endif
19+
20+
#ifndef STDOUT_FILENO
21+
#define STDOUT_FILENO 1
22+
#endif
23+
24+
#ifndef STDERR_FILENO
25+
#define STDERR_FILENO 2
26+
#endif
27+
28+
ZTEST(posix_device_io, test_FD_CLR)
29+
{
30+
fd_set fds;
31+
32+
FD_CLR(0, &fds);
33+
}
34+
35+
ZTEST(posix_device_io, test_FD_ISSET)
36+
{
37+
fd_set fds;
38+
39+
zassert_false(FD_ISSET(0, &fds));
40+
}
41+
42+
ZTEST(posix_device_io, test_FD_SET)
43+
{
44+
fd_set fds;
45+
46+
FD_SET(0, &fds);
47+
zassert_true(FD_ISSET(0, &fds));
48+
}
49+
50+
ZTEST(posix_device_io, test_FD_ZERO)
51+
{
52+
fd_set fds;
53+
54+
FD_ZERO(&fds);
55+
zassert_false(FD_ISSET(0, &fds));
56+
}
57+
58+
ZTEST(posix_device_io, test_close)
59+
{
60+
zassert_not_ok(close(-1));
61+
}
62+
63+
ZTEST(posix_device_io, test_fdopen)
64+
{
65+
zassert_not_ok(fdopen(1, "r"));
66+
}
67+
68+
ZTEST(posix_device_io, test_fileno)
69+
{
70+
zassert_equal(fileno(stdout), STDOUT_FILENO);
71+
}
72+
73+
ZTEST(posix_device_io, test_open)
74+
{
75+
zassert_equal(open("/dev/null", O_RDONLY), -1);
76+
}
77+
78+
ZTEST(posix_device_io, test_poll)
79+
{
80+
struct pollfd fds[1] = {{.fd = STDIN_FILENO, .events = POLLIN}};
81+
82+
zassert_ok(poll(fds, ARRAY_SIZE(fds), 0));
83+
}
84+
85+
ZTEST(posix_device_io, test_pread)
86+
{
87+
uint8_t buf[8];
88+
89+
zassert_ok(pread(STDIN_FILENO, buf, sizeof(buf), 0));
90+
}
91+
92+
ZTEST(posix_device_io, test_pselect)
93+
{
94+
fd_set fds;
95+
struct timespec timeout = {.tv_sec = 0, .tv_nsec = 0};
96+
97+
FD_ZERO(&fds);
98+
FD_SET(STDIN_FILENO, &fds);
99+
100+
zassert_ok(pselect(STDIN_FILENO + 1, &fds, NULL, NULL, &timeout, NULL));
101+
}
102+
103+
ZTEST(posix_device_io, test_pwrite)
104+
{
105+
zassert_ok(pwrite(STDOUT_FILENO, "x", 1, 0));
106+
}
107+
108+
ZTEST(posix_device_io, test_read)
109+
{
110+
uint8_t buf[8];
111+
112+
zassert_ok(read(STDIN_FILENO, buf, sizeof(buf)));
113+
}
114+
115+
ZTEST(posix_device_io, test_select)
116+
{
117+
fd_set fds;
118+
struct timeval timeout = {.tv_sec = 0, .tv_usec = 0};
119+
120+
FD_ZERO(&fds);
121+
FD_SET(STDIN_FILENO, &fds);
122+
123+
zassert_ok(select(STDIN_FILENO + 1, &fds, NULL, NULL, &timeout));
124+
}
125+
126+
ZTEST(posix_device_io, test_write)
127+
{
128+
zassert_ok(pwrite(STDOUT_FILENO, "x", 1, 0));
129+
}
130+
131+
ZTEST_SUITE(posix_device_io, NULL, NULL, NULL, NULL, NULL);

tests/posix/device_io/testcase.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
common:
2+
filter: not CONFIG_NATIVE_LIBC
3+
tags:
4+
- posix
5+
- device_io
6+
# 1 tier0 platform per supported architecture
7+
platform_key:
8+
- arch
9+
- simulation
10+
integration_platforms:
11+
- qemu_cortex_m0
12+
tests:
13+
portability.posix.device_io:
14+
extra_configs:
15+
- CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=256
16+
portability.posix.device_io.armclang_std_libc:
17+
toolchain_allow: armclang
18+
extra_configs:
19+
- CONFIG_ARMCLANG_STD_LIBC=y
20+
portability.posix.device_io.arcmwdtlib:
21+
toolchain_allow: arcmwdt
22+
extra_configs:
23+
- CONFIG_ARCMWDT_LIBC=y
24+
portability.posix.device_io.minimal:
25+
extra_configs:
26+
- CONFIG_MINIMAL_LIBC=y
27+
- CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=256
28+
portability.posix.device_io.newlib:
29+
filter: TOOLCHAIN_HAS_NEWLIB == 1
30+
extra_configs:
31+
- CONFIG_NEWLIB_LIBC=y
32+
portability.posix.device_io.picolibc:
33+
tags: picolibc
34+
filter: CONFIG_PICOLIBC_SUPPORTED
35+
extra_configs:
36+
- CONFIG_PICOLIBC=y

0 commit comments

Comments
 (0)