Skip to content

Commit 59dff46

Browse files
smcvslouken
authored andcommitted
Add a simple automated test
This loads and saves various supported formats, and asserts that the same pixels (or close enough) end up in the output files. For a distribution that knows it intends to support particular formats, if an environment variable like SVG_IMAGE_TEST_REQUIRE_LOAD_JPG or SVG_IMAGE_TEST_REQUIRE_SAVE_JPG is set, the test will fail if the given format is not available. Signed-off-by: Simon McVittie <smcv@debian.org>
1 parent 9950cf3 commit 59dff46

26 files changed

+1749
-0
lines changed

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ include(CMakeDependentOption)
239239
include(CMakePackageConfigHelpers)
240240
include(GNUInstallDirs)
241241

242+
option(BUILD_TESTS "Build unit tests?" OFF)
243+
cmake_dependent_option(INSTALL_TESTS "Install unit tests?" OFF BUILD_TESTS OFF)
242244
option(VENDORED_DEFAULT "Default value for *_VENDORED options. Can be overridden for each library. Is only used in the first configure run." ON)
243245
option(SDL2_IMAGE_DISABLE_INSTALL "Disable installing SDL2_image" OFF)
244246

@@ -767,3 +769,8 @@ if (BUILD_SAMPLES)
767769
endif()
768770
endforeach()
769771
endif()
772+
773+
if (BUILD_TESTS)
774+
include(CTest)
775+
add_subdirectory(test)
776+
endif()

Makefile.am

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ noinst_PROGRAMS = showimage showanim
7474
showimage_LDADD = libSDL2_image.la
7575
showanim_LDADD = libSDL2_image.la
7676

77+
SUBDIRS = .
78+
79+
if BUILD_TESTS
80+
SUBDIRS += test
81+
endif
82+
7783
# Rule to build tar-gzipped distribution package
7884
$(PACKAGE)-$(VERSION).tar.gz: distcheck
7985

configure.ac

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,12 @@ AC_ARG_ENABLE([webp-shared], [AS_HELP_STRING([--enable-webp-shared], [dynamicall
250250
[], [enable_webp_shared=yes])
251251
AC_ARG_ENABLE([qoi], [AS_HELP_STRING([--enable-qoi], [support loading QOI images [default=yes]])],
252252
[], [enable_qoi=yes])
253+
AC_ARG_ENABLE([tests],
254+
[AS_HELP_STRING([--enable-tests], [build tests [default=no]])],
255+
[], [enable_tests=no])
256+
AC_ARG_ENABLE([installed-tests],
257+
[AS_HELP_STRING([--enable-installed-tests], [install tests [default=no]])],
258+
[], [enable_installed_tests=no])
253259

254260
dnl Check for SDL
255261
SDL_VERSION=2.0.8
@@ -665,6 +671,9 @@ AC_SUBST([IMG_LIBS])
665671
AC_SUBST([PC_LIBS])
666672
AC_SUBST([PC_REQUIRES])
667673

674+
AM_CONDITIONAL([BUILD_TESTS], [test "x$enable_tests" = xyes])
675+
AM_CONDITIONAL([INSTALL_TESTS], [test "x$enable_installed_tests" = xyes])
676+
668677
dnl check for GCC warning options
669678
CheckWarnAll
670679

@@ -678,5 +687,6 @@ AC_CONFIG_FILES([
678687
Makefile
679688
SDL2_image.spec
680689
SDL2_image.pc
690+
test/Makefile
681691
])
682692
AC_OUTPUT

test/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/*.test
2+
/CompareSurfaces*.bmp
3+
/save-*.bmp
4+
/save.*
5+
/testimage

test/CMakeLists.txt

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
add_executable(testimage main.c)
2+
3+
set(ALL_TESTS
4+
testimage
5+
)
6+
set(RESOURCE_FILES
7+
palette.bmp
8+
palette.gif
9+
sample.avif
10+
sample.bmp
11+
sample.cur
12+
sample.ico
13+
sample.jpg
14+
sample.jxl
15+
sample.png
16+
sample.pnm
17+
sample.qoi
18+
sample.tif
19+
sample.webp
20+
sample.xcf
21+
sample.xpm
22+
svg.bmp
23+
svg.svg
24+
svg64.bmp
25+
)
26+
27+
set(TESTS_ENVIRONMENT
28+
"SDL_TEST_SRCDIR=${CMAKE_CURRENT_SOURCE_DIR}"
29+
"SDL_TEST_BUILDDIR=${CMAKE_CURRENT_BINARY_DIR}"
30+
"SDL_VIDEODRIVER=dummy"
31+
)
32+
33+
foreach(prog ${ALL_TESTS})
34+
target_compile_definitions(${prog} PRIVATE $<TARGET_PROPERTY:SDL2_image,COMPILE_DEFINITIONS>)
35+
target_link_libraries(${prog} PRIVATE SDL2_image::${sdl2_image_export_name})
36+
if (TARGET SDL2::SDL2main)
37+
target_link_libraries(${prog} PRIVATE SDL2::SDL2main)
38+
endif()
39+
target_link_libraries(${prog} PRIVATE SDL2_test)
40+
if (BUILD_SHARED_LIBS)
41+
target_link_libraries(${prog} PRIVATE SDL2::SDL2)
42+
else()
43+
target_link_libraries(${prog} PRIVATE SDL2::SDL2-static)
44+
endif()
45+
46+
add_test(
47+
NAME ${prog}
48+
COMMAND ${prog}
49+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
50+
)
51+
set_tests_properties(
52+
${prog}
53+
PROPERTIES ENVIRONMENT "${TESTS_ENVIRONMENT}"
54+
)
55+
if(INSTALL_TESTS)
56+
set(exe ${prog})
57+
set(installedtestsdir "${CMAKE_INSTALL_FULL_LIBEXECDIR}/installed-tests/${CMAKE_PROJECT_NAME}")
58+
configure_file(template.test.in "${exe}.test" @ONLY)
59+
install(
60+
FILES "${CMAKE_CURRENT_BINARY_DIR}/${exe}.test"
61+
DESTINATION "${CMAKE_INSTALL_DATADIR}/installed-tests/${CMAKE_PROJECT_NAME}"
62+
)
63+
endif()
64+
endforeach()
65+
66+
if(INSTALL_TESTS)
67+
install(
68+
TARGETS ${ALL_TESTS}
69+
DESTINATION "${CMAKE_INSTALL_LIBEXECDIR}/installed-tests/${CMAKE_PROJECT_NAME}"
70+
)
71+
install(
72+
FILES ${RESOURCE_FILES}
73+
DESTINATION "${CMAKE_INSTALL_LIBEXECDIR}/installed-tests/${CMAKE_PROJECT_NAME}"
74+
)
75+
endif()

test/Makefile.am

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
testmetadir = $(datadir)/installed-tests/$(PACKAGE_TARNAME)
2+
testexecdir = $(libexecdir)/installed-tests/$(PACKAGE_TARNAME)
3+
4+
test_programs = \
5+
testimage \
6+
$(NULL)
7+
8+
testimage_CPPFLAGS = -I$(top_srcdir)
9+
testimage_SOURCES = main.c
10+
testimage_LDADD = \
11+
../libSDL2_image.la \
12+
$(SDL_LIBS) \
13+
-lSDL2_test \
14+
$(NULL)
15+
16+
AM_TESTS_ENVIRONMENT = \
17+
SDL_TEST_SRCDIR=$(abs_srcdir) \
18+
SDL_TEST_BUILDDIR=$(abs_builddir) \
19+
SDL_VIDEODRIVER=dummy \
20+
$(NULL)
21+
22+
if INSTALL_TESTS
23+
testexec_PROGRAMS = $(test_programs)
24+
else
25+
noinst_PROGRAMS = $(test_programs)
26+
endif
27+
28+
TESTS = $(test_programs)
29+
30+
if INSTALL_TESTS
31+
dist_testexec_DATA = \
32+
palette.bmp \
33+
palette.gif \
34+
sample.avif \
35+
sample.bmp \
36+
sample.cur \
37+
sample.ico \
38+
sample.jpg \
39+
sample.jxl \
40+
sample.png \
41+
sample.pnm \
42+
sample.qoi \
43+
sample.tif \
44+
sample.webp \
45+
sample.xcf \
46+
sample.xpm \
47+
svg.bmp \
48+
svg.svg \
49+
svg64.bmp \
50+
$(NULL)
51+
52+
all-local: generatetestmeta
53+
generatetestmeta:
54+
rm -f *.test
55+
set -e; for exe in $(test_programs); do \
56+
sed \
57+
-e 's#@installedtestsdir@#$(testexecdir)#g' \
58+
-e "s#@exe@#$$exe#g" \
59+
< $(srcdir)/template.test.in > $$exe.test; \
60+
done
61+
62+
install-data-hook: installtestmeta
63+
installtestmeta: generatetestmeta
64+
install -m644 -D -t $(DESTDIR)$(testmetadir) *.test
65+
66+
clean-local:
67+
rm -f *.test
68+
rm -f save.jpg save.bmp CompareSurfaces*.bmp
69+
endif

0 commit comments

Comments
 (0)