gintctl/src/libs/printf.c

183 lines
5.0 KiB
C
Raw Normal View History

2020-06-17 16:10:03 +02:00
#include <gint/std/stdio.h>
#include <gint/std/string.h>
#include <gint/display.h>
#include <gint/keyboard.h>
#include <stdarg.h>
#include <stdint.h>
#include <gintctl/gint.h>
#include <gintctl/util.h>
#define SCROLL_HEIGHT _(8,12)
struct printf_test {
char const *format;
/* Type, argument, string, answer, passed are set by macros below */
enum { TYPE_I32, TYPE_U32, TYPE_STR, TYPE_PTR, TYPE_U64 } type;
union {
int i32;
uint32_t u32;
char const *str;
void *ptr;
uint64_t u64;
};
char const *argument_as_string;
char const *solution;
};
#define I32(i) TYPE_I32, { .i32 = i }, #i
#define U32(u) TYPE_U32, { .u32 = u }, #u
#define STR(s) TYPE_STR, { .str = s }, #s
#define PTR(p) TYPE_PTR, { .ptr = (void *)p }, #p
#define U64(u) TYPE_U64, { .u64 = u }, #u
2020-06-17 16:10:03 +02:00
static struct printf_test const tests[] = {
2020-06-17 16:10:03 +02:00
/* Base cases with length and precision */
{ "%d", I32(-849), "-849" },
{ "%7i", I32(78372), " 78372" },
{ "%3d", I32(65536), "65536" },
{ "%6.4d", I32(17), " 0017" },
{ "%6.3d", I32(-1876), " -1876" },
/* Sign */
{ "%+i", I32(15), "+15" },
{ "% 7i", I32(78372), " 78372" },
{ "% d", I32(65536), " 65536" },
/* Alignment */
{ "%-6.4d", I32(17), "0017 " },
{ "%-+6.4i", I32(17), "+0017 " },
/* Bases */
{ "%d", I32(0xcb7), "3255" },
{ "%x", U32(0xcb7), "cb7" },
{ "%X", U32(0xcb7), "CB7" },
{ "%o", U32(0xcb7), "6267" },
/* Argument size */
{ "%lld", U64(10000000000ll), "10000000000" },
{ "%llx", U64(0x123456789abull), "123456789ab" },
/* Alternative prefixes */
{ "%#x", U32(0x7b), "0x7b" },
{ "%#X", U32(0x7b), "0X7B" },
{ "%#o", U32(255), "0377" },
/* Pointers */
{ "%p", PTR(0xa44b0000), "0xa44b0000" },
/* Characters and strings */
{ "%s", STR("HellWrld!"), "HellWrld!" },
{ "%-8.5s", STR("Hello, World!"), "Hello " },
{ "%c", I32(100), "d" },
{ "%6c", I32('#'), " #", },
/* NULL terminator */
{ NULL }
};
static void run_tests(struct printf_test const *tests, char answers[][16],
int *passed)
2020-06-17 16:10:03 +02:00
{
for(int i = 0; tests[i].format; i++)
{
struct printf_test const *t = &tests[i];
2020-06-17 16:10:03 +02:00
#define run(TYPE, field) case TYPE: \
snprintf(answers[i], 32, t->format, t->field); \
2020-06-17 16:10:03 +02:00
break;
switch(t->type)
{
run(TYPE_I32, i32)
run(TYPE_U32, u32)
run(TYPE_STR, str)
run(TYPE_PTR, ptr)
run(TYPE_U64, u64)
}
passed[i] = !strcmp(answers[i], t->solution);
2020-06-17 16:10:03 +02:00
}
}
static void draw(struct printf_test const *tests, char answers[][16],
int *passed, int offset)
2020-06-17 16:10:03 +02:00
{
int total_passed=0, total=0;
2020-06-17 16:10:03 +02:00
for(int i = 0; tests[i].format; i++)
{
total_passed += passed[i];
2020-06-17 16:10:03 +02:00
total++;
}
dclear(C_WHITE);
#ifdef FX9860G
extern font_t font_hexa;
font_t const *old_font = dfont(&font_hexa);
2020-06-17 16:10:03 +02:00
dprint( 1, 0, C_BLACK, "ID");
dprint(13, 0, C_BLACK, "Format");
dprint(43, 0, C_BLACK, "Output");
dprint(91, 0, C_BLACK, "Valid");
2020-06-17 16:10:03 +02:00
for(int i = 0; i < SCROLL_HEIGHT; i++)
{
struct printf_test const *t = &tests[offset+i];
int y = (i+1) * 6;
dprint( 1, y, C_BLACK, "%d", offset+i+1);
dprint(13, y, C_BLACK, "%s", t->format);
dprint(43, y, C_BLACK, "%s", answers[offset+i]);
dprint(91, y, C_BLACK, "%s", passed[offset+i]?"Ok":"Err");
2020-06-17 16:10:03 +02:00
}
dfont(old_font);
row_print(8, 1, "Passed %d of %d.", total_passed, total);
2020-06-17 16:10:03 +02:00
#endif
#ifdef FXCG50
row_title("libc: Formatted printing functions");
row_print(1, 2, "ID");
row_print(1, 5, "Format");
row_print(1, 13, "Argument");
row_print(1, 29, "Answer");
for(int i = 0; i < SCROLL_HEIGHT; i++)
{
struct printf_test const *t = &tests[offset+i];
row_print(i+2, 2, "%d", offset+i+1);
row_print(i+2, 5, "%s", t->format);
row_print(i+2, 13, "%s", t->argument_as_string);
int fg = passed[offset+i] ? C_RGB(0,31,0) : C_RGB(31,0,0);
row_print_color(i+2, 29, fg, C_NONE, "%s", answers[offset+i]);
2020-06-17 16:10:03 +02:00
}
row_print(14, 1, "Passed: %d/%d", total_passed, total);
2020-06-17 16:10:03 +02:00
#endif
if(offset > 0) triangle_up(_(7,38));
if(offset < total - SCROLL_HEIGHT) triangle_down(_(49,192));
dupdate();
}
/* gintctl_libs_printf(): printf() function */
void gintctl_libs_printf(void)
{
key_event_t ev;
int key=0, total=0, offset=0;
for(int i = 0; tests[i].format; i++) total++;
int test_count = sizeof tests / sizeof tests[0];
char answers[test_count][16];
int passed[test_count];
run_tests(tests, answers, passed);
2020-06-17 16:10:03 +02:00
while(key != KEY_EXIT)
{
draw(tests, answers, passed, offset);
2020-06-17 16:10:03 +02:00
key = (ev = getkey()).key;
if(key == KEY_UP && offset > 0) offset--;
if(key == KEY_UP && ev.shift) offset = 0;
if(key == KEY_DOWN && offset < total - SCROLL_HEIGHT) offset++;
if(key == KEY_DOWN && ev.shift) offset = total - SCROLL_HEIGHT;
}
}