test.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #ifndef _LSUP_TEST_H
  2. #define _LSUP_TEST_H
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "lsup_rdf.h"
  6. /**
  7. * Minimal unit testing framework.
  8. * Inspired from http://www.jera.com/techinfo/jtns/jtn002.html
  9. */
  10. // NT tests are exposed to multiple test units.
  11. char *start_nt_doc, *bad_nt_doc;
  12. char *end_nt_doc[7];
  13. LSUP_Codec codec;
  14. LSUP_Term **init_terms (void);
  15. void init_triples(LSUP_Term **terms);
  16. void free_terms (LSUP_Term **terms);
  17. int test_encode_nt_term();
  18. int test_encode_nt_graph();
  19. /** @brief assert true.
  20. */
  21. #define ASSERT(test, msg) do { \
  22. if (!(test)) {\
  23. fprintf(\
  24. stderr, "!!! Assertion failed at %s:%d. Message: %s\n", \
  25. __FILE__, __LINE__, msg); \
  26. return -1; \
  27. }\
  28. } while (0)
  29. /** @brief Expect that two int values are equal.
  30. */
  31. #define EXPECT_INT_EQ(got, exp) do { \
  32. if ((exp) != (got)) {\
  33. fprintf(\
  34. stderr, "!!! Test failed at %s:%d. Expected: %lu; got: %lu\n",\
  35. __FILE__, __LINE__, (size_t)(exp), (size_t)(got)); \
  36. return -1; \
  37. }\
  38. } while (0)
  39. /** @brief Expect that two string values are equal.
  40. */
  41. #define EXPECT_STR_EQ(got, exp) do { \
  42. const char *_str1 = (exp); \
  43. const char *_str2 = (got); \
  44. if (((_str1 == NULL) ^ (_str2 == NULL)) || strcmp(_str1, _str2) != 0) {\
  45. fprintf(\
  46. stderr, "!!! Test failed at %s:%d. Expected: %s; got: %s\n", \
  47. __FILE__, __LINE__, (exp), (got)); \
  48. return -1; \
  49. }\
  50. } while (0)
  51. #define EXPECT_PASS(exp) do { \
  52. int _rc = (exp); \
  53. if (_rc != LSUP_OK) {\
  54. fprintf(\
  55. stderr, "!!! Test failed at %s:%d. Error: %s (%d)\n",\
  56. __FILE__, __LINE__, LSUP_strerror(_rc), _rc); \
  57. return -1; \
  58. }\
  59. } while (0)
  60. /** @brief Run a test by function name.
  61. *
  62. * The function must not accept any parameters.
  63. */
  64. #define RUN(test) do { int rc = test(); tests_run++; \
  65. if (rc != 0) return -1; } while (0)
  66. int tests_run;
  67. #endif /* _LSUP_TEST_H */