testing_crash_scenarios.cpp (1581B)
1 #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN 2 #include "doctest.h" 3 4 DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN 5 #include <iostream> 6 #include <iostream> 7 #include <thread> 8 #include <exception> 9 #include <functional> 10 #include <stdexcept> 11 using namespace std; 12 DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END 13 14 DOCTEST_GCC_SUPPRESS_WARNING("-Wterminate") 15 DOCTEST_GCC_SUPPRESS_WARNING("-Wdiv-by-zero") 16 17 DOCTEST_MSVC_SUPPRESS_WARNING(4722) 18 DOCTEST_MSVC_SUPPRESS_WARNING(4297) 19 DOCTEST_MSVC_SUPPRESS_WARNING(4723) 20 DOCTEST_MSVC_SUPPRESS_WARNING(4702) 21 22 // echo %errorlevel% 23 24 #define FROM_A_SEPARATE_THREAD 1 25 26 TEST_CASE("uncomment to test these out") { 27 std::function<void(void)> f; 28 29 //for(;;); // infinite loop - to test SIGTERM for CTRL+C ==> doesn't work! 30 31 //SUBCASE("null pointer access") { 32 // f = []() { std::cout << *static_cast<int*>(nullptr); }; 33 //} 34 35 //SUBCASE("div by zero") { 36 // f = []() { 37 // int a = 1; 38 // --a; 39 // std::cout << 5 / a; 40 // }; 41 //} 42 43 //SUBCASE("call terminate") { 44 // f = []() { std::terminate(); }; 45 //} 46 47 //SUBCASE("throw from destructor`") { 48 // f = []() { 49 // struct dtor 50 // { 51 // ~dtor() { 52 // //REQUIRE(1 == 2); 53 // throw 42; 54 // } 55 // }; 56 57 // dtor(); 58 // }; 59 //} 60 61 //SUBCASE("escaping exception") { 62 // f = []() { throw 42; }; 63 //} 64 65 #if FROM_A_SEPARATE_THREAD 66 std::thread t(f); 67 t.join(); 68 #else 69 f(); 70 #endif 71 }