diff --git a/README.md b/README.md index df9fcb8..c83db54 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ other platforms. Please see [CONTRIBUTING.md](./CONTRIBUTING.md) for more inform This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. # Usage of Third Party Libraries -This project makes use of the [Catch](https://github.com/philsquared/catch) testing library. Please see the [ThirdPartyNotices.txt](./ThirdPartyNotices.txt) file for details regarding the licensing of Catch. +This project makes use of the [Google Test](https://github.com/google/gooletest) testing library. Please see the [ThirdPartyNotices.txt](./ThirdPartyNotices.txt) file for details regarding the licensing of Google Test. # Quick Start ## Supported Platforms diff --git a/ThirdPartyNotices.txt b/ThirdPartyNotices.txt index 1bb6a2b..552b254 100644 --- a/ThirdPartyNotices.txt +++ b/ThirdPartyNotices.txt @@ -3,8 +3,6 @@ THIRD-PARTY SOFTWARE NOTICES AND INFORMATION Do Not Translate or Localize GSL: Guidelines Support Library incorporates third party material from the projects listed below. -The original copyright notice and the license under which Microsoft received such third party material are set forth below. -Microsoft reserves all other rights not expressly granted, whether by implication, estoppel or otherwise. ------------------------------------------------------------------------------- Software: Google Test diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7b9dd22..ef70547 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -188,5 +188,5 @@ function(add_gsl_test_noexcept name) set_property(TARGET ${name} PROPERTY FOLDER "GSL_tests_noexcept") endfunction() -# add_gsl_test_noexcept(no_exception_throw_tests) -# add_gsl_test_noexcept(no_exception_ensure_tests) +add_gsl_test_noexcept(no_exception_throw_tests) +add_gsl_test_noexcept(no_exception_ensure_tests) diff --git a/tests/algorithm_tests.cpp b/tests/algorithm_tests.cpp index eb6641c..299bb96 100644 --- a/tests/algorithm_tests.cpp +++ b/tests/algorithm_tests.cpp @@ -21,7 +21,7 @@ #endif #if __clang__ || __GNUC__ -//disable warnings from gtest +// disable warnings from gtest #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wundef" #endif @@ -34,13 +34,18 @@ #include // for copy #include // for span - #include // for array +#include #include // for size_t -namespace gsl { +namespace{ +constexpr std::string_view deathstring("Expected Death"); +} + +namespace gsl +{ struct fail_fast; -} // namespace gsl +} // namespace gsl using namespace std; using namespace gsl; @@ -58,7 +63,8 @@ TEST(algorithm_tests, same_type) copy(src_span, dst_span); copy(src_span, dst_span.subspan(src_span.size())); - for (std::size_t i = 0; i < src.size(); ++i) { + for (std::size_t i = 0; i < src.size(); ++i) + { EXPECT_TRUE(dst[i] == src[i]); EXPECT_TRUE(dst[i + src.size()] == src[i]); } @@ -75,7 +81,8 @@ TEST(algorithm_tests, same_type) copy(src_span, dst_span); copy(src_span, dst_span.subspan(src_span.size())); - for (std::size_t i = 0; i < src.size(); ++i) { + for (std::size_t i = 0; i < src.size(); ++i) + { EXPECT_TRUE(dst[i] == src[i]); EXPECT_TRUE(dst[i + src.size()] == src[i]); } @@ -92,7 +99,8 @@ TEST(algorithm_tests, same_type) copy(src_span, dst_span); copy(src_span, dst_span.subspan(src_span.size())); - for (std::size_t i = 0; i < src.size(); ++i) { + for (std::size_t i = 0; i < src.size(); ++i) + { EXPECT_TRUE(dst[i] == src[i]); EXPECT_TRUE(dst[i + src.size()] == src[i]); } @@ -109,7 +117,8 @@ TEST(algorithm_tests, same_type) copy(src_span, dst_span); copy(src_span, dst_span.subspan(src_span.size())); - for (std::size_t i = 0; i < src.size(); ++i) { + for (std::size_t i = 0; i < src.size(); ++i) + { EXPECT_TRUE(dst[i] == src[i]); EXPECT_TRUE(dst[i + src.size()] == src[i]); } @@ -129,7 +138,8 @@ TEST(algorithm_tests, compatible_type) copy(src_span, dst_span); copy(src_span, dst_span.subspan(src_span.size())); - for (std::size_t i = 0; i < src.size(); ++i) { + for (std::size_t i = 0; i < src.size(); ++i) + { EXPECT_TRUE(dst[i] == src[i]); EXPECT_TRUE(dst[i + src.size()] == src[i]); } @@ -146,7 +156,8 @@ TEST(algorithm_tests, compatible_type) copy(src_span, dst_span); copy(src_span, dst_span.subspan(src_span.size())); - for (std::size_t i = 0; i < src.size(); ++i) { + for (std::size_t i = 0; i < src.size(); ++i) + { EXPECT_TRUE(dst[i] == src[i]); EXPECT_TRUE(dst[i + src.size()] == src[i]); } @@ -163,7 +174,8 @@ TEST(algorithm_tests, compatible_type) copy(src_span, dst_span); copy(src_span, dst_span.subspan(src_span.size())); - for (std::size_t i = 0; i < src.size(); ++i) { + for (std::size_t i = 0; i < src.size(); ++i) + { EXPECT_TRUE(dst[i] == src[i]); EXPECT_TRUE(dst[i + src.size()] == src[i]); } @@ -180,7 +192,8 @@ TEST(algorithm_tests, compatible_type) copy(src_span, dst_span); copy(src_span, dst_span.subspan(src_span.size())); - for (std::size_t i = 0; i < src.size(); ++i) { + for (std::size_t i = 0; i < src.size(); ++i) + { EXPECT_TRUE(dst[i] == src[i]); EXPECT_TRUE(dst[i + src.size()] == src[i]); } @@ -208,6 +221,11 @@ TEST(algorithm_tests, incompatible_type) TEST(algorithm_tests, small_destination_span) { + std::set_terminate([] { + std::cerr << "Expected Death. small_destination_span"; + std::abort(); + }); + std::array src{1, 2, 3, 4}; std::array dst{}; @@ -216,9 +234,9 @@ TEST(algorithm_tests, small_destination_span) const span dst_span_dyn(dst); const span dst_span_static(dst); - EXPECT_DEATH(copy(src_span_dyn, dst_span_dyn), ".*"); - EXPECT_DEATH(copy(src_span_dyn, dst_span_static), ".*"); - EXPECT_DEATH(copy(src_span_static, dst_span_dyn), ".*"); + EXPECT_DEATH(copy(src_span_dyn, dst_span_dyn), deathstring.data()); + EXPECT_DEATH(copy(src_span_dyn, dst_span_static), deathstring.data()); + EXPECT_DEATH(copy(src_span_static, dst_span_dyn), deathstring.data()); #ifdef CONFIRM_COMPILATION_ERRORS copy(src_span_static, dst_span_static); diff --git a/tests/assertion_tests.cpp b/tests/assertion_tests.cpp index e445ceb..e873f75 100644 --- a/tests/assertion_tests.cpp +++ b/tests/assertion_tests.cpp @@ -34,7 +34,7 @@ #include #include // for fail_fast (ptr only), Ensures, Expects - +#include using namespace gsl; namespace @@ -51,19 +51,31 @@ int g(int i) Ensures(i > 0 && i < 10); return i; } + +constexpr std::string_view deathstring("Expected Death"); } // namespace TEST(assertion_tests, expects) { + std::set_terminate([] { + std::cerr << "Expected Death. expects"; + std::abort(); + }); + EXPECT_TRUE(f(2) == 2); - EXPECT_DEATH(f(10), ".*"); + EXPECT_DEATH(f(10), deathstring.data()); } TEST(assertion_tests, ensures) { + std::set_terminate([] { + std::cerr << "Expected Death. ensures"; + std::abort(); + }); + EXPECT_TRUE(g(2) == 3); - EXPECT_DEATH(g(9), ".*"); + EXPECT_DEATH(g(9), deathstring.data()); } #if __clang__ || __GNUC__ diff --git a/tests/at_tests.cpp b/tests/at_tests.cpp index 6069e78..0d2196a 100644 --- a/tests/at_tests.cpp +++ b/tests/at_tests.cpp @@ -40,6 +40,10 @@ #include // for initializer_list #include // for vector +namespace{ +constexpr std::string_view deathstring("Expected Death"); +} + TEST(at_tests, static_array) { int a[4] = {1, 2, 3, 4}; @@ -50,10 +54,15 @@ TEST(at_tests, static_array) EXPECT_TRUE(&gsl::at(c_a, i) == &a[i]); } - EXPECT_DEATH(gsl::at(a, -1), ".*"); - EXPECT_DEATH(gsl::at(a, 4), ".*"); - EXPECT_DEATH(gsl::at(c_a, -1), ".*"); - EXPECT_DEATH(gsl::at(c_a, 4), ".*"); + std::set_terminate([] { + std::cerr << "Expected Death. static_array"; + std::abort(); + }); + + EXPECT_DEATH(gsl::at(a, -1), deathstring.data()); + EXPECT_DEATH(gsl::at(a, 4), deathstring.data()); + EXPECT_DEATH(gsl::at(c_a, -1), deathstring.data()); + EXPECT_DEATH(gsl::at(c_a, 4), deathstring.data()); } TEST(at_tests, std_array) @@ -66,13 +75,18 @@ TEST(at_tests, std_array) EXPECT_TRUE(&gsl::at(c_a, i) == &a[static_cast(i)]); } - EXPECT_DEATH(gsl::at(a, -1), ".*"); - EXPECT_DEATH(gsl::at(a, 4), ".*"); - EXPECT_DEATH(gsl::at(c_a, -1), ".*"); - EXPECT_DEATH(gsl::at(c_a, 4), ".*"); + std::set_terminate([] { + std::cerr << "Expected Death. std_array"; + std::abort(); + }); + + EXPECT_DEATH(gsl::at(a, -1), deathstring.data()); + EXPECT_DEATH(gsl::at(a, 4), deathstring.data()); + EXPECT_DEATH(gsl::at(c_a, -1), deathstring.data()); + EXPECT_DEATH(gsl::at(c_a, 4), deathstring.data()); } -TEST(at_tests, StdVector) +TEST(at_tests, std_vector) { std::vector a = {1, 2, 3, 4}; const std::vector& c_a = a; @@ -82,10 +96,15 @@ TEST(at_tests, StdVector) EXPECT_TRUE(&gsl::at(c_a, i) == &a[static_cast(i)]); } - EXPECT_DEATH(gsl::at(a, -1), ".*"); - EXPECT_DEATH(gsl::at(a, 4), ".*"); - EXPECT_DEATH(gsl::at(c_a, -1), ".*"); - EXPECT_DEATH(gsl::at(c_a, 4), ".*"); + std::set_terminate([] { + std::cerr << "Expected Death. std_vector"; + std::abort(); + }); + + EXPECT_DEATH(gsl::at(a, -1), deathstring.data()); + EXPECT_DEATH(gsl::at(a, 4), deathstring.data()); + EXPECT_DEATH(gsl::at(c_a, -1), deathstring.data()); + EXPECT_DEATH(gsl::at(c_a, 4), deathstring.data()); } TEST(at_tests, InitializerList) @@ -96,11 +115,16 @@ TEST(at_tests, InitializerList) EXPECT_TRUE(gsl::at(a, i) == i + 1); EXPECT_TRUE(gsl::at({1, 2, 3, 4}, i) == i + 1); } + + std::set_terminate([] { + std::cerr << "Expected Death. InitializerList"; + std::abort(); + }); - EXPECT_DEATH(gsl::at(a, -1), ".*"); - EXPECT_DEATH(gsl::at(a, 4), ".*"); - EXPECT_DEATH(gsl::at({1, 2, 3, 4}, -1), ".*"); - EXPECT_DEATH(gsl::at({1, 2, 3, 4}, 4), ".*"); + EXPECT_DEATH(gsl::at(a, -1), deathstring.data()); + EXPECT_DEATH(gsl::at(a, 4), deathstring.data()); + EXPECT_DEATH(gsl::at({1, 2, 3, 4}, -1), deathstring.data()); + EXPECT_DEATH(gsl::at({1, 2, 3, 4}, 4), deathstring.data()); } #if !defined(_MSC_VER) || defined(__clang__) || _MSC_VER >= 1910 diff --git a/tests/bounds_tests.cpp b/tests/bounds_tests.cpp index d08c018..5e73042 100644 --- a/tests/bounds_tests.cpp +++ b/tests/bounds_tests.cpp @@ -107,6 +107,11 @@ TEST(bounds_tests, bounds_convertible) static_bounds b5; static_bounds<34> b6; + std::set_terminate([] { + std::cerr << "Expected Death. bounds_convertible"; + std::abort(); + }); + b5 = static_bounds<20>(); EXPECT_DEATH(b6 = b5, ".*"); b5 = static_bounds<34>(); diff --git a/tests/multi_span_tests.cpp b/tests/multi_span_tests.cpp index 5005eaa..24bcf61 100644 --- a/tests/multi_span_tests.cpp +++ b/tests/multi_span_tests.cpp @@ -138,6 +138,8 @@ void fn(const Bounds&) { static_assert(Bounds::static_size == 60, "static bounds is wrong size"); } + +constexpr std::string_view deathstring("Expected Death"); } // namespace TEST(multi_span_test, default_constructor) @@ -253,20 +255,25 @@ TEST(multi_span_test, from_nullptr_length_constructor) { EXPECT_TRUE(cs.data() == nullptr); } + std::set_terminate([] { + std::cerr << "Expected Death. from_nullptr_length_constructor"; + std::abort(); + }); + { auto workaround_macro = []() { const multi_span s{nullptr, 1}; }; - EXPECT_DEATH(workaround_macro(), ".*"); + EXPECT_DEATH(workaround_macro(), deathstring.data()); auto const_workaround_macro = []() { const multi_span cs{nullptr, 1}; }; - EXPECT_DEATH(const_workaround_macro(), ".*"); + EXPECT_DEATH(const_workaround_macro(), deathstring.data()); } { auto workaround_macro = []() { const multi_span s{nullptr, 1}; }; - EXPECT_DEATH(workaround_macro(), ".*"); + EXPECT_DEATH(workaround_macro(), deathstring.data()); auto const_workaround_macro = []() { const multi_span s{nullptr, 1}; }; - EXPECT_DEATH(const_workaround_macro(), ".*"); + EXPECT_DEATH(const_workaround_macro(), deathstring.data()); } { @@ -370,10 +377,15 @@ TEST(multi_span_test, from_pointer_length_constructor) EXPECT_TRUE(s.data() == nullptr); } + std::set_terminate([] { + std::cerr << "Expected Death. from_pointer_length_constructor"; + std::abort(); + }); + { int* p = nullptr; auto workaround_macro = [=]() { const multi_span s{p, 2}; }; - EXPECT_DEATH(workaround_macro(), ".*"); + EXPECT_DEATH(workaround_macro(), deathstring.data()); } } @@ -409,27 +421,33 @@ TEST(multi_span_test, from_pointer_pointer_constructor) EXPECT_TRUE(s.data() == &arr[0]); } + + std::set_terminate([] { + std::cerr << "Expected Death. from_pointer_pointer_constructor"; + std::abort(); + }); + { auto workaround_macro = [&]() { const multi_span s{&arr[1], &arr[0]}; }; - EXPECT_DEATH(workaround_macro(), ".*"); + EXPECT_DEATH(workaround_macro(), deathstring.data()); } { int* p = nullptr; auto workaround_macro = [&]() { const multi_span s{&arr[0], p}; }; - EXPECT_DEATH(workaround_macro(), ".*"); + EXPECT_DEATH(workaround_macro(), deathstring.data()); } { int* p = nullptr; auto workaround_macro = [&]() { const multi_span s{p, p}; }; - EXPECT_DEATH(workaround_macro(), ".*"); + EXPECT_DEATH(workaround_macro(), deathstring.data()); } { int* p = nullptr; auto workaround_macro = [&]() { const multi_span s{&arr[0], p}; }; - EXPECT_DEATH(workaround_macro(), ".*"); + EXPECT_DEATH(workaround_macro(), deathstring.data()); } } @@ -578,11 +596,16 @@ TEST(multi_span_test, from_dynamic_array_constructor) { double(*arr)[3][4] = new double[100][3][4]; + std::set_terminate([] { + std::cerr << "Expected Death. from_nullptr_length_constructor"; + std::abort(); + }); + { multi_span s(arr, 10); EXPECT_TRUE(s.length() == 120); EXPECT_TRUE(s.data() == &arr[0][0][0]); - EXPECT_DEATH(s[10][3][4], ".*"); + EXPECT_DEATH(s[10][3][4], deathstring.data()); } { @@ -809,8 +832,13 @@ TEST(multi_span_test, from_convertible_span_constructor) #ifdef CONFIRM_COMPILATION_ERRORS multi_span av1(nullptr, b1); + std::set_terminate([] { + std::cerr << "Expected Death. from_convertible_span_constructor"; + std::abort(); + }); + auto f = [&]() { multi_span av1(nullptr); }; - EXPECT_DEATH(f(), ".*"); + EXPECT_DEATH(f(), deathstring.data()); #endif #ifdef CONFIRM_COMPILATION_ERRORS @@ -910,7 +938,13 @@ TEST(multi_span_test, first) EXPECT_TRUE(av.first<6>().length() == 6); EXPECT_TRUE(av.first<-1>().length() == -1); #endif - EXPECT_DEATH(av.first(6).length(), ".*"); + + std::set_terminate([] { + std::cerr << "Expected Death. first"; + std::abort(); + }); + + EXPECT_DEATH(av.first(6).length(), deathstring.data()); } { @@ -952,7 +986,13 @@ TEST(multi_span_test, last) EXPECT_TRUE(av.last<6>().bounds() == static_bounds<6>()); EXPECT_TRUE(av.last<6>().length() == 6); #endif - EXPECT_DEATH(av.last(6).length(), ".*"); + + std::set_terminate([] { + std::cerr << "Expected Death. last"; + std::abort(); + }); + + EXPECT_DEATH(av.last(6).length(), deathstring.data()); } { @@ -982,13 +1022,18 @@ TEST(multi_span_test, subspan) EXPECT_TRUE(av.subspan(0, 0).length() == 0); } + + std::set_terminate([] { + std::cerr << "Expected Death. subspan"; + std::abort(); + }); { multi_span av = arr; EXPECT_TRUE((av.subspan<0, 5>().bounds()) == static_bounds<5>()); EXPECT_TRUE((av.subspan<0, 5>().length()) == 5); EXPECT_TRUE(av.subspan(0, 5).length() == 5); - EXPECT_DEATH(av.subspan(0, 6).length(), ".*"); - EXPECT_DEATH(av.subspan(1, 5).length(), ".*"); + EXPECT_DEATH(av.subspan(0, 6).length(), deathstring.data()); + EXPECT_DEATH(av.subspan(1, 5).length(), deathstring.data()); } { @@ -996,7 +1041,7 @@ TEST(multi_span_test, subspan) EXPECT_TRUE((av.subspan<5, 0>().bounds()) == static_bounds<0>()); EXPECT_TRUE((av.subspan<5, 0>().length()) == 0); EXPECT_TRUE(av.subspan(5, 0).length() == 0); - EXPECT_DEATH(av.subspan(6, 0).length(), ".*"); + EXPECT_DEATH(av.subspan(6, 0).length(), deathstring.data()); } { @@ -1004,13 +1049,13 @@ TEST(multi_span_test, subspan) EXPECT_TRUE((av.subspan<0, 0>().bounds()) == static_bounds<0>()); EXPECT_TRUE((av.subspan<0, 0>().length()) == 0); EXPECT_TRUE(av.subspan(0, 0).length() == 0); - EXPECT_DEATH((av.subspan<1, 0>().length()), ".*"); + EXPECT_DEATH((av.subspan<1, 0>().length()), deathstring.data()); } { multi_span av; EXPECT_TRUE(av.subspan(0).length() == 0); - EXPECT_DEATH(av.subspan(1).length(), ".*"); + EXPECT_DEATH(av.subspan(1).length(), deathstring.data()); } { @@ -1019,7 +1064,7 @@ TEST(multi_span_test, subspan) EXPECT_TRUE(av.subspan(1).length() == 4); EXPECT_TRUE(av.subspan(4).length() == 1); EXPECT_TRUE(av.subspan(5).length() == 0); - EXPECT_DEATH(av.subspan(6).length(), ".*"); + EXPECT_DEATH(av.subspan(6).length(), deathstring.data()); auto av2 = av.subspan(1); for (int i = 0; i < 4; ++i) EXPECT_TRUE(av2[i] == i + 2); } @@ -1030,7 +1075,7 @@ TEST(multi_span_test, subspan) EXPECT_TRUE(av.subspan(1).length() == 4); EXPECT_TRUE(av.subspan(4).length() == 1); EXPECT_TRUE(av.subspan(5).length() == 0); - EXPECT_DEATH(av.subspan(6).length(), ".*"); + EXPECT_DEATH(av.subspan(6).length(), deathstring.data()); auto av2 = av.subspan(1); for (int i = 0; i < 4; ++i) EXPECT_TRUE(av2[i] == i + 2); } @@ -1059,11 +1104,16 @@ TEST(multi_span_test, rank) TEST(multi_span_test, extent) { + std::set_terminate([] { + std::cerr << "Expected Death. extent"; + std::abort(); + }); + { multi_span s; EXPECT_TRUE(s.extent() == 0); EXPECT_TRUE(s.extent(0) == 0); - EXPECT_DEATH(s.extent(1), ".*"); + EXPECT_DEATH(s.extent(1), deathstring.data()); #ifdef CONFIRM_COMPILATION_ERRORS EXPECT_TRUE(s.extent<1>() == 0); #endif @@ -1073,7 +1123,7 @@ TEST(multi_span_test, extent) multi_span s; EXPECT_TRUE(s.extent() == 0); EXPECT_TRUE(s.extent(0) == 0); - EXPECT_DEATH(s.extent(1), ".*"); + EXPECT_DEATH(s.extent(1), deathstring.data()); } { @@ -1085,7 +1135,7 @@ TEST(multi_span_test, extent) EXPECT_TRUE(s.extent<1>() == 2); EXPECT_TRUE(s.extent(0) == 1); EXPECT_TRUE(s.extent(1) == 2); - EXPECT_DEATH(s.extent(3), ".*"); + EXPECT_DEATH(s.extent(3), deathstring.data()); } { @@ -1097,7 +1147,7 @@ TEST(multi_span_test, extent) EXPECT_TRUE(s.extent<1>() == 2); EXPECT_TRUE(s.extent(0) == 0); EXPECT_TRUE(s.extent(1) == 2); - EXPECT_DEATH(s.extent(3), ".*"); + EXPECT_DEATH(s.extent(3), deathstring.data()); } } @@ -1105,10 +1155,15 @@ TEST(multi_span_test, operator_function_call) { int arr[4] = {1, 2, 3, 4}; + std::set_terminate([] { + std::cerr << "Expected Death. operator_function_call"; + std::abort(); + }); + { multi_span s = arr; EXPECT_TRUE(s(0) == 1); - EXPECT_DEATH(s(5), ".*"); + EXPECT_DEATH(s(5), deathstring.data()); } int arr2d[2][3] = {1, 2, 3, 4, 5, 6}; @@ -1261,18 +1316,23 @@ TEST(multi_span_test, bounds_checks) av[2][0] = 1; av[1][1] = 3; + std::set_terminate([] { + std::cerr << "Expected Death. bounds_check"; + std::abort(); + }); + // out of bounds - EXPECT_DEATH(av[1][3] = 3, ".*"); - EXPECT_DEATH((av[{1, 3}] = 3), ".*"); + EXPECT_DEATH(av[1][3] = 3, deathstring.data()); + EXPECT_DEATH((av[{1, 3}] = 3), deathstring.data()); - EXPECT_DEATH(av[10][2], ".*"); - EXPECT_DEATH((av[{10, 2}]), ".*"); + EXPECT_DEATH(av[10][2], deathstring.data()); + EXPECT_DEATH((av[{10, 2}]), deathstring.data()); - EXPECT_DEATH(av[-1][0], ".*"); - EXPECT_DEATH((av[{-1, 0}]), ".*"); + EXPECT_DEATH(av[-1][0], deathstring.data()); + EXPECT_DEATH((av[{-1, 0}]), deathstring.data()); - EXPECT_DEATH(av[0][-1], ".*"); - EXPECT_DEATH((av[{0, -1}]), ".*"); + EXPECT_DEATH(av[0][-1], deathstring.data()); + EXPECT_DEATH((av[{0, -1}]), deathstring.data()); } TEST(multi_span_test, span_parameter_test) @@ -1390,13 +1450,18 @@ TEST(multi_span_test, as_multi_span) TEST(multi_span_test, empty_spans) { + std::set_terminate([] { + std::cerr << "Expected Death. empty_spans"; + std::abort(); + }); + { multi_span empty_av(nullptr); EXPECT_TRUE(empty_av.bounds().index_bounds() == multi_span_index<1>{0}); - EXPECT_DEATH(empty_av[0], ".*"); - EXPECT_DEATH(empty_av.begin()[0], ".*"); - EXPECT_DEATH(empty_av.cbegin()[0], ".*"); + EXPECT_DEATH(empty_av[0], deathstring.data()); + EXPECT_DEATH(empty_av.begin()[0], deathstring.data()); + EXPECT_DEATH(empty_av.cbegin()[0], deathstring.data()); for (auto& v : empty_av) { (void) v; @@ -1407,9 +1472,9 @@ TEST(multi_span_test, empty_spans) { multi_span empty_av = {}; EXPECT_TRUE(empty_av.bounds().index_bounds() == multi_span_index<1>{0}); - EXPECT_DEATH(empty_av[0], ".*"); - EXPECT_DEATH(empty_av.begin()[0], ".*"); - EXPECT_DEATH(empty_av.cbegin()[0], ".*"); + EXPECT_DEATH(empty_av[0], deathstring.data()); + EXPECT_DEATH(empty_av.begin()[0], deathstring.data()); + EXPECT_DEATH(empty_av.cbegin()[0], deathstring.data()); for (auto& v : empty_av) { (void) v; @@ -1639,7 +1704,12 @@ TEST(multi_span_test, span_structure_size) }; EXPECT_TRUE(sizeof(av1) == sizeof(EffectiveStructure)); - EXPECT_DEATH(av1[10][3][4], ".*"); + std::set_terminate([] { + std::cerr << "Expected Death. span_structure_size"; + std::abort(); + }); + + EXPECT_DEATH(av1[10][3][4], deathstring.data()); multi_span av2 = as_multi_span(av1, dim(5), dim<6>(), dim<4>()); @@ -1727,12 +1797,18 @@ TEST(multi_span_test, fixed_size_conversions) multi_span av4 = av2; } #endif + + std::set_terminate([] { + std::cerr << "Expected Death. fixed_size_conversions"; + std::abort(); + }); + { auto f = [&]() { const multi_span av9 = {arr2, 2}; (void) av9; }; - EXPECT_DEATH(f(), ".*"); + EXPECT_DEATH(f(), deathstring.data()); } // this should fail - we are trying to assign a small dynamic a_v to a fixed_size larger one @@ -1741,7 +1817,7 @@ TEST(multi_span_test, fixed_size_conversions) const multi_span av2 = av; (void) av2; }; - EXPECT_DEATH(f(), ".*"); + EXPECT_DEATH(f(), deathstring.data()); } TEST(multi_span_test, as_writeable_bytes) diff --git a/tests/notnull_tests.cpp b/tests/notnull_tests.cpp index cccd77e..4ad7f8d 100644 --- a/tests/notnull_tests.cpp +++ b/tests/notnull_tests.cpp @@ -52,6 +52,10 @@ struct fail_fast; using namespace gsl; +namespace{ +constexpr std::string_view deathstring("Expected Death"); +} + struct MyBase { }; @@ -163,6 +167,10 @@ TEST(notnull_tests, TestNotNullConstructors) #endif } + std::set_terminate([] { + std::cerr << "Expected Death. TestNotNullConstructors"; + std::abort(); + }); { // from shared pointer int i = 12; @@ -174,7 +182,7 @@ TEST(notnull_tests, TestNotNullConstructors) std::make_shared(10)); // shared_ptr is nullptr assignable int* pi = nullptr; - EXPECT_DEATH((not_null(pi)), ".*"); + EXPECT_DEATH((not_null(pi)), deathstring.data()); } { @@ -231,8 +239,8 @@ TEST(notnull_tests, TestNotNullConstructors) { // from returned pointer - EXPECT_DEATH(helper(return_pointer()), ".*"); - EXPECT_DEATH(helper_const(return_pointer()), ".*"); + EXPECT_DEATH(helper(return_pointer()), deathstring.data()); + EXPECT_DEATH(helper_const(return_pointer()), deathstring.data()); } } @@ -292,12 +300,17 @@ TEST(notnull_tests, TestNotNullCasting) TEST(notnull_tests, TestNotNullAssignment) { + std::set_terminate([] { + std::cerr << "Expected Death. TestNotNullAssignmentd"; + std::abort(); + }); + int i = 12; not_null p(&i); EXPECT_TRUE(helper(p)); int* q = nullptr; - EXPECT_DEATH(p = not_null(q), ".*"); + EXPECT_DEATH(p = not_null(q), deathstring.data()); } TEST(notnull_tests, TestNotNullRawPointerComparison) @@ -445,13 +458,18 @@ TEST(notnull_tests, TestNotNullConstructorTypeDeduction) EXPECT_TRUE(*x == 42); } + + std::set_terminate([] { + std::cerr << "Expected Death. TestNotNullConstructorTypeDeduction"; + std::abort(); + }); { auto workaround_macro = []() { int* p1 = nullptr; const not_null x{p1}; }; - EXPECT_DEATH(workaround_macro(), ".*"); + EXPECT_DEATH(workaround_macro(), deathstring.data()); } { @@ -459,14 +477,14 @@ TEST(notnull_tests, TestNotNullConstructorTypeDeduction) const int* p1 = nullptr; const not_null x{p1}; }; - EXPECT_DEATH(workaround_macro(), ".*"); + EXPECT_DEATH(workaround_macro(), deathstring.data()); } { int* p = nullptr; - EXPECT_DEATH(helper(not_null{p}), ".*"); - EXPECT_DEATH(helper_const(not_null{p}), ".*"); + EXPECT_DEATH(helper(not_null{p}), deathstring.data()); + EXPECT_DEATH(helper_const(not_null{p}), deathstring.data()); } #ifdef CONFIRM_COMPILATION_ERRORS @@ -502,13 +520,18 @@ TEST(notnull_tests, TestMakeNotNull) EXPECT_TRUE(*x == 42); } + std::set_terminate([] { + std::cerr << "Expected Death. TestMakeNotNull"; + std::abort(); + }); + { const auto workaround_macro = []() { int* p1 = nullptr; const auto x = make_not_null(p1); EXPECT_TRUE(*x == 42); }; - EXPECT_DEATH(workaround_macro(), ".*"); + EXPECT_DEATH(workaround_macro(), deathstring.data()); } { @@ -517,21 +540,21 @@ TEST(notnull_tests, TestMakeNotNull) const auto x = make_not_null(p1); EXPECT_TRUE(*x == 42); }; - EXPECT_DEATH(workaround_macro(), ".*"); + EXPECT_DEATH(workaround_macro(), deathstring.data()); } { int* p = nullptr; - EXPECT_DEATH(helper(make_not_null(p)), ".*"); - EXPECT_DEATH(helper_const(make_not_null(p)), ".*"); + EXPECT_DEATH(helper(make_not_null(p)), deathstring.data()); + EXPECT_DEATH(helper_const(make_not_null(p)), deathstring.data()); } #ifdef CONFIRM_COMPILATION_ERRORS { - EXPECT_DEATH(make_not_null(nullptr), ".*"); - EXPECT_DEATH(helper(make_not_null(nullptr)), ".*"); - EXPECT_DEATH(helper_const(make_not_null(nullptr)), ".*"); + EXPECT_DEATH(make_not_null(nullptr), deathstring.data()); + EXPECT_DEATH(helper(make_not_null(nullptr)), deathstring.data()); + EXPECT_DEATH(helper_const(make_not_null(nullptr)), deathstring.data()); } #endif } diff --git a/tests/span_tests.cpp b/tests/span_tests.cpp index cdc2f72..7746c0a 100644 --- a/tests/span_tests.cpp +++ b/tests/span_tests.cpp @@ -75,6 +75,7 @@ struct AddressOverloaded return {}; } }; +constexpr std::string_view deathstring("Expected Death"); } // namespace TEST(span_test, constructors) @@ -121,6 +122,10 @@ TEST(span_test, size_optimization) TEST(span_test, from_nullptr_size_constructor) { + std::set_terminate([] { + std::cerr << "Expected Death. from_nullptr_size_constructor"; + std::abort(); + }); { span s{nullptr, narrow_cast::index_type>(0)}; EXPECT_TRUE(s.size() == 0); @@ -134,21 +139,21 @@ TEST(span_test, from_nullptr_size_constructor) auto workaround_macro = []() { const span s{nullptr, narrow_cast::index_type>(0)}; }; - EXPECT_DEATH(workaround_macro(), ".*"); + EXPECT_DEATH(workaround_macro(), deathstring.data()); } { auto workaround_macro = []() { const span s{nullptr, 1}; }; - EXPECT_DEATH(workaround_macro(), ".*"); + EXPECT_DEATH(workaround_macro(), deathstring.data()); auto const_workaround_macro = []() { const span s{nullptr, 1}; }; - EXPECT_DEATH(const_workaround_macro(), ".*"); + EXPECT_DEATH(const_workaround_macro(), deathstring.data()); } { auto workaround_macro = []() { const span s{nullptr, 1}; }; - EXPECT_DEATH(workaround_macro(), ".*"); + EXPECT_DEATH(workaround_macro(), deathstring.data()); auto const_workaround_macro = []() { const span s{nullptr, 1}; }; - EXPECT_DEATH(const_workaround_macro(), ".*"); + EXPECT_DEATH(const_workaround_macro(), deathstring.data()); } { span s{nullptr, narrow_cast::index_type>(0)}; @@ -163,6 +168,10 @@ TEST(span_test, from_nullptr_size_constructor) TEST(span_test, from_pointer_length_constructor) { + std::set_terminate([] { + std::cerr << "Expected Death. from_pointer_length_constructor"; + std::abort(); + }); int arr[4] = {1, 2, 3, 4}; { @@ -214,7 +223,7 @@ TEST(span_test, from_pointer_length_constructor) { int* p = nullptr; auto workaround_macro = [=]() { const span s{p, 2}; }; - EXPECT_DEATH(workaround_macro(), ".*"); + EXPECT_DEATH(workaround_macro(), deathstring.data()); } { @@ -235,7 +244,7 @@ TEST(span_test, from_pointer_length_constructor) { int* p = nullptr; auto workaround_macro = [=]() { make_span(p, 2); }; - EXPECT_DEATH(workaround_macro(), ".*"); + EXPECT_DEATH(workaround_macro(), deathstring.data()); } } @@ -273,14 +282,14 @@ TEST(span_test, from_pointer_pointer_construction) // this will fail the std::distance() precondition, which asserts on MSVC debug builds //{ // auto workaround_macro = [&]() { span s{&arr[1], &arr[0]}; }; - // EXPECT_DEATH(workaround_macro(), ".*"); + // EXPECT_DEATH(workaround_macro(), deathstring.data()); //} // this will fail the std::distance() precondition, which asserts on MSVC debug builds //{ // int* p = nullptr; // auto workaround_macro = [&]() { span s{&arr[0], p}; }; - // EXPECT_DEATH(workaround_macro(), ".*"); + // EXPECT_DEATH(workaround_macro(), deathstring.data()); //} { @@ -301,7 +310,7 @@ TEST(span_test, from_pointer_pointer_construction) //{ // int* p = nullptr; // auto workaround_macro = [&]() { span s{&arr[0], p}; }; - // EXPECT_DEATH(workaround_macro(), ".*"); + // EXPECT_DEATH(workaround_macro(), deathstring.data()); //} { @@ -823,6 +832,10 @@ TEST(span_test, from_array_constructor) TEST(span_test, first) { + std::set_terminate([] { + std::cerr << "Expected Death. first"; + std::abort(); + }); int arr[5] = {1, 2, 3, 4, 5}; { @@ -849,7 +862,7 @@ TEST(span_test, from_array_constructor) EXPECT_TRUE(av.first<6>().size() == 6); EXPECT_TRUE(av.first<-1>().size() == -1); #endif - EXPECT_DEATH(av.first(6).size(), ".*"); + EXPECT_DEATH(av.first(6).size(), deathstring.data()); } { @@ -861,6 +874,10 @@ TEST(span_test, from_array_constructor) TEST(span_test, last) { + std::set_terminate([] { + std::cerr << "Expected Death. last"; + std::abort(); + }); int arr[5] = {1, 2, 3, 4, 5}; { @@ -886,7 +903,7 @@ TEST(span_test, from_array_constructor) #ifdef CONFIRM_COMPILATION_ERRORS EXPECT_TRUE(av.last<6>().size() == 6); #endif - EXPECT_DEATH(av.last(6).size(), ".*"); + EXPECT_DEATH(av.last(6).size(), deathstring.data()); } { @@ -898,6 +915,10 @@ TEST(span_test, from_array_constructor) TEST(span_test, subspan) { + std::set_terminate([] { + std::cerr << "Expected Death. subspan"; + std::abort(); + }); int arr[5] = {1, 2, 3, 4, 5}; { @@ -921,8 +942,8 @@ TEST(span_test, from_array_constructor) EXPECT_TRUE(decltype(av.subspan<0, 5>())::extent == 5); EXPECT_TRUE(av.subspan(0, 5).size() == 5); - EXPECT_DEATH(av.subspan(0, 6).size(), ".*"); - EXPECT_DEATH(av.subspan(1, 5).size(), ".*"); + EXPECT_DEATH(av.subspan(0, 6).size(), deathstring.data()); + EXPECT_DEATH(av.subspan(1, 5).size(), deathstring.data()); } { @@ -931,7 +952,7 @@ TEST(span_test, from_array_constructor) EXPECT_TRUE(decltype(av.subspan<4, 0>())::extent == 0); EXPECT_TRUE(av.subspan(4, 0).size() == 0); EXPECT_TRUE(av.subspan(5, 0).size() == 0); - EXPECT_DEATH(av.subspan(6, 0).size(), ".*"); + EXPECT_DEATH(av.subspan(6, 0).size(), deathstring.data()); } { @@ -945,13 +966,13 @@ TEST(span_test, from_array_constructor) EXPECT_TRUE((av.subspan<0, 0>().size()) == 0); EXPECT_TRUE(decltype(av.subspan<0, 0>())::extent == 0); EXPECT_TRUE(av.subspan(0, 0).size() == 0); - EXPECT_DEATH((av.subspan<1, 0>().size()), ".*"); + EXPECT_DEATH((av.subspan<1, 0>().size()), deathstring.data()); } { span av; EXPECT_TRUE(av.subspan(0).size() == 0); - EXPECT_DEATH(av.subspan(1).size(), ".*"); + EXPECT_DEATH(av.subspan(1).size(), deathstring.data()); } { @@ -960,7 +981,7 @@ TEST(span_test, from_array_constructor) EXPECT_TRUE(av.subspan(1).size() == 4); EXPECT_TRUE(av.subspan(4).size() == 1); EXPECT_TRUE(av.subspan(5).size() == 0); - EXPECT_DEATH(av.subspan(6).size(), ".*"); + EXPECT_DEATH(av.subspan(6).size(), deathstring.data()); const auto av2 = av.subspan(1); for (int i = 0; i < 4; ++i) EXPECT_TRUE(av2[i] == i + 2); } @@ -971,7 +992,7 @@ TEST(span_test, from_array_constructor) EXPECT_TRUE(av.subspan(1).size() == 4); EXPECT_TRUE(av.subspan(4).size() == 1); EXPECT_TRUE(av.subspan(5).size() == 0); - EXPECT_DEATH(av.subspan(6).size(), ".*"); + EXPECT_DEATH(av.subspan(6).size(), deathstring.data()); const auto av2 = av.subspan(1); for (int i = 0; i < 4; ++i) EXPECT_TRUE(av2[i] == i + 2); } @@ -979,12 +1000,16 @@ TEST(span_test, from_array_constructor) TEST(span_test, at_call) { + std::set_terminate([] { + std::cerr << "Expected Death. at_call"; + std::abort(); + }); int arr[4] = {1, 2, 3, 4}; { span s = arr; EXPECT_TRUE(s.at(0) == 1); - EXPECT_DEATH(s.at(5), ".*"); + EXPECT_DEATH(s.at(5), deathstring.data()); } { @@ -992,18 +1017,22 @@ TEST(span_test, from_array_constructor) span s = arr2d; EXPECT_TRUE(s.at(0) == 1); EXPECT_TRUE(s.at(1) == 6); - EXPECT_DEATH(s.at(2), ".*"); + EXPECT_DEATH(s.at(2), deathstring.data()); } } TEST(span_test, operator_function_call) { + std::set_terminate([] { + std::cerr << "Expected Death. operator_function_call"; + std::abort(); + }); int arr[4] = {1, 2, 3, 4}; { span s = arr; EXPECT_TRUE(s(0) == 1); - EXPECT_DEATH(s(5), ".*"); + EXPECT_DEATH(s(5), deathstring.data()); } { @@ -1011,7 +1040,7 @@ TEST(span_test, from_array_constructor) span s = arr2d; EXPECT_TRUE(s(0) == 1); EXPECT_TRUE(s(1) == 6); - EXPECT_DEATH(s(2), ".*"); + EXPECT_DEATH(s(2), deathstring.data()); } } @@ -1100,6 +1129,10 @@ TEST(span_test, from_array_constructor) TEST(span_test, begin_end) { + std::set_terminate([] { + std::cerr << "Expected Death. begin_end"; + std::abort(); + }); { int a[] = {1, 2, 3, 4}; span s = a; @@ -1124,7 +1157,7 @@ TEST(span_test, from_array_constructor) auto beyond = s.end(); EXPECT_TRUE(it != beyond); - EXPECT_DEATH(*beyond, ".*"); + EXPECT_DEATH(*beyond, deathstring.data()); EXPECT_TRUE(beyond - first == 4); EXPECT_TRUE(first - first == 0); @@ -1154,6 +1187,10 @@ TEST(span_test, from_array_constructor) TEST(span_test, cbegin_cend) { + std::set_terminate([] { + std::cerr << "Expected Death. cbegin_cend"; + std::abort(); + }); { int a[] = {1, 2, 3, 4}; span s = a; @@ -1178,7 +1215,7 @@ TEST(span_test, from_array_constructor) auto beyond = s.cend(); EXPECT_TRUE(it != beyond); - EXPECT_DEATH(*beyond, ".*"); + EXPECT_DEATH(*beyond, deathstring.data()); EXPECT_TRUE(beyond - first == 4); EXPECT_TRUE(first - first == 0); @@ -1207,6 +1244,10 @@ TEST(span_test, from_array_constructor) TEST(span_test, rbegin_rend) { + std::set_terminate([] { + std::cerr << "Expected Death. rbegin_rend"; + std::abort(); + }); { int a[] = {1, 2, 3, 4}; span s = a; @@ -1218,7 +1259,7 @@ TEST(span_test, from_array_constructor) auto beyond = s.rend(); EXPECT_TRUE(it != beyond); - EXPECT_DEATH(auto _ = *beyond , ".*"); + EXPECT_DEATH(auto _ = *beyond , deathstring.data()); EXPECT_TRUE(beyond - first == 4); EXPECT_TRUE(first - first == 0); @@ -1248,6 +1289,10 @@ TEST(span_test, from_array_constructor) TEST(span_test, crbegin_crend) { + std::set_terminate([] { + std::cerr << "Expected Death. crbegin_crend"; + std::abort(); + }); { int a[] = {1, 2, 3, 4}; span s = a; @@ -1259,7 +1304,7 @@ TEST(span_test, from_array_constructor) auto beyond = s.crend(); EXPECT_TRUE(it != beyond); - EXPECT_DEATH(auto _ = *beyond, ".*"); + EXPECT_DEATH(auto _ = *beyond, deathstring.data()); EXPECT_TRUE(beyond - first == 4); EXPECT_TRUE(first - first == 0); @@ -1472,6 +1517,10 @@ TEST(span_test, from_array_constructor) TEST(span_test, fixed_size_conversions) { + std::set_terminate([] { + std::cerr << "Expected Death. fixed_size_conversions"; + std::abort(); + }); int arr[] = {1, 2, 3, 4}; // converting to an span from an equal size array is ok @@ -1503,7 +1552,7 @@ TEST(span_test, from_array_constructor) const span s2 = s; static_cast(s2); }; - EXPECT_DEATH(f(), ".*"); + EXPECT_DEATH(f(), deathstring.data()); } // but doing so explicitly is ok @@ -1542,7 +1591,7 @@ TEST(span_test, from_array_constructor) const span _s4 = {arr2, 2}; static_cast(_s4); }; - EXPECT_DEATH(f(), ".*"); + EXPECT_DEATH(f(), deathstring.data()); } // this should fail - we are trying to assign a small dynamic span to a fixed_size larger one @@ -1550,7 +1599,7 @@ TEST(span_test, from_array_constructor) const span _s4 = av; static_cast(_s4); }; - EXPECT_DEATH(f(), ".*"); + EXPECT_DEATH(f(), deathstring.data()); } TEST(span_test, interop_with_std_regex) diff --git a/tests/strict_notnull_tests.cpp b/tests/strict_notnull_tests.cpp index 649007c..faa51c9 100644 --- a/tests/strict_notnull_tests.cpp +++ b/tests/strict_notnull_tests.cpp @@ -42,6 +42,10 @@ namespace gsl struct fail_fast; } // namespace gsl +namespace{ +constexpr std::string_view deathstring("Expected Death"); +} + using namespace gsl; GSL_SUPPRESS(f.4) // NO-FORMAT: attribute @@ -146,6 +150,11 @@ TEST(strict_notnull_tests, TestStrictNotNull) TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction) { + std::set_terminate([] { + std::cerr << "Expected Death. TestStrictNotNullConstructorTypeDeduction"; + std::abort(); + }); + { int i = 42; @@ -172,7 +181,7 @@ TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction) int* p1 = nullptr; const strict_not_null x{p1}; }; - EXPECT_DEATH(workaround_macro(), ".*"); + EXPECT_DEATH(workaround_macro(), deathstring.data()); } { @@ -180,14 +189,14 @@ TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction) const int* p1 = nullptr; const strict_not_null x{p1}; }; - EXPECT_DEATH(workaround_macro(), ".*"); + EXPECT_DEATH(workaround_macro(), deathstring.data()); } { int* p = nullptr; - EXPECT_DEATH(helper(strict_not_null{p}), ".*"); - EXPECT_DEATH(helper_const(strict_not_null{p}), ".*"); + EXPECT_DEATH(helper(strict_not_null{p}), deathstring.data()); + EXPECT_DEATH(helper_const(strict_not_null{p}), deathstring.data()); } #ifdef CONFIRM_COMPILATION_ERRORS diff --git a/tests/strided_span_tests.cpp b/tests/strided_span_tests.cpp index bc6d899..3016735 100644 --- a/tests/strided_span_tests.cpp +++ b/tests/strided_span_tests.cpp @@ -56,6 +56,7 @@ using namespace gsl; namespace { +constexpr std::string_view deathstring("Expected Death"); struct BaseClass { }; @@ -419,19 +420,24 @@ TEST(strided_span_tests, strided_span_bounds) { int arr[] = {0, 1, 2, 3}; multi_span av(arr); + + std::set_terminate([] { + std::cerr << "Expected Death. strided_span_bounds"; + std::abort(); + }); { // incorrect sections - EXPECT_DEATH(av.section(0, 0)[0], ".*"); - EXPECT_DEATH(av.section(1, 0)[0], ".*"); - EXPECT_DEATH(av.section(1, 1)[1], ".*"); + EXPECT_DEATH(av.section(0, 0)[0], deathstring.data()); + EXPECT_DEATH(av.section(1, 0)[0], deathstring.data()); + EXPECT_DEATH(av.section(1, 1)[1], deathstring.data()); - EXPECT_DEATH(av.section(2, 5), ".*"); - EXPECT_DEATH(av.section(5, 2), ".*"); - EXPECT_DEATH(av.section(5, 0), ".*"); - EXPECT_DEATH(av.section(0, 5), ".*"); - EXPECT_DEATH(av.section(5, 5), ".*"); + EXPECT_DEATH(av.section(2, 5), deathstring.data()); + EXPECT_DEATH(av.section(5, 2), deathstring.data()); + EXPECT_DEATH(av.section(5, 0), deathstring.data()); + EXPECT_DEATH(av.section(0, 5), deathstring.data()); + EXPECT_DEATH(av.section(5, 5), deathstring.data()); } { @@ -439,19 +445,19 @@ TEST(strided_span_tests, strided_span_bounds) strided_span sav{av, {{4}, {}}}; EXPECT_TRUE(sav[0] == 0); EXPECT_TRUE(sav[3] == 0); - EXPECT_DEATH(sav[4], ".*"); + EXPECT_DEATH(sav[4], deathstring.data()); } { // zero extent strided_span sav{av, {{}, {1}}}; - EXPECT_DEATH(sav[0], ".*"); + EXPECT_DEATH(sav[0], deathstring.data()); } { // zero extent and stride strided_span sav{av, {{}, {}}}; - EXPECT_DEATH(sav[0], ".*"); + EXPECT_DEATH(sav[0], deathstring.data()); } { @@ -459,7 +465,7 @@ TEST(strided_span_tests, strided_span_bounds) strided_span sav{arr, {4, 1}}; EXPECT_TRUE(sav.bounds().index_bounds() == multi_span_index<1>{4}); EXPECT_TRUE(sav[3] == 3); - EXPECT_DEATH(sav[4], ".*"); + EXPECT_DEATH(sav[4], deathstring.data()); } { @@ -467,7 +473,7 @@ TEST(strided_span_tests, strided_span_bounds) strided_span sav{arr, {2, 1}}; EXPECT_TRUE(sav.bounds().index_bounds() == multi_span_index<1>{2}); EXPECT_TRUE(sav[1] == 1); - EXPECT_DEATH(sav[2], ".*"); + EXPECT_DEATH(sav[2], deathstring.data()); } { @@ -476,35 +482,35 @@ TEST(strided_span_tests, strided_span_bounds) EXPECT_TRUE(sav.bounds().index_bounds() == multi_span_index<1>{2}); EXPECT_TRUE(sav[0] == 0); EXPECT_TRUE(sav[1] == 3); - EXPECT_DEATH(sav[2], ".*"); + EXPECT_DEATH(sav[2], deathstring.data()); } { // bounds cross data boundaries - from static arrays - EXPECT_DEATH((strided_span{arr, {3, 2}}), ".*"); - EXPECT_DEATH((strided_span{arr, {3, 3}}), ".*"); - EXPECT_DEATH((strided_span{arr, {4, 5}}), ".*"); - EXPECT_DEATH((strided_span{arr, {5, 1}}), ".*"); - EXPECT_DEATH((strided_span{arr, {5, 5}}), ".*"); + EXPECT_DEATH((strided_span{arr, {3, 2}}), deathstring.data()); + EXPECT_DEATH((strided_span{arr, {3, 3}}), deathstring.data()); + EXPECT_DEATH((strided_span{arr, {4, 5}}), deathstring.data()); + EXPECT_DEATH((strided_span{arr, {5, 1}}), deathstring.data()); + EXPECT_DEATH((strided_span{arr, {5, 5}}), deathstring.data()); } { // bounds cross data boundaries - from array view - EXPECT_DEATH((strided_span{av, {3, 2}}), ".*"); - EXPECT_DEATH((strided_span{av, {3, 3}}), ".*"); - EXPECT_DEATH((strided_span{av, {4, 5}}), ".*"); - EXPECT_DEATH((strided_span{av, {5, 1}}), ".*"); - EXPECT_DEATH((strided_span{av, {5, 5}}), ".*"); + EXPECT_DEATH((strided_span{av, {3, 2}}), deathstring.data()); + EXPECT_DEATH((strided_span{av, {3, 3}}), deathstring.data()); + EXPECT_DEATH((strided_span{av, {4, 5}}), deathstring.data()); + EXPECT_DEATH((strided_span{av, {5, 1}}), deathstring.data()); + EXPECT_DEATH((strided_span{av, {5, 5}}), deathstring.data()); } { // bounds cross data boundaries - from dynamic arrays - EXPECT_DEATH((strided_span{av.data(), 4, {3, 2}}), ".*"); - EXPECT_DEATH((strided_span{av.data(), 4, {3, 3}}), ".*"); - EXPECT_DEATH((strided_span{av.data(), 4, {4, 5}}), ".*"); - EXPECT_DEATH((strided_span{av.data(), 4, {5, 1}}), ".*"); - EXPECT_DEATH((strided_span{av.data(), 4, {5, 5}}), ".*"); - EXPECT_DEATH((strided_span{av.data(), 2, {2, 2}}), ".*"); + EXPECT_DEATH((strided_span{av.data(), 4, {3, 2}}), deathstring.data()); + EXPECT_DEATH((strided_span{av.data(), 4, {3, 3}}), deathstring.data()); + EXPECT_DEATH((strided_span{av.data(), 4, {4, 5}}), deathstring.data()); + EXPECT_DEATH((strided_span{av.data(), 4, {5, 1}}), deathstring.data()); + EXPECT_DEATH((strided_span{av.data(), 4, {5, 5}}), deathstring.data()); + EXPECT_DEATH((strided_span{av.data(), 2, {2, 2}}), deathstring.data()); } #ifdef CONFIRM_COMPILATION_ERRORS @@ -535,6 +541,11 @@ TEST(strided_span_tests, strided_span_type_conversion) { int arr[] = {0, 1, 2, 3}; multi_span av(arr); + + std::set_terminate([] { + std::cerr << "Expected Death. strided_span_type_conversion"; + std::abort(); + }); { strided_span sav{av.data(), av.size(), {av.size() / 2, 2}}; @@ -558,8 +569,8 @@ TEST(strided_span_tests, strided_span_type_conversion) strided_span sav3 = sav2.as_strided_span(); EXPECT_TRUE(sav3[0][0] == 0); EXPECT_TRUE(sav3[1][0] == 2); - EXPECT_DEATH(sav3[1][1], ".*"); - EXPECT_DEATH(sav3[0][1], ".*"); + EXPECT_DEATH(sav3[1][1], deathstring.data()); + EXPECT_DEATH(sav3[0][1], deathstring.data()); } // retype strided array with regular strides - from multi_span @@ -571,8 +582,8 @@ TEST(strided_span_tests, strided_span_type_conversion) strided_span sav3 = sav2.as_strided_span(); EXPECT_TRUE(sav3[0][0] == 0); EXPECT_TRUE(sav3[1][0] == 2); - EXPECT_DEATH(sav3[1][1], ".*"); - EXPECT_DEATH(sav3[0][1], ".*"); + EXPECT_DEATH(sav3[1][1], deathstring.data()); + EXPECT_DEATH(sav3[0][1], deathstring.data()); } // retype strided array with not enough elements - last dimension of the array is too small @@ -581,7 +592,7 @@ TEST(strided_span_tests, strided_span_type_conversion) multi_span bytes2 = as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2)); strided_span sav2{bytes2, bounds}; - EXPECT_DEATH(sav2.as_strided_span(), ".*"); + EXPECT_DEATH(sav2.as_strided_span(), deathstring.data()); } // retype strided array with not enough elements - strides are too small @@ -590,7 +601,7 @@ TEST(strided_span_tests, strided_span_type_conversion) multi_span bytes2 = as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2)); strided_span sav2{bytes2, bounds}; - EXPECT_DEATH(sav2.as_strided_span(), ".*"); + EXPECT_DEATH(sav2.as_strided_span(), deathstring.data()); } // retype strided array with not enough elements - last dimension does not divide by the new @@ -600,7 +611,7 @@ TEST(strided_span_tests, strided_span_type_conversion) multi_span bytes2 = as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2)); strided_span sav2{bytes2, bounds}; - EXPECT_DEATH(sav2.as_strided_span(), ".*"); + EXPECT_DEATH(sav2.as_strided_span(), deathstring.data()); } // retype strided array with not enough elements - strides does not divide by the new @@ -610,35 +621,40 @@ TEST(strided_span_tests, strided_span_type_conversion) multi_span bytes2 = as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2)); strided_span sav2{bytes2, bounds}; - EXPECT_DEATH(sav2.as_strided_span(), ".*"); + EXPECT_DEATH(sav2.as_strided_span(), deathstring.data()); } // retype strided array with irregular strides - from raw data { strided_bounds<1> bounds{bytes.size() / 2, 2}; strided_span sav2{bytes.data(), bytes.size(), bounds}; - EXPECT_DEATH(sav2.as_strided_span(), ".*"); + EXPECT_DEATH(sav2.as_strided_span(), deathstring.data()); } // retype strided array with irregular strides - from multi_span { strided_bounds<1> bounds{bytes.size() / 2, 2}; strided_span sav2{bytes, bounds}; - EXPECT_DEATH(sav2.as_strided_span(), ".*"); + EXPECT_DEATH(sav2.as_strided_span(), deathstring.data()); } } TEST(strided_span_tests, empty_strided_spans) { + std::set_terminate([] { + std::cerr << "Expected Death. empty_strided_spans"; + std::abort(); + }); + { multi_span empty_av(nullptr); strided_span empty_sav{empty_av, {0, 1}}; EXPECT_TRUE(empty_sav.bounds().index_bounds() == multi_span_index<1>{0}); EXPECT_TRUE(empty_sav.empty()); - EXPECT_DEATH(empty_sav[0], ".*"); - EXPECT_DEATH(empty_sav.begin()[0], ".*"); - EXPECT_DEATH(empty_sav.cbegin()[0], ".*"); + EXPECT_DEATH(empty_sav[0], deathstring.data()); + EXPECT_DEATH(empty_sav.begin()[0], deathstring.data()); + EXPECT_DEATH(empty_sav.cbegin()[0], deathstring.data()); for (const auto& v : empty_sav) { (void) v; @@ -650,9 +666,9 @@ TEST(strided_span_tests, empty_strided_spans) strided_span empty_sav{nullptr, 0, {0, 1}}; EXPECT_TRUE(empty_sav.bounds().index_bounds() == multi_span_index<1>{0}); - EXPECT_DEATH(empty_sav[0], ".*"); - EXPECT_DEATH(empty_sav.begin()[0], ".*"); - EXPECT_DEATH(empty_sav.cbegin()[0], ".*"); + EXPECT_DEATH(empty_sav[0], deathstring.data()); + EXPECT_DEATH(empty_sav.begin()[0], deathstring.data()); + EXPECT_DEATH(empty_sav.cbegin()[0], deathstring.data()); for (const auto& v : empty_sav) { (void) v; @@ -740,6 +756,11 @@ TEST(strided_span_tests, dynamic_strided_span_section_iteration_3d) TEST(strided_span_tests, strided_span_conversion) { + std::set_terminate([] { + std::cerr << "Expected Death. strided_span_conversion"; + std::abort(); + }); + // get an multi_span of 'c' values from the list of X's struct X @@ -783,7 +804,7 @@ TEST(strided_span_tests, strided_span_conversion) strided_span result = transposed[0]; EXPECT_TRUE(result.bounds().index_bounds()[0] == 4); - EXPECT_DEATH(result.bounds().index_bounds()[1], ".*"); + EXPECT_DEATH(result.bounds().index_bounds()[1], deathstring.data()); int i = 0; for (auto& num : result) { diff --git a/tests/string_span_tests.cpp b/tests/string_span_tests.cpp index 9fd2d1b..64f7ea0 100644 --- a/tests/string_span_tests.cpp +++ b/tests/string_span_tests.cpp @@ -72,6 +72,7 @@ auto strnlen(const CharT* s, std::size_t n) namespace { +constexpr std::string_view deathstring("Expected Death"); template T move_wrapper(T&& t) @@ -964,6 +965,10 @@ TEST(string_span_tests, Conversion) TEST(string_span_tests, zstring) { + std::set_terminate([] { + std::cerr << "Expected Death. zstring"; + std::abort(); + }); // create zspan from zero terminated string { @@ -983,7 +988,7 @@ TEST(string_span_tests, zstring) buf[0] = 'a'; auto workaround_macro = [&]() { const zstring_span<> zspan({buf, 1}); }; - EXPECT_DEATH(workaround_macro(), ".*"); + EXPECT_DEATH(workaround_macro(), deathstring.data()); } // usage scenario: create zero-terminated temp file name and pass to a legacy API @@ -1001,7 +1006,11 @@ TEST(string_span_tests, zstring) TEST(string_span_tests, wzstring) { - + std::set_terminate([] { + std::cerr << "Expected Death. wzstring"; + std::abort(); + }); + // create zspan from zero terminated string { wchar_t buf[1]; @@ -1020,7 +1029,7 @@ TEST(string_span_tests, wzstring) buf[0] = L'a'; const auto workaround_macro = [&]() { const wzstring_span<> zspan({buf, 1}); }; - EXPECT_DEATH(workaround_macro(), ".*"); + EXPECT_DEATH(workaround_macro(), deathstring.data()); } // usage scenario: create zero-terminated temp file name and pass to a legacy API @@ -1037,7 +1046,11 @@ TEST(string_span_tests, wzstring) } TEST(string_span_tests, u16zstring) -{ +{ + std::set_terminate([] { + std::cerr << "Expected Death. u16zstring"; + std::abort(); + }); // create zspan from zero terminated string { @@ -1057,7 +1070,7 @@ TEST(string_span_tests, u16zstring) buf[0] = u'a'; const auto workaround_macro = [&]() { const u16zstring_span<> zspan({buf, 1}); }; - EXPECT_DEATH(workaround_macro(), ".*"); + EXPECT_DEATH(workaround_macro(), deathstring.data()); } // usage scenario: create zero-terminated temp file name and pass to a legacy API @@ -1075,6 +1088,10 @@ TEST(string_span_tests, u16zstring) TEST(string_span_tests, u32zstring) { + std::set_terminate([] { + std::cerr << "Expected Death. u31zstring"; + std::abort(); + }); // create zspan from zero terminated string { @@ -1094,7 +1111,7 @@ TEST(string_span_tests, u32zstring) buf[0] = u'a'; const auto workaround_macro = [&]() { const u32zstring_span<> zspan({buf, 1}); }; - EXPECT_DEATH(workaround_macro(), ".*"); + EXPECT_DEATH(workaround_macro(), deathstring.data()); } // usage scenario: create zero-terminated temp file name and pass to a legacy API diff --git a/tests/test.cpp b/tests/test.cpp deleted file mode 100644 index 735f85d..0000000 --- a/tests/test.cpp +++ /dev/null @@ -1,26 +0,0 @@ -/////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) 2015 Microsoft Corporation. All rights reserved. -// -// This code is licensed under the MIT License (MIT). -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// -/////////////////////////////////////////////////////////////////////////////// - -#define CATCH_CONFIG_MAIN - -#ifdef _MSC_VER - -// blanket turn off warnings from CppCoreCheck from catch -// so people aren't annoyed by them when running the tool. -#include -#pragma warning(disable : ALL_CODE_ANALYSIS_WARNINGS) // from catch -#endif // _MSC_VER - diff --git a/tests/utils_tests.cpp b/tests/utils_tests.cpp index 31eedb6..6393da4 100644 --- a/tests/utils_tests.cpp +++ b/tests/utils_tests.cpp @@ -44,9 +44,13 @@ using namespace gsl; +namespace{ +constexpr std::string_view deathstring("Expected Death"); void f(int& i) { i += 1; } static int j = 0; void g() { j += 1; } +} + TEST(utils_tests, sanity_check_for_gsl_index_typedef) { @@ -115,13 +119,18 @@ TEST(utils_tests, narrow_cast) } TEST(utils_tests, narrow) -{ +{ + std::set_terminate([] { + std::cerr << "Expected Death. narrow"; + std::abort(); + }); + int n = 120; const char c = narrow(n); EXPECT_TRUE(c == 120); n = 300; - EXPECT_DEATH(narrow(n), ".*"); + EXPECT_DEATH(narrow(n), deathstring.data()); const auto int32_max = std::numeric_limits::max(); const auto int32_min = std::numeric_limits::min(); @@ -130,11 +139,11 @@ TEST(utils_tests, narrow) EXPECT_TRUE(narrow(int32_t(1)) == 1); EXPECT_TRUE(narrow(int32_max) == static_cast(int32_max)); - EXPECT_DEATH(narrow(int32_t(-1)), ".*"); - EXPECT_DEATH(narrow(int32_min), ".*"); + EXPECT_DEATH(narrow(int32_t(-1)), deathstring.data()); + EXPECT_DEATH(narrow(int32_min), deathstring.data()); n = -42; - EXPECT_DEATH(narrow(n), ".*"); + EXPECT_DEATH(narrow(n), deathstring.data()); #if GSL_CONSTEXPR_NARROW static_assert(narrow(120) == 120, "Fix GSL_CONSTEXPR_NARROW");