From 2d70c894b9aa9f0a64e18ad143474add983f5c41 Mon Sep 17 00:00:00 2001 From: "Ryan M. Lederman" Date: Sun, 7 Jan 2024 05:07:24 -0700 Subject: [PATCH] Create cmake-multi-platform.yml (#6) * Create cmake-multi-platform.yml Signed-off-by: Ryan M. Lederman * fix typos * ...more typos * gcc does not like this sprintf. * fix c++ configuration provider * fix assert statement * cstring include * _GNU_SOURCE already defined * __has_include, std::bit_cast, std::source_location * bump version 0.3.0, update copyright year --------- Signed-off-by: Ryan M. Lederman --- .github/workflows/cmake-multi-platform.yml | 82 ++++++++++++++++++++++ .vscode/c_cpp_properties.json | 3 +- CMakeLists.txt | 2 +- include/bal.h | 4 +- include/bal.hh | 50 ++++++++----- include/bal/errors.h | 9 ++- include/bal/helpers.h | 13 ++-- include/bal/internal.h | 4 +- include/bal/platform.h | 12 ++-- include/bal/state.h | 4 +- include/bal/types.h | 6 +- include/bal/version.h.in | 4 +- sample/balclient.cc | 4 +- sample/balcommon.cc | 4 +- sample/balcommon.hh | 4 +- sample/balserver.cc | 6 +- sample/balserver.hh | 4 +- src/bal.c | 4 +- src/balerrors.c | 14 ++-- src/balinternal.c | 4 +- src/balstate.c | 4 +- tests/tests++.cc | 4 +- tests/tests++.hh | 35 ++++++--- tests/tests.c | 4 +- tests/tests.h | 4 +- tests/tests_shared.c | 4 +- tests/tests_shared.h | 4 +- 27 files changed, 209 insertions(+), 87 deletions(-) create mode 100644 .github/workflows/cmake-multi-platform.yml diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml new file mode 100644 index 0000000..fde0b19 --- /dev/null +++ b/.github/workflows/cmake-multi-platform.yml @@ -0,0 +1,82 @@ +# This starter workflow is for a CMake project running on multiple platforms. There is a different starter workflow if you just want a single platform. +# See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-single-platform.yml +name: CMake on multiple platforms + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +jobs: + build: + runs-on: ${{ matrix.os }} + + strategy: + # Set fail-fast to false to ensure that feedback is delivered for all matrix combinations. Consider changing this to true when your workflow is stable. + fail-fast: false + + # Set up a matrix to run the following 3 configurations: + # 1. + # 2. + # 3. + # + # To add more build types (Release, Debug, RelWithDebInfo, etc.) customize the build_type list. + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] + build_type: [Release] + c_compiler: [gcc, clang, cl] + include: + - os: windows-latest + c_compiler: cl + cpp_compiler: cl + - os: ubuntu-latest + c_compiler: gcc + cpp_compiler: g++ + - os: ubuntu-latest + c_compiler: clang + cpp_compiler: clang++ + - os: macos-latest + c_compiler: clang + cpp_compiler: clang++ + exclude: + - os: windows-latest + c_compiler: gcc + - os: windows-latest + c_compiler: clang + - os: ubuntu-latest + c_compiler: cl + - os: macos-latest + c_compiler: cl + - os: macos-latest + c_compiler: gcc + + steps: + - uses: actions/checkout@v3 + + - name: Set reusable strings + # Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file. + id: strings + shell: bash + run: | + echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT" + + - name: Configure CMake + # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make. + # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type + run: > + cmake -B ${{ steps.strings.outputs.build-output-dir }} + -DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }} + -DCMAKE_C_COMPILER=${{ matrix.c_compiler }} + -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} + -S ${{ github.workspace }} + + - name: Build + # Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator). + run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }} + + - name: Test + working-directory: ${{ steps.strings.outputs.build-output-dir }} + # Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator). + # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail + run: ctest --build-config ${{ matrix.build_type }} diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index c23b4a4..0cfe452 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -12,7 +12,8 @@ ], "cStandard": "c17", "cppStandard": "c++20", - "intelliSenseMode": "macos-clang-x64" + "intelliSenseMode": "macos-clang-x64", + "configurationProvider": "ms-vscode.cmake-tools" }, { "name": "Linux", diff --git a/CMakeLists.txt b/CMakeLists.txt index 30b9cba..bb7d0e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,7 +56,7 @@ endif() # define the project project( ${PROJECT_NAME} - VERSION 0.2.0 + VERSION 0.3.0 LANGUAGES C CXX DESCRIPTION "Berkeley Abstraction Layer library" ) diff --git a/include/bal.h b/include/bal.h index 8aa57e9..9ef84c6 100644 --- a/include/bal.h +++ b/include/bal.h @@ -2,8 +2,8 @@ * bal.h * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of diff --git a/include/bal.hh b/include/bal.hh index 56146d9..1650d03 100644 --- a/include/bal.hh +++ b/include/bal.hh @@ -2,8 +2,8 @@ * bal.hh * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of @@ -31,10 +31,29 @@ # include # include # include +# include # include # include # include -# include +# include + +# if defined(__has_include) +# define __HAS_INCLUDE(hdr) __has_include(hdr) +# else +# define __HAS_INCLUDE(hdr) false +# endif + +# if defined(__cpp_lib_source_location) && __HAS_INCLUDE() +# include +# define source_location std::source_location +# endif + +# if defined(__cpp_lib_bit_cast) && __HAS_INCLUDE() +# include +# define bit_cast std::bit_cast +# else +# define bit_cast reinterpret_cast +# endif /** The one and only namespace for libbal. */ namespace bal @@ -551,27 +570,27 @@ namespace bal return throw_on_policy(ret, false); } - bool get_send_timeout(int* value) const + bool get_send_timeout(bal_tvsec* sec, bal_tvusec* usec) const { - const auto ret = bal_get_send_timeout(_s, value); + const auto ret = bal_get_send_timeout(_s, sec, usec); return throw_on_policy(ret, false); } - bool set_send_timeout(int value) const + bool set_send_timeout(bal_tvsec sec, bal_tvusec usec) const { - const auto ret = bal_set_send_timeout(_s, value); + const auto ret = bal_set_send_timeout(_s, sec, usec); return throw_on_policy(ret, false); } - bool get_recv_timeout(int* value) const + bool get_recv_timeout(bal_tvsec* sec, bal_tvusec* usec) const { - const auto ret = bal_get_recv_timeout(_s, value); + const auto ret = bal_get_recv_timeout(_s, sec, usec); return throw_on_policy(ret, false); } - bool set_recv_timeout(int value) const + bool set_recv_timeout(bal_tvsec sec, bal_tvusec usec) const { - const auto ret = bal_set_recv_timeout(_s, value); + const auto ret = bal_set_recv_timeout(_s, sec, usec); return throw_on_policy(ret, false); } @@ -652,12 +671,12 @@ namespace bal static socket_base* from_user_data(bal_socket* s) { - return std::bit_cast(s->user_data); + return bit_cast(s->user_data); } uintptr_t to_user_data() const { - return std::bit_cast(this); + return bit_cast(this); } async_io_cb on_read; @@ -705,7 +724,7 @@ namespace bal if (self == nullptr) { _bal_dbglog("no user_data for socket " BAL_SOCKET_SPEC " (0x%" PRIxPTR ", mask = %08" PRIx32 ")", s->sd, - std::bit_cast(s), s->state.mask); + bit_cast(s), s->state.mask); return; } @@ -714,8 +733,7 @@ namespace bal # if defined(BAL_DBGLOG) _bal_dbglog("early return for socket " BAL_SOCKET_SPEC " (0x%" PRIxPTR ", evt = %08" PRIx32 ", self = 0x%" PRIxPTR ")", - s->sd, std::bit_cast(s), evt, - std::bit_cast(self)); + s->sd, bit_cast(s), evt, bit_cast(self)); # else BAL_UNUSED(s); BAL_UNUSED(self); diff --git a/include/bal/errors.h b/include/bal/errors.h index 0b4417a..eb79bd9 100644 --- a/include/bal/errors.h +++ b/include/bal/errors.h @@ -2,8 +2,8 @@ * errors.h * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of @@ -29,7 +29,6 @@ # include "types.h" # if defined(__cplusplus) -# include extern "C" { # endif @@ -162,10 +161,10 @@ bool __bal_validate(bool expr, int err, const char* func, const char* file, # if defined(BAL_DBGLOG) void __bal_dbglog(const char* func, const char* file, uint32_t line, const char* format, ...); -# if defined(__cplusplus) +# if defined(__cplusplus) && defined(source_location) # define _bal_dbglog(...) \ do { \ - std::source_location loc = std::source_location::current(); \ + source_location loc = source_location::current(); \ __bal_dbglog(loc.function_name(), loc.file_name(), loc.line(), __VA_ARGS__); \ } while (false) # else diff --git a/include/bal/helpers.h b/include/bal/helpers.h index b6de1c9..dbebbab 100644 --- a/include/bal/helpers.h +++ b/include/bal/helpers.h @@ -2,8 +2,8 @@ * helpers.h * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of @@ -29,13 +29,17 @@ # include "types.h" # include "errors.h" +/** Allows a parameter to be unreferenced without compiler warnings. */ +# define BAL_UNUSED(var) (void)(var) + /** Performs a case-insensitive string comparison and returns true if the * strings are the same. */ static inline bool _bal_strsame(const char* lhs, const char* rhs, size_t len) { # if defined(__WIN__) - return 0 == StrStrIA(lhs, rhs, len); + BAL_UNUSED(len); + return 0 == StrStrIA(lhs, rhs); # else return 0 == strncasecmp(lhs, rhs, len); # endif @@ -78,9 +82,6 @@ void __bal_safefree(void** pp) (void)snprintf(dst, _n, __VA_ARGS__); \ } while (false) -/** Allows a parameter to be unreferenced without compiler warnings. */ -# define BAL_UNUSED(var) (void)(var) - /** getnameinfo flags: do not perform DNS queries. */ # define _BAL_NI_NODNS (NI_NUMERICHOST | NI_NUMERICSERV) diff --git a/include/bal/internal.h b/include/bal/internal.h index 6c8df6d..3046356 100644 --- a/include/bal/internal.h +++ b/include/bal/internal.h @@ -2,8 +2,8 @@ * internal.h * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of diff --git a/include/bal/platform.h b/include/bal/platform.h index 15da976..a6e56fd 100644 --- a/include/bal/platform.h +++ b/include/bal/platform.h @@ -2,8 +2,8 @@ * platform.h * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of @@ -33,7 +33,9 @@ # define _DARWIN_C_SOURCE # define __HAVE_LIBC_STRLCPY__ # elif defined(__linux__) -# define _GNU_SOURCE +# if !defined(_GNU_SOURCE) +# define _GNU_SOURCE +# endif # define __HAVE_POLLRDHUP__ # elif defined(__OpenBSD__) # define __BSD__ @@ -277,7 +279,9 @@ typedef unsigned bal_threadret; # include # include -# define BAL_MAXERROR 256 +# define BAL_MAXERROR 256 +# define BAL_MAXERRORMISC 256 +# define BAL_MAXERRORFMT BAL_MAXERROR + BAL_MAXERRORMISC # define BAL_UNKNOWN "" # define BAL_AS_IPV6 "IPv6" diff --git a/include/bal/state.h b/include/bal/state.h index 63b888e..a400199 100644 --- a/include/bal/state.h +++ b/include/bal/state.h @@ -2,8 +2,8 @@ * state.h * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of diff --git a/include/bal/types.h b/include/bal/types.h index c1be907..bc3cf60 100644 --- a/include/bal/types.h +++ b/include/bal/types.h @@ -2,8 +2,8 @@ * types.h * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of @@ -76,7 +76,7 @@ typedef struct { /** The public error type. */ typedef struct { int code; - char message[BAL_MAXERROR]; + char message[BAL_MAXERRORFMT]; } bal_error; /** The internal error type. */ diff --git a/include/bal/version.h.in b/include/bal/version.h.in index c995978..20ac2ee 100644 --- a/include/bal/version.h.in +++ b/include/bal/version.h.in @@ -2,8 +2,8 @@ * version.h * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of diff --git a/sample/balclient.cc b/sample/balclient.cc index 4bcee10..27628e7 100644 --- a/sample/balclient.cc +++ b/sample/balclient.cc @@ -2,8 +2,8 @@ * balclient.cc * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of diff --git a/sample/balcommon.cc b/sample/balcommon.cc index ba5b6f6..65d1975 100644 --- a/sample/balcommon.cc +++ b/sample/balcommon.cc @@ -2,8 +2,8 @@ * balcommon.cc * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of diff --git a/sample/balcommon.hh b/sample/balcommon.hh index 740d8c3..6920adf 100644 --- a/sample/balcommon.hh +++ b/sample/balcommon.hh @@ -2,8 +2,8 @@ * balcommon.hh * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of diff --git a/sample/balserver.cc b/sample/balserver.cc index 4afb60e..55d858a 100644 --- a/sample/balserver.cc +++ b/sample/balserver.cc @@ -2,8 +2,8 @@ * balserver.cc * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of @@ -119,7 +119,7 @@ int main(int argc, char** argv) PRINT("got connection from %s %s:%s on " BAL_SOCKET_SPEC " (0x%" PRIxPTR ");" " now have %zu client(s)", addrinfo.get_type().c_str(), addrinfo.get_addr().c_str(), addrinfo.get_port().c_str(), - client_sock.get_descriptor(), std::bit_cast(client_sock.get()), + client_sock.get_descriptor(), bit_cast(client_sock.get()), _clients.size() + 1); _clients[client_sock.get_descriptor()] = std::move(client_sock); diff --git a/sample/balserver.hh b/sample/balserver.hh index ef2b63d..f65a400 100644 --- a/sample/balserver.hh +++ b/sample/balserver.hh @@ -2,8 +2,8 @@ * balserver.hh * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of diff --git a/src/bal.c b/src/bal.c index e40ab3b..d1f363b 100644 --- a/src/bal.c +++ b/src/bal.c @@ -2,8 +2,8 @@ * bal.c * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of diff --git a/src/balerrors.c b/src/balerrors.c index ae3c622..1aa2b20 100644 --- a/src/balerrors.c +++ b/src/balerrors.c @@ -2,8 +2,8 @@ * balerrors.c * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of @@ -81,20 +81,20 @@ int _bal_get_error(bal_error* err, bool extended) if (bal_errors[n].code == _bal_tei.code) { char* heap_msg = NULL; if (_BAL_E_PLATFORM == bal_errors[n].code) { - heap_msg = calloc(BAL_MAXERROR, sizeof(char)); + heap_msg = calloc(BAL_MAXERROR + 33, sizeof(char)); if (NULL != heap_msg) { - _bal_snprintf_trunc(heap_msg, BAL_MAXERROR, bal_errors[n].msg, + _bal_snprintf_trunc(heap_msg, BAL_MAXERROR + 33, bal_errors[n].msg, _bal_tei.os.code, _bal_okstrnf(_bal_tei.os.msg) ? _bal_tei.os.msg : BAL_UNKNOWN); } } if (extended) { - _bal_snprintf_trunc(err->message, BAL_MAXERROR, BAL_ERRFMTEXT, + _bal_snprintf_trunc(err->message, BAL_MAXERRORFMT, BAL_ERRFMTEXT, _bal_tei.loc.func, _bal_tei.loc.file, _bal_tei.loc.line, _bal_okptrnf(heap_msg) ? heap_msg : bal_errors[n].msg); } else { - _bal_snprintf_trunc(err->message, BAL_MAXERROR, BAL_ERRFMT, + _bal_snprintf_trunc(err->message, BAL_MAXERRORFMT, BAL_ERRFMT, _bal_okptrnf(heap_msg) ? heap_msg : bal_errors[n].msg); } @@ -190,7 +190,7 @@ bool __bal_handle_error(int code, const char* func, const char* file, _bal_strcpy(msg, BAL_MAXERROR, tmp, strnlen(tmp, BAL_MAXERROR)); # endif # if defined(__HAVE_XSI_STRERROR_R__) || defined(__HAVE_STRERROR_S__) - assert(0 == finderr); + BAL_ASSERT_UNUSED(finderr, 0 == finderr); # else BAL_UNUSED(finderr); # endif diff --git a/src/balinternal.c b/src/balinternal.c index 6eec2a6..8662457 100644 --- a/src/balinternal.c +++ b/src/balinternal.c @@ -2,8 +2,8 @@ * balinternal.c * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of diff --git a/src/balstate.c b/src/balstate.c index 3c25959..485589c 100644 --- a/src/balstate.c +++ b/src/balstate.c @@ -2,8 +2,8 @@ * balstate.c * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of diff --git a/tests/tests++.cc b/tests/tests++.cc index 0640bca..b9923fe 100644 --- a/tests/tests++.cc +++ b/tests/tests++.cc @@ -2,8 +2,8 @@ * tests++.cc * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of diff --git a/tests/tests++.hh b/tests/tests++.hh index 2913fc9..d9aa234 100644 --- a/tests/tests++.hh +++ b/tests/tests++.hh @@ -2,8 +2,8 @@ * tests++.hh * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of @@ -27,7 +27,6 @@ # define _BAL_TESTSXX_HH_INCLUDED # include -# include # include "tests_shared.h" /** @@ -71,16 +70,34 @@ namespace bal::tests try { \ initializer balinit; +/** Emits an error message when an unexpected exception is caught. */ +# define _BAL_TEST_LOG_UNEXPECTED_EXCEPTION(func, what) \ + ERROR_MSG("unexpected exception in %s: '%s'", (func), (what)); \ + pass = false + +/** Emits a message when an expected exception is caught. */ +# define _BAL_TEST_LOG_EXPECTED_EXCEPTION(func, what) \ + TEST_MSG(GREEN("expected exception in %s: '%s'"), (func), (what)) + /** Implements recovery in the event that an unexpected exception is caught. */ -# define _BAL_TEST_ON_EXCEPTION(ex) \ - std::source_location loc = std::source_location::current(); \ - ERROR_MSG("unexpected exception in %s: '%s'", loc.function_name(), ex.what()); \ - pass = false +# if defined(source_location) +# define _BAL_TEST_ON_EXCEPTION(ex) \ + source_location loc = source_location::current(); \ + _BAL_TEST_LOG_UNEXPECTED_EXCEPTION(loc.function_name(), ex.what()) +# else +# define _BAL_TEST_ON_EXCEPTION(ex) \ + _BAL_TEST_LOG_UNEXPECTED_EXCEPTION(__func__, ex.what()) +# endif /** Handles an expected exception. */ +# if defined(source_location) +# define _BAL_TEST_ON_EXPECTED_EXCEPTION(ex) \ + source_location loc = source_location::current(); \ + _BAL_TEST_LOG_EXPECTED_EXCEPTION(loc.function_name(), ex.what()) +# else # define _BAL_TEST_ON_EXPECTED_EXCEPTION(ex) \ - std::source_location loc = std::source_location::current(); \ - TEST_MSG(GREEN("expected exception in %s: '%s'"), loc.function_name(), ex.what()) + _BAL_TEST_LOG_EXPECTED_EXCEPTION(__func__, ex.what()) +# endif # define _BAL_TEST_CONCLUDE \ } catch (bal::exception& ex) { \ diff --git a/tests/tests.c b/tests/tests.c index 5f85ce3..1a144ca 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -2,8 +2,8 @@ * tests.c * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of diff --git a/tests/tests.h b/tests/tests.h index 66192ef..d3d6ce4 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -2,8 +2,8 @@ * tests.h * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of diff --git a/tests/tests_shared.c b/tests/tests_shared.c index 0a6b917..53469c4 100644 --- a/tests/tests_shared.c +++ b/tests/tests_shared.c @@ -2,8 +2,8 @@ * tests_shared.c * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of diff --git a/tests/tests_shared.h b/tests/tests_shared.h index 4f121d6..c6ce693 100644 --- a/tests/tests_shared.h +++ b/tests/tests_shared.h @@ -2,8 +2,8 @@ * tests_shared.h * * Author: Ryan M. Lederman - * Copyright: Copyright (c) 2004-2023 - * Version: 0.2.0 + * Copyright: Copyright (c) 2004-2024 + * Version: 0.3.0 * License: The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy of