Skip to content

Add asserts for C tests #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions test/c_api/disjoint_pool.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2023 Intel Corporation
// Copyright (C) 2023-2024 Intel Corporation
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

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

// TODO: use asserts
if (retp != UMF_RESULT_SUCCESS) {
abort();
}
UT_ASSERTeq(retp, UMF_RESULT_SUCCESS);

umfPoolDestroy(pool);
umfMemoryProviderDestroy(provider);
Expand All @@ -36,10 +33,7 @@ void test_disjoint_pool_shared_limits(void) {

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

// TODO: use asserts
if (retp != UMF_RESULT_SUCCESS) {
abort();
}
UT_ASSERTeq(retp, UMF_RESULT_SUCCESS);

umfPoolDestroy(pool);
umfMemoryProviderDestroy(provider);
Expand Down
59 changes: 57 additions & 2 deletions test/common/test_helpers.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright (C) 2023 Intel Corporation
// Copyright (C) 2023-2024 Intel Corporation
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// This file contains tests for UMF pool API
// This file contains helpers for tests for UMF pool API

#ifndef UMF_TEST_HELPERS_H
#define UMF_TEST_HELPERS_H 1

#include <stdarg.h>
#include <stdio.h>
#include <umf/base.h>
#include <umf/memory_pool.h>
#include <umf/memory_provider_ops.h>
Expand All @@ -14,6 +16,59 @@
extern "C" {
#endif

static inline void UT_FATAL(const char *format, ...) {
va_list args_list;
va_start(args_list, format);
vfprintf(stderr, format, args_list);
va_end(args_list);

fprintf(stderr, "\n");

abort();
}

static inline void UT_OUT(const char *format, ...) {
va_list args_list;
va_start(args_list, format);
vfprintf(stdout, format, args_list);
va_end(args_list);

fprintf(stdout, "\n");
}

// Assert a condition is true at runtime
#define UT_ASSERT(cnd) \
((void)((cnd) || (UT_FATAL("%s:%d %s - assertion failure: %s", __FILE__, \
__LINE__, __func__, #cnd), \
0)))

// Assertion with extra info printed if assertion fails at runtime
#define UT_ASSERTinfo(cnd, info) \
((void)((cnd) || \
(UT_FATAL("%s:%d %s - assertion failure: %s (%s = %s)", __FILE__, \
__LINE__, __func__, #cnd, #info, info), \
0)))

// Assert two integer values are equal at runtime
#define UT_ASSERTeq(lhs, rhs) \
((void)(((lhs) == (rhs)) || \
(UT_FATAL("%s:%d %s - assertion failure: %s (0x%llx) == %s " \
"(0x%llx)", \
__FILE__, __LINE__, __func__, #lhs, \
(unsigned long long)(lhs), #rhs, \
(unsigned long long)(rhs)), \
0)))

// Assert two integer values are not equal at runtime
#define UT_ASSERTne(lhs, rhs) \
((void)(((lhs) != (rhs)) || \
(UT_FATAL("%s:%d %s - assertion failure: %s (0x%llx) != %s " \
"(0x%llx)", \
__FILE__, __LINE__, __func__, #lhs, \
(unsigned long long)(lhs), #rhs, \
(unsigned long long)(rhs)), \
0)))

int bufferIsFilledWithChar(void *ptr, size_t size, char c);

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