Skip to content

Commit 97d342f

Browse files
committed
Add C++ test
1 parent c2cc327 commit 97d342f

8 files changed

Lines changed: 220 additions & 3 deletions

File tree

.github/workflows/_build_linux.yml

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

packaging/spdl_io/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ Documentation = "https://facebookresearch.github.io/spdl/main/index.html"
1919
[build-system]
2020
requires = [
2121
"setuptools>=77.0.0",
22-
"ninja",
23-
"cmake>=3.24",
2422
"typing_extensions",
23+
# "ninja",
24+
# "cmake>=3.24",
2525
]
2626
build-backend = "setuptools.build_meta"

packaging/spdl_io/setup.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def _env(var, default=False):
4040
_SPDL_USE_NVCODEC = _env("SPDL_USE_NVCODEC")
4141
_SPDL_USE_NVJPEG = _env("SPDL_USE_NVJPEG")
4242
_SPDL_USE_CUDA = _env("SPDL_USE_CUDA", _SPDL_USE_NVCODEC or _SPDL_USE_NVJPEG)
43-
43+
_SPDL_BUILD_TESTING = _env("SPDL_BUILD_TESTING")
4444

4545
def _is_gil_enabled():
4646
try:
@@ -84,6 +84,7 @@ def _b(var):
8484
"-DCMAKE_INSTALL_MESSAGE=NEVER",
8585
"-DCMAKE_POLICY_VERSION_MINIMUM=3.5",
8686
f"-DCMAKE_INSTALL_PREFIX={build_dir}",
87+
f"-DSPDL_BUILD_TESTING={_SPDL_BUILD_TESTING}",
8788
"-GNinja",
8889
"-Wno-dev",
8990
],
@@ -121,6 +122,7 @@ def _b(var):
121122
f"-DSPDL_USE_NVCODEC={_b(_SPDL_USE_NVCODEC)}",
122123
f"-DSPDL_USE_NVJPEG={_b(_SPDL_USE_NVJPEG)}",
123124
f"-DSPDL_USE_NPPI={_b(_env('SPDL_USE_NPPI'))}",
125+
f"-DSPDL_BUILD_TESTING={_SPDL_BUILD_TESTING}",
124126
f"-DSPDL_LINK_STATIC_NVJPEG={_b(_env('SPDL_LINK_STATIC_NVJPEG'))}",
125127
f"-DSPDL_USE_FFMPEG_VERSION={_SPDL_USE_FFMPEG_VERSION}",
126128
f"-DSPDL_DEBUG_REFCOUNT={_b(_env('SPDL_DEBUG_REFCOUNT'))}",
@@ -137,8 +139,17 @@ def _b(var):
137139
],
138140
# fmt: on
139141
]
142+
test_cmd = [
143+
[
144+
"ctest",
145+
"--test-dir", main_build_dir,
146+
"--output-on-failure",
147+
]
148+
]
140149
if _env("SPDL_SKIP_DEPS"):
141150
return main_build_cmd
151+
if _SPDL_BUILD_TESTING:
152+
return deps_cmd + main_build_cmd + test_cmd
142153
return deps_cmd + main_build_cmd
143154

144155

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)