TestChildren.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #include "TestSuite.h"
  2. #include "../Source/JSONNode.h"
  3. void TestSuite::TestChildren(void){
  4. UnitTest::SetPrefix("TestChildren.cpp - Children");
  5. #ifdef JSON_LIBRARY
  6. #define assertChild(node, index, func, value)\
  7. if (JSONNODE * blabla = json_at(node, index)){\
  8. assertEquals(func(blabla), value);\
  9. } else {\
  10. FAIL("no child");\
  11. }
  12. JSONNODE * test1 = json_new(JSON_NODE);
  13. JSONNODE * test2 = json_new(JSON_NODE);
  14. TestSuite::testParsingItself(test1);
  15. TestSuite::testParsingItself(test2);
  16. assertEquals(json_type(test1), JSON_NODE);
  17. assertEquals(json_type(test2), JSON_NODE);
  18. assertEquals(json_size(test1), 0);
  19. assertEquals(json_size(test2), 0);
  20. assertTrue(json_equal(test1, test2));
  21. json_push_back(test1, json_new_a(JSON_TEXT("hi"), JSON_TEXT("world")));
  22. assertEquals(json_size(test1), 1);
  23. assertFalse(json_equal(test1, test2));
  24. json_push_back(test2, json_new_a(JSON_TEXT("hi"), JSON_TEXT("world")));
  25. assertEquals(json_size(test2), 1);
  26. assertTrue(json_equal(test1, test2));
  27. TestSuite::testParsingItself(test1);
  28. TestSuite::testParsingItself(test2);
  29. json_merge(test1, test2);
  30. #ifdef JSON_UNIT_TEST
  31. #ifdef JSON_REF_COUNT
  32. assertEquals(((JSONNode*)test1) -> internal, ((JSONNode*)test2) -> internal);
  33. #else
  34. assertNotEquals(((JSONNode*)test1) -> internal, ((JSONNode*)test2) -> internal);
  35. #endif
  36. #endif
  37. UnitTest::SetPrefix("TestChildren.cpp - Children 2");
  38. if (JSONNODE * temp = json_at(test1, 0)){
  39. json_char * str = json_as_string(temp);
  40. assertCStringSame(str, JSON_TEXT("world"));
  41. json_free(str);
  42. str = json_name(temp);
  43. assertCStringSame(str, JSON_TEXT("hi"));
  44. json_free(str);
  45. } else {
  46. FAIL("at failed");
  47. }
  48. TestSuite::testParsingItself(test1);
  49. TestSuite::testParsingItself(test2);
  50. assertEquals(json_size(test1), 1);
  51. if (JSONNODE * temp = json_pop_back_at(test1, 0)){
  52. json_char * str = json_as_string(temp);
  53. assertCStringSame(str, JSON_TEXT("world"));
  54. json_free(str);
  55. assertEquals(json_size(test1), 0);
  56. json_delete(temp);
  57. } else {
  58. FAIL("POP FAILED");
  59. }
  60. UnitTest::SetPrefix("TestChildren.cpp - Children 3");
  61. json_push_back(test1, json_new_a(JSON_TEXT("hi"), JSON_TEXT("world")));
  62. if (JSONNODE * temp = json_pop_back(test1, JSON_TEXT("hi"))){
  63. json_char * str = json_as_string(temp);
  64. assertCStringSame(str, JSON_TEXT("world"));
  65. json_free(str);
  66. assertEquals(json_size(test1), 0);
  67. json_delete(temp);
  68. } else {
  69. FAIL("POP name FAILED");
  70. }
  71. #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS
  72. json_push_back(test1, json_new_a(JSON_TEXT("hi"), JSON_TEXT("world")));
  73. if (JSONNODE * temp = json_pop_back_nocase(test1, JSON_TEXT("HI"))){
  74. json_char * str = json_as_string(temp);
  75. assertCStringSame(str, JSON_TEXT("world"));
  76. json_free(str);
  77. assertEquals(json_size(test1), 0);
  78. json_delete(temp);
  79. } else {
  80. FAIL("POP name FAILED");
  81. }
  82. #endif
  83. TestSuite::testParsingItself(test1);
  84. TestSuite::testParsingItself(test2);
  85. UnitTest::SetPrefix("TestChildren.cpp - Children 4");
  86. assertEquals(json_size(test1), 0);
  87. json_push_back(test1, json_new_i(JSON_TEXT("one"), 1));
  88. json_push_back(test1, json_new_i(JSON_TEXT("two"), 2));
  89. json_push_back(test1, json_new_i(JSON_TEXT("three"), 3));
  90. json_push_back(test1, json_new_i(JSON_TEXT("four"), 4));
  91. json_push_back(test1, json_new_i(JSON_TEXT("five"), 5));
  92. json_push_back(test1, json_new_i(JSON_TEXT("six"), 6));
  93. assertEquals(json_size(test1), 6);
  94. TestSuite::testParsingItself(test1);
  95. TestSuite::testParsingItself(test2);
  96. if (JSONNODE * temp = json_pop_back(test1, JSON_TEXT("four"))){
  97. assertEquals(json_as_int(temp), 4);
  98. assertChild(test1, 0, json_as_int, 1);
  99. assertChild(test1, 1, json_as_int, 2);
  100. assertChild(test1, 2, json_as_int, 3);
  101. assertChild(test1, 3, json_as_int, 5);
  102. assertChild(test1, 4, json_as_int, 6);
  103. assertEquals(json_size(test1), 5);
  104. TestSuite::testParsingItself(test1);
  105. TestSuite::testParsingItself(test2);
  106. json_delete(temp);
  107. } else {
  108. FAIL("no pop");
  109. }
  110. UnitTest::SetPrefix("TestChildren.cpp - Children 5");
  111. #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS
  112. if (JSONNODE * temp = json_pop_back_nocase(test1, JSON_TEXT("SIX"))){
  113. #else
  114. if (JSONNODE * temp = json_pop_back(test1, JSON_TEXT("six"))){
  115. #endif
  116. assertEquals(json_as_int(temp), 6);
  117. assertChild(test1, 0, json_as_int, 1);
  118. assertChild(test1, 1, json_as_int, 2);
  119. assertChild(test1, 2, json_as_int, 3);
  120. assertChild(test1, 3, json_as_int, 5);
  121. assertEquals(json_size(test1), 4);
  122. TestSuite::testParsingItself(test1);
  123. TestSuite::testParsingItself(test2);
  124. json_delete(temp);
  125. } else {
  126. FAIL("no pop_nocase");
  127. }
  128. UnitTest::SetPrefix("TestChildren.cpp - Children 6");
  129. if (JSONNODE * temp = json_pop_back_at(test1, 2)){
  130. assertEquals(json_as_int(temp), 3);
  131. assertChild(test1, 0, json_as_int, 1);
  132. assertChild(test1, 1, json_as_int, 2);
  133. assertChild(test1, 2, json_as_int, 5);
  134. assertEquals(json_size(test1), 3);
  135. TestSuite::testParsingItself(test1);
  136. TestSuite::testParsingItself(test2);
  137. json_delete(temp);
  138. } else {
  139. FAIL("no pop 2");
  140. }
  141. json_delete(test1);
  142. json_delete(test2);
  143. #ifdef JSON_UNIT_TEST
  144. JSONNODE * fresh = json_new(JSON_NODE);
  145. json_reserve(fresh, 3);
  146. assertEquals(((JSONNode*)fresh) -> internal -> CHILDREN -> mycapacity, 3);
  147. assertEquals(((JSONNode*)fresh) -> internal -> CHILDREN -> mysize, 0);
  148. json_push_back(fresh, json_new(JSON_NULL));
  149. assertEquals(((JSONNode*)fresh) -> internal -> CHILDREN -> mycapacity, 3);
  150. assertEquals(((JSONNode*)fresh) -> internal -> CHILDREN -> mysize, 1);
  151. json_push_back(fresh, json_new(JSON_NULL));
  152. assertEquals(((JSONNode*)fresh) -> internal -> CHILDREN -> mycapacity, 3);
  153. assertEquals(((JSONNode*)fresh) -> internal -> CHILDREN -> mysize, 2);
  154. json_push_back(fresh, json_new(JSON_NULL));
  155. assertEquals(((JSONNode*)fresh) -> internal -> CHILDREN -> mycapacity, 3);
  156. assertEquals(((JSONNode*)fresh) -> internal -> CHILDREN -> mysize, 3);
  157. json_delete(fresh);
  158. #endif
  159. #else
  160. JSONNode test1;
  161. JSONNode test2;
  162. TestSuite::testParsingItself(test1);
  163. TestSuite::testParsingItself(test2);
  164. assertEquals(test1.type(), JSON_NODE);
  165. assertEquals(test2.type(), JSON_NODE);
  166. assertEquals(test1.size(), 0);
  167. assertEquals(test2.size(), 0);
  168. assertEquals(test1, test2);
  169. test1.push_back(JSONNode(JSON_TEXT("hi"), JSON_TEXT("world")));
  170. assertEquals(test1.size(), 1);
  171. assertNotEquals(test1, test2);
  172. test2.push_back(JSONNode(JSON_TEXT("hi"), JSON_TEXT("world")));
  173. assertEquals(test2.size(), 1);
  174. assertEquals(test1, test2);
  175. TestSuite::testParsingItself(test1);
  176. TestSuite::testParsingItself(test2);
  177. test1.merge(test2);
  178. #ifdef JSON_UNIT_TEST
  179. #ifdef JSON_REF_COUNT
  180. assertEquals(test1.internal, test2.internal);
  181. #else
  182. assertNotEquals(test1.internal, test2.internal);
  183. #endif
  184. #endif
  185. try {
  186. assertEquals(test1.at(0), JSON_TEXT("world"));
  187. assertEquals(test1.at(0).name(), JSON_TEXT("hi"));
  188. } catch (std::out_of_range){
  189. FAIL("exception caught");
  190. }
  191. TestSuite::testParsingItself(test1);
  192. TestSuite::testParsingItself(test2);
  193. assertEquals(test1.size(), 1);
  194. try {
  195. JSONNode res = test1.pop_back(0);
  196. assertEquals(res, JSON_TEXT("world"));
  197. assertEquals(test1.size(), 0);
  198. test1.push_back(JSONNode(JSON_TEXT("hi"), JSON_TEXT("world")));
  199. res = test1.pop_back(JSON_TEXT("hi"));
  200. assertEquals(res, JSON_TEXT("world"));
  201. assertEquals(test1.size(), 0);
  202. #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS
  203. test1.push_back(JSONNode(JSON_TEXT("hi"), JSON_TEXT("world")));
  204. res = test1.pop_back_nocase(JSON_TEXT("HI"));
  205. assertEquals(res, JSON_TEXT("world"));
  206. assertEquals(test1.size(), 0);
  207. #endif
  208. } catch (std::out_of_range){
  209. FAIL("exception caught 2");
  210. }
  211. TestSuite::testParsingItself(test1);
  212. TestSuite::testParsingItself(test2);
  213. assertEquals(test1.size(), 0);
  214. test1.push_back(JSONNode(JSON_TEXT("one"), 1));
  215. test1.push_back(JSONNode(JSON_TEXT("two"), 2));
  216. test1.push_back(JSONNode(JSON_TEXT("three"), 3));
  217. test1.push_back(JSONNode(JSON_TEXT("four"), 4));
  218. test1.push_back(JSONNode(JSON_TEXT("five"), 5));
  219. test1.push_back(JSONNode(JSON_TEXT("six"), 6));
  220. assertEquals(test1.size(), 6);
  221. TestSuite::testParsingItself(test1);
  222. TestSuite::testParsingItself(test2);
  223. //echo(test1.dump().write_formatted());
  224. JSONNode res;
  225. try {
  226. res = test1.pop_back(JSON_TEXT("four"));
  227. assertEquals(res, 4);
  228. assertEquals(test1[0], 1);
  229. assertEquals(test1[1], 2);
  230. assertEquals(test1[2], 3);
  231. assertEquals(test1[3], 5);
  232. assertEquals(test1[4], 6);
  233. assertEquals(test1.size(), 5);
  234. TestSuite::testParsingItself(test1);
  235. TestSuite::testParsingItself(test2);
  236. } catch (std::out_of_range){
  237. FAIL("exception caught pop");
  238. }
  239. try {
  240. #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS
  241. res = test1.pop_back_nocase(JSON_TEXT("SIX"));
  242. #else
  243. res = test1.pop_back(JSON_TEXT("six"));
  244. #endif
  245. assertEquals(res, 6);
  246. assertEquals(test1[0], 1);
  247. assertEquals(test1[1], 2);
  248. assertEquals(test1[2], 3);
  249. assertEquals(test1[3], 5);
  250. assertEquals(test1.size(), 4);
  251. TestSuite::testParsingItself(test1);
  252. TestSuite::testParsingItself(test2);
  253. } catch (std::out_of_range){
  254. FAIL("exception caught pop_nocase");
  255. }
  256. try {
  257. res = test1.pop_back(2);
  258. assertEquals(res, 3);
  259. assertEquals(test1[0], 1);
  260. assertEquals(test1[1], 2);
  261. assertEquals(test1[2], 5);
  262. assertEquals(test1.size(), 3);
  263. TestSuite::testParsingItself(test1);
  264. TestSuite::testParsingItself(test2);
  265. } catch (std::out_of_range){
  266. FAIL("exception caught pop 2");
  267. }
  268. #ifdef JSON_UNIT_TEST
  269. JSONNode fresh(JSON_NODE);
  270. fresh.reserve(3);
  271. assertEquals(fresh.internal -> CHILDREN -> mycapacity, 3);
  272. assertEquals(fresh.internal -> CHILDREN -> mysize, 0);
  273. fresh.push_back(JSONNode(JSON_NULL));
  274. assertEquals(fresh.internal -> CHILDREN -> mycapacity, 3);
  275. assertEquals(fresh.internal -> CHILDREN -> mysize, 1);
  276. fresh.push_back(JSONNode(JSON_NULL));
  277. assertEquals(fresh.internal -> CHILDREN -> mycapacity, 3);
  278. assertEquals(fresh.internal -> CHILDREN -> mysize, 2);
  279. fresh.push_back(JSONNode(JSON_NULL));
  280. assertEquals(fresh.internal -> CHILDREN -> mycapacity, 3);
  281. assertEquals(fresh.internal -> CHILDREN -> mysize, 3);
  282. #endif
  283. #endif
  284. }