From 9a36ae7c1e29e20cb84a768aacc3ff2296577f96 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 8 Jan 2023 11:06:37 +0100 Subject: Fix and test libfonts_get_default_font_name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- Makefile | 1 + libfonts.h | 1 - libfonts_get_default_font_name.c | 79 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index cc2a08b..86e2ab9 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,7 @@ OBJ =\ TESTS =\ libfonts_calculate_subpixel_order.test\ + libfonts_get_default_font_name.test\ libfonts_get_subpixel_order_class.test\ libfonts_unget_subpixel_order_class.test diff --git a/libfonts.h b/libfonts.h index 326f301..06f8d48 100644 --- a/libfonts.h +++ b/libfonts.h @@ -1619,7 +1619,6 @@ ssize_t libfonts_get_default_font(enum libfonts_default_font, char *, size_t); * @throws EINVAL Unrecognised value on `font` */ ssize_t libfonts_get_default_font_name(enum libfonts_default_font, char *, size_t, size_t); -/* TODO add test */ /** * Get a default font a font name represents diff --git a/libfonts_get_default_font_name.c b/libfonts_get_default_font_name.c index 5fe7704..9165d8a 100644 --- a/libfonts_get_default_font_name.c +++ b/libfonts_get_default_font_name.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "common.h" +#ifndef TEST ssize_t @@ -45,5 +46,81 @@ libfonts_get_default_font_name(enum libfonts_default_font font, char *name, size memcpy(name, alias, size); name[size] = '\0'; } - return (ssize_t)len; + return (ssize_t)(len + 1); } + + +#else + + +int +main(void) +{ +#define T(FONT, INDEX, SIZE, NAME, NAMELEN)\ + do {\ + errno = 0;\ + ASSERT(libfonts_get_default_font_name(FONT, buf, SIZE, INDEX) == NAMELEN);\ + ASSERT(!buf[SIZE - 1]);\ + ASSERT(!strcmp(buf, NAME));\ + ASSERT(!errno);\ + } while (0) + +#define X(FONT, INDEX, SIZE)\ + do {\ + errno = 0;\ + ASSERT(libfonts_get_default_font_name(FONT, buf, SIZE, INDEX) == 0);\ + ASSERT(!buf[SIZE - 1]);\ + ASSERT(!errno);\ + } while (0) + + char buf[50]; + + errno = 0; + ASSERT(libfonts_get_default_font_name(-1, buf, sizeof(buf), 0) == -1); + ASSERT(errno == EINVAL); + + errno = 0; + ASSERT(libfonts_get_default_font_name(99, buf, sizeof(buf), 0) == -1); + ASSERT(errno == EINVAL); + + T(LIBFONTS_DEFAULT_SANS_SERIF, 0, sizeof(buf), "sans-serif", 11); + T(LIBFONTS_DEFAULT_SANS_SERIF, 1, sizeof(buf), "sans serif", 11); + T(LIBFONTS_DEFAULT_SANS_SERIF, 2, sizeof(buf), "sans", 5); + X(LIBFONTS_DEFAULT_SANS_SERIF, 3, sizeof(buf)); + + T(LIBFONTS_DEFAULT_SERIF, 0, sizeof(buf), "serif", 6); + X(LIBFONTS_DEFAULT_SERIF, 1, sizeof(buf)); + + T(LIBFONTS_DEFAULT_MONOSPACE, 0, sizeof(buf), "monospace", 10); + T(LIBFONTS_DEFAULT_MONOSPACE, 1, sizeof(buf), "monospaced", 11); + X(LIBFONTS_DEFAULT_MONOSPACE, 2, sizeof(buf)); + + T(LIBFONTS_DEFAULT_SANS_SERIF, 0, 4, "san", 11); + T(LIBFONTS_DEFAULT_SANS_SERIF, 1, 4, "san", 11); + T(LIBFONTS_DEFAULT_SANS_SERIF, 2, 4, "san", 5); + X(LIBFONTS_DEFAULT_SANS_SERIF, 3, 4); + + T(LIBFONTS_DEFAULT_SERIF, 0, 4, "ser", 6); + X(LIBFONTS_DEFAULT_SERIF, 1, 4); + + T(LIBFONTS_DEFAULT_MONOSPACE, 0, 4, "mon", 10); + T(LIBFONTS_DEFAULT_MONOSPACE, 1, 4, "mon", 11); + X(LIBFONTS_DEFAULT_MONOSPACE, 2, 4); + + errno = 0; + + ASSERT(libfonts_get_default_font_name(LIBFONTS_DEFAULT_SANS_SERIF, NULL, 0, 0) == 11); + ASSERT(libfonts_get_default_font_name(LIBFONTS_DEFAULT_SERIF, NULL, 0, 0) == 6); + ASSERT(libfonts_get_default_font_name(LIBFONTS_DEFAULT_MONOSPACE, NULL, 0, 0) == 10); + + ASSERT(libfonts_get_default_font_name(LIBFONTS_DEFAULT_SANS_SERIF, NULL, 0, 3) == 0); + ASSERT(libfonts_get_default_font_name(LIBFONTS_DEFAULT_SERIF, NULL, 0, 1) == 0); + ASSERT(libfonts_get_default_font_name(LIBFONTS_DEFAULT_MONOSPACE, NULL, 0, 2) == 0); + + ASSERT(!errno); + + return 0; +} + + +#endif -- cgit v1.2.3-70-g09d2