|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (C) 2023-2024 Intel Corporation |
| 4 | + * |
| 5 | + * Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. |
| 6 | + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | + * |
| 8 | + */ |
| 9 | + |
| 10 | +/* |
| 11 | + The project uses GTEST framework for testing, which is not supported in C |
| 12 | + These asserts should NOT be used in other purposes than for testing C API |
| 13 | + */ |
| 14 | + |
| 15 | +#ifndef UMF_TEST_UT_ASSERTS_H |
| 16 | +#define UMF_TEST_UT_ASSERTS_H 1 |
| 17 | + |
| 18 | +#include <stdarg.h> |
| 19 | +#include <stdio.h> |
| 20 | +#include <stdlib.h> |
| 21 | + |
| 22 | +static inline void UT_FATAL(const char *format, ...) { |
| 23 | + va_list args_list; |
| 24 | + va_start(args_list, format); |
| 25 | + vfprintf(stderr, format, args_list); |
| 26 | + va_end(args_list); |
| 27 | + |
| 28 | + fprintf(stderr, "\n"); |
| 29 | + |
| 30 | + abort(); |
| 31 | +} |
| 32 | + |
| 33 | +static inline void UT_OUT(const char *format, ...) { |
| 34 | + va_list args_list; |
| 35 | + va_start(args_list, format); |
| 36 | + vfprintf(stdout, format, args_list); |
| 37 | + va_end(args_list); |
| 38 | + |
| 39 | + fprintf(stdout, "\n"); |
| 40 | +} |
| 41 | + |
| 42 | +// Assert a condition is true at runtime |
| 43 | +#define UT_ASSERT(cnd) \ |
| 44 | + ((void)((cnd) || (UT_FATAL("%s:%d %s - assertion failure: %s", __FILE__, \ |
| 45 | + __LINE__, __func__, #cnd), \ |
| 46 | + 0))) |
| 47 | + |
| 48 | +// Assertion with extra info printed if assertion fails at runtime |
| 49 | +#define UT_ASSERTinfo(cnd, info) \ |
| 50 | + ((void)((cnd) || \ |
| 51 | + (UT_FATAL("%s:%d %s - assertion failure: %s (%s = %s)", __FILE__, \ |
| 52 | + __LINE__, __func__, #cnd, #info, info), \ |
| 53 | + 0))) |
| 54 | + |
| 55 | +// Assert two integer values are equal at runtime |
| 56 | +#define UT_ASSERTeq(lhs, rhs) \ |
| 57 | + ((void)(((lhs) == (rhs)) || \ |
| 58 | + (UT_FATAL("%s:%d %s - assertion failure: %s (0x%llx) == %s " \ |
| 59 | + "(0x%llx)", \ |
| 60 | + __FILE__, __LINE__, __func__, #lhs, \ |
| 61 | + (unsigned long long)(lhs), #rhs, \ |
| 62 | + (unsigned long long)(rhs)), \ |
| 63 | + 0))) |
| 64 | + |
| 65 | +// Assert two integer values are not equal at runtime |
| 66 | +#define UT_ASSERTne(lhs, rhs) \ |
| 67 | + ((void)(((lhs) != (rhs)) || \ |
| 68 | + (UT_FATAL("%s:%d %s - assertion failure: %s (0x%llx) != %s " \ |
| 69 | + "(0x%llx)", \ |
| 70 | + __FILE__, __LINE__, __func__, #lhs, \ |
| 71 | + (unsigned long long)(lhs), #rhs, \ |
| 72 | + (unsigned long long)(rhs)), \ |
| 73 | + 0))) |
| 74 | + |
| 75 | +#endif /* UMF_TEST_UT_ASSERTS_H */ |
0 commit comments