summaryrefslogtreecommitdiff
path: root/attocc.c
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2024-10-09 09:52:11 +0200
committermetamuffin <metamuffin@disroot.org>2024-10-09 09:52:11 +0200
commit1109973fc1aae971439331c76eb46cbaf976690b (patch)
treec55826d4cca08387725ddef706b05687c654f850 /attocc.c
parent32b5bc923add402eb33555157d329e1d379583c9 (diff)
downloadattocc-1109973fc1aae971439331c76eb46cbaf976690b.tar
attocc-1109973fc1aae971439331c76eb46cbaf976690b.tar.bz2
attocc-1109973fc1aae971439331c76eb46cbaf976690b.tar.zst
unit testing
Diffstat (limited to 'attocc.c')
-rw-r--r--attocc.c54
1 files changed, 41 insertions, 13 deletions
diff --git a/attocc.c b/attocc.c
index d083335..1389f9d 100644
--- a/attocc.c
+++ b/attocc.c
@@ -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;