From 15d0b86c6235c02dd7f9e8e63551b31fe0ec91b8 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Wed, 16 Dec 2020 11:06:39 +0100 Subject: [PATCH] Update documentation & add tests as per lastest changes --- README.md | 4 ++-- tests/test-mcore.c | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1e5706b4..103abfd1 100644 --- a/README.md +++ b/README.md @@ -5938,14 +5938,14 @@ However, you can use the break instruction to quit the block (the clear function and you can chain the M\_LET macro to create several different variables. -##### M\_LET\_IF(init_code, test_code, clear_code) +##### M\_LET\_IF(init_code, test_code, clear_code [, else_code] ) This macro defines the variable(s) in 'init_code', executes the next block of instructions where the variable(s) is(are) used if the initialization succeeds by testing 'test_code', then it executes the 'clear_code'. 'test_code' returns a boolean indicating if the initialization succeeds (true) or not. -If the initialization fails, it won't call the 'clear_code'. +If the initialization fails, it won't call the 'clear_code', but the 'else_code' if present. The syntax of 'init_code' is the same as a for loop. diff --git a/tests/test-mcore.c b/tests/test-mcore.c index 593cdb0c..1a30cdc7 100644 --- a/tests/test-mcore.c +++ b/tests/test-mcore.c @@ -206,6 +206,15 @@ static void test_let(void) *p = 4; } + M_LET_IF( assert(c++ == 7), (assert(c++ == 8), true), assert(c++ == 10), assert(0) ) { + assert(c++ == 9); + } + M_LET_IF( assert(c++ == 11), (assert(c++ == 12), false), assert(0), assert(c++ == 13) ) { + assert(0); + } + assert(c++ == 14); + + c = 7; int *p = (int*)malloc(sizeof(int)); assert(p != NULL); M_DEFER ( (assert(c++ == 8), free(p)) ) {