Skip to content

Commit 72b7760

Browse files
committed
Add C++ test
1 parent a11ac4b commit 72b7760

File tree

5 files changed

+200
-0
lines changed

5 files changed

+200
-0
lines changed

.github/workflows/_build_linux.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,56 @@ jobs:
114114
pip install --no-dependencies $(find "${HOME}/package" -name '*.whl' -depth -maxdepth 1)
115115
python .github/scripts/check_deps.py
116116
117+
cpp-unit-test:
118+
if: "${{ inputs.run-test == 'true' }}"
119+
needs: ["build"]
120+
name: "C++ test (ffmpeg ${{ matrix.ffmpeg-version }})"
121+
strategy:
122+
fail-fast: false
123+
matrix:
124+
ffmpeg-version: ["8.0"]
125+
runs-on: ${{ inputs.machine }}
126+
defaults:
127+
run:
128+
shell: bash -el {0}
129+
steps:
130+
- uses: actions/checkout@v4
131+
with:
132+
persist-credentials: false
133+
134+
- uses: conda-incubator/setup-miniconda@v3
135+
with:
136+
python-version: "3.12"
137+
channels: conda-forge
138+
conda-remove-defaults: "true"
139+
140+
- name: Setup
141+
run: |
142+
set -ex
143+
144+
# Install build dependencies
145+
conda install -q cmake ninja gtest fmt glog "ffmpeg==${{ matrix.ffmpeg-version }}" pkg-config
146+
147+
# For some reason, the dynamic libraries are not found.
148+
echo "LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${CONDA_PREFIX}/lib" >> $GITHUB_ENV
149+
150+
- name: Build and run C++ tests
151+
run: |
152+
set -ex
153+
154+
# Configure with testing enabled
155+
cmake -S src/libspdl -B build \
156+
-GNinja \
157+
-DCMAKE_BUILD_TYPE=Release \
158+
-DSPDL_BUILD_TESTING=ON \
159+
-DCMAKE_PREFIX_PATH="${CONDA_PREFIX}"
160+
161+
# Build
162+
cmake --build build
163+
164+
# Run tests
165+
ctest --test-dir build --output-on-failure
166+
117167
unit-test:
118168
if: "${{ inputs.run-test == 'true' }}"
119169
needs: ["build"]

src/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ option(SPDL_LINK_STATIC_NVJPEG "Link nvJPEG and NPPI statically." OFF)
1111
option(SPDL_DEBUG_REFCOUNT "Enable debug print for reference counting of AVFrame objects." OFF)
1212
option(SPDL_IS_GIL_ENABLED "Whether the target Python has the GIL enabled" ON)
1313
option(SPDL_BUILD_PYTHON_BINDING "Build Python binding" ON)
14+
option(SPDL_BUILD_TESTING "Build C++ test" ON)
1415

