assert.h 4.3 KB

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