From: Lukas Krickl Date: Fri, 17 Nov 2023 15:42:18 +0000 (+0100) Subject: Implemented tests for int token and char token X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=74017716add15cd6e3769b80e4559a45559782df;p=ulas%2F.git Implemented tests for int token and char token --- diff --git a/include/ulas.h b/include/ulas.h index f5a87c2..0ef1802 100644 --- a/include/ulas.h +++ b/include/ulas.h @@ -97,8 +97,8 @@ enum ulas_type { ULAS_INT, ULAS_STR }; // data type value union ulas_val { - int int_value; - char *str_value; + int intv; + char *strv; }; // literal value diff --git a/makefile b/makefile index 0fed56f..e2755f7 100644 --- a/makefile +++ b/makefile @@ -53,7 +53,7 @@ install: .PHONY: tags tags: - ctags --recurse=yes --exclude=.git --exclude=bin --exclude=obj --exclude=scripts + ctags --recurse=yes --exclude=.git --exclude=bin --exclude=obj --extras=* --fields=* --c-kinds=* --language-force=C .PHONY: ccmds: diff --git a/src/test.c b/src/test.c index 2433618..7790083 100644 --- a/src/test.c +++ b/src/test.c @@ -142,8 +142,35 @@ void test_preproc(void) { ulascfg.preproc_only = 0; } +#define ASSERT_INT_TOTOK(expected_val, expected_rc, token) \ + { \ + int rc = 0; \ + struct ulas_tok tok = ulas_totok((token), strlen(token), &rc); \ + assert((expected_rc) == rc); \ + assert(tok.type == ULAS_TOKLITERAL); \ + assert(tok.lit.type == ULAS_INT); \ + assert(tok.lit.val.intv == (expected_val)); \ + } + void test_totok(void) { TESTBEGIN("totok"); + + // regular ints + ASSERT_INT_TOTOK(10, 0, "10"); + ASSERT_INT_TOTOK(0x1A, 0, "0x1A"); + ASSERT_INT_TOTOK(5, 0, "0b101"); + + // chars + ASSERT_INT_TOTOK('a', 0, "'a'"); + ASSERT_INT_TOTOK('\n', 0, "'\\n'"); + ASSERT_INT_TOTOK('\\', 0, "'\\\\'"); + // char - not terminated + ASSERT_INT_TOTOK('a', -1, "'a"); + // bad escape + ASSERT_INT_TOTOK(0, -1, "'\\z'"); + // unterminated escape + ASSERT_INT_TOTOK('\n', -1, "'\\n"); + TESTEND("totok"); } diff --git a/src/ulas.c b/src/ulas.c index 5510227..763079b 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -269,22 +269,30 @@ struct ulas_tok ulas_totok(char *buf, unsigned long n, int *rc) { // integer tok.type = ULAS_TOKLITERAL; tok.lit.type = ULAS_INT; - tok.lit.val.int_value = (int)strtol(buf, &buf, 0); + + // 0b prefix is not supported in strtol... so we implement it by hand + if (*buf == 'b') { + buf++; + tok.lit.val.intv = (int)strtol(buf, &buf, 2); + } else { + tok.lit.val.intv = (int)strtol(buf - 1, &buf, 0); + } } else if (first == '\'') { tok.type = ULAS_TOKLITERAL; tok.lit.type = ULAS_INT; - buf++; if (*buf == '\\') { buf++; - tok.lit.val.int_value = ulas_unescape(*buf, rc); + tok.lit.val.intv = ulas_unescape(*buf, rc); } else { - tok.lit.val.int_value = (int)*buf; + tok.lit.val.intv = (int)*buf; } buf++; if (*buf != '\'') { *rc = -1; ULASERR("Unterminated character sequence\n"); + goto end; } + buf++; break; } else if (ulas_isname(buf, n)) { // literal. we can resolve it now @@ -294,7 +302,6 @@ struct ulas_tok ulas_totok(char *buf, unsigned long n, int *rc) { tok.type = ULAS_TOKSYMBOL; tok.lit.type = ULAS_STR; } else { - ULASERR("Unexpected token: %s\n", buf); *rc = -1; goto end; @@ -821,7 +828,7 @@ int ulas_litint(struct ulas_lit *lit, int *rc) { return 0; } - return lit->val.int_value; + return lit->val.intv; } char *ulas_litchar(struct ulas_lit *lit, int *rc) { @@ -830,7 +837,7 @@ char *ulas_litchar(struct ulas_lit *lit, int *rc) { return NULL; } - return lit->val.str_value; + return lit->val.strv; } struct ulas_tokbuf ulas_tokbuf(void) {