assert.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include <iostream>
  2. #include <cstdlib>
  3. #if __GNUC__ >= 4
  4. #pragma GCC diagnostic push
  5. #pragma GCC diagnostic ignored "-Wmissing-declarations"
  6. #endif
  7. #define equals(x,y) assertEquals(y, x, __LINE__)
  8. #define equalsNot(x,y) assertEqualsNot(y, x, __LINE__)
  9. template < typename X, typename Y >
  10. void OutputAssertEqual(X expect, char const* compare, Y get, unsigned long const &line) {
  11. std::cerr << "Test FAILED: »" << expect << "« " << compare << " »" << get << "« at line " << line << std::endl;
  12. std::exit(EXIT_FAILURE);
  13. }
  14. template < typename X, typename Y >
  15. void assertEquals(X expect, Y get, unsigned long const &line) {
  16. if (expect == get)
  17. return;
  18. OutputAssertEqual(expect, "==", get, line);
  19. }
  20. template < typename X, typename Y >
  21. void assertEqualsNot(X expect, Y get, unsigned long const &line) {
  22. if (expect != get)
  23. return;
  24. OutputAssertEqual(expect, "!=", get, line);
  25. }
  26. void assertEquals(unsigned int const &expect, int const &get, unsigned long const &line) {
  27. if (get < 0)
  28. OutputAssertEqual(expect, "==", get, line);
  29. assertEquals<unsigned int const&, unsigned int const&>(expect, get, line);
  30. }
  31. void assertEquals(int const &expect, unsigned int const &get, unsigned long const &line) {
  32. if (expect < 0)
  33. OutputAssertEqual(expect, "==", get, line);
  34. assertEquals<unsigned int const&, unsigned int const&>(expect, get, line);
  35. }
  36. void assertEquals(unsigned long const &expect, int const &get, unsigned long const &line) {
  37. if (get < 0)
  38. OutputAssertEqual(expect, "==", get, line);
  39. assertEquals<unsigned long const&, unsigned long const&>(expect, get, line);
  40. }
  41. void assertEquals(int const &expect, unsigned long const &get, unsigned long const &line) {
  42. if (expect < 0)
  43. OutputAssertEqual(expect, "==", get, line);
  44. assertEquals<unsigned long const&, unsigned long const&>(expect, get, line);
  45. }
  46. #define equalsOr2(x,y,z) assertEqualsOr2(y, z, x, __LINE__)
  47. template < typename X, typename Y >
  48. void OutputAssertEqualOr2(X expect1, X expect2, char const* compare, Y get, unsigned long const &line) {
  49. std::cerr << "Test FAILED: »" << expect1 << "« or »" << expect2 << "« " << compare << " »" << get << "« at line " << line << std::endl;
  50. }
  51. template < typename X, typename Y >
  52. void assertEqualsOr2(X expect1, X expect2, Y get, unsigned long const &line) {
  53. if (expect1 == get || expect2 == get)
  54. return;
  55. OutputAssertEqualOr2(expect1, expect2, "==", get, line);
  56. }
  57. void assertEqualsOr2(unsigned int const &expect1, unsigned int const &expect2, int const &get, unsigned long const &line) {
  58. if (get < 0)
  59. OutputAssertEqualOr2(expect1, expect2, "==", get, line);
  60. assertEqualsOr2<unsigned int const&, unsigned int const&>(expect1, expect2, get, line);
  61. }
  62. void assertEqualsOr2(int const &expect1, int const &expect2, unsigned int const &get, unsigned long const &line) {
  63. if (expect1 < 0 && expect2 < 0)
  64. OutputAssertEqualOr2(expect1, expect2, "==", get, line);
  65. assertEqualsOr2<unsigned int const&, unsigned int const&>(expect1, expect2, get, line);
  66. }
  67. #define equalsOr3(w,x,y,z) assertEqualsOr3(x, y, z, w, __LINE__)
  68. template < typename X, typename Y >
  69. void OutputAssertEqualOr3(X expect1, X expect2, X expect3, char const* compare, Y get, unsigned long const &line) {
  70. std::cerr << "Test FAILED: »" << expect1 << "« or »" << expect2 << "« or »" << expect3 << "« " << compare << " »" << get << "« at line " << line << std::endl;
  71. }
  72. template < typename X, typename Y >
  73. void assertEqualsOr3(X expect1, X expect2, X expect3, Y get, unsigned long const &line) {
  74. if (expect1 == get || expect2 == get || expect3 == get)
  75. return;
  76. OutputAssertEqualOr3(expect1, expect2, expect3, "==", get, line);
  77. }
  78. #define equalsOr4(v,w,x,y,z) assertEqualsOr4(w, x, y, z, v, __LINE__)
  79. template < typename X, typename Y >
  80. void OutputAssertEqualOr4(X expect1, X expect2, X expect3, X expect4, char const* compare, Y get, unsigned long const &line) {
  81. std::cerr << "Test FAILED: »" << expect1 << "« or »" << expect2 << "« or »" << expect3 << "« or »" << expect4 << "« " << compare << " »" << get << "« at line " << line << std::endl;
  82. }
  83. template < typename X, typename Y >
  84. void assertEqualsOr4(X expect1, X expect2, X expect3, X expect4, Y get, unsigned long const &line) {
  85. if (expect1 == get || expect2 == get || expect3 == get || expect4 == get)
  86. return;
  87. OutputAssertEqualOr4(expect1, expect2, expect3, expect4, "==", get, line);
  88. }
  89. // simple helper to quickly output a vectors
  90. template < typename X >
  91. void dumpVector(X vec) {
  92. for (typename X::const_iterator v = vec.begin();
  93. v != vec.end(); ++v)
  94. std::cout << *v << std::endl;
  95. }
  96. #if __GNUC__ >= 4
  97. #pragma GCC diagnostic pop
  98. #endif