Skip to content

Commit e387c2a

Browse files
committed
[AI]Add test case code for posix module coverage improve: from 23% to 50%
Signed-off-by: Gong Pu <[email protected]>
1 parent b124f70 commit e387c2a

11 files changed

+2940
-0
lines changed

tests/unit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ add_subdirectory(aot-stack-frame)
6868
add_subdirectory(linux-perf)
6969
add_subdirectory(gc)
7070
add_subdirectory(tid-allocator)
71+
add_subdirectory(posix)
7172

7273
if (NOT WAMR_BUILD_TARGET STREQUAL "X86_32")
7374
# should enable 32-bit llvm when X86_32

tests/unit/posix/CMakeLists.txt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright (C) 2025 WAMR Community. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
cmake_minimum_required(VERSION 3.14)
5+
6+
project(test-posix-platform)
7+
8+
add_definitions(-DRUN_ON_LINUX)
9+
10+
set(WAMR_BUILD_LIBC_BUILTIN 1)
11+
set(WAMR_BUILD_LIBC_WASI 1)
12+
13+
include(../unit_common.cmake)
14+
15+
# Include paths
16+
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
17+
18+
# Step 6: posix_clock_test.cc, posix_sleep_test.cc, posix_time_test.cc, posix_malloc_test.cc, posix_file_test.cc, posix_socket_test.cc, and posix_blocking_op_test.cc
19+
set(POSIX_TEST_SOURCE
20+
${CMAKE_CURRENT_SOURCE_DIR}/posix_clock_test.cc
21+
${CMAKE_CURRENT_SOURCE_DIR}/posix_sleep_test.cc
22+
${CMAKE_CURRENT_SOURCE_DIR}/posix_time_test.cc
23+
${CMAKE_CURRENT_SOURCE_DIR}/posix_malloc_test.cc
24+
${CMAKE_CURRENT_SOURCE_DIR}/posix_file_test.cc
25+
${CMAKE_CURRENT_SOURCE_DIR}/posix_socket_test.cc
26+
${CMAKE_CURRENT_SOURCE_DIR}/posix_blocking_op_test.cc
27+
)
28+
29+
# POSIX platform sources - Step 6: posix_clock.c, posix_sleep.c, posix_time.c, posix_malloc.c, posix_file.c, posix_socket.c, and posix_blocking_op.c
30+
set(POSIX_PLATFORM_DIR ${WAMR_ROOT_DIR}/core/shared/platform/common/posix)
31+
set(POSIX_SOURCE_FILES
32+
${POSIX_PLATFORM_DIR}/posix_clock.c
33+
${POSIX_PLATFORM_DIR}/posix_sleep.c
34+
${POSIX_PLATFORM_DIR}/posix_time.c
35+
${POSIX_PLATFORM_DIR}/posix_malloc.c
36+
${POSIX_PLATFORM_DIR}/posix_file.c
37+
${POSIX_PLATFORM_DIR}/posix_socket.c
38+
${POSIX_PLATFORM_DIR}/posix_blocking_op.c
39+
)
40+
41+
# All sources for the test executable
42+
set(unit_test_sources
43+
${POSIX_TEST_SOURCE}
44+
${POSIX_SOURCE_FILES}
45+
${WAMR_RUNTIME_LIB_SOURCE}
46+
)
47+
48+
# Create test executable
49+
add_executable(posix_platform_test ${unit_test_sources})
50+
enable_testing()
51+
include(GoogleTest)
52+
# Link with Google Test
53+
target_link_libraries(posix_platform_test gtest_main)
54+
55+
# Discover tests
56+
gtest_discover_tests(posix_platform_test)
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (C) 2025 WAMR Community. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
#include "test_helper.h"
7+
#include "gtest/gtest.h"
8+
#include "platform_api_extension.h"
9+
#include <unistd.h>
10+
#include <sys/types.h>
11+
12+
class PosixBlockingOpTest : public testing::Test
13+
{
14+
protected:
15+
virtual void SetUp()
16+
{
17+
// Initialize test environment
18+
blocking_op_started = false;
19+
}
20+
21+
virtual void TearDown()
22+
{
23+
// Cleanup
24+
if (blocking_op_started) {
25+
os_end_blocking_op();
26+
blocking_op_started = false;
27+
}
28+
}
29+
30+
public:
31+
WAMRRuntimeRAII<512 * 1024> runtime;
32+
bool blocking_op_started;
33+
};
34+
35+
// Step 3: Blocking Operations Tests for Coverage Improvement
36+
37+
TEST_F(PosixBlockingOpTest, BlockingOpInitialization)
38+
{
39+
// Test os_begin_blocking_op initialization
40+
os_begin_blocking_op();
41+
blocking_op_started = true;
42+
43+
// Test that we can call begin multiple times (should be idempotent)
44+
os_begin_blocking_op();
45+
// Verify the operation can be ended properly after multiple begins
46+
EXPECT_NO_THROW(os_end_blocking_op());
47+
}
48+
49+
TEST_F(PosixBlockingOpTest, BlockingOpCleanup)
50+
{
51+
// Start blocking operation first
52+
os_begin_blocking_op();
53+
blocking_op_started = true;
54+
55+
// Test os_end_blocking_op cleanup
56+
os_end_blocking_op();
57+
blocking_op_started = false;
58+
59+
// Test that ending again is safe (should be idempotent)
60+
os_end_blocking_op();
61+
SUCCEED() << "Multiple calls to os_end_blocking_op handled successfully";
62+
}
63+
64+
TEST_F(PosixBlockingOpTest, BlockingOpCancelMechanism)
65+
{
66+
// Test blocking operation cancel functionality
67+
os_begin_blocking_op();
68+
blocking_op_started = true;
69+
70+
// Verify we can end the operation (simulates cancel)
71+
EXPECT_NO_THROW(os_end_blocking_op());
72+
blocking_op_started = false;
73+
}
74+
75+
76+
77+
TEST_F(PosixBlockingOpTest, NestedBlockingOperations)
78+
{
79+
// Test nested blocking operations (should handle gracefully)
80+
os_begin_blocking_op();
81+
blocking_op_started = true;
82+
83+
// Try to start another blocking operation (should be handled)
84+
os_begin_blocking_op();
85+
86+
// End operations in reverse order
87+
os_end_blocking_op();
88+
os_end_blocking_op();
89+
blocking_op_started = false;
90+
SUCCEED() << "Nested blocking operations handled successfully";
91+
}
92+
93+
TEST_F(PosixBlockingOpTest, BlockingOpWithoutInit)
94+
{
95+
// Test ending blocking operation without starting it
96+
os_end_blocking_op();
97+
// Should handle gracefully (implementation dependent)
98+
SUCCEED() << "End blocking operation without init handled gracefully";
99+
}
100+
101+
TEST_F(PosixBlockingOpTest, BlockingOpStateConsistency)
102+
{
103+
// Test state consistency across multiple operations
104+
for (int i = 0; i < 3; i++) {
105+
os_begin_blocking_op();
106+
blocking_op_started = true;
107+
108+
EXPECT_NO_THROW(os_end_blocking_op());
109+
blocking_op_started = false;
110+
}
111+
}
112+
113+
114+
115+
116+
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright (C) 2025 WAMR Community. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
#include "test_helper.h"
7+
#include "gtest/gtest.h"
8+
#include "platform_api_extension.h"
9+
10+
class PosixClockTest : public testing::Test
11+
{
12+
protected:
13+
virtual void SetUp() {}
14+
virtual void TearDown() {}
15+
16+
public:
17+
WAMRRuntimeRAII<512 * 1024> runtime;
18+
};
19+
20+
TEST_F(PosixClockTest, ClockResolutionGet_ValidClocks)
21+
{
22+
__wasi_timestamp_t resolution;
23+
24+
// Test MONOTONIC clock
25+
EXPECT_EQ(__WASI_ESUCCESS,
26+
os_clock_res_get(__WASI_CLOCK_MONOTONIC, &resolution));
27+
EXPECT_GT(resolution, 0);
28+
29+
// Test REALTIME clock
30+
EXPECT_EQ(__WASI_ESUCCESS,
31+
os_clock_res_get(__WASI_CLOCK_REALTIME, &resolution));
32+
EXPECT_GT(resolution, 0);
33+
34+
// Test PROCESS_CPUTIME_ID clock if supported
35+
__wasi_errno_t result =
36+
os_clock_res_get(__WASI_CLOCK_PROCESS_CPUTIME_ID, &resolution);
37+
if (result == __WASI_ESUCCESS) {
38+
EXPECT_GT(resolution, 0);
39+
}
40+
else {
41+
EXPECT_EQ(__WASI_ENOTSUP, result);
42+
}
43+
44+
// Test THREAD_CPUTIME_ID clock if supported
45+
result = os_clock_res_get(__WASI_CLOCK_THREAD_CPUTIME_ID, &resolution);
46+
if (result == __WASI_ESUCCESS) {
47+
EXPECT_GT(resolution, 0);
48+
}
49+
else {
50+
EXPECT_EQ(__WASI_ENOTSUP, result);
51+
}
52+
}
53+
54+
TEST_F(PosixClockTest, ClockTimeGet_ValidClocks)
55+
{
56+
__wasi_timestamp_t time1, time2;
57+
58+
// Test MONOTONIC clock
59+
EXPECT_EQ(__WASI_ESUCCESS,
60+
os_clock_time_get(__WASI_CLOCK_MONOTONIC, 0, &time1));
61+
EXPECT_EQ(__WASI_ESUCCESS,
62+
os_clock_time_get(__WASI_CLOCK_MONOTONIC, 0, &time2));
63+
EXPECT_GE(time2, time1);
64+
65+
// Test REALTIME clock
66+
EXPECT_EQ(__WASI_ESUCCESS,
67+
os_clock_time_get(__WASI_CLOCK_REALTIME, 0, &time1));
68+
EXPECT_GT(time1, 0);
69+
70+
// Test with different precision values
71+
EXPECT_EQ(__WASI_ESUCCESS,
72+
os_clock_time_get(__WASI_CLOCK_REALTIME, 1000000, &time1));
73+
EXPECT_GT(time1, 0);
74+
}
75+
76+
TEST_F(PosixClockTest, ClockTimeGet_InvalidClock)
77+
{
78+
__wasi_timestamp_t time;
79+
80+
// Test invalid clock ID
81+
EXPECT_EQ(__WASI_EINVAL,
82+
os_clock_time_get((__wasi_clockid_t)999, 0, &time));
83+
}
84+
85+
TEST_F(PosixClockTest, ClockResolutionGet_InvalidClock)
86+
{
87+
__wasi_timestamp_t resolution;
88+
89+
// Test invalid clock ID
90+
EXPECT_EQ(__WASI_EINVAL,
91+
os_clock_res_get((__wasi_clockid_t)999, &resolution));
92+
}
93+
94+
TEST_F(PosixClockTest, ClockTimeGet_ProcessCpuTime)
95+
{
96+
__wasi_timestamp_t time;
97+
98+
// Test PROCESS_CPUTIME_ID clock
99+
__wasi_errno_t result =
100+
os_clock_time_get(__WASI_CLOCK_PROCESS_CPUTIME_ID, 0, &time);
101+
if (result == __WASI_ESUCCESS) {
102+
EXPECT_GE(time, 0);
103+
}
104+
else {
105+
EXPECT_EQ(__WASI_ENOTSUP, result);
106+
}
107+
}
108+
109+
TEST_F(PosixClockTest, ClockTimeGet_ThreadCpuTime)
110+
{
111+
__wasi_timestamp_t time;
112+
113+
// Test THREAD_CPUTIME_ID clock
114+
__wasi_errno_t result =
115+
os_clock_time_get(__WASI_CLOCK_THREAD_CPUTIME_ID, 0, &time);
116+
if (result == __WASI_ESUCCESS) {
117+
EXPECT_GE(time, 0);
118+
}
119+
else {
120+
EXPECT_EQ(__WASI_ENOTSUP, result);
121+
}
122+
}

0 commit comments

Comments
 (0)