llvm-project/libcxx/test/support/debug_macros.h
Kristina Bessonova 9f4f012c10 [libcxx][test] Attempt to make debug mode tests more bulletproof
The problem with debug mode tests is that it isn't known which particular
_LIBCPP_ASSERT causes the test to exit, and as shown by
https://reviews.llvm.org/D100029 and 2908eb20ba7 it might be not the
expected one.

The patch adds TEST_LIBCPP_ASSERT_FAILURE macro that allows checking
_LIBCPP_ASSERT message to ensure we caught an expected failure.

Reviewed By: Quuxplusone, ldionne

Differential Revision: https://reviews.llvm.org/D100595
2021-05-18 14:52:34 +02:00

34 lines
1.5 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef TEST_SUPPORT_DEBUG_MACROS_H
#define TEST_SUPPORT_DEBUG_MACROS_H
#include <__debug>
#include <cassert>
#include <string>
static const char* expected_libcpp_assert_message = 0;
static void test_debug_function(std::__libcpp_debug_info const& info) {
if (0 == std::strcmp(info.__msg_, expected_libcpp_assert_message))
std::exit(0);
std::fprintf(stderr, "%s\n", info.what().c_str());
std::abort();
}
#define TEST_LIBCPP_ASSERT_FAILURE(expr, m) \
do { \
::expected_libcpp_assert_message = m; \
std::__libcpp_set_debug_function(&::test_debug_function); \
(void)(expr); \
assert(false); \
} while (false)
#endif // TEST_SUPPORT_DEBUG_MACROS_H