2
0

common.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #ifndef CJSON_TESTS_COMMON_H
  20. #define CJSON_TESTS_COMMON_H
  21. #include "../cJSON.c"
  22. void reset(cJSON *item);
  23. void reset(cJSON *item) {
  24. if ((item != NULL) && (item->child != NULL))
  25. {
  26. cJSON_Delete(item->child);
  27. }
  28. if ((item->valuestring != NULL) && !(item->type & cJSON_IsReference))
  29. {
  30. global_hooks.deallocate(item->valuestring);
  31. }
  32. if ((item->string != NULL) && !(item->type & cJSON_StringIsConst))
  33. {
  34. global_hooks.deallocate(item->string);
  35. }
  36. memset(item, 0, sizeof(cJSON));
  37. }
  38. char* read_file(const char *filename);
  39. char* read_file(const char *filename) {
  40. FILE *file = NULL;
  41. long length = 0;
  42. char *content = NULL;
  43. size_t read_chars = 0;
  44. /* open in read binary mode */
  45. file = fopen(filename, "rb");
  46. if (file == NULL)
  47. {
  48. goto cleanup;
  49. }
  50. /* get the length */
  51. if (fseek(file, 0, SEEK_END) != 0)
  52. {
  53. goto cleanup;
  54. }
  55. length = ftell(file);
  56. if (length < 0)
  57. {
  58. goto cleanup;
  59. }
  60. if (fseek(file, 0, SEEK_SET) != 0)
  61. {
  62. goto cleanup;
  63. }
  64. /* allocate content buffer */
  65. content = (char*)malloc((size_t)length + sizeof(""));
  66. if (content == NULL)
  67. {
  68. goto cleanup;
  69. }
  70. /* read the file into memory */
  71. read_chars = fread(content, sizeof(char), (size_t)length, file);
  72. if ((long)read_chars != length)
  73. {
  74. free(content);
  75. content = NULL;
  76. goto cleanup;
  77. }
  78. content[read_chars] = '\0';
  79. cleanup:
  80. if (file != NULL)
  81. {
  82. fclose(file);
  83. }
  84. return content;
  85. }
  86. /* assertion helper macros */
  87. #define assert_has_type(item, item_type) TEST_ASSERT_BITS_MESSAGE(0xFF, item_type, item->type, "Item doesn't have expected type.")
  88. #define assert_has_no_reference(item) TEST_ASSERT_BITS_MESSAGE(cJSON_IsReference, 0, item->type, "Item should not have a string as reference.")
  89. #define assert_has_no_const_string(item) TEST_ASSERT_BITS_MESSAGE(cJSON_StringIsConst, 0, item->type, "Item should not have a const string.")
  90. #define assert_has_valuestring(item) TEST_ASSERT_NOT_NULL_MESSAGE(item->valuestring, "Valuestring is NULL.")
  91. #define assert_has_no_valuestring(item) TEST_ASSERT_NULL_MESSAGE(item->valuestring, "Valuestring is not NULL.")
  92. #define assert_has_string(item) TEST_ASSERT_NOT_NULL_MESSAGE(item->string, "String is NULL")
  93. #define assert_has_no_string(item) TEST_ASSERT_NULL_MESSAGE(item->string, "String is not NULL.")
  94. #define assert_not_in_list(item) \
  95. TEST_ASSERT_NULL_MESSAGE(item->next, "Linked list next pointer is not NULL.");\
  96. TEST_ASSERT_NULL_MESSAGE(item->prev, "Linked list previous pointer is not NULL.")
  97. #define assert_has_child(item) TEST_ASSERT_NOT_NULL_MESSAGE(item->child, "Item doesn't have a child.")
  98. #define assert_has_no_child(item) TEST_ASSERT_NULL_MESSAGE(item->child, "Item has a child.")
  99. #define assert_is_invalid(item) \
  100. assert_has_type(item, cJSON_Invalid);\
  101. assert_not_in_list(item);\
  102. assert_has_no_child(item);\
  103. assert_has_no_string(item);\
  104. assert_has_no_valuestring(item)
  105. #endif