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 //===----------------------------------------------------------------------===// 8 9 // UNSUPPORTED: c++03, windows 10 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0|12.0}} 11 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1 12 13 #include <cassert> 14 #include <cstdio> 15 #include <string> 16 17 #include "check_assertion.h" 18 19 template <class Func> 20 inline bool TestDeathTest(const char* stmt, Func&& func, DeathTest::ResultKind ExpectResult, AssertionInfoMatcher Matcher = AnyMatcher) { 21 DeathTest DT(Matcher); 22 DeathTest::ResultKind RK = DT.Run(func); 23 auto OnFailure = [&](std::string msg) { 24 std::fprintf(stderr, "EXPECT_DEATH( %s ) failed! (%s)\n\n", stmt, msg.c_str()); 25 if (!DT.getChildStdErr().empty()) { 26 std::fprintf(stderr, "---------- standard err ----------\n%s\n", DT.getChildStdErr().c_str()); 27 } 28 if (!DT.getChildStdOut().empty()) { 29 std::fprintf(stderr, "---------- standard out ----------\n%s\n", DT.getChildStdOut().c_str()); 30 } 31 return false; 32 }; 33 if (RK != ExpectResult) 34 return OnFailure(std::string("expected result did not occur: expected ") + DeathTest::ResultKindToString(ExpectResult) + " got: " + DeathTest::ResultKindToString(RK)); 35 return true; 36 } 37 #define TEST_DEATH_TEST(RK, ...) assert((TestDeathTest(#__VA_ARGS__, [&]() { __VA_ARGS__; }, RK, AnyMatcher ))) 38 39 #define TEST_DEATH_TEST_MATCHES(RK, Matcher, ...) assert((TestDeathTest(#__VA_ARGS__, [&]() { __VA_ARGS__; }, RK, Matcher))) 40 41 void my_libcpp_assert() { 42 _LIBCPP_ASSERT(false, "other"); 43 } 44 45 void test_no_match_found() { 46 AssertionInfoMatcher ExpectMatch("my message"); 47 TEST_DEATH_TEST_MATCHES(DeathTest::RK_MatchFailure, ExpectMatch, my_libcpp_assert()); 48 } 49 50 void test_did_not_die() { 51 TEST_DEATH_TEST(DeathTest::RK_DidNotDie, ((void)0)); 52 } 53 54 void test_unknown() { 55 TEST_DEATH_TEST(DeathTest::RK_Unknown, std::exit(13)); 56 } 57 58 int main(int, char**) { 59 test_no_match_found(); 60 test_did_not_die(); 61 test_unknown(); 62 return 0; 63 } 64