Skip to content

Commit 84d57ed

Browse files
authored
Merge pull request #226 from Libvisual/enhance-tests
Core (LV::Video, Tests): Add LV::Video testing functions.
2 parents d8b4866 + ade41c2 commit 84d57ed

File tree

10 files changed

+160
-9
lines changed

10 files changed

+160
-9
lines changed

libvisual/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ ENDIF()
224224
# Tests
225225
OPTION(ENABLE_TESTS "Build tests" no)
226226
IF(ENABLE_TESTS)
227+
ADD_COMPILE_DEFINITIONS(LV_ENABLE_TESTS)
228+
227229
ENABLE_TESTING()
228230
ADD_SUBDIRECTORY(tests)
229231
ENDIF()

libvisual/cmake/LVBuildTest.cmake

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ FUNCTION(LV_BUILD_TEST TEST_NAME)
1212

1313
ADD_EXECUTABLE(${TEST_NAME} ${PARSED_ARGS_SOURCES})
1414

15+
TARGET_COMPILE_DEFINITIONS(${TEST_NAME} PRIVATE _LV_BUILD_PRIVATE_TEST_FUNCS)
16+
1517
IF(${PARSED_ARGS_COMPILE_DEFS})
1618
TARGET_COMPILE_DEFINITIONS(${TEST_NAME} PRIVATE ${PARSED_ARGS_COMPILE_DEFS})
1719
ENDIF()
@@ -45,7 +47,9 @@ FUNCTION(LV_BUILD_TEST TEST_NAME)
4547
${PARSE_ARGS_LINK_OPTIONS}
4648
)
4749

