Skip to content

Commit 67e66d0

Browse files
author
Damian Duy
committed
Add asserts for C tests
1 parent 42a3bd8 commit 67e66d0

File tree

2 files changed

+77
-11
lines changed

2 files changed

+77
-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: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,91 @@
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 C 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>
1214

15+
#ifdef __cplusplus
16+
#include <stdexcept>
17+
static inline void UT_FATAL(const char *format, ...) {
18+
va_list args_list;
19+
va_start(args_list, format);
20+
char buffer[10240]; // big enough to hold error message
21+
int written = vsnprintf(buffer, sizeof(buffer), format, args_list);
22+
if (written == sizeof(buffer)) {
23+
buffer[written - 1] = '\0';
24+
}
25+
26+
va_end(args_list);
27+
28+
throw std::runtime_error(buffer);
29+
}
30+
#else
31+
static inline void UT_FATAL(const char *format, ...) {
32+
va_list args_list;
33+
va_start(args_list, format);
34+
vfprintf(stderr, format, args_list);
35+
va_end(args_list);
36+
37+
fprintf(stderr, "\n");
38+
39+
abort();
40+
}
41+
#endif
42+
1343
#ifdef __cplusplus
1444
extern "C" {
1545
#endif
1646

47+
static inline void UT_OUT(const char *format, ...) {
48+
va_list args_list;
49+
va_start(args_list, format);
50+
vfprintf(stdout, format, args_list);
51+
va_end(args_list);
52+
53+
fprintf(stdout, "\n");
54+
}
55+
56+
// Assert a condition is true at runtime
57+
#define UT_ASSERT(cnd) \
58+
((void)((cnd) || (UT_FATAL("%s:%d %s - assertion failure: %s", __FILE__, \
59+
__LINE__, __func__, #cnd), \
60+
0)))
61+
62+
// Assertion with extra info printed if assertion fails at runtime
63+
#define UT_ASSERTinfo(cnd, info) \
64+
((void)((cnd) || \
65+
(UT_FATAL("%s:%d %s - assertion failure: %s (%s = %s)", __FILE__, \
66+
__LINE__, __func__, #cnd, #info, info), \
67+
0)))
68+
69+
// Assert two integer values are equal at runtime
70+
#define UT_ASSERTeq(lhs, rhs) \
71+
((void)(((lhs) == (rhs)) || \
72+
(UT_FATAL("%s:%d %s - assertion failure: %s (0x%llx) == %s " \
73+
"(0x%llx)", \
74+
__FILE__, __LINE__, __func__, #lhs, \
75+
(unsigned long long)(lhs), #rhs, \
76+
(unsigned long long)(rhs)), \
77+
0)))
78+
79+
// Assert two integer values are not equal at runtime
80+
#define UT_ASSERTne(lhs, rhs) \
81+
((void)(((lhs) != (rhs)) || \
82+
(UT_FATAL("%s:%d %s - assertion failure: %s (0x%llx) != %s " \
83+
"(0x%llx)", \
84+
__FILE__, __LINE__, __func__, #lhs, \
85+
(unsigned long long)(lhs), #rhs, \
86+
(unsigned long long)(rhs)), \
87+
0)))
88+
1789
int bufferIsFilledWithChar(void *ptr, size_t size, char c);
1890

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

0 commit comments

Comments
 (0)