util.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
  3. *
  4. * Jansson is free software; you can redistribute it and/or modify
  5. * it under the terms of the MIT license. See LICENSE for details.
  6. */
  7. #ifndef UTIL_H
  8. #define UTIL_H
  9. #ifdef HAVE_CONFIG_H
  10. #include <jansson_private_config.h>
  11. #endif
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #if HAVE_LOCALE_H
  15. #include <locale.h>
  16. #endif
  17. #include <jansson.h>
  18. #define failhdr fprintf(stderr, "%s:%d: ", __FILE__, __LINE__)
  19. #define fail(msg) \
  20. do { \
  21. failhdr; \
  22. fprintf(stderr, "%s\n", msg); \
  23. exit(1); \
  24. } while(0)
  25. /* Assumes json_error_t error */
  26. #define check_errors(texts_, num_, source_, line_, column_, position_) \
  27. do { \
  28. int i_, found_ = 0; \
  29. for(i_ = 0; i_ < num_; i_++) { \
  30. if(strcmp(error.text, texts_[i_]) == 0) { \
  31. found_ = 1; \
  32. break; \
  33. } \
  34. } \
  35. if (!found_) { \
  36. failhdr; \
  37. if (num_ == 1) { \
  38. fprintf(stderr, "text: \"%s\" != \"%s\"\n", error.text, texts_[0]); \
  39. } else { \
  40. fprintf(stderr, "text: \"%s\" does not match\n", error.text); \
  41. } \
  42. exit(1); \
  43. } \
  44. if(strcmp(error.source, source_) != 0) { \
  45. failhdr; \
  46. \
  47. fprintf(stderr, "source: \"%s\" != \"%s\"\n", error.source, source_); \
  48. exit(1); \
  49. } \
  50. if(error.line != line_) { \
  51. failhdr; \
  52. fprintf(stderr, "line: %d != %d\n", error.line, line_); \
  53. exit(1); \
  54. } \
  55. if(error.column != column_) { \
  56. failhdr; \
  57. fprintf(stderr, "column: %d != %d\n", error.column, column_); \
  58. exit(1); \
  59. } \
  60. if(error.position != position_) { \
  61. failhdr; \
  62. fprintf(stderr, "position: %d != %d\n", error.position, position_); \
  63. exit(1); \
  64. } \
  65. } while(0)
  66. /* Assumes json_error_t error */
  67. #define check_error(text_, source_, line_, column_, position_) \
  68. check_errors(&text_, 1, source_, line_, column_, position_)
  69. static void run_tests();
  70. int main() {
  71. #ifdef HAVE_SETLOCALE
  72. setlocale(LC_ALL, "");
  73. #endif
  74. run_tests();
  75. return 0;
  76. }
  77. #endif