test.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "lsup_rdf.h"
  4. #ifndef _LSUP_TEST_H
  5. #define _LSUP_TEST_H
  6. /**
  7. * Minimal unit testing framework.
  8. * Inspired from http://www.jera.com/techinfo/jtns/jtn002.html
  9. */
  10. #define ASSERT(test, msg) do { \
  11. if (!(test)) {\
  12. fprintf(\
  13. stderr, "!!! Assertion failed at %s:%d. Message: %s\n", \
  14. __FILE__, __LINE__, msg); \
  15. return -1; \
  16. }\
  17. } while (0)
  18. #define EXPECT_INT_EQ(got, exp) do { \
  19. if ((exp) != (got)) {\
  20. fprintf(\
  21. stderr, "!!! Test failed at %s:%d. Expected: %lu; got: %lu\n",\
  22. __FILE__, __LINE__, (size_t)(exp), (size_t)(got)); \
  23. return -1; \
  24. }\
  25. } while (0)
  26. #define EXPECT_STR_EQ(got, exp) do { \
  27. if (strcmp((exp), (got)) != 0) {\
  28. fprintf(\
  29. stderr, "!!! Test failed at %s:%d. Expected: %s; got: %s\n", \
  30. __FILE__, __LINE__, (exp), (got)); \
  31. return -1; \
  32. }\
  33. } while (0)
  34. #define RUN(test) do { int rc = test(); tests_run++; \
  35. if (rc != 0) return -1; } while (0)
  36. int tests_run;
  37. #endif