test.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #ifndef _LSR_TEST_H
  2. #define _LSR_TEST_H
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "lsup_repo.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. const char *_str1 = (exp); \
  34. const char *_str2 = (got); \
  35. if (((_str1 == NULL) ^ (_str2 == NULL)) || strcmp(_str1, _str2) != 0) {\
  36. fprintf(\
  37. stderr, "!!! Test failed at %s:%d. Expected: %s; got: %s\n", \
  38. __FILE__, __LINE__, (exp), (got)); \
  39. return -1; \
  40. }\
  41. } while (0)
  42. #define EXPECT_PASS(exp) do { \
  43. int rc = (exp); \
  44. if (rc != LSUP_OK) {\
  45. fprintf(\
  46. stderr, "!!! Test failed at %s:%d. Error: %s (%d)\n",\
  47. __FILE__, __LINE__, LSUP_strerror(rc), rc); \
  48. return -1; \
  49. }\
  50. } while (0)
  51. /** @brief Run a test by function name.
  52. *
  53. * The function must not accept any parameters.
  54. */
  55. #define RUN(test) do { int rc = test(); tests_run++; \
  56. if (rc != 0) return -1; } while (0)
  57. int tests_run;
  58. #endif /* _LSR_TEST_H */