TestMutex.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. #include "TestSuite.h"
  2. #include "../Source/JSONNode.h"
  3. #ifdef JSON_MUTEX_CALLBACKS
  4. int testMutex = 0;
  5. bool doassert = true;
  6. int managerlock;
  7. static void lock(void * mutex){
  8. if (mutex == &managerlock) return;
  9. if (doassert) assertEquals(mutex, &testMutex);
  10. if (mutex != &testMutex) return; //to avoid access violations to tests fail, but don't crash
  11. ++(*((int*)mutex));
  12. }
  13. static void unlock(void * mutex){
  14. if (mutex == &managerlock) return;
  15. if (doassert) assertEquals(mutex, &testMutex);
  16. if (mutex != &testMutex) return; //to avoid access violations to tests fail, but don't crash
  17. --(*((int*)mutex));
  18. }
  19. void * currentMutexTest = 0;
  20. #ifdef JSON_MUTEX_MANAGE
  21. #include "../Source/JSONGlobals.h"
  22. static void destroy(void * mutex){
  23. assertEquals(mutex, currentMutexTest);
  24. assertEquals(*((int*)mutex), 0);
  25. }
  26. #endif
  27. void TestSuite::TestMutex(void){
  28. UnitTest::SetPrefix("TestMutex.cpp - Mutex");
  29. #ifdef JSON_LIBRARY
  30. #ifdef JSON_MUTEX_MANAGE
  31. json_register_mutex_callbacks(lock, unlock, destroy, &managerlock);
  32. #else
  33. json_register_mutex_callbacks(lock, unlock, &managerlock);
  34. #endif
  35. currentMutexTest = &testMutex;
  36. {
  37. JSONNODE * test1 = json_new(JSON_NODE);
  38. #ifdef JSON_UNIT_TEST
  39. assertNull(((JSONNode*)test1) -> internal -> mylock);
  40. #endif
  41. JSONNODE * test2 = json_copy(test1);
  42. assertNotEquals(test1, test2);
  43. #ifdef JSON_UNIT_TEST
  44. assertNull(((JSONNode*)test2) -> internal -> mylock);
  45. #endif
  46. json_set_mutex(test2, &testMutex);
  47. #ifdef JSON_UNIT_TEST
  48. assertEquals(((JSONNode*)test2) -> internal -> mylock, &testMutex);
  49. assertNull(((JSONNode*)test1) -> internal -> mylock);
  50. #endif
  51. JSONNODE * test3 = json_copy(test2);
  52. #ifdef JSON_UNIT_TEST
  53. assertEquals(((JSONNode*)test3) -> internal -> mylock, &testMutex);
  54. assertEquals(((JSONNode*)test2) -> internal -> mylock, &testMutex);
  55. #endif
  56. json_set_a(test3, JSON_TEXT("Hello World"));
  57. #ifdef JSON_UNIT_TEST
  58. assertEquals(((JSONNode*)test3) -> internal -> mylock, &testMutex);
  59. #endif
  60. #ifdef JSON_CASTABLE
  61. json_cast(test3, JSON_NODE);
  62. #ifdef JSON_UNIT_TEST
  63. assertEquals(((JSONNode*)test3) -> internal -> mylock, &testMutex);
  64. #endif
  65. JSONNODE * tree = json_new(JSON_NODE);
  66. json_push_back(tree, json_new_a(JSON_TEXT("Hello"), JSON_TEXT("world")));
  67. json_push_back(tree, json_new_a(JSON_TEXT("Hello"), JSON_TEXT("Mars")));
  68. json_push_back(tree, json_new_a(JSON_TEXT("Hello"), JSON_TEXT("USA")));
  69. json_push_back(test3, json_copy(tree));
  70. #ifdef JSON_UNIT_TEST
  71. assertEquals(((JSONNode*)test3) -> internal -> mylock, &testMutex);
  72. assertEquals(((JSONNode*)json_at(test3, 0)) -> internal -> mylock, &testMutex);
  73. assertEquals(((JSONNode*)json_at(json_at(test3, 0), 0)) -> internal -> mylock, &testMutex);
  74. assertEquals(((JSONNode*)json_at(json_at(test3, 0), 1)) -> internal -> mylock, &testMutex);
  75. assertEquals(((JSONNode*)json_at(json_at(test3, 0), 2)) -> internal -> mylock, &testMutex);
  76. #endif
  77. json_clear(test3);
  78. json_set_mutex(test3, 0);
  79. assertEquals(json_size(test3), 0);
  80. assertEquals(json_size(tree), 3);
  81. #ifdef JSON_UNIT_TEST
  82. assertNull(((JSONNode*)tree) -> internal -> mylock);
  83. assertNull(((JSONNode*)json_at(tree, 0)) -> internal -> mylock);
  84. assertNull(((JSONNode*)json_at(tree, 1)) -> internal -> mylock);
  85. assertNull(((JSONNode*)json_at(tree, 2)) -> internal -> mylock);
  86. #endif
  87. json_set_mutex(tree, &testMutex);
  88. #ifdef JSON_UNIT_TEST
  89. assertEquals(((JSONNode*)tree) -> internal -> mylock, &testMutex);
  90. assertEquals(((JSONNode*)json_at(tree, 0)) -> internal -> mylock, &testMutex);
  91. assertEquals(((JSONNode*)json_at(tree, 1)) -> internal -> mylock, &testMutex);
  92. assertEquals(((JSONNode*)json_at(tree, 2)) -> internal -> mylock, &testMutex);
  93. #endif
  94. json_push_back(test3, tree);
  95. #ifdef JSON_UNIT_TEST
  96. assertNull(((JSONNode*)test3) -> internal -> mylock);
  97. assertEquals(((JSONNode*)json_at(test3, 0)) -> internal -> mylock, &testMutex);
  98. assertEquals(((JSONNode*)json_at(json_at(test3, 0), 0)) -> internal -> mylock, &testMutex);
  99. assertEquals(((JSONNode*)json_at(json_at(test3, 0), 1)) -> internal -> mylock, &testMutex);
  100. assertEquals(((JSONNode*)json_at(json_at(test3, 0), 2)) -> internal -> mylock, &testMutex);
  101. #endif
  102. assertEquals(testMutex, 0);
  103. #endif
  104. #ifdef JSON_MUTEX_MANAGE
  105. UnitTest::SetPrefix("TestMutex.cpp - Mutex Management");
  106. {
  107. JSONNODE * deleteTest = json_new(JSON_NODE);
  108. int i = 0;
  109. currentMutexTest = &i;
  110. json_set_mutex(deleteTest, &i);
  111. JSON_MAP(void *, unsigned int)::iterator it = json_global(MUTEX_MANAGER).find((void*)&i);
  112. assertEquals(json_global(MUTEX_MANAGER).size(), 2);
  113. assertNotEquals(it, json_global(MUTEX_MANAGER).end());
  114. assertEquals(it -> first, (void*)&i);
  115. assertEquals(it -> second, 1);
  116. json_set_mutex(deleteTest, &testMutex);
  117. currentMutexTest = &testMutex;
  118. json_delete(deleteTest);
  119. }
  120. #endif
  121. json_delete(test1);
  122. json_delete(test2);
  123. json_delete(test3);
  124. }
  125. #ifdef JSON_MUTEX_MANAGE
  126. JSON_MAP(void *, unsigned int)::iterator it = json_global(MUTEX_MANAGER).find((void*)&testMutex);
  127. assertEquals(json_global(MUTEX_MANAGER).size(), 0);
  128. assertEquals(it, json_global(MUTEX_MANAGER).end());
  129. #endif
  130. #else
  131. #ifdef JSON_MUTEX_MANAGE
  132. libjson::register_mutex_callbacks(lock, unlock, destroy, &managerlock);
  133. #else
  134. libjson::register_mutex_callbacks(lock, unlock, &managerlock);
  135. #endif
  136. currentMutexTest = &testMutex;
  137. {
  138. JSONNode test1;
  139. #ifdef JSON_UNIT_TEST
  140. assertNull(test1.internal -> mylock);
  141. #endif
  142. JSONNode test2 = JSONNode(test1);
  143. #ifdef JSON_UNIT_TEST
  144. assertNull(test1.internal -> mylock);
  145. #endif
  146. test2.set_mutex(&testMutex);
  147. #ifdef JSON_UNIT_TEST
  148. assertEquals(test2.internal -> mylock, &testMutex);
  149. assertNull(test1.internal -> mylock);
  150. #endif
  151. JSONNode test3 = test2;
  152. #ifdef JSON_UNIT_TEST
  153. assertEquals(test3.internal -> mylock, &testMutex);
  154. assertEquals(test2.internal -> mylock, &testMutex);
  155. #endif
  156. test3 = JSON_TEXT("Hello World");
  157. #ifdef JSON_UNIT_TEST
  158. assertEquals(test3.internal -> mylock, &testMutex);
  159. #endif
  160. #ifdef JSON_CASTABLE
  161. test3.cast(JSON_NODE);
  162. #ifdef JSON_UNIT_TEST
  163. assertEquals(test3.internal -> mylock, &testMutex);
  164. #endif
  165. JSONNode tree = JSONNode(JSON_NODE);
  166. tree.push_back(JSONNode(JSON_TEXT("Hello"), JSON_TEXT("world")));
  167. tree.push_back(JSONNode(JSON_TEXT("Hello"), JSON_TEXT("Mars")));
  168. tree.push_back(JSONNode(JSON_TEXT("Hello"), JSON_TEXT("USA")));
  169. test3.push_back(tree);
  170. #ifdef JSON_UNIT_TEST
  171. assertEquals(test3.internal -> mylock, &testMutex);
  172. assertEquals(test3[0].internal -> mylock, &testMutex);
  173. assertEquals(test3[0][0].internal -> mylock, &testMutex);
  174. assertEquals(test3[0][1].internal -> mylock, &testMutex);
  175. assertEquals(test3[0][2].internal -> mylock, &testMutex);
  176. #endif
  177. test3.clear();
  178. test3.set_mutex(0);
  179. assertEquals(test3.size(), 0);
  180. assertEquals(tree.size(), 3);
  181. #ifdef JSON_UNIT_TEST
  182. assertNull(tree.internal -> mylock);
  183. assertNull(tree[0].internal -> mylock);
  184. assertNull(tree[1].internal -> mylock);
  185. assertNull(tree[2].internal -> mylock);
  186. #endif
  187. tree.set_mutex(&testMutex);
  188. #ifdef JSON_UNIT_TEST
  189. assertEquals(tree.internal -> mylock, &testMutex);
  190. assertEquals(tree[0].internal -> mylock, &testMutex);
  191. assertEquals(tree[1].internal -> mylock, &testMutex);
  192. assertEquals(tree[2].internal -> mylock, &testMutex);
  193. #endif
  194. test3.push_back(tree);
  195. #ifdef JSON_UNIT_TEST
  196. assertNull(test3.internal -> mylock);
  197. assertEquals(test3[0].internal -> mylock, &testMutex);
  198. assertEquals(test3[0][0].internal -> mylock, &testMutex);
  199. assertEquals(test3[0][1].internal -> mylock, &testMutex);
  200. assertEquals(test3[0][2].internal -> mylock, &testMutex);
  201. #endif
  202. #ifndef JSON_SAFE
  203. doassert = false;
  204. #endif
  205. {
  206. JSONNode::auto_lock temp1(test3, 1); //null, so it should do nothing
  207. JSONNode::auto_lock temp2(tree, 1);
  208. assertEquals(testMutex, 1);
  209. }
  210. #ifndef JSON_SAFE
  211. doassert = true;
  212. #endif
  213. #endif
  214. assertEquals(testMutex, 0);
  215. #ifdef JSON_MUTEX_MANAGE
  216. UnitTest::SetPrefix("TestMutex.cpp - Mutex Management");
  217. {
  218. JSONNode deleteTest = JSONNode(JSON_NODE);
  219. int i = 0;
  220. currentMutexTest = &i;
  221. deleteTest.set_mutex(&i);
  222. JSON_MAP(void *, unsigned int)::iterator it = json_global(MUTEX_MANAGER).find((void*)&i);
  223. assertEquals(json_global(MUTEX_MANAGER).size(), 2);
  224. assertNotEquals(it, json_global(MUTEX_MANAGER).end());
  225. assertEquals(it -> first, (void*)&i);
  226. assertEquals(it -> second, 1);
  227. deleteTest.set_mutex(&testMutex);
  228. currentMutexTest = &testMutex;
  229. }
  230. #endif
  231. }
  232. #ifdef JSON_MUTEX_MANAGE
  233. std::map<void *, unsigned int>::iterator it = json_global(MUTEX_MANAGER).find((void*)&testMutex);
  234. assertEquals(json_global(MUTEX_MANAGER).size(), 0);
  235. assertEquals(it, json_global(MUTEX_MANAGER).end());
  236. #endif
  237. #endif
  238. }
  239. #ifdef JSON_MUTEX_CALLBACKS
  240. int handler = 0;
  241. static void lock_mutex(void * mutex){
  242. if (mutex == &handler) return;
  243. assertEquals(mutex, &testMutex);
  244. if (mutex != &testMutex) return; //to avoid access violations to tests fail, but don't crash
  245. ++(*((int*)mutex));
  246. }
  247. static void unlock_mutex(void * mutex){
  248. if (mutex == &handler) return;
  249. assertEquals(mutex, &testMutex);
  250. if (mutex != &testMutex) return; //to avoid access violations to tests fail, but don't crash
  251. --(*((int*)mutex));
  252. }
  253. static void destroy_mutex(void * ){}
  254. void TestSuite::TestThreading(void){
  255. //going to fake two threads os that I don't need pthread to link
  256. UnitTest::SetPrefix("TestMutex.cpp - Threading");
  257. testMutex = 0;
  258. #ifdef JSON_LIBRARY
  259. //create the JSONNode
  260. JSONNODE * test = json_new(JSON_NODE);
  261. #ifdef JSON_MUTEX_MANAGE
  262. json_register_mutex_callbacks(lock_mutex, unlock_mutex, destroy_mutex, &handler);
  263. #else
  264. json_register_mutex_callbacks(lock_mutex, unlock_mutex, &handler);
  265. #endif
  266. json_set_mutex(test, &testMutex);
  267. json_lock(test, 1);
  268. assertEquals(testMutex, 1);
  269. json_lock(test, 1);
  270. assertEquals(testMutex, 1);
  271. json_lock(test, 2);
  272. assertEquals(testMutex, 2);
  273. json_unlock(test, 1);
  274. assertEquals(testMutex, 2); //because this thread locked it twice
  275. json_unlock(test, 1);
  276. assertEquals(testMutex, 1);
  277. json_unlock(test, 2);
  278. assertEquals(testMutex, 0);
  279. json_delete(test);
  280. #else
  281. //create the JSONNode
  282. JSONNode test;
  283. #ifdef JSON_MUTEX_MANAGE
  284. libjson::register_mutex_callbacks(lock_mutex, unlock_mutex, destroy_mutex, &handler);
  285. #else
  286. libjson::register_mutex_callbacks(lock_mutex, unlock_mutex, &handler);
  287. #endif
  288. test.set_mutex(&testMutex);
  289. test.lock(1);
  290. assertEquals(testMutex, 1);
  291. test.lock(1);
  292. assertEquals(testMutex, 1);
  293. test.lock(2);
  294. assertEquals(testMutex, 2);
  295. test.unlock(1);
  296. assertEquals(testMutex, 2); //because this thread locked it twice
  297. test.unlock(1);
  298. assertEquals(testMutex, 1);
  299. test.unlock(2);
  300. assertEquals(testMutex, 0);
  301. #endif
  302. #endif
  303. }
  304. #endif