diff options
author | Mattias Andrée <maandree@kth.se> | 2023-01-08 11:06:37 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2023-01-08 11:06:37 +0100 |
commit | 9a36ae7c1e29e20cb84a768aacc3ff2296577f96 (patch) | |
tree | 3c5382eb5e9301a4b73dc183c366c89cca3a9fa9 | |
parent | m (diff) | |
download | libfonts-9a36ae7c1e29e20cb84a768aacc3ff2296577f96.tar.gz libfonts-9a36ae7c1e29e20cb84a768aacc3ff2296577f96.tar.bz2 libfonts-9a36ae7c1e29e20cb84a768aacc3ff2296577f96.tar.xz |
Fix and test libfonts_get_default_font_name
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | libfonts.h | 1 | ||||
-rw-r--r-- | libfonts_get_default_font_name.c | 79 |
3 files changed, 79 insertions, 2 deletions
@@ -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 @@ -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 |