no_exception_ensure_tests.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
  4. //
  5. // This code is licensed under the MIT License (MIT).
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  8. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  9. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  10. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  11. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  12. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  13. // THE SOFTWARE.
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. #include <chrono>
  17. #include <cstdlib> // for std::exit
  18. #include <gsl/span> // for span
  19. #include <iostream>
  20. #include <thread>
  21. int operator_subscript_no_throw() noexcept
  22. {
  23. int arr[10];
  24. const gsl::span<int> sp{arr};
  25. return sp[11];
  26. }
  27. [[noreturn]] void test_terminate() { std::exit(0); }
  28. void setup_termination_handler() noexcept
  29. {
  30. #if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
  31. auto& handler = gsl::details::get_terminate_handler();
  32. handler = &test_terminate;
  33. #else
  34. std::set_terminate(test_terminate);
  35. #endif
  36. }
  37. int main() noexcept
  38. {
  39. std::cout << "Running main() from " __FILE__ "\n";
  40. #if defined(IOS_PROCESS_DELAY_WORKAROUND)
  41. std::this_thread::sleep_for(std::chrono::seconds(1));
  42. #endif
  43. setup_termination_handler();
  44. operator_subscript_no_throw();
  45. return -1;
  46. }