From b6bb688199abc342a6e85f80859c8cbb5a41b58a Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 23 Jan 2023 20:34:03 +0100 Subject: Add some tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- .gitignore | 1 + Makefile | 16 ++- TODO | 1 - common.h | 13 +++ config.mk | 4 +- libglitter.h | 8 +- libglitter_colour_space_convert_rasters_double.c | 14 +++ libglitter_compose_double.c | 18 +++- libglitter_compose_uint64.c | 14 +++ libglitter_create_render_context.c | 14 +++ libglitter_desaturate_double.c | 14 +++ libglitter_enable_acceleration.c | 60 ++++++++++++ libglitter_free_render_context.c | 15 +++ ...ter_get_colour_space_conversion_matrix_double.c | 15 +++ ...tter_get_colour_space_conversion_matrix_float.c | 14 +++ libglitter_per_channel_desaturate_double.c | 14 +++ libglitter_reorder_rasters.c | 38 +++++++ libglitter_split_uint64_raster.c | 109 ++++++++++++++++++++- libglitter_update_render_context.c | 14 +++ 19 files changed, 382 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index a071ed4..d8a83f0 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ *.gcov *.gcno *.gcda +*.test diff --git a/Makefile b/Makefile index 8ae2b95..d031557 100644 --- a/Makefile +++ b/Makefile @@ -44,9 +44,10 @@ HDR =\ libglitter.h LOBJ = $(OBJ:.o=.lo) +TESTS = $(OBJ:.o=.test) -all: libglitter.a libglitter.$(LIBEXT) +all: libglitter.a libglitter.$(LIBEXT) $(TESTS) $(OBJ): $(HDR) $(LOBJ): $(HDR) libglitter_compose_float.o: libglitter_compose_double.c @@ -57,6 +58,7 @@ libglitter_compose_uint8.o: libglitter_compose_uint64.c libglitter_compose_doubl libglitter_desaturate_float.o: libglitter_desaturate_double.c libglitter_per_channel_desaturate_float.o: libglitter_per_channel_desaturate_double.c libglitter_split_uint32_raster.o: libglitter_split_uint64_raster.c +$(TESTS): $(HDR) libglitter.a .c.o: $(CC) -c -o $@ $< $(CFLAGS) $(CPPFLAGS) @@ -64,6 +66,9 @@ libglitter_split_uint32_raster.o: libglitter_split_uint64_raster.c .c.lo: $(CC) -fPIC -c -o $@ $< $(CFLAGS) $(CPPFLAGS) +.c.test: + $(CC) -o $@ $< libglitter.a $(CFLAGS) $(CPPFLAGS) -DTEST $(LDFLAGS) + libglitter.a: $(OBJ) @rm -f -- $@ $(AR) rc $@ $(OBJ) @@ -72,6 +77,9 @@ libglitter.a: $(OBJ) libglitter.$(LIBEXT): $(LOBJ) $(CC) $(LIBFLAGS) -o $@ $(LOBJ) $(LDFLAGS) +check: $(TESTS) + @for t in $(TESTS); do printf './%s\n' $$t; ./$$t || exit 1; done + install: libglitter.a libglitter.$(LIBEXT) mkdir -p -- "$(DESTDIR)$(PREFIX)/lib" mkdir -p -- "$(DESTDIR)$(PREFIX)/include" @@ -90,10 +98,10 @@ uninstall: -rm -f -- "$(DESTDIR)$(PREFIX)/include/libglitter.h" clean: - -rm -f -- *.o *.a *.lo *.su *.so *.so.* *.dll *.dylib + -rm -f -- *.o *.a *.lo *.su *.so *.so.* *.dll *.dylib *.test -rm -f -- *.gch *.gcov *.gcno *.gcda *.$(LIBEXT) .SUFFIXES: -.SUFFIXES: .lo .o .c +.SUFFIXES: .lo .o .c .test -.PHONY: all install uninstall clean +.PHONY: all check install uninstall clean diff --git a/TODO b/TODO index 438b612..6c085d3 100644 --- a/TODO +++ b/TODO @@ -1,2 +1 @@ Add man pages -Add tests diff --git a/common.h b/common.h index 6566fd0..2252215 100644 --- a/common.h +++ b/common.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -30,4 +31,16 @@ struct libglitter_render_context { }; +#ifdef TEST + +# define ASSERT(ASSERTION)\ + do {\ + if (!(ASSERTION)) {\ + fprintf(stderr, "Failed assertion at line %i: %s\n", __LINE__, #ASSERTION);\ + exit(1);\ + }\ + } while (0) + +#endif + #endif diff --git a/config.mk b/config.mk index 64c9979..ef01e26 100644 --- a/config.mk +++ b/config.mk @@ -1,8 +1,8 @@ PREFIX = /usr MANPREFIX = $(PREFIX)/share/man -CC = cc +CC = c99 CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_GNU_SOURCE -CFLAGS = -std=c99 -Wall -g +CFLAGS = -Wall -g LDFLAGS = -lm diff --git a/libglitter.h b/libglitter.h index 79af0fd..0bc65b1 100644 --- a/libglitter.h +++ b/libglitter.h @@ -469,16 +469,16 @@ void libglitter_get_colour_space_conversion_matrix_float(float[3][3], float, flo * @param height The vertical number of pixels in the rasters * @param matrix Colour space conversion matrix, in column-major order */ -void libglitter_colour_space_convert_rasters_double(size_t n, size_t m, double **, const double **, - size_t, size_t, size_t, size_t, size_t, size_t, const double[n][m]); +void libglitter_colour_space_convert_rasters_double(size_t n, size_t m, double **, const double **, size_t, + size_t, size_t, size_t, size_t, size_t, const double[n][m]); /** * This value is identical to `libglitter_colour_space_convert_rasters_double`, * apart from it parameter types, see `libglitter_colour_space_convert_rasters_double` * for details about this function */ -void libglitter_colour_space_convert_rasters_float(size_t n, size_t m, float **, const float **, - size_t, size_t, size_t, size_t, size_t, size_t, const float[n][m]); +void libglitter_colour_space_convert_rasters_float(size_t n, size_t m, float **, const float **, size_t, + size_t, size_t, size_t, size_t, size_t, const float[n][m]); #endif diff --git a/libglitter_colour_space_convert_rasters_double.c b/libglitter_colour_space_convert_rasters_double.c index 3923911..8fa497e 100644 --- a/libglitter_colour_space_convert_rasters_double.c +++ b/libglitter_colour_space_convert_rasters_double.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "common.h" +#ifndef TEST static void @@ -73,3 +74,16 @@ libglitter_colour_space_convert_rasters_double(size_t n, size_t m, double **outp multiply_nm(n, m, outputs, output_i, inputs, input_i, matrix, buffer); } } + + +#else + + +int +main(void) +{ + return 0; /* TODO add test */ +} + + +#endif diff --git a/libglitter_compose_double.c b/libglitter_compose_double.c index 5a8d361..53da733 100644 --- a/libglitter_compose_double.c +++ b/libglitter_compose_double.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "common.h" +#ifndef TEST static void @@ -56,7 +57,7 @@ hstrips(double **outputs_, const double *restrict input, size_t output_rowsize, } -#ifndef ONLY_INT_COMPATIBLE +# ifndef ONLY_INT_COMPATIBLE static void @@ -109,4 +110,19 @@ libglitter_compose_double(double **outputs, const double *restrict input, size_t } +# endif + + +#else +# ifndef ONLY_INT_COMPATIBLE + + +int +main(void) +{ + return 0; /* TODO add test */ +} + + +# endif #endif diff --git a/libglitter_compose_uint64.c b/libglitter_compose_uint64.c index 91d6b88..6784055 100644 --- a/libglitter_compose_uint64.c +++ b/libglitter_compose_uint64.c @@ -9,6 +9,7 @@ # define greater_t double # define MIX(A, B) (((A) >> 1) + ((B) >> 1) + ((A) & (B) & 1)) #endif +#ifndef TEST static void @@ -90,3 +91,16 @@ libglitter_compose_uint64(uint64_t **outputs, const uint64_t *restrict input, si render_ctx->cellmap, render_ctx->ncellvalues); } } + + +#else + + +int +main(void) +{ + return 0; /* TODO add test */ +} + + +#endif diff --git a/libglitter_create_render_context.c b/libglitter_create_render_context.c index 53b0705..7898ff8 100644 --- a/libglitter_create_render_context.c +++ b/libglitter_create_render_context.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "common.h" +#ifndef TEST LIBGLITTER_RENDER_CONTEXT * @@ -45,3 +46,16 @@ fail: libglitter_free_render_context(this); return NULL; } + + +#else + + +int +main(void) +{ + return 0; /* TODO add test */ +} + + +#endif diff --git a/libglitter_desaturate_double.c b/libglitter_desaturate_double.c index 43fc990..ba588f4 100644 --- a/libglitter_desaturate_double.c +++ b/libglitter_desaturate_double.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "common.h" +#ifndef TEST #if defined(__GNUC__) && !defined(__clang__) @@ -46,3 +47,16 @@ libglitter_desaturate_double(double **rasters, size_t nrasters, size_t rowsize, } } } + + +#else + + +int +main(void) +{ + return 0; /* TODO add test */ +} + + +#endif diff --git a/libglitter_enable_acceleration.c b/libglitter_enable_acceleration.c index f012da9..791fe71 100644 --- a/libglitter_enable_acceleration.c +++ b/libglitter_enable_acceleration.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "common.h" +#ifndef TEST int @@ -15,3 +16,62 @@ libglitter_enable_acceleration(uint64_t features, int async, void (*callback)(in return 0; } + + +#else + + +static size_t callback_calls = 0; + +static void +callback(int ret, int err, void *user) +{ + callback_calls++; + ASSERT(!ret); + ASSERT(!err); + ASSERT(user); + ASSERT(*(const char *)user == 'u'); +} + +static void +callback_null(int ret, int err, void *user) +{ + callback_calls++; + ASSERT(!ret); + ASSERT(!err); + ASSERT(!user); +} +int +main(void) +{ + char data = 'u'; + ASSERT(!libglitter_enable_acceleration(0, 0, NULL, NULL)); + ASSERT(!libglitter_enable_acceleration(0, 1, NULL, NULL)); + ASSERT(!libglitter_enable_acceleration(~0, 0, NULL, NULL)); + ASSERT(!libglitter_enable_acceleration(~0, 1, NULL, NULL)); + ASSERT(!libglitter_enable_acceleration(0, 0, NULL, &data)); + ASSERT(!libglitter_enable_acceleration(0, 1, NULL, &data)); + ASSERT(!libglitter_enable_acceleration(~0, 0, NULL, &data)); + ASSERT(!libglitter_enable_acceleration(~0, 1, NULL, &data)); + ASSERT(callback_calls == 0); + ASSERT(!libglitter_enable_acceleration(0, 0, callback, &data)); + ASSERT(callback_calls == 1); + ASSERT(!libglitter_enable_acceleration(0, 1, callback, &data)); + ASSERT(callback_calls == 2); + ASSERT(!libglitter_enable_acceleration(~0, 0, callback, &data)); + ASSERT(callback_calls == 3); + ASSERT(!libglitter_enable_acceleration(~0, 1, callback, &data)); + ASSERT(callback_calls == 4); + ASSERT(!libglitter_enable_acceleration(0, 0, callback_null, NULL)); + ASSERT(callback_calls == 5); + ASSERT(!libglitter_enable_acceleration(0, 1, callback_null, NULL)); + ASSERT(callback_calls == 6); + ASSERT(!libglitter_enable_acceleration(~0, 0, callback_null, NULL)); + ASSERT(callback_calls == 7); + ASSERT(!libglitter_enable_acceleration(~0, 1, callback_null, NULL)); + ASSERT(callback_calls == 8); + return 0; +} + + +#endif diff --git a/libglitter_free_render_context.c b/libglitter_free_render_context.c index 4eb15c2..05f27eb 100644 --- a/libglitter_free_render_context.c +++ b/libglitter_free_render_context.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "common.h" +#ifndef TEST void @@ -11,3 +12,17 @@ libglitter_free_render_context(LIBGLITTER_RENDER_CONTEXT *this) free(this); } } + + +#else + + +int +main(void) +{ + libglitter_free_render_context(NULL); + return 0; +} + + +#endif diff --git a/libglitter_get_colour_space_conversion_matrix_double.c b/libglitter_get_colour_space_conversion_matrix_double.c index e0f6aa1..285f625 100644 --- a/libglitter_get_colour_space_conversion_matrix_double.c +++ b/libglitter_get_colour_space_conversion_matrix_double.c @@ -1,5 +1,7 @@ /* See LICENSE file for copyright and license details. */ #include "common.h" +#ifndef TEST + #define X(x, y) ((x) / (y)) #define Z(x, y) ((1 - (x)) / (y) - 1) @@ -96,3 +98,16 @@ libglitter_get_colour_space_conversion_matrix_double(double matrix[3][3], double matrix[0][2] = z1, matrix[1][2] = z2, matrix[2][2] = z3; } } + + +#else + + +int +main(void) +{ + return 0; /* TODO add test */ +} + + +#endif diff --git a/libglitter_get_colour_space_conversion_matrix_float.c b/libglitter_get_colour_space_conversion_matrix_float.c index 6e215c9..6f71baf 100644 --- a/libglitter_get_colour_space_conversion_matrix_float.c +++ b/libglitter_get_colour_space_conversion_matrix_float.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "common.h" +#ifndef TEST void @@ -23,3 +24,16 @@ libglitter_get_colour_space_conversion_matrix_float(float matrix[3][3], float c1 matrix[2][1] = (float)double_matrix[2][1]; matrix[2][2] = (float)double_matrix[2][2]; } + + +#else + + +int +main(void) +{ + return 0; /* TODO add test */ +} + + +#endif diff --git a/libglitter_per_channel_desaturate_double.c b/libglitter_per_channel_desaturate_double.c index 04edbf3..1f2fcdc 100644 --- a/libglitter_per_channel_desaturate_double.c +++ b/libglitter_per_channel_desaturate_double.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "common.h" +#ifndef TEST #if defined(__GNUC__) && !defined(__clang__) @@ -47,3 +48,16 @@ libglitter_per_channel_desaturate_double(double **rasters, size_t nrasters, size } } } + + +#else + + +int +main(void) +{ + return 0; /* TODO add test */ +} + + +#endif diff --git a/libglitter_reorder_rasters.c b/libglitter_reorder_rasters.c index 5df590e..c56873c 100644 --- a/libglitter_reorder_rasters.c +++ b/libglitter_reorder_rasters.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "common.h" +#ifndef TEST void @@ -13,3 +14,40 @@ libglitter_reorder_rasters(void **rasters, enum libglitter_colour colour1, rasters[1] = channel2; rasters[2] = channel3; } + + +#else + + +int +main(void) +{ + const char r = 'R', g = 'G', b = 'B'; + const void *rasters[3]; + +#define T(CH1, CH2, CH3)\ + do {\ + rasters[0] = &r;\ + rasters[1] = &g;\ + rasters[2] = &b;\ + libglitter_reorder_rasters((void *)rasters,\ + LIBGLITTER_CHANNEL_##CH1,\ + LIBGLITTER_CHANNEL_##CH2,\ + LIBGLITTER_CHANNEL_##CH3);\ + ASSERT(*(const char *)rasters[0] == *#CH1);\ + ASSERT(*(const char *)rasters[1] == *#CH2);\ + ASSERT(*(const char *)rasters[2] == *#CH3);\ + } while (0) + + T(RED, GREEN, BLUE); + T(RED, BLUE, GREEN); + T(GREEN, RED, BLUE); + T(GREEN, BLUE, RED); + T(BLUE, RED, GREEN); + T(BLUE, GREEN, RED); + + return 0; +} + + +#endif diff --git a/libglitter_split_uint64_raster.c b/libglitter_split_uint64_raster.c index a831ef7..75ec90b 100644 --- a/libglitter_split_uint64_raster.c +++ b/libglitter_split_uint64_raster.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "common.h" +#ifndef TEST void @@ -8,7 +9,7 @@ libglitter_split_uint64_raster(uint16_t *rasters[3], uint16_t **alphap, uint64_t { uint64_t alpha, map; size_t position; - size_t channel; + unsigned char channel; alpha = ~(red | green | blue); red /= 0xFF; @@ -22,10 +23,114 @@ libglitter_split_uint64_raster(uint16_t *rasters[3], uint16_t **alphap, uint64_t map = red | green | blue | alpha; for (position = 0; position < 4; position++) { - channel = ((const unsigned char *)&map)[position * 2]; + channel = ((const unsigned char *)&map)[position * sizeof(uint16_t)]; if (channel < 3) rasters[channel] = &((uint16_t *)raster)[position]; else if (alphap) *alphap = &((uint16_t *)raster)[position]; } } + + +#else + + +static void +check(int with_alpha, int r, int g, int b, int a) +{ + int red_shift = r * sizeof(uint16_t) * 8; + int green_shift = g * sizeof(uint16_t) * 8; + int blue_shift = b * sizeof(uint16_t) * 8; + int alpha_shift = a * sizeof(uint16_t) * 8; + uint16_t full = ~(uint16_t)0; + uint16_t red = (uint16_t)~0 / (uint16_t)0xF * (uint16_t)0x5; + uint16_t green = (uint16_t)~0 / (uint16_t)0xF * (uint16_t)0x8; + uint16_t blue = (uint16_t)~0 / (uint16_t)0xF * (uint16_t)0xA; + uint16_t alpha = (uint16_t)~0 / (uint16_t)0xF * (uint16_t)0x9; + uint64_t red_mask = (uint64_t)red << red_shift; + uint64_t green_mask = (uint64_t)green << green_shift; + uint64_t blue_mask = (uint64_t)blue << blue_shift; + uint64_t alpha_mask = (uint64_t)alpha << alpha_shift; + uint64_t raster = red_mask | green_mask | blue_mask | alpha_mask; + uint16_t *rasters[4] = {NULL, NULL, NULL, NULL}; + + ASSERT(r != g && r != b && r != a); + ASSERT(g != b && g != a); + ASSERT(b != a); + + libglitter_split_uint64_raster(rasters, + with_alpha ? &rasters[3] : NULL, + &raster, + (uint64_t)full << red_shift, + (uint64_t)full << green_shift, + (uint64_t)full << blue_shift); + ASSERT(rasters[0]); + ASSERT(rasters[1]); + ASSERT(rasters[2]); + ASSERT(*rasters[0] == red); + ASSERT(*rasters[1] == green); + ASSERT(*rasters[2] == blue); + if (with_alpha) { + ASSERT(rasters[3]); + ASSERT(*rasters[3] == alpha); + } else { + ASSERT(!rasters[3]); + } +} + +int +main(void) +{ + check(0, 0, 1, 2, 3); + check(0, 0, 1, 3, 2); + check(0, 0, 2, 1, 3); + check(0, 0, 2, 3, 1); + check(0, 0, 3, 1, 2); + check(0, 0, 3, 2, 1); + check(0, 1, 0, 2, 3); + check(0, 1, 0, 3, 2); + check(0, 1, 2, 0, 3); + check(0, 1, 2, 3, 0); + check(0, 1, 3, 0, 2); + check(0, 1, 3, 2, 0); + check(0, 2, 0, 1, 3); + check(0, 2, 0, 3, 1); + check(0, 2, 1, 0, 3); + check(0, 2, 1, 3, 0); + check(0, 2, 3, 0, 1); + check(0, 2, 3, 1, 0); + check(0, 3, 0, 1, 2); + check(0, 3, 0, 2, 1); + check(0, 3, 1, 0, 2); + check(0, 3, 1, 2, 0); + check(0, 3, 2, 0, 1); + check(0, 3, 2, 1, 0); + check(1, 0, 1, 2, 3); + check(1, 0, 1, 3, 2); + check(1, 0, 2, 1, 3); + check(1, 0, 2, 3, 1); + check(1, 0, 3, 1, 2); + check(1, 0, 3, 2, 1); + check(1, 1, 0, 2, 3); + check(1, 1, 0, 3, 2); + check(1, 1, 2, 0, 3); + check(1, 1, 2, 3, 0); + check(1, 1, 3, 0, 2); + check(1, 1, 3, 2, 0); + check(1, 2, 0, 1, 3); + check(1, 2, 0, 3, 1); + check(1, 2, 1, 0, 3); + check(1, 2, 1, 3, 0); + check(1, 2, 3, 0, 1); + check(1, 2, 3, 1, 0); + check(1, 3, 0, 1, 2); + check(1, 3, 0, 2, 1); + check(1, 3, 1, 0, 2); + check(1, 3, 1, 2, 0); + check(1, 3, 2, 0, 1); + check(1, 3, 2, 1, 0); + return 0; +} + + +#endif diff --git a/libglitter_update_render_context.c b/libglitter_update_render_context.c index 318ec87..39e5146 100644 --- a/libglitter_update_render_context.c +++ b/libglitter_update_render_context.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "common.h" +#ifndef TEST void @@ -26,3 +27,16 @@ libglitter_update_render_context(LIBGLITTER_RENDER_CONTEXT *this, size_t rowsize this->cells[2][1] = this->cells[2][0]; } } + + +#else + + +int +main(void) +{ + return 0; /* TODO add test */ +} + + +#endif -- cgit v1.2.3-70-g09d2