48-
ADD_TEST(${TEST_NAME}
49-
${CMAKE_COMMAND} -E chdir ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/${TEST_NAME} ${PARSE_ARGS_ARGS}
50+
ADD_TEST(NAME ${TEST_NAME}
51+
COMMAND ${TEST_NAME} ${PARSE_ARGS_ARGS}
52+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
53+
COMMAND_EXPAND_LISTS
5054
)
5155
ENDFUNCTION()

libvisual/libvisual/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ SET_TARGET_PROPERTIES(libvisual
150150
LINK_FLAGS -Wl,--no-undefined
151151
)
152152

153+
IF(ENABLE_TESTS)
154+
TARGET_COMPILE_DEFINITIONS(libvisual PRIVATE _LV_BUILD_PRIVATE_TEST_FUNCS)
155+
ENDIF()
156+
153157
TARGET_INCLUDE_DIRECTORIES(libvisual
154158
PRIVATE
155159
${PROJECT_SOURCE_DIR}

libvisual/libvisual/lv_video.cpp

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "private/lv_video_transform.hpp"
3737
#include "private/lv_video_bmp.hpp"
3838
#include "private/lv_video_png.hpp"
39+
#include <cstring>
3940
#include <fstream>
4041

4142
namespace LV {
@@ -292,6 +293,108 @@ namespace LV {
292293
return true;
293294
}
294295

296+
#ifdef _LV_BUILD_PRIVATE_TEST_FUNCS
297+
bool Video::validate() const
298+
{
299+
// Validate dimensions.
300+
301+
if (m_impl->width <= 0) {
302+
visual_log (VISUAL_LOG_ERROR, "Video has a zero or negative width (%d).", m_impl->width);
303+
return false;
304+
}
305+
306+
if (m_impl->height <= 0) {
307+
visual_log (VISUAL_LOG_ERROR, "Video has a zero or negative height (%d).", m_impl->height);
308+
return false;
309+
}
310+
311+
if (m_impl->pitch <= 0) {
312+
visual_log (VISUAL_LOG_ERROR, "Video has a zero or negative pitch (%d).", m_impl->pitch);
313+
return false;
314+
}
315+
316+
// Validate extents.
317+
318+
if (m_impl->width != m_impl->extents.width) {
319+
visual_log (VISUAL_LOG_ERROR, "Width (%d) and width of extents (%d) do not agree.",
320+
m_impl->width, m_impl->extents.width);
321+
return false;
322+
}
323+
324+
if (m_impl->height != m_impl->extents.height) {
325+
visual_log (VISUAL_LOG_ERROR, "Height (%d) and height of extents (%d) do not agree.",
326+
m_impl->height, m_impl->extents.height);
327+
return false;
328+
}
329+
330+
// Validate parent-child relations.
331+
332+
if (m_impl->parent) {
333+
auto const parent_impl = m_impl->parent->m_impl.get ();
334+
if (!parent_impl->extents.contains (m_impl->extents)) {
335+
visual_log (VISUAL_LOG_ERROR, "Sub-video is not fully contained by parent.");
336+
return false;
337+
}
338+
}
339+
340+
if (m_impl->depth != VISUAL_VIDEO_DEPTH_GL) {
341+
// Validate depth and bytes per pixel consistency.
342+
343+
if (m_impl->bpp != visual_video_depth_bpp (m_impl->depth) / 8) {
344+
visual_log (VISUAL_LOG_ERROR, "Depth (%s) is not consistent with BPP (%d).",
345+
visual_video_depth_name (m_impl->depth), m_impl->bpp);
346+
return false;
347+
}
348+
349+
// Validate pixel row table
350+
351+
for (int y = 0; y < m_impl->height; y++) {
352+
auto const pixel_row_ptr = static_cast<uint8_t const*> (get_pixels ()) + y * m_impl->pitch;
353+
354+
if (m_impl->pixel_rows[y] != pixel_row_ptr) {
355+
visual_log (VISUAL_LOG_ERROR, "Pixel row pointer table is wrong at y=%d.", y);
356+
return false;
357+
}
358+
}
359+
}
360+
361+
return true;
362+
}
363+
#endif // defined(_LV_BUILD_PRIVATE_TEST_FUNCS)
364+
365+
bool Video::has_same_content (VideoConstPtr const& video) const
366+
{
367+
// Comparison of GL LV::Video is not supported.
368+
if (m_impl->depth == VISUAL_VIDEO_DEPTH_GL || video->m_impl->depth == VISUAL_VIDEO_DEPTH_GL) {
369+
return false;
370+
}
371+
372+
// Videos must have the same dimensions and pixel format.
373+
374+
if (m_impl->width != video->m_impl->width)
375+
return false;
376+
377+
if (m_impl->height != video->m_impl->height)
378+
return false;
379+
380+
if (m_impl->depth != video->m_impl->depth)
381+
return false;
382+
383+
// Videos must have the same colours at every pixel.
384+
385+
// Determine the size (in bytes) of the blocks in each row to compare. This must exclude padding.
386+
// It must also exclude pixels outside of a subvideo's extents.
387+
std::size_t const content_bytes_per_row = m_impl->width * m_impl->bpp;
388+
389+
for (int y = 0; y < m_impl->height; y++) {
390+
if (std::memcmp (m_impl->pixel_rows[y], video->m_impl->pixel_rows[y], content_bytes_per_row)) {
391+
return false;
392+
}
393+
}
394+
395+
return true;
396+
}
397+
295398
void Video::set_palette (Palette const& palette)
296399
{
297400
m_impl->palette = palette;

libvisual/libvisual/lv_video.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,26 @@ namespace LV {
294294
*/
295295
bool compare_attrs_ignore_pitch (VideoConstPtr const& src) const;
296296

297+
#ifdef _LV_BUILD_PRIVATE_TEST_FUNCS
298+
/**
299+
* Validates the state of this Video.
300+
*
301+
* @remark For debugging and testing purposes.
302+
*
303+
* @return true if video has consistent state, false otherwise.
304+
*/
305+
bool validate() const;
306+
#endif // defined(_LV_BUILD_PRIVATE_TEST_FUNCS)
307+
308+
/**
309+
* Checks if this Video has the same (pixel) content as another Video.
310+
*
311+
* @param src Video to compare against
312+
*
313+
* @return true if both videos have the same content, false otherwise
314+
*/
315+
bool has_same_content (VideoConstPtr const& video) const;
316+
297317
/**
298318
* Returns the size of the pixel buffer
299319
*

libvisual/tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ INCLUDE_DIRECTORIES(
55
)
66

77
ADD_SUBDIRECTORY(audio_test)
8-
ADD_SUBDIRECTORY(scale_test)
8+
ADD_SUBDIRECTORY(video_test)
99
ADD_SUBDIRECTORY(time_test)

libvisual/tests/scale_test/CMakeLists.txt

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
LV_BUILD_TEST(video_check_test
2+
SOURCES video_check_test.cpp
3+
)
4+
5+
IF(HAVE_SDL)
6+
LV_BUILD_TEST(video_scale_test
7+
SOURCES video_scale_test.cpp
8+
LINK_LIBS SDL::SDL
9+
)
10+
ENDIF()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "test.h"
2+
#include <libvisual/libvisual.h>
3+
4+
int main (int argc, char* argv[])
5+
{
6+
LV::System::init (argc, argv);
7+
8+
auto video = LV::Video::create(640, 480, VISUAL_VIDEO_DEPTH_16BIT);
9+
10+
LV_TEST_ASSERT (video->validate ());
11+
LV_TEST_ASSERT (video->has_same_content (video));
12+
13+
LV::System::destroy ();
14+
}

0 commit comments

Comments
 (0)