assert.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #define equalsOr2(x,y,z) assertEqualsOr2(y, z, x, __LINE__)
  24. template < typename X, typename Y >
  25. void OutputAssertEqualOr2(X expect1, X expect2, char const* compare, Y get, unsigned long const &line) {
  26. std::cerr << "Test FAILED: »" << expect1 << "« or »" << expect2 << "« " << compare << " »" << get << "« at line " << line << std::endl;
  27. }
  28. template < typename X, typename Y >
  29. void assertEqualsOr2(X expect1, X expect2, Y get, unsigned long const &line) {
  30. if (expect1 == get || expect2 == get)
  31. return;
  32. OutputAssertEqualOr2(expect1, expect2, "==", get, line);
  33. }
  34. void assertEqualsOr2(unsigned int const &expect1, unsigned int const &expect2, int const &get, unsigned long const &line) {
  35. if (get < 0)
  36. OutputAssertEqualOr2(expect1, expect2, "==", get, line);
  37. assertEqualsOr2<unsigned int const&, unsigned int const&>(expect1, expect2, get, line);
  38. }
  39. void assertEqualsOr2(int const &expect1, int const &expect2, unsigned int const &get, unsigned long const &line) {
  40. if (expect1 < 0 && expect2 < 0)
  41. OutputAssertEqualOr2(expect1, expect2, "==", get, line);
  42. assertEqualsOr2<unsigned int const&, unsigned int const&>(expect1, expect2, get, line);
  43. }
  44. // simple helper to quickly output a vectors
  45. template < typename X >
  46. void dumpVector(X vec) {
  47. for (typename X::const_iterator v = vec.begin();
  48. v != vec.end(); ++v)
  49. std::cout << *v << std::endl;
  50. }