t.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // test utilities
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <yrmcds.h>
  6. static int n_failures = 0;
  7. static void check_error(yrmcds_error e) {
  8. if( e == YRMCDS_OK )
  9. return;
  10. fprintf(stderr, "yrmcds error: %s\n", yrmcds_strerror(e));
  11. exit(1);
  12. }
  13. static yrmcds* get_yrmcds(yrmcds* c) {
  14. const char* host = getenv("YRMCDS_HOST");
  15. if( host == NULL ) {
  16. return NULL;
  17. }
  18. uint16_t port = 11211;
  19. if( getenv("YRMCDS_PORT") ) {
  20. port = (uint16_t)atoi(getenv("YRMCDS_PORT"));
  21. }
  22. check_error( yrmcds_connect(c, host, port) );
  23. return c;
  24. }
  25. static void test_main(yrmcds* c);
  26. #define TEST_MAIN() static void test_main(yrmcds* c)
  27. int main(int argc, char** argv) {
  28. yrmcds c_;
  29. yrmcds* c = get_yrmcds(&c_);
  30. if( c == NULL ) {
  31. fprintf(stderr, "No YRMCDS_HOST. Skipped.\n");
  32. return 0;
  33. }
  34. test_main(c);
  35. yrmcds_close(c);
  36. if( n_failures > 0 ) {
  37. fprintf(stderr, "%d tests failed.\n", n_failures);
  38. return 1;
  39. }
  40. fprintf(stderr, "Passed.\n");
  41. return 0;
  42. }
  43. #define DEF_TEST(name) void test_##name(yrmcds* c)
  44. #define CALL_TEST(name) \
  45. fprintf(stderr, "[%s]\n", #name); \
  46. test_##name(c); \
  47. uint32_t serial_##name; \
  48. check_error( yrmcds_flush(c, 0, 0, &serial_##name) ); \
  49. while( 1 ) { \
  50. yrmcds_response r; \
  51. check_error( yrmcds_recv(c, &r) ); \
  52. if( r.serial == serial_##name ) break; \
  53. } \
  54. sleep(1)
  55. #define ASSERT(expr, to_return) \
  56. if( ! (expr) ) { \
  57. fprintf(stderr, "assertion failure at line %d: %s\n", \
  58. __LINE__, #expr); \
  59. n_failures++; \
  60. if( to_return ) return; \
  61. }