windows_throw_last_error.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include<__vic/windows/throw_last_error.h>
  2. #include<__vic/windows/error.h>
  3. #include<windows.h>
  4. #include<iostream>
  5. #include<exception>
  6. #include<cstring>
  7. #include<cassert>
  8. void run_tests()
  9. {
  10. try
  11. {
  12. ::SetLastError(ERROR_ACCESS_DENIED);
  13. assert(::GetLastError() == ERROR_ACCESS_DENIED);
  14. __vic::windows::throw_last_error("");
  15. assert(false);
  16. }
  17. catch(const __vic::windows::error &ex)
  18. {
  19. __vic::windows::error err("", ex.code());
  20. assert(err.code() == ERROR_ACCESS_DENIED);
  21. assert(std::strcmp(ex.what(), err.what()) == 0);
  22. }
  23. const DWORD err_code = ERROR_INVALID_HANDLE;
  24. try
  25. {
  26. ::SetLastError(ERROR_SUCCESS);
  27. __vic::windows::throw_last_error("", err_code);
  28. assert(false);
  29. }
  30. catch(const __vic::windows::error &ex)
  31. {
  32. __vic::windows::error err("", err_code);
  33. assert(ex.code() == err_code);
  34. assert(ex.code() == err.code());
  35. assert(std::strcmp(ex.what(), err.what()) == 0);
  36. }
  37. }
  38. int main()
  39. {
  40. try
  41. {
  42. run_tests();
  43. return 0;
  44. }
  45. catch(const std::exception &ex)
  46. {
  47. std::cerr << ex.what() << '\n';
  48. }
  49. return 1;
  50. }