1516
if (SPDL_USE_NVCODEC OR SPDL_USE_NVJPEG)
1617
if (NOT SPDL_USE_CUDA)
@@ -54,6 +55,10 @@ if (NOT DEFINED SPDL_PYTHON_BINDING_INSTALL_PREFIX)
5455
set(SPDL_PYTHON_BINDING_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
5556
endif ()
5657

58+
if(SPDL_BUILD_TESTING)
59+
add_subdirectory(third_party/gtest)
60+
endif()
61+
5762
message(STATUS "SPDL_CXX_CPU_COMPILE_FLAGS=${SPDL_CXX_CPU_COMPILE_FLAGS}")
5863

5964
add_subdirectory(libspdl)

src/libspdl/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
17
set(SPDL_COMMON_DEFS)
28
if (SPDL_USE_TRACING)
39
list(APPEND SPDL_COMMON_DEFS SPDL_USE_TRACING)
@@ -25,3 +31,9 @@ add_subdirectory(core)
2531
if (SPDL_USE_CUDA)
2632
add_subdirectory(cuda)
2733
endif()
34+
35+
# Enable testing
36+
if (SPDL_BUILD_TESTING)
37+
enable_testing()
38+
add_subdirectory(tests)
39+
endif()

src/libspdl/tests/CMakeLists.txt

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
message(STATUS "########################################")
8+
message(STATUS "# Configuring libspdl tests")
9+
message(STATUS "########################################")
10+
11+
# Find GTest package
12+
find_package(GTest REQUIRED)
13+
14+
# Generate test data directory
15+
set(TEST_DATA_DIR "${CMAKE_CURRENT_BINARY_DIR}/test_data")
16+
file(MAKE_DIRECTORY "${TEST_DATA_DIR}")
17+
18+
# Helper function to generate test data files using FFmpeg
19+
function(generate_test_data)
20+
find_program(FFMPEG_EXECUTABLE ffmpeg REQUIRED)
21+
22+
# Generate test audio (AAC, 5 seconds, 44.1kHz, stereo)
23+
execute_process(
24+
COMMAND ${FFMPEG_EXECUTABLE}
25+
-f lavfi
26+
-i sine=frequency=440:duration=5:sample_rate=44100
27+
-ac 2
28+
-c:a aac
29+
-b:a 128k
30+
${TEST_DATA_DIR}/test_audio.aac
31+
-y
32+
OUTPUT_QUIET
33+
ERROR_QUIET
34+
RESULT_VARIABLE AUDIO_RESULT
35+
)
36+
37+
# Generate test image (JPEG, 320x240)
38+
execute_process(
39+
COMMAND ${FFMPEG_EXECUTABLE}
40+
-f lavfi
41+
-i testsrc=duration=1:size=320x240:rate=1
42+
-frames:v 1
43+
${TEST_DATA_DIR}/test_image.jpg
44+
-y
45+
OUTPUT_QUIET
46+
ERROR_QUIET
47+
RESULT_VARIABLE IMAGE_RESULT
48+
)
49+
50+
# Generate test video (H.264/MP4, 2 seconds, 320x240, 30fps)
51+
execute_process(
52+
COMMAND ${FFMPEG_EXECUTABLE}
53+
-f lavfi
54+
-i testsrc=duration=2:size=320x240:rate=30
55+
-c:v libx264
56+
-pix_fmt yuv420p
57+
${TEST_DATA_DIR}/test_video.mp4
58+
-y
59+
OUTPUT_QUIET
60+
ERROR_QUIET
61+
RESULT_VARIABLE VIDEO_RESULT
62+
)
63+
64+
if(AUDIO_RESULT EQUAL 0 AND IMAGE_RESULT EQUAL 0 AND VIDEO_RESULT EQUAL 0)
65+
message(STATUS "Test data generated successfully in ${TEST_DATA_DIR}")
66+
else()
67+
message(WARNING "Failed to generate some test data files. Tests may fail.")
68+
endif()
69+
endfunction()
70+
71+
# Generate test data at configure time
72+
generate_test_data()
73+
74+
# Create smoke test executable for each FFmpeg version
75+
function(add_smoke_test ffmpeg_version)
76+
set(test_name "smoke_test_ffmpeg${ffmpeg_version}")
77+
set(lib_name "spdl_ffmpeg${ffmpeg_version}")
78+
79+
message(STATUS "Building test: ${test_name}")
80+
81+
add_executable(${test_name} smoke_test.cpp)
82+
83+
target_compile_definitions(${test_name} PRIVATE
84+
"TEST_DATA_DIR=\"${TEST_DATA_DIR}\""
85+
)
86+
87+
target_link_libraries(${test_name}
88+
PRIVATE
89+
${lib_name}
90+
GTest::gtest
91+
GTest::gtest_main
92+
)
93+
94+
target_include_directories(${test_name}
95+
PRIVATE
96+
"${PROJECT_SOURCE_DIR}"
97+
)
98+
99+
# Register test with CTest
100+
add_test(
101+
NAME ${test_name}
102+
COMMAND ${test_name}
103+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
104+
)
105+
106+
# Set test environment
107+
set_tests_properties(${test_name} PROPERTIES
108+
ENVIRONMENT "TEST_DATA_DIR=${TEST_DATA_DIR}"
109+
)
110+
endfunction()
111+
112+
# Build tests for configured FFmpeg versions
113+
set(ffmpeg_versions 8 7 6 5 4)
114+
if (SPDL_USE_FFMPEG_VERSION IN_LIST ffmpeg_versions)
115+
add_smoke_test("${SPDL_USE_FFMPEG_VERSION}")
116+
else()
117+
add_smoke_test(8)
118+
add_smoke_test(7)
119+
add_smoke_test(6)
120+
add_smoke_test(5)
121+
add_smoke_test(4)
122+
endif()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
message(STATUS "########################################")
2+
message(STATUS "# Configuring glog")
3+
message(STATUS "########################################")
4+
5+
FetchContent_Declare(
6+
gtest
7+
URL https://github.com/google/googletest/releases/download/v1.17.0/googletest-1.17.0.tar.gz
8+
URL_HASH SHA256=d5558cd419c8d46bdc958064cb97f963d1ea793866414c025906ec15033512ed
9+
DOWNLOAD_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../cache"
10+
)
11+
FetchContent_MakeAvailable(gtest)

0 commit comments

Comments
 (0)