Skip to content

Commit 55b9e0f

Browse files
authored
Merge pull request #109 from DamianDuy/addAssertsC
Add asserts for C tests
2 parents 2372e9f + 259899c commit 55b9e0f

File tree

2 files changed

+60
-11
lines changed

2 files changed

+60
-11
lines changed

test/c_api/disjoint_pool.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2023 Intel Corporation
1+
// Copyright (C) 2023-2024 Intel Corporation
22
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
33
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44

@@ -15,10 +15,7 @@ void test_disjoint_pool_default_params(void) {
1515
umf_disjoint_pool_params_t params = umfDisjointPoolParamsDefault();
1616
retp = umfPoolCreate(&UMF_DISJOINT_POOL_OPS, provider, &params, &pool);
1717

18-
// TODO: use asserts
19-
if (retp != UMF_RESULT_SUCCESS) {
20-
abort();
21-
}
18+
UT_ASSERTeq(retp, UMF_RESULT_SUCCESS);
2219

2320
umfPoolDestroy(pool);
2421
umfMemoryProviderDestroy(provider);
@@ -36,10 +33,7 @@ void test_disjoint_pool_shared_limits(void) {
3633

3734
retp = umfPoolCreate(&UMF_DISJOINT_POOL_OPS, provider, &params, &pool);
3835

39-
// TODO: use asserts
40-
if (retp != UMF_RESULT_SUCCESS) {
41-
abort();
42-
}
36+
UT_ASSERTeq(retp, UMF_RESULT_SUCCESS);
4337

4438
umfPoolDestroy(pool);
4539
umfMemoryProviderDestroy(provider);

test/common/test_helpers.h

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
// Copyright (C) 2023 Intel Corporation
1+
// Copyright (C) 2023-2024 Intel Corporation
22
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
33
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4-
// This file contains tests for UMF pool API
4+
// This file contains helpers for tests for UMF pool API
55

66
#ifndef UMF_TEST_HELPERS_H
77
#define UMF_TEST_HELPERS_H 1
88

9+
#include <stdarg.h>
10+
#include <stdio.h>
911
#include <umf/base.h>
1012
#include <umf/memory_pool.h>
1113
#include <umf/memory_provider_ops.h>
@@ -14,6 +16,59 @@
1416
extern "C" {
1517
#endif
1618

19+
static inline void UT_FATAL(const char *format, ...) {
20+
va_list args_list;
21+
va_start(args_list, format);
22+
vfprintf(stderr, format, args_list);
23+
va_end(args_list);
24+
25+
fprintf(stderr, "\n");
26+
27+
abort();
28+
}
29+
30+
static inline void UT_OUT(const char *format, ...) {
31+
va_list args_list;
32+
va_start(args_list, format);
33+
vfprintf(stdout, format, args_list);
34+
va_end(args_list);
35+
36+
fprintf(stdout, "\n");
37+
}
38+
39+
// Assert a condition is true at runtime
40+
#define UT_ASSERT(cnd) \
41+
((void)((cnd) || (UT_FATAL("%s:%d %s - assertion failure: %s", __FILE__, \
42+
__LINE__, __func__, #cnd), \
43+
0)))
44+
45+
// Assertion with extra info printed if assertion fails at runtime
46+
#define UT_ASSERTinfo(cnd, info) \
47+
((void)((cnd) || \
48+
(UT_FATAL("%s:%d %s - assertion failure: %s (%s = %s)", __FILE__, \
49+
__LINE__, __func__, #cnd, #info, info), \
50+
0)))
51+
52+
// Assert two integer values are equal at runtime
53+
#define UT_ASSERTeq(lhs, rhs) \
54+
((void)(((lhs) == (rhs)) || \
55+
(UT_FATAL("%s:%d %s - assertion failure: %s (0x%llx) == %s " \
56+
"(0x%llx)", \
57+
__FILE__, __LINE__, __func__, #lhs, \
58+
(unsigned long long)(lhs), #rhs, \
59+
(unsigned long long)(rhs)), \
60+
0)))
61+
62+
// Assert two integer values are not equal at runtime
63+
#define UT_ASSERTne(lhs, rhs) \
64+
((void)(((lhs) != (rhs)) || \
65+
(UT_FATAL("%s:%d %s - assertion failure: %s (0x%llx) != %s " \
66+
"(0x%llx)", \
67+
__FILE__, __LINE__, __func__, #lhs, \
68+
(unsigned long long)(lhs), #rhs, \
69+
(unsigned long long)(rhs)), \
70+
0)))
71+
1772
int bufferIsFilledWithChar(void *ptr, size_t size, char c);
1873

1974
int buffersHaveSameContent(void *first, void *second, size_t size);

0 commit comments

Comments
 (0)