[flang][runtime] Make defined formatted I/O process format elementally (#74150)
[llvm-project.git] / libcxx / test / support / test.support / test_check_assertion.pass.cpp
blob7cf0e0966ce89ddcafdba0f1507c7f93eb70ae02
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 // REQUIRES: has-unix-headers
10 // UNSUPPORTED: c++03
11 // UNSUPPORTED: libcpp-hardening-mode=none
12 // XFAIL: availability-verbose_abort-missing
14 #include <cassert>
15 #include <cstdio>
16 #include <string>
18 #include "check_assertion.h"
20 template <class Func>
21 inline bool TestDeathTest(const char* stmt, Func&& func, DeathTest::ResultKind ExpectResult, AssertionInfoMatcher Matcher = AnyMatcher) {
22 DeathTest DT(Matcher);
23 DeathTest::ResultKind RK = DT.Run(func);
24 auto OnFailure = [&](std::string msg) {
25 std::fprintf(stderr, "EXPECT_DEATH( %s ) failed! (%s)\n\n", stmt, msg.c_str());
26 if (!DT.getChildStdErr().empty()) {
27 std::fprintf(stderr, "---------- standard err ----------\n%s\n", DT.getChildStdErr().c_str());
29 if (!DT.getChildStdOut().empty()) {
30 std::fprintf(stderr, "---------- standard out ----------\n%s\n", DT.getChildStdOut().c_str());
32 return false;
34 if (RK != ExpectResult)
35 return OnFailure(std::string("expected result did not occur: expected ") + DeathTest::ResultKindToString(ExpectResult) + " got: " + DeathTest::ResultKindToString(RK));
36 return true;
38 #define TEST_DEATH_TEST(RK, ...) assert((TestDeathTest(#__VA_ARGS__, [&]() { __VA_ARGS__; }, RK, AnyMatcher )))
40 #define TEST_DEATH_TEST_MATCHES(RK, Matcher, ...) assert((TestDeathTest(#__VA_ARGS__, [&]() { __VA_ARGS__; }, RK, Matcher)))
42 void my_libcpp_assert() {
43 _LIBCPP_ASSERT(false, "other");
46 void test_no_match_found() {
47 AssertionInfoMatcher ExpectMatch("my message");
48 TEST_DEATH_TEST_MATCHES(DeathTest::RK_MatchFailure, ExpectMatch, my_libcpp_assert());
51 void test_did_not_die() {
52 TEST_DEATH_TEST(DeathTest::RK_DidNotDie, ((void)0));
55 void test_unknown() {
56 TEST_DEATH_TEST(DeathTest::RK_Unknown, std::exit(13));
59 int main(int, char**) {
60 test_no_match_found();
61 test_did_not_die();
62 test_unknown();
63 return 0;