From bcf354989d4d9b62d3eba6260bea26e39dfbd7ae Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Wed, 27 Dec 2017 01:02:56 -0600 Subject: [PATCH] Tests for bitfont and screen --- tests/vm_bitfont.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++ tests/vm_screen.c | 45 +++++++++++++++++++++++++++++++--- 2 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 tests/vm_bitfont.c diff --git a/tests/vm_bitfont.c b/tests/vm_bitfont.c new file mode 100644 index 0000000..bf3632b --- /dev/null +++ b/tests/vm_bitfont.c @@ -0,0 +1,61 @@ +/* + * vm_bitfont.c + */ + +#include + +#include "vm_bitfont.h" + +static vm_bitfont *font; + +static void +setup() +{ + vm_screen *screen; + + screen = vm_screen_create(); + font = vm_bitfont_create(screen, "apple2-system", 7, 8, 0x7F); +} + +static void +teardown() +{ + vm_bitfont_free(font); +} + +TestSuite(vm_bitfont, .init = setup, .fini = teardown); + +Test(vm_bitfont, create) +{ + cr_assert_neq(font, NULL); + + cr_assert_eq(font->width, 7); + cr_assert_eq(font->height, 8); + cr_assert_eq(font->cmask, 0x7F); + + // This should be NULL because we have no screen + cr_assert_eq(font->texture, NULL); +} + +Test(vm_bitfont, offset) +{ + char ch = 'p'; + int x = 0; + int y = 0; + + vm_bitfont_offset(font, ch, &x, &y); + + cr_assert_eq(x, (ch & 0x0f) * font->width); + cr_assert_eq(y, (ch >> 4) * font->height); +} + +/* + * A note: I omitted a test for vm_bitfont_render(), as this function is + * a) reliant on vm_bitfont_offset for the right glyph to render, and b) + * is otherwise reliant on our graphics library to properly render the + * character. As we run our tests in a headless mode, we are unable to + * automate that specific kind of test. + */ +Test(vm_bitfont, render) +{ +} diff --git a/tests/vm_screen.c b/tests/vm_screen.c index 237d3d8..a61279c 100644 --- a/tests/vm_screen.c +++ b/tests/vm_screen.c @@ -1,11 +1,34 @@ +/* + * vm_screen.c + * + * Generally speaking the tests here are very incomplete; this is partly + * because a lot of the code in vm_screen depends on a third-party + * graphics library, but I think we can do a better job of decoupling + * some concepts (like screen areas) from the graphics library, such + * that we can make the code that uses those testable. + */ + #include #include "vm_screen.h" -Test(vm_screen, create) { - vm_screen *screen; +static vm_screen *screen; +static void +setup() +{ screen = vm_screen_create(); +} + +static void +teardown() +{ + vm_screen_free(screen); +} + +TestSuite(vm_screen, .init = setup, .fini = teardown); + +Test(vm_screen, create) { cr_assert_neq(screen, NULL); cr_assert_eq(screen->window, NULL); @@ -16,6 +39,20 @@ Test(vm_screen, create) { cr_assert_eq(screen->rect.h, 0); cr_assert_eq(screen->xcoords, 0); cr_assert_eq(screen->ycoords, 0); - - vm_screen_free(screen); +} + +Test(vm_screen, xcoords) +{ + screen->xcoords = 123; + cr_assert_eq(screen->xcoords, 123); + screen->xcoords = 234; + cr_assert_eq(screen->xcoords, 234); +} + +Test(vm_screen, ycoords) +{ + screen->ycoords = 123; + cr_assert_eq(screen->ycoords, 123); + screen->ycoords = 234; + cr_assert_eq(screen->ycoords, 234); }