assertions.hpp 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #pragma once
  2. #include <cmath>
  3. #include <exception>
  4. #include <xlnt/utils/exceptions.hpp>
  5. #define XLNT_STRINGIFYX(x) #x
  6. #define XLNT_STRINGIFY(x) XLNT_STRINGIFYX(x)
  7. #define xlnt_assert(expression) \
  8. do \
  9. { \
  10. try \
  11. { \
  12. if (expression) break; \
  13. } \
  14. catch (std::exception & ex) \
  15. { \
  16. throw ex; \
  17. } \
  18. catch (...) \
  19. { \
  20. } \
  21. throw xlnt::exception( \
  22. "assert failed at L:" XLNT_STRINGIFY(__LINE__) "\n" XLNT_STRINGIFY(expression)); \
  23. } while (false)
  24. #define xlnt_assert_throws_nothing(expression) \
  25. do \
  26. { \
  27. try \
  28. { \
  29. expression; \
  30. break; \
  31. } \
  32. catch (...) \
  33. { \
  34. } \
  35. throw xlnt::exception("assert throws nothing failed at L:" XLNT_STRINGIFY(__LINE__) "\n" XLNT_STRINGIFY(expression)); \
  36. } while (false)
  37. #define xlnt_assert_throws(expression, exception_type) \
  38. do \
  39. { \
  40. try \
  41. { \
  42. expression; \
  43. } \
  44. catch (exception_type) \
  45. { \
  46. break; \
  47. } \
  48. catch (...) \
  49. { \
  50. } \
  51. throw xlnt::exception("assert throws failed at L:" XLNT_STRINGIFY(__LINE__) "\n" XLNT_STRINGIFY(expression)); \
  52. } while (false)
  53. #define xlnt_assert_equals(left, right) xlnt_assert((left) == (right))
  54. #define xlnt_assert_differs(left, right) xlnt_assert((left) != (right))
  55. #define xlnt_assert_delta(left, right, delta) xlnt_assert(std::abs((left) - (right)) <= (delta))