summaryrefslogtreecommitdiff
path: root/test/test-glyphs.c
diff options
context:
space:
mode:
Diffstat (limited to 'test/test-glyphs.c')
-rw-r--r--test/test-glyphs.c219
1 files changed, 0 insertions, 219 deletions
diff --git a/test/test-glyphs.c b/test/test-glyphs.c
deleted file mode 100644
index 46e4529..0000000
--- a/test/test-glyphs.c
+++ /dev/null
@@ -1,219 +0,0 @@
-#include "test-common.h"
-
-static int font_open(void)
-{
- int fd;
-
- fd = open(PKGDATADIR "/mmap-unifont.bin", O_RDONLY | O_CLOEXEC);
- ck_assert_int_ge(fd, 0);
-
- return fd;
-}
-
-static void font_close(int fd)
-{
- int r;
-
- r = close(fd);
- ck_assert_int_ge(r, 0);
-}
-
-static void *font_mmap(int fd, size_t *out_size)
-{
- struct stat st;
- void *p;
- int r;
-
- r = fstat(fd, &st);
- ck_assert_int_ge(r, 0);
-
- p = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
- ck_assert_ptr_ne(p, MAP_FAILED);
-
- if (out_size)
- *out_size = st.st_size;
-
- return p;
-}
-
-static void font_unmap(void *p, size_t size)
-{
- int r;
-
- r = munmap(p, size);
- ck_assert_int_ge(r, 0);
-}
-
-static int font_fd = -1;
-static void *font_p = NULL;
-static size_t font_size = 0;
-
-static void font_init(void)
-{
- font_fd = font_open();
- font_p = font_mmap(font_fd, &font_size);
-}
-
-static void font_destroy(void)
-{
- if (font_fd < 0)
- return;
-
- font_unmap(font_p, font_size);
- font_close(font_fd);
- font_fd = -1;
-}
-
-START_TEST(test_misc_setup)
-{
- int fd;
- void *p;
- size_t size;
-
- fd = font_open();
- p = font_mmap(fd, &size);
- font_unmap(p, size);
- font_close(fd);
-
- font_init();
- ck_assert_int_ge(font_fd, 0);
- font_destroy();
- ck_assert_int_lt(font_fd, 0);
- font_destroy();
- ck_assert_int_lt(font_fd, 0);
-}
-END_TEST
-
-static void font_render(char *w, const uint8_t *glyph, size_t width)
-{
- unsigned int i, j;
-
- for (j = 0; j < 16; ++j) {
- for (i = 0; i < 8 * width; ++i) {
- if (glyph[i / 8] & (1 << (7 - i % 8)))
- *w++ = '#';
- else
- *w++ = ' ';
- }
- *w++ = '\n';
- glyph += 1 * width;
- }
-
- *w++ = 0;
-}
-
-START_TEST(test_misc_draw)
-{
- char buf[1024];
- const uint8_t *p, *end, *glyph;
- size_t size, width;
- uint32_t header_size, glyph_size;
-
- font_init();
-
- p = font_p;
- size = font_size;
- end = p + font_size;
-
- /* parse 4-byte header-size */
- {
- ck_assert_int_ge(size, 4);
- header_size = *(uint32_t*)p;
- header_size = le32toh(header_size);
- p += sizeof(uint32_t);
- }
-
- /* parse header */
- {
- glyph_size = 33;
-
- switch (header_size) {
- default:
- /* fallthrough */
- case 4:
- glyph_size = *(uint32_t*)p;
- glyph_size = le32toh(glyph_size);
- /* fallthrough */
- case 0 ... 3:
- break;
- }
-
- ck_assert_int_ge(glyph_size, 33);
- }
-
- /* forward @p to end of header */
- p += header_size;
-
- /* access glyph 'A' */
- glyph = p + glyph_size * 'A';
- ck_assert(end >= glyph + glyph_size);
- width = *glyph++;
- ck_assert_int_eq(width, 1);
-
- /* draw glyph 'A' */
- font_render(buf, glyph, width);
- ck_assert(!strcmp(buf,
- " \n"
- " \n"
- " \n"
- " \n"
- " ## \n"
- " # # \n"
- " # # \n"
- " # # \n"
- " # # \n"
- " ###### \n"
- " # # \n"
- " # # \n"
- " # # \n"
- " # # \n"
- " \n"
- " \n"
- ));
-
- /* access wide glyph 0x4ec0 ('什') */
- glyph = p + glyph_size * 0x4ec0;
- ck_assert(end >= glyph + glyph_size);
- width = *glyph++;
- ck_assert_int_eq(width, 2);
-
- /* draw wide glyph */
- font_render(buf, glyph, width);
- ck_assert(!strcmp(buf,
- " # # \n"
- " # # \n"
- " # # \n"
- " # # \n"
- " # # \n"
- " ## # \n"
- " ## ########## \n"
- " # # # \n"
- "# # # \n"
- " # # \n"
- " # # \n"
- " # # \n"
- " # # \n"
- " # # \n"
- " # # \n"
- " # # \n"
- ));
-
- /* close font */
- font_destroy();
-}
-END_TEST
-
-TEST_DEFINE_CASE(misc)
- TEST(test_misc_setup)
- TEST(test_misc_draw)
-TEST_END_CASE
-
-int main(int argc, char **argv)
-{
- if (access(PKGDATADIR "/mmap-unifont.bin", F_OK))
- return 77;
-
- return test_run_suite(TEST_SUITE(glyphs,
- TEST_CASE(misc),
- TEST_END));
-}