assert.h 3.4 KB

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