From 1f043d699252d3e77a295b4ed35e12c189ccd566 Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Thu, 10 Feb 2022 21:52:34 +0000 Subject: [PATCH] Add some unit tests for pixel conversions --- meson.build | 4 ++ meson_options.txt | 1 + test/meson.build | 11 +++++ test/test-pixels.c | 101 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 test/meson.build create mode 100644 test/test-pixels.c diff --git a/meson.build b/meson.build index fbc478d..5cfd533 100644 --- a/meson.build +++ b/meson.build @@ -147,6 +147,10 @@ if get_option('benchmarks') subdir('bench') endif +if get_option('tests') + subdir('test') +endif + install_headers('include/neatvnc.h') pkgconfig = import('pkgconfig') diff --git a/meson_options.txt b/meson_options.txt index ccc6785..c6cf9a3 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -1,5 +1,6 @@ option('benchmarks', type: 'boolean', value: false, description: 'Build benchmarks') option('examples', type: 'boolean', value: false, description: 'Build examples') +option('tests', type: 'boolean', value: false, description: 'Build unit tests') option('jpeg', type: 'feature', value: 'auto', description: 'Enable JPEG compression') option('tls', type: 'feature', value: 'auto', description: 'Enable encryption & authentication') option('systemtap', type: 'boolean', value: false, description: 'Enable tracing using sdt') diff --git a/test/meson.build b/test/meson.build new file mode 100644 index 0000000..a8d4621 --- /dev/null +++ b/test/meson.build @@ -0,0 +1,11 @@ +pixels = executable('pixels', + [ + 'test-pixels.c', + '../src/pixels.c', + ], + include_directories: inc, + dependencies: [ + pixman, + ], +) +test('pixels', pixels) diff --git a/test/test-pixels.c b/test/test-pixels.c new file mode 100644 index 0000000..c152e9d --- /dev/null +++ b/test/test-pixels.c @@ -0,0 +1,101 @@ +#include "pixels.h" + +#include +#include +#include +#include +#include + +#define swap32(x) (((x >> 24) & 0xff) | ((x << 8) & 0xff0000) | \ + ((x >> 8) & 0xff00) | ((x << 24) & 0xff000000)) + +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +#define u32_le(x) x +#else +#define u32_le(x) swap32(x) +#endif + +#define XSTR(s) STR(s) +#define STR(s) #s + +#define UDIV_UP(a, b) (((a) + (b) - 1) / (b)) +#define ARRAY_LEN(a) (sizeof(a) / (sizeof(a[0]))) + +static bool test_fourcc_to_pixman_fmt(void) +{ + pixman_format_code_t r; + +#define check(a, b) \ + r = 0; \ + if (!fourcc_to_pixman_fmt(&r, b) || a != r) { \ + fprintf(stderr, "Failed check for %s\n", XSTR(a)); \ + return false; \ + } while(0) + +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + check(PIXMAN_a2r10g10b10, DRM_FORMAT_ARGB2101010); + check(PIXMAN_r8g8b8a8, DRM_FORMAT_RGBA8888); + check(PIXMAN_b8g8r8a8, DRM_FORMAT_BGRA8888); + check(PIXMAN_r5g6b5, DRM_FORMAT_RGB565); +#else + check(PIXMAN_r8g8b8a8, DRM_FORMAT_ABGR8888); + check(PIXMAN_b8g8r8a8, DRM_FORMAT_ARGB8888); + check(PIXMAN_r5g6b5, DRM_FORMAT_BGR565); +#endif + +#undef check + return true; +} + +static bool test_extract_alpha_mask_rgba8888(void) +{ + uint32_t test_data[] = { + u32_le(0x00000000), // Black, transparent + u32_le(0xff000000), // Red, transparent + u32_le(0x00ff0000), // Red, transparent + u32_le(0x0000ff00), // Green, transparent + u32_le(0x000000ff), // Black, opaque + u32_le(0xff0000ff), // Red, opaque + u32_le(0x00ff00ff), // Red, opaque + u32_le(0x0000ffff), // Green, opaque + }; + + uint8_t mask[UDIV_UP(ARRAY_LEN(test_data), 8)] = { 0 }; + + bool ok = extract_alpha_mask(mask, test_data, DRM_FORMAT_RGBA8888, + ARRAY_LEN(test_data)); + if (!ok) { + fprintf(stderr, "Failed to extract alpha mask"); + return false; + } + + if (mask[0] != 0x0f) { + fprintf(stderr, "Expected alpha mask to be 0b00001111 (0x0f), but it was %x", + mask[0]); + return false; + } + + memset(mask, 0, sizeof(mask)); + + ok = extract_alpha_mask(mask, test_data, DRM_FORMAT_BGRA8888, + ARRAY_LEN(test_data)); + if (!ok) { + fprintf(stderr, "Failed to extract alpha mask"); + return false; + } + + if (mask[0] != 0x0f) { + fprintf(stderr, "Expected alpha mask to be 0b00001111 (0x0f), but it was %x", + mask[0]); + return false; + } + + return true; +} + +int main() +{ + bool ok = test_fourcc_to_pixman_fmt() && + test_extract_alpha_mask_rgba8888(); + return ok ? 0 : 1; +}