MainTest.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. #include <iostream>
  2. #include "data/UnsortedArrayList.h"
  3. #include "data/HashMap.h"
  4. #include "server/CommandUtils.h"
  5. using namespace std;
  6. // -----------------------------------------------------------------------------
  7. // test feedback
  8. // -----------------------------------------------------------------------------
  9. const char* RED = "\033[0;31m";
  10. const char* GREEN = "\033[0;32m";
  11. int tests = 0;
  12. int successTests = 0;
  13. void notifySuccess(string text)
  14. {
  15. tests++;
  16. successTests++;
  17. cout << GREEN << tests << ". " << text << endl;
  18. }
  19. void notifyFail(string text)
  20. {
  21. tests++;
  22. cout << RED << tests << ". " << text << endl;
  23. }
  24. void finalizeTest()
  25. {
  26. cout << successTests << " / " << tests << " succeeded" << endl;
  27. tests = 0;
  28. successTests = 0;
  29. }
  30. // -----------------------------------------------------------------------------
  31. // checks
  32. // -----------------------------------------------------------------------------
  33. void checkEqual(int a, int b, string text)
  34. {
  35. if(a == b)
  36. {
  37. notifySuccess(text);
  38. }
  39. else
  40. {
  41. notifyFail(text + " - expected '" + std::to_string(a) + "' got '" + std::to_string(b) + "'");
  42. }
  43. }
  44. void checkEqual(string a, string b, string text)
  45. {
  46. if(a == b)
  47. {
  48. notifySuccess(text);
  49. }
  50. else
  51. {
  52. notifyFail(text + " - expected '" + a + "' got '" + b + "'");
  53. }
  54. }
  55. void checkBool(bool a, bool b, string text)
  56. {
  57. if(a == b)
  58. {
  59. notifySuccess(text);
  60. }
  61. else
  62. {
  63. notifyFail(text + " - expected '" + std::to_string(a) + "' got '" + std::to_string(b) + "'");
  64. }
  65. }
  66. void checkGreaterOrEqual(int a, int b, string text)
  67. {
  68. if(a >= b)
  69. {
  70. notifySuccess(text);
  71. }
  72. else
  73. {
  74. notifyFail(text + " - expected " + std::to_string(a) + " >= " + std::to_string(b));
  75. }
  76. }
  77. // -----------------------------------------------------------------------------
  78. // tests of UnsortedArrayList
  79. // -----------------------------------------------------------------------------
  80. void testUnsortedArrayList1(string text)
  81. {
  82. UnsortedArrayList<int> list;
  83. list.add(1);
  84. list.add(5);
  85. list.add(10);
  86. checkEqual(3, list.getSize(), text);
  87. checkEqual(1, list.get(0, -1), text);
  88. checkEqual(5, list.get(1, -1), text);
  89. checkEqual(10, list.get(2, -1), text);
  90. }
  91. void testUnsortedArrayList2(string text)
  92. {
  93. UnsortedArrayList<int> list;
  94. list.add(1);
  95. list.add(5);
  96. list.add(10);
  97. list.removeIndex(1);
  98. checkEqual(2, list.getSize(), text);
  99. checkEqual(1, list.get(0, -1), text);
  100. checkEqual(10, list.get(1, -1), text);
  101. }
  102. void testUnsortedArrayList3(string text)
  103. {
  104. UnsortedArrayList<int> list;
  105. list.add(1);
  106. list.add(5);
  107. list.add(10);
  108. list.removeIndex(0);
  109. list.add(20);
  110. checkEqual(3, list.getSize(), text);
  111. checkEqual(10, list.get(0, -1), text);
  112. checkEqual(5, list.get(1, -1), text);
  113. checkEqual(20, list.get(2, -1), text);
  114. }
  115. void testUnsortedArrayList4(string text)
  116. {
  117. UnsortedArrayList<int> list;
  118. list.add(1);
  119. list.add(5);
  120. list.add(10);
  121. list.removeIndex(-5);
  122. list.removeIndex(5);
  123. checkEqual(3, list.getSize(), text);
  124. checkEqual(1, list.get(0, -1), text);
  125. checkEqual(5, list.get(1, -1), text);
  126. checkEqual(10, list.get(2, -1), text);
  127. }
  128. void testUnsortedArrayList5(string text)
  129. {
  130. UnsortedArrayList<int> list;
  131. list.add(1);
  132. list.add(5);
  133. list.add(1);
  134. list.add(20);
  135. list.remove(2);
  136. checkEqual(4, list.getSize(), text);
  137. checkEqual(1, list.get(0, -1), text);
  138. checkEqual(5, list.get(1, -1), text);
  139. checkEqual(1, list.get(2, -1), text);
  140. checkEqual(20, list.get(3, -1), text);
  141. }
  142. void testUnsortedArrayList6(string text)
  143. {
  144. UnsortedArrayList<int> list;
  145. int r = rand();
  146. checkEqual(r, list.get(-100, r), text);
  147. r = rand();
  148. checkEqual(r, list.get(100, r), text);
  149. }
  150. void testUnsortedArrayList7(string text)
  151. {
  152. UnsortedArrayList<int> list;
  153. list.add(1);
  154. list.add(1);
  155. list.add(5);
  156. list.add(1);
  157. list.add(20);
  158. list.remove(1);
  159. checkEqual(2, list.getSize(), text);
  160. checkEqual(20, list.get(0, -1), text);
  161. checkEqual(5, list.get(1, -1), text);
  162. }
  163. void testUnsortedArrayList()
  164. {
  165. testUnsortedArrayList1("add, size and get");
  166. testUnsortedArrayList2("remove index");
  167. testUnsortedArrayList3("add after remove index, move last to first");
  168. testUnsortedArrayList4("remove of non existent element");
  169. testUnsortedArrayList5("remove index of non existent index");
  170. testUnsortedArrayList6("get returns error value");
  171. testUnsortedArrayList7("remove deletes all occurencies");
  172. finalizeTest();
  173. }
  174. // -----------------------------------------------------------------------------
  175. // tests of HashMap
  176. // -----------------------------------------------------------------------------
  177. unsigned int hashInt(int i)
  178. {
  179. return i;
  180. }
  181. void testHashMap1(string text)
  182. {
  183. HashMap<int, int> map(16, hashInt);
  184. int cap = map.getCapacity();
  185. checkGreaterOrEqual(cap, 16, text);
  186. HashMap<int, int> map2(cap, hashInt);
  187. HashMap<int, int> map3(cap + 1, hashInt);
  188. checkEqual(cap, map2.getCapacity(), text);
  189. checkGreaterOrEqual(map3.getCapacity(), cap + 1, text);
  190. }
  191. void testHashMap2(string text)
  192. {
  193. HashMap<int, int> map(16, hashInt);
  194. for(int i = 0; i < 5; i++)
  195. {
  196. map.put(i, i);
  197. }
  198. checkEqual(5, map.getSize(), text);
  199. }
  200. void testHashMap3(string text)
  201. {
  202. HashMap<int, int> map(16, hashInt);
  203. int cap = map.getCapacity();
  204. if(cap < 0)
  205. {
  206. cap = 0;
  207. }
  208. cap += 3;
  209. for(int i = 0; i < cap; i++)
  210. {
  211. map.put(i, i);
  212. }
  213. checkEqual(cap, map.getSize(), text);
  214. }
  215. void testHashMap4(string text)
  216. {
  217. HashMap<int, int> map(16, hashInt);
  218. for(int i = 0; i < 3; i++)
  219. {
  220. map.put(i, i + 20);
  221. }
  222. for(int i = 0; i < 3; i++)
  223. {
  224. checkEqual(i + 20, map.get(i, -1), text);
  225. }
  226. }
  227. void testHashMap5(string text)
  228. {
  229. HashMap<int, int> map(16, hashInt);
  230. int cap = map.getCapacity();
  231. if(cap < 0)
  232. {
  233. cap = 0;
  234. }
  235. cap += 3;
  236. for(int i = 0; i < cap; i++)
  237. {
  238. map.put(i, i + 20);
  239. }
  240. checkBool(true, map.remove(cap / 2), text);
  241. checkEqual(-1, map.get(cap / 2, -1), text);
  242. checkEqual(cap - 1, map.getSize(), text);
  243. }
  244. void testHashMap6(string text)
  245. {
  246. HashMap<int, int> map(16, hashInt);
  247. int cap = map.getCapacity();
  248. if(cap < 0)
  249. {
  250. cap = 0;
  251. }
  252. cap += 3;
  253. for(int i = 0; i < cap; i++)
  254. {
  255. map.put(i, i + 20);
  256. }
  257. checkBool(false, map.remove(cap + 5), text);
  258. checkBool(false, map.remove(cap + 20), text);
  259. checkBool(false, map.remove(cap + 25), text);
  260. checkEqual(cap, map.getSize(), text);
  261. }
  262. void testHashMap7(string text)
  263. {
  264. HashMap<int, int> map(16, hashInt);
  265. for(int i = 0; i < 3; i++)
  266. {
  267. map.put(i, i + 20);
  268. }
  269. checkEqual(3, map.getSize(), text);
  270. map.clear();
  271. checkEqual(0, map.getSize(), text);
  272. checkEqual(-1, map.get(0, -1), text);
  273. checkEqual(-1, map.get(1, -1), text);
  274. checkEqual(-1, map.get(2, -1), text);
  275. }
  276. void testHashMap8(string text)
  277. {
  278. HashMap<int, int> map(16, hashInt);
  279. for(int i = 0; i < 3; i++)
  280. {
  281. map.put(i, i + 20);
  282. }
  283. for(int i = 0; i < 3; i++)
  284. {
  285. map.put(i, i + 25);
  286. }
  287. for(int i = 0; i < 3; i++)
  288. {
  289. checkEqual(i + 25, map.get(i, -1), text);
  290. }
  291. }
  292. void testHashMap9(string text)
  293. {
  294. HashMap<int, int> map(16, hashInt);
  295. int cap = map.getCapacity();
  296. if(cap < 0)
  297. {
  298. cap = 0;
  299. }
  300. cap += 3;
  301. for(int i = 0; i < cap; i++)
  302. {
  303. map.put(i, i + 20);
  304. }
  305. checkEqual(cap / 2 + 20, map.remove(cap / 2, -1), text);
  306. checkEqual(cap - 1, map.getSize(), text);
  307. }
  308. void testHashMap10(string text)
  309. {
  310. HashMap<int, int> map(16, hashInt);
  311. int cap = map.getCapacity();
  312. if(cap < 0)
  313. {
  314. cap = 0;
  315. }
  316. cap += 3;
  317. for(int i = 0; i < cap; i++)
  318. {
  319. map.put(i, i + 20);
  320. }
  321. checkEqual(-1, map.remove(cap + 5, -1), text);
  322. checkEqual(-1, map.remove(cap + 20, -1), text);
  323. checkEqual(-1, map.remove(cap + 25, -1), text);
  324. checkEqual(cap, map.getSize(), text);
  325. }
  326. void testHashMap()
  327. {
  328. testHashMap1("capacity greater or equal than inital value");
  329. testHashMap2("put increases size");
  330. testHashMap3("put over capacity");
  331. testHashMap4("get");
  332. testHashMap5("remove of existent value");
  333. testHashMap6("remove of non existent value");
  334. testHashMap7("clear");
  335. testHashMap8("put overwrites old value");
  336. testHashMap9("remove with return value of existent value");
  337. testHashMap10("remove with return value of non existent value");
  338. finalizeTest();
  339. }
  340. // -----------------------------------------------------------------------------
  341. // tests of command parser
  342. // -----------------------------------------------------------------------------
  343. void checkCommandParser(string rawCommand, string command, vector<string>& args, string text)
  344. {
  345. string parsedCommand;
  346. vector<string> parsedArgs;
  347. CommandUtils::splitString(rawCommand, parsedCommand, parsedArgs);
  348. checkEqual(command, parsedCommand, text);
  349. checkEqual(args.size(), parsedArgs.size(), text);
  350. for(int i = 0; i < args.size() && i < parsedArgs.size(); i++)
  351. {
  352. checkEqual(args[i], parsedArgs[i], text);
  353. }
  354. }
  355. void testCommandParser()
  356. {
  357. vector<string> args;
  358. args = {};
  359. checkCommandParser("test", "test", args, "command without arguments");
  360. args = {"aaa"};
  361. checkCommandParser("test aaa", "test", args, "command with one argument");
  362. args = {"aaa", "bbbb"};
  363. checkCommandParser("test aaa bbbb", "test", args, "command with two arguments");
  364. args = {"aaa", "bbbb", "ccccc"};
  365. checkCommandParser("test aaa bbbb ccccc", "test", args, "command with three arguments");
  366. args = {};
  367. checkCommandParser("test ", "test", args, "command with spaces");
  368. args = {"aaa", "bbbb"};
  369. checkCommandParser("test aaa bbbb", "test", args, "command with arguments and spaces");
  370. args = {"aaa", "bbbb"};
  371. checkCommandParser("test aaa bbbb", "test", args, "command with arguments and spaces");
  372. args = {"aaa bbbb"};
  373. checkCommandParser("test \"aaa bbbb\"", "test", args, "command with one argument and quotation marks");
  374. args = {"aaa bbbb", "ccc"};
  375. checkCommandParser("test \"aaa bbbb\" ccc", "test", args, "command with two arguments and quotation marks");
  376. args = {"ddd", "aaa bbbb", "ccc"};
  377. checkCommandParser("test ddd \"aaa bbbb\" ccc", "test", args, "command with tree arguments and quotation marks");
  378. args = {};
  379. checkCommandParser("test \"", "", args, "command syntax exception 1");
  380. args = {};
  381. checkCommandParser("test aaa\"", "", args, "command syntax exception 2");
  382. args = {};
  383. checkCommandParser("test aaa\"bbb\"", "", args, "command syntax exception 3");
  384. args = {};
  385. checkCommandParser("test aaa \"bbb\"ccc", "", args, "command syntax exception 4");
  386. }
  387. // -----------------------------------------------------------------------------
  388. // main
  389. // -----------------------------------------------------------------------------
  390. int main(int argc, char** argv)
  391. {
  392. testUnsortedArrayList();
  393. testHashMap();
  394. testCommandParser();
  395. return 0;
  396. }