configuration_tests.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "unity/examples/unity_config.h"
  23. #include "unity/src/unity.h"
  24. #include "common.h"
  25. static void create_configuration_should_create_a_configuration(void)
  26. {
  27. cJSON *json = NULL;
  28. internal_configuration *configuration = NULL;
  29. int userdata = 1;
  30. json = cJSON_Parse("{\"format\":false,\"case_sensitive\":false,\"allow_data_after_json\":false}");
  31. TEST_ASSERT_NOT_NULL(json);
  32. configuration = (internal_configuration*)cJSON_CreateConfiguration(json, NULL, &userdata);
  33. cJSON_Delete(json);
  34. json = NULL;
  35. TEST_ASSERT_NOT_NULL(configuration);
  36. TEST_ASSERT_EQUAL_MESSAGE(configuration->buffer_size, 256, "buffer_size has an incorrect value.");
  37. TEST_ASSERT_FALSE_MESSAGE(configuration->format, "format has an incorrect value.");
  38. TEST_ASSERT_FALSE_MESSAGE(configuration->case_sensitive, "case_sensitive has an incorrect value.");
  39. TEST_ASSERT_FALSE_MESSAGE(configuration->allow_data_after_json, "allow_data_after_json has an incorrect value.");
  40. TEST_ASSERT_TRUE_MESSAGE(configuration->userdata == &userdata, "Incorrect userdata");
  41. TEST_ASSERT_TRUE_MESSAGE(global_allocate_wrapper == configuration->allocators.allocate, "Wrong malloc.");
  42. TEST_ASSERT_TRUE_MESSAGE(global_reallocate_wrapper == configuration->allocators.reallocate, "Wrong realloc.");
  43. TEST_ASSERT_TRUE_MESSAGE(global_deallocate_wrapper == configuration->allocators.deallocate, "Wrong realloc.");
  44. free(configuration);
  45. }
  46. static void create_configuration_should_work_with_an_empty_object(void)
  47. {
  48. internal_configuration *configuration = NULL;
  49. int userdata = 1;
  50. configuration = (internal_configuration*)cJSON_CreateConfiguration(NULL, NULL, &userdata);
  51. TEST_ASSERT_NOT_NULL(configuration);
  52. TEST_ASSERT_EQUAL_MESSAGE(configuration->buffer_size, 256, "buffer_size has an incorrect value.");
  53. TEST_ASSERT_TRUE_MESSAGE(configuration->format, "format has an incorrect value.");
  54. TEST_ASSERT_TRUE_MESSAGE(configuration->case_sensitive, "case_sensitive has an incorrect value.");
  55. TEST_ASSERT_TRUE_MESSAGE(configuration->allow_data_after_json, "allow_data_after_json has an incorrect value.");
  56. TEST_ASSERT_TRUE_MESSAGE(configuration->userdata == &userdata, "Incorrect userdata");
  57. TEST_ASSERT_TRUE_MESSAGE(global_allocate_wrapper == configuration->allocators.allocate, "Wrong malloc.");
  58. TEST_ASSERT_TRUE_MESSAGE(global_reallocate_wrapper == configuration->allocators.reallocate, "Wrong realloc.");
  59. TEST_ASSERT_TRUE_MESSAGE(global_deallocate_wrapper == configuration->allocators.deallocate, "Wrong free.");
  60. free(configuration);
  61. }
  62. static void* custom_allocator(size_t size, void *userdata)
  63. {
  64. *((size_t*)userdata) = size;
  65. return malloc(size);
  66. }
  67. static void custom_deallocator(void *pointer, void *userdata)
  68. {
  69. *((size_t*)userdata) = (size_t)pointer;
  70. free(pointer);
  71. }
  72. static void create_configuration_should_take_custom_allocators(void)
  73. {
  74. internal_configuration *configuration = NULL;
  75. cJSON_Allocators allocators = {custom_allocator, custom_deallocator, NULL};
  76. size_t userdata = 0;
  77. configuration = (internal_configuration*)cJSON_CreateConfiguration(NULL, &allocators, &userdata);
  78. TEST_ASSERT_NOT_NULL(configuration);
  79. TEST_ASSERT_EQUAL_MESSAGE(userdata, sizeof(internal_configuration), "custom allocator wasn't run properly.");
  80. TEST_ASSERT_TRUE_MESSAGE(custom_allocator == configuration->allocators.allocate, "Wrong allocator.");
  81. TEST_ASSERT_TRUE_MESSAGE(custom_deallocator == configuration->allocators.deallocate, "Wrong deallocator.");
  82. TEST_ASSERT_NULL_MESSAGE(configuration->allocators.reallocate, "Reallocator is not null");
  83. custom_deallocator(configuration, &userdata);
  84. }
  85. static void configuration_change_allocators_should_change_allocators(void)
  86. {
  87. internal_configuration *configuration = NULL;
  88. cJSON_Allocators allocators = {custom_allocator, custom_deallocator, NULL};
  89. size_t userdata = 0;
  90. configuration = (internal_configuration*)cJSON_CreateConfiguration(NULL, &allocators, &userdata);
  91. TEST_ASSERT_NOT_NULL(configuration);
  92. configuration = (internal_configuration*)cJSON_ConfigurationChangeAllocators(configuration, allocators);
  93. TEST_ASSERT_NOT_NULL(configuration);
  94. TEST_ASSERT_TRUE_MESSAGE(custom_allocator == configuration->allocators.allocate, "Wrong allocator.");
  95. TEST_ASSERT_TRUE_MESSAGE(custom_deallocator == configuration->allocators.deallocate, "Wrong deallocator.");
  96. TEST_ASSERT_NULL_MESSAGE(configuration->allocators.reallocate, "Reallocator is not null");
  97. custom_deallocator(configuration, &userdata);
  98. }
  99. static void configuration_change_userdata_should_change_userdata(void)
  100. {
  101. internal_configuration *configuration = NULL;
  102. size_t userdata = 0;
  103. configuration = (internal_configuration*)cJSON_CreateConfiguration(NULL, NULL, NULL);
  104. TEST_ASSERT_NOT_NULL(configuration);
  105. configuration = (internal_configuration*)cJSON_ConfigurationChangeUserdata(configuration, &userdata);
  106. TEST_ASSERT_TRUE_MESSAGE(configuration->userdata == &userdata, "Userdata is incorrect.");
  107. free(configuration);
  108. }
  109. static void configuration_change_parse_end_should_change_parse_end(void)
  110. {
  111. size_t end_position = 0;
  112. internal_configuration *configuration = (internal_configuration*)cJSON_CreateConfiguration(NULL, NULL, NULL);
  113. TEST_ASSERT_NOT_NULL(configuration);
  114. configuration = (internal_configuration*)cJSON_ConfigurationChangeParseEnd(configuration, &end_position);
  115. TEST_ASSERT_NOT_NULL(configuration);
  116. TEST_ASSERT_TRUE_MESSAGE(configuration->end_position == &end_position, "Failed to set parse end.");
  117. free(configuration);
  118. }
  119. static void configuration_change_prebuffer_size_should_change_buffer_size(void)
  120. {
  121. internal_configuration *configuration = (internal_configuration*)cJSON_CreateConfiguration(NULL, NULL, NULL);
  122. TEST_ASSERT_NOT_NULL(configuration);
  123. configuration = (internal_configuration*)cJSON_ConfigurationChangePrebufferSize(configuration, 1024);
  124. TEST_ASSERT_NOT_NULL(configuration);
  125. TEST_ASSERT_EQUAL_MESSAGE(configuration->buffer_size, 1024, "Didn't set the buffer size correctly.");
  126. free(configuration);
  127. }
  128. static void configuration_change_prebuffer_size_should_not_allow_empty_sizes(void)
  129. {
  130. internal_configuration *configuration = (internal_configuration*)cJSON_CreateConfiguration(NULL, NULL, NULL);
  131. TEST_ASSERT_NOT_NULL(configuration);
  132. TEST_ASSERT_NULL(cJSON_ConfigurationChangePrebufferSize(configuration, 0));
  133. free(configuration);
  134. }
  135. int main(void)
  136. {
  137. UNITY_BEGIN();
  138. RUN_TEST(create_configuration_should_create_a_configuration);
  139. RUN_TEST(create_configuration_should_work_with_an_empty_object);
  140. RUN_TEST(create_configuration_should_take_custom_allocators);
  141. RUN_TEST(configuration_change_allocators_should_change_allocators);
  142. RUN_TEST(configuration_change_userdata_should_change_userdata);
  143. RUN_TEST(configuration_change_parse_end_should_change_parse_end);
  144. RUN_TEST(configuration_change_prebuffer_size_should_change_buffer_size);
  145. RUN_TEST(configuration_change_prebuffer_size_should_not_allow_empty_sizes);
  146. return UNITY_END();
  147. }