diff options
author | metamuffin <metamuffin@disroot.org> | 2024-10-09 09:52:11 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-10-09 09:52:11 +0200 |
commit | 1109973fc1aae971439331c76eb46cbaf976690b (patch) | |
tree | c55826d4cca08387725ddef706b05687c654f850 | |
parent | 32b5bc923add402eb33555157d329e1d379583c9 (diff) | |
download | attocc-1109973fc1aae971439331c76eb46cbaf976690b.tar attocc-1109973fc1aae971439331c76eb46cbaf976690b.tar.bz2 attocc-1109973fc1aae971439331c76eb46cbaf976690b.tar.zst |
unit testing
-rw-r--r-- | attocc.c | 54 | ||||
-rw-r--r-- | makefile | 10 |
2 files changed, 50 insertions, 14 deletions
@@ -18,8 +18,14 @@ #include <fcntl.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <unistd.h> +#ifdef LINT +#define TEST +#define DEBUG +#endif + const int NUM_KEYWORDS = 32; static char *KEYWORDS[] = { "auto", "break", "case", "char", "const", "continue", @@ -275,8 +281,8 @@ int strncmp(const char *a, const char *b, unsigned long len) { unsigned long strlen(const char *s) { int len = 0; - while (s[len++] != 0) - ; + while (*s++) + len++; return len - 1; } char *strdup(const char *s) { @@ -311,14 +317,12 @@ struct token *tokenize(char *source) { while (*p) { unsigned long remaining = end - p; printf("%li\n", p - source); - printf("a\n"); //* whitespace if (p[0] == ' ' || p[0] == '\t' || p[0] == '\n') { p++; continue; } - printf("b\n"); //* comments if (remaining >= 2) { @@ -339,7 +343,6 @@ struct token *tokenize(char *source) { continue; } } - printf("c\n"); //* seperators char match = 0; @@ -358,14 +361,10 @@ struct token *tokenize(char *source) { if (match) continue; - printf("ca\n"); //* operators match = 0; for (int i = 0; i < NUM_OPERATORS; i++) { - printf("caa\n"); char *op = OPERATORS[i]; - printf("cab\n"); - printf("%li %s\n", strlen(op), op); if (remaining >= strlen(op)) { if (strncmp(op, p, strlen(op)) == 0) { struct token *new_token = token_push(&tokens, &num_tokens); @@ -380,10 +379,8 @@ struct token *tokenize(char *source) { } } } - printf("cb\n"); if (match) continue; - printf("d\n"); //* keyword match = 0; @@ -437,7 +434,6 @@ struct token *tokenize(char *source) { new_token->data.constant_int_value = value; continue; } - printf("e\n"); //* string constant if (p[0] == '"') { @@ -483,7 +479,6 @@ struct token *tokenize(char *source) { new_token->data.constant_char_value = chr; continue; } - printf("f\n"); //* identifier if (is_ident(p[0]) && !is_numeric(p[0])) { @@ -747,7 +742,40 @@ void debug_node(struct node *node) { } #endif +#ifdef TEST +char test_clean = 1; +void assert(char cond, char *label) { + if (!cond) { + fprintf(stderr, "FAIL %s\n", label); + test_clean = 0; + } +} +void assert_eq(int a, int b, char *label) { + if (a != b) { + fprintf(stderr, "FAIL %s (%i == %i)\n", label, a, b); + test_clean = 0; + } +} +void test() { + assert_eq(strlen("sizeof"), 6, "strlen 1"); + assert_eq(strlen(""), 0, "strlen 2"); + assert_eq(strlen("a"), 1, "strlen 3"); + assert(is_alpha('a'), "alpha 1"); + assert(is_alpha('z'), "alpha 2"); + assert(is_alpha('k'), "alpha 3"); + assert(!!tokenize("enum"), "tok 1"); + assert(!!tokenize("int x = 0"), "tok 1"); + // assert(!!tokenize("\"Hello\""), "tok 1"); + assert(!!tokenize("'\n'"), "tok 1"); +} +#endif + int main(int argc, char **argv) { +#ifdef TEST + test(); + return test_clean ? 0 : 1; +#endif + if (argc < 3) { fprintf(stderr, "USAGE:\n\tattocc <input> <output>\n"); return 1; @@ -1,8 +1,11 @@ -.PHONY: all clean +.PHONY: all clean test ALL = attocc attocc-small attocc-debug all: $(ALL) clean: rm $(ALL) +test: attocc-test attocc-test-opt + ./attocc-test + ./attocc-test-opt attocc: attocc.c cc -DDEBUG -O3 -o $@ $< @@ -13,3 +16,8 @@ attocc-debug: attocc.c attocc-small: attocc.c cc -DDEBUG -Wl,--gc-sections -flto -ffunction-sections -fdata-sections -fPIC -s -Os -o $@ $< strip -x $@ + +attocc-test: attocc.c + cc -DDEBUG -DTEST -o $@ $< +attocc-test-opt: attocc.c + cc -DDEBUG -DTEST -O3 -o $@ $< |