calculator/tests/charness.c
2025-03-09 21:01:58 +10:00

55 lines
1.2 KiB
C

#include "../calc.h"
#define BUF_SIZE 80
#define make_str(s) { sizeof(s) - 1, (const u8 *)s }
#define make_test(name) { make_str(#name), name }
extern bool streq(const u8 *, uint, const u8 *, uint);
extern int read(u8 *, uint); // read from stdin
extern void exit(int);
// Tests
extern void count_digits(void);
extern void fmt_decimal(void);
typedef struct {
u8 len;
const u8 *data;
} Str;
typedef void (*Testfn)(void);
typedef struct {
Str name;
Testfn fn;
} Test;
static Test tests[3] = {
make_test(count_digits),
make_test(fmt_decimal),
{ { 0, NULL }, NULL }
};
void _start(void) {
u8 buf[BUF_SIZE];
int nread0 = read(buf, BUF_SIZE);
if (nread0 < 0) {
for(;;); // TODO: add abort function
}
uint nread = (uint)nread0;
if (nread > 0 && buf[nread - 1] == '\n') {
nread--; // ignore trailing newline
}
Test t;
uint i;
for(i = 0, t = tests[i]; t.name.data != NULL; i++, t = tests[i]) {
if (streq(buf, nread, t.name.data, t.name.len)) {
t.fn();
exit(0);
}
}
exit(2);
}