Skip to content

Commit 889a6f4

Browse files
committed
Add C++ test
1 parent 43a74e9 commit 889a6f4

6 files changed

Lines changed: 206 additions & 0 deletions

File tree

.github/workflows/_build_linux.yml

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

src/CMakeLists.txt

Lines changed: 4 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)
@@ -41,6 +42,9 @@ find_package(fmt REQUIRED NO_SYSTEM_ENVIRONMENT_PATH NO_CMAKE_PACKAGE_REGISTRY N
4142
find_package(glog REQUIRED NO_SYSTEM_ENVIRONMENT_PATH NO_CMAKE_PACKAGE_REGISTRY NO_CMAKE_SYSTEM_PACKAGE_REGISTRY)
4243
find_package(libdeflate REQUIRED NO_SYSTEM_ENVIRONMENT_PATH NO_CMAKE_PACKAGE_REGISTRY NO_CMAKE_SYSTEM_PACKAGE_REGISTRY)
4344
find_package(ZLIB REQUIRED NO_SYSTEM_ENVIRONMENT_PATH NO_CMAKE_PACKAGE_REGISTRY NO_CMAKE_SYSTEM_PACKAGE_REGISTRY)
45+
if(SPDL_BUILD_TESTING)
46+
find_package(GTest REQUIRED NO_SYSTEM_ENVIRONMENT_PATH NO_CMAKE_PACKAGE_REGISTRY NO_CMAKE_SYSTEM_PACKAGE_REGISTRY)
47+
endif()
4448

4549
add_subdirectory(third_party/ffmpeg/multi)
4650
if (SPDL_USE_TRACING)

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: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
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+
fmt::fmt
91+
glog::glog
92+
GTest::gtest
93+
GTest::gtest_main
94+
)
95+
96+
target_include_directories(${test_name}
97+
PRIVATE
98+
"${PROJECT_SOURCE_DIR}"
99+
)
100+
101+
# Register test with CTest
102+
add_test(
103+
NAME ${test_name}
104+
COMMAND ${test_name}
105+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
106+
)
107+
108+
# Set test environment
109+
set_tests_properties(${test_name} PROPERTIES
110+
ENVIRONMENT "TEST_DATA_DIR=${TEST_DATA_DIR}"
111+
)
112+
endfunction()
113+
114+
# Build tests for configured FFmpeg versions
115+
set(ffmpeg_versions 8 7 6 5 4)
116+
if (SPDL_USE_FFMPEG_VERSION IN_LIST ffmpeg_versions)
117+
add_smoke_test("${SPDL_USE_FFMPEG_VERSION}")
118+
else()
119+
add_smoke_test(8)
120+
add_smoke_test(7)
121+
add_smoke_test(6)
122+
add_smoke_test(5)
123+
add_smoke_test(4)
124+
endif()

src/third_party/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ add_subdirectory(fmt)
2424
add_subdirectory(glog)
2525
add_subdirectory(zlib)
2626
add_subdirectory(libdeflate)
27+
if(SPDL_BUILD_TESTING)
28+
add_subdirectory(gtest)
29+
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 GoogleTest")
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=65fab701d9829d38cb77c14acdc431d2108bfdbf8979e40eb8ae567edf10b27c
9+
DOWNLOAD_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../cache"
10+
)
11+
FetchContent_MakeAvailable(gtest)

0 commit comments

Comments
 (0)