test_simple.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright (c) 2009-2016 Petri Lehtinen <[email protected]>
  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. #include "util.h"
  8. #include <jansson.h>
  9. #include <string.h>
  10. static void test_bad_args(void) {
  11. json_t *num = json_integer(1);
  12. json_t *txt = json_string("test");
  13. if (!num || !txt)
  14. fail("failed to allocate test objects");
  15. if (json_string_nocheck(NULL) != NULL)
  16. fail("json_string_nocheck with NULL argument did not return NULL");
  17. if (json_stringn_nocheck(NULL, 0) != NULL)
  18. fail("json_stringn_nocheck with NULL argument did not return NULL");
  19. if (json_string(NULL) != NULL)
  20. fail("json_string with NULL argument did not return NULL");
  21. if (json_stringn(NULL, 0) != NULL)
  22. fail("json_stringn with NULL argument did not return NULL");
  23. if (json_string_length(NULL) != 0)
  24. fail("json_string_length with non-string argument did not return 0");
  25. if (json_string_length(num) != 0)
  26. fail("json_string_length with non-string argument did not return 0");
  27. if (json_string_value(NULL) != NULL)
  28. fail("json_string_value with non-string argument did not return NULL");
  29. if (json_string_value(num) != NULL)
  30. fail("json_string_value with non-string argument did not return NULL");
  31. if (!json_string_setn_nocheck(NULL, "", 0))
  32. fail("json_string_setn with non-string argument did not return error");
  33. if (!json_string_setn_nocheck(num, "", 0))
  34. fail("json_string_setn with non-string argument did not return error");
  35. if (!json_string_setn_nocheck(txt, NULL, 0))
  36. fail("json_string_setn_nocheck with NULL value did not return error");
  37. if (!json_string_set_nocheck(txt, NULL))
  38. fail("json_string_set_nocheck with NULL value did not return error");
  39. if (!json_string_set(txt, NULL))
  40. fail("json_string_set with NULL value did not return error");
  41. if (!json_string_setn(txt, NULL, 0))
  42. fail("json_string_setn with NULL value did not return error");
  43. if (num->refcount != 1)
  44. fail("unexpected reference count for num");
  45. if (txt->refcount != 1)
  46. fail("unexpected reference count for txt");
  47. json_decref(num);
  48. json_decref(txt);
  49. }
  50. /* Call the simple functions not covered by other tests of the public API */
  51. static void run_tests() {
  52. json_t *value;
  53. value = json_boolean(1);
  54. if (!json_is_true(value))
  55. fail("json_boolean(1) failed");
  56. json_decref(value);
  57. value = json_boolean(-123);
  58. if (!json_is_true(value))
  59. fail("json_boolean(-123) failed");
  60. json_decref(value);
  61. value = json_boolean(0);
  62. if (!json_is_false(value))
  63. fail("json_boolean(0) failed");
  64. if (json_boolean_value(value) != 0)
  65. fail("json_boolean_value failed");
  66. json_decref(value);
  67. value = json_integer(1);
  68. if (json_typeof(value) != JSON_INTEGER)
  69. fail("json_typeof failed");
  70. if (json_is_object(value))
  71. fail("json_is_object failed");
  72. if (json_is_array(value))
  73. fail("json_is_array failed");
  74. if (json_is_string(value))
  75. fail("json_is_string failed");
  76. if (!json_is_integer(value))
  77. fail("json_is_integer failed");
  78. if (json_is_real(value))
  79. fail("json_is_real failed");
  80. if (!json_is_number(value))
  81. fail("json_is_number failed");
  82. if (json_is_true(value))
  83. fail("json_is_true failed");
  84. if (json_is_false(value))
  85. fail("json_is_false failed");
  86. if (json_is_boolean(value))
  87. fail("json_is_boolean failed");
  88. if (json_is_null(value))
  89. fail("json_is_null failed");
  90. json_decref(value);
  91. value = json_string("foo");
  92. if (!value)
  93. fail("json_string failed");
  94. if (strcmp(json_string_value(value), "foo"))
  95. fail("invalid string value");
  96. if (json_string_length(value) != 3)
  97. fail("invalid string length");
  98. if (json_string_set(value, "barr"))
  99. fail("json_string_set failed");
  100. if (strcmp(json_string_value(value), "barr"))
  101. fail("invalid string value");
  102. if (json_string_length(value) != 4)
  103. fail("invalid string length");
  104. if (json_string_setn(value, "hi\0ho", 5))
  105. fail("json_string_set failed");
  106. if (memcmp(json_string_value(value), "hi\0ho\0", 6))
  107. fail("invalid string value");
  108. if (json_string_length(value) != 5)
  109. fail("invalid string length");
  110. json_decref(value);
  111. value = json_string(NULL);
  112. if (value)
  113. fail("json_string(NULL) failed");
  114. /* invalid UTF-8 */
  115. value = json_string("a\xefz");
  116. if (value)
  117. fail("json_string(<invalid utf-8>) failed");
  118. value = json_string_nocheck("foo");
  119. if (!value)
  120. fail("json_string_nocheck failed");
  121. if (strcmp(json_string_value(value), "foo"))
  122. fail("invalid string value");
  123. if (json_string_length(value) != 3)
  124. fail("invalid string length");
  125. if (json_string_set_nocheck(value, "barr"))
  126. fail("json_string_set_nocheck failed");
  127. if (strcmp(json_string_value(value), "barr"))
  128. fail("invalid string value");
  129. if (json_string_length(value) != 4)
  130. fail("invalid string length");
  131. if (json_string_setn_nocheck(value, "hi\0ho", 5))
  132. fail("json_string_set failed");
  133. if (memcmp(json_string_value(value), "hi\0ho\0", 6))
  134. fail("invalid string value");
  135. if (json_string_length(value) != 5)
  136. fail("invalid string length");
  137. json_decref(value);
  138. /* invalid UTF-8 */
  139. value = json_string_nocheck("qu\xff");
  140. if (!value)
  141. fail("json_string_nocheck failed");
  142. if (strcmp(json_string_value(value), "qu\xff"))
  143. fail("invalid string value");
  144. if (json_string_length(value) != 3)
  145. fail("invalid string length");
  146. if (json_string_set_nocheck(value, "\xfd\xfe\xff"))
  147. fail("json_string_set_nocheck failed");
  148. if (strcmp(json_string_value(value), "\xfd\xfe\xff"))
  149. fail("invalid string value");
  150. if (json_string_length(value) != 3)
  151. fail("invalid string length");
  152. json_decref(value);
  153. value = json_integer(123);
  154. if (!value)
  155. fail("json_integer failed");
  156. if (json_integer_value(value) != 123)
  157. fail("invalid integer value");
  158. if (json_number_value(value) != 123.0)
  159. fail("invalid number value");
  160. if (json_integer_set(value, 321))
  161. fail("json_integer_set failed");
  162. if (json_integer_value(value) != 321)
  163. fail("invalid integer value");
  164. if (json_number_value(value) != 321.0)
  165. fail("invalid number value");
  166. json_decref(value);
  167. value = json_real(123.123);
  168. if (!value)
  169. fail("json_real failed");
  170. if (json_real_value(value) != 123.123)
  171. fail("invalid integer value");
  172. if (json_number_value(value) != 123.123)
  173. fail("invalid number value");
  174. if (json_real_set(value, 321.321))
  175. fail("json_real_set failed");
  176. if (json_real_value(value) != 321.321)
  177. fail("invalid real value");
  178. if (json_number_value(value) != 321.321)
  179. fail("invalid number value");
  180. json_decref(value);
  181. value = json_true();
  182. if (!value)
  183. fail("json_true failed");
  184. json_decref(value);
  185. value = json_false();
  186. if (!value)
  187. fail("json_false failed");
  188. json_decref(value);
  189. value = json_null();
  190. if (!value)
  191. fail("json_null failed");
  192. json_decref(value);
  193. /* Test reference counting on singletons (true, false, null) */
  194. value = json_true();
  195. if (value->refcount != (size_t)-1)
  196. fail("refcounting true works incorrectly");
  197. json_decref(value);
  198. if (value->refcount != (size_t)-1)
  199. fail("refcounting true works incorrectly");
  200. json_incref(value);
  201. if (value->refcount != (size_t)-1)
  202. fail("refcounting true works incorrectly");
  203. value = json_false();
  204. if (value->refcount != (size_t)-1)
  205. fail("refcounting false works incorrectly");
  206. json_decref(value);
  207. if (value->refcount != (size_t)-1)
  208. fail("refcounting false works incorrectly");
  209. json_incref(value);
  210. if (value->refcount != (size_t)-1)
  211. fail("refcounting false works incorrectly");
  212. value = json_null();
  213. if (value->refcount != (size_t)-1)
  214. fail("refcounting null works incorrectly");
  215. json_decref(value);
  216. if (value->refcount != (size_t)-1)
  217. fail("refcounting null works incorrectly");
  218. json_incref(value);
  219. if (value->refcount != (size_t)-1)
  220. fail("refcounting null works incorrectly");
  221. #ifdef json_auto_t
  222. value = json_string("foo");
  223. {
  224. json_auto_t *test = json_incref(value);
  225. /* Use test so GCC doesn't complain it is unused. */
  226. if (!json_is_string(test))
  227. fail("value type check failed");
  228. }
  229. if (value->refcount != 1)
  230. fail("automatic decrement failed");
  231. json_decref(value);
  232. #endif
  233. test_bad_args();
  234. }