Imre Kis | 721104f | 2020-11-30 20:52:38 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: BSD-3-Clause */ |
| 2 | /* |
| 3 | * Copyright (c) 2020-2021, Arm Limited. All rights reserved. |
| 4 | */ |
| 5 | |
| 6 | #ifndef LIBSP_TEST_MOCK_ASSERT_H_ |
| 7 | #define LIBSP_TEST_MOCK_ASSERT_H_ |
| 8 | |
| 9 | #ifdef __cplusplus |
| 10 | extern "C" { |
| 11 | #endif |
| 12 | |
| 13 | #include <setjmp.h> |
| 14 | |
| 15 | typedef jmp_buf assert_environment_t; |
| 16 | |
| 17 | /* |
| 18 | * SETUP_ASSERT_ENVIRONMENT |
| 19 | * Both expect_assert and setjmp must be called without putting them into a new |
| 20 | * function. The only way is to use an operator between them. Logical operators |
| 21 | * would introduce branches which could introduce uncovered branches. The |
| 22 | * solution is use arithmetic operators. expect_assert always return 0 so the |
| 23 | * value of the sum is determined by the setjmp return value. |
| 24 | * |
| 25 | * Example usage: |
| 26 | * assert_environment_t env; |
| 27 | * |
| 28 | * if (SETUP_ASSERT_ENVIRONMENT(env) { |
| 29 | * function_with_assert_fail(); |
| 30 | * } |
| 31 | */ |
| 32 | #define SETUP_ASSERT_ENVIRONMENT(env) (expect_assert(&env) + (setjmp(env) == 0)) |
| 33 | |
| 34 | int expect_assert(assert_environment_t *env); |
| 35 | |
| 36 | #ifdef __cplusplus |
| 37 | } |
| 38 | #endif |
| 39 | |
| 40 | #endif /* LIBSP_TEST_MOCK_ASSERT_H_ */ |