test.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. /** @brief assert true.
  11. */
  12. #define ASSERT(test, msg) do { \
  13. if (!(test)) {\
  14. fprintf(\
  15. stderr, "!!! Assertion failed at %s:%d. Message: %s\n", \
  16. __FILE__, __LINE__, msg); \
  17. return -1; \
  18. }\
  19. } while (0)
  20. /** @brief Expect that two int values are equal.
  21. */
  22. #define EXPECT_INT_EQ(got, exp) do { \
  23. if ((exp) != (got)) {\
  24. fprintf(\
  25. stderr, "!!! Test failed at %s:%d. Expected: %lu; got: %lu\n",\
  26. __FILE__, __LINE__, (size_t)(exp), (size_t)(got)); \
  27. return -1; \
  28. }\
  29. } while (0)
  30. /** @brief Expect that two string values are equal.
  31. */
  32. #define EXPECT_STR_EQ(got, exp) do { \
  33. if (strcmp((exp), (got)) != 0) {\
  34. fprintf(\
  35. stderr, "!!! Test failed at %s:%d. Expected: %s; got: %s\n", \
  36. __FILE__, __LINE__, (exp), (got)); \
  37. return -1; \
  38. }\
  39. } while (0)
  40. #define EXPECT_PASS(exp) do { \
  41. int rc = (exp); \
  42. if (rc != LSUP_OK) {\
  43. fprintf(\
  44. stderr, "!!! Test failed at %s:%d. Error code: %d\n",\
  45. __FILE__, __LINE__, rc); \
  46. return -1; \
  47. }\
  48. } while (0)
  49. /** @brief Run a test by function name.
  50. *
  51. * The function must not accept any parameters.
  52. */
  53. #define RUN(test) do { int rc = test(); tests_run++; \
  54. if (rc != 0) return -1; } while (0)
  55. int tests_run;
  56. #endif