test.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. // These are used by other codecs than just NT.
  15. LSUP_Term **init_terms (void);
  16. void init_triples(LSUP_Term **terms);
  17. void free_terms (LSUP_Term **terms);
  18. int test_encode_nt_term();
  19. int test_encode_nt_graph();
  20. int test_decode_nt_graph();
  21. int test_decode_nt_bad_graph();
  22. /** @brief assert true.
  23. */
  24. #define ASSERT(test, msg) do { \
  25. if (!(test)) {\
  26. log_error (\
  27. "!!! Assertion failed at %s:%d. Message: %s\n", \
  28. __FILE__, __LINE__, msg); \
  29. return -1; \
  30. }\
  31. } while (0)
  32. /** @brief Expect that two int values are equal.
  33. */
  34. #define EXPECT_INT_EQ(got, exp) do { \
  35. if ((exp) != (got)) {\
  36. log_error (\
  37. "!!! Test failed at %s:%d. Expected: %lu; got: %lu\n",\
  38. __FILE__, __LINE__, (size_t)(exp), (size_t)(got)); \
  39. return -1; \
  40. }\
  41. } while (0)
  42. /** @brief Expect that two string values are equal.
  43. */
  44. #define EXPECT_STR_EQ(got, exp) do { \
  45. const char *_str1 = (exp); \
  46. const char *_str2 = (got); \
  47. if (((_str1 == NULL) ^ (_str2 == NULL)) || strcmp(_str1, _str2) != 0) {\
  48. log_error (\
  49. "!!! Test failed at %s:%d. Expected: %s; got: %s\n", \
  50. __FILE__, __LINE__, (exp), (got)); \
  51. return -1; \
  52. }\
  53. } while (0)
  54. #define EXPECT_PASS(exp) do { \
  55. int _rc = (exp); \
  56. if (_rc != LSUP_OK) {\
  57. log_error (\
  58. "!!! Test failed at %s:%d. Error: %s (%d)\n",\
  59. __FILE__, __LINE__, LSUP_strerror(_rc), _rc); \
  60. return -1; \
  61. }\
  62. } while (0)
  63. /** @brief Run a test by function name.
  64. *
  65. * The function must not accept any parameters.
  66. */
  67. #define RUN(test) do { int rc = test(); tests_run++; \
  68. if (rc != 0) return -1; } while (0)
  69. int tests_run;
  70. #endif /* _LSUP_TEST_H */