From 1b4dd419d95fa94e678a23188c9c2fe84c2ddf09 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Thu, 20 Mar 2025 05:22:30 +0100 Subject: [PATCH] tests: Added first failure tests --- src/test.c | 32 ++++++++++++++++++++++++++++++-- src/ulas.c | 6 ++++-- tests/fail_endscope.s | 1 + tests/fail_scope_nesting.s | 2 ++ 4 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 tests/fail_endscope.s create mode 100644 tests/fail_scope_nesting.s diff --git a/src/test.c b/src/test.c index 444c1d5..6ee4b61 100644 --- a/src/test.c +++ b/src/test.c @@ -90,7 +90,7 @@ void test_strbuf(void) { FILE *dst = fmemopen(dstbuf, ULAS_LINEMAX, "we"); \ assert(ulas_preproc(dst, src) == (expect_ret)); \ fclose(src); \ - fclose(dst); \ + fclose(dst); \ assert(strcmp(dstbuf, (expect_dst)) == 0); \ } @@ -411,7 +411,8 @@ void test_symscope(void) { { \ struct ulas_config cfg = ulas_cfg_from_env(); \ char *defs[] = {"ULAS_PREDEF"}; \ - cfg.defs = defs; cfg.defslen = 1; \ + cfg.defs = defs; \ + cfg.defslen = 1; \ ASSERT_FULL(expect_rc, in_path, expect_path) \ } @@ -424,6 +425,32 @@ void test_full_asm(void) { TESTEND("testfullasm"); } +#define ASSERT_FULL_ASM_FAIL(expect_rc, in_path) \ + { \ + struct ulas_config cfg = ulas_cfg_from_env(); \ + printf("[source: %s;]\n", in_path); \ + ulaslstout = stdout; \ + ulassymout = stdout; \ + cfg.verbose = 1; \ + char dstbuf[ULAS_FULLEN]; \ + memset(dstbuf, 0, ULAS_FULLEN); \ + ulasout = fmemopen(dstbuf, ULAS_FULLEN, "we"); \ + ulasin = fopen(in_path, "re"); \ + assert(ulas_main(cfg) == (expect_rc)); \ + fclose(ulasout); \ + ulasin = stdin; \ + ulasout = stdout; \ + } + +void test_full_asm_fail(void) { + TESTBEGIN("testfullasmfail"); + + ASSERT_FULL_ASM_FAIL(-1, "tests/fail_scope_nesting.s"); + ASSERT_FULL_ASM_FAIL(-1, "tests/fail_endscope.s"); + + TESTEND("testfullasm"); +} + #define ASSERT_FULL_DASM(expect_rc, in_path, expect_path) \ { \ struct ulas_config cfg = ulas_cfg_from_env(); \ @@ -492,6 +519,7 @@ int main(int argc, char **argv) { // this will re-init everything on its own, // so call after free test_full_dasm(); + test_full_asm_fail(); test_full_asm(); TESTEND("ulas test"); diff --git a/src/ulas.c b/src/ulas.c index 9c26509..1f8d8a7 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -2621,14 +2621,16 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) { break; case ULAS_ASMDIR_BEGIN_SCOPE: if (ulas.prev_scope != 0) { - ULASPANIC("nested .beginscope detected\n"); + ULASERR("nested .beginscope detected\n"); + rc = -1; } ulas.prev_scope = ulas.scope; ulas.scope = -1 - ulas_icntr(); break; case ULAS_ASMDIR_END_SCOPE: if (ulas.prev_scope == 0) { - ULASPANIC("calling .endscope without starting a scope\n"); + ULASERR("calling .endscope without starting a scope\n"); + rc = -1; } ulas.scope = ulas.prev_scope; ulas.prev_scope = 0; diff --git a/tests/fail_endscope.s b/tests/fail_endscope.s new file mode 100644 index 0000000..410dd82 --- /dev/null +++ b/tests/fail_endscope.s @@ -0,0 +1 @@ +.endscope diff --git a/tests/fail_scope_nesting.s b/tests/fail_scope_nesting.s new file mode 100644 index 0000000..17a97f0 --- /dev/null +++ b/tests/fail_scope_nesting.s @@ -0,0 +1,2 @@ +.beginscope +.beginscope -- 2.30.2