test.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #include "test.h"
  2. #include "datastring.h"
  3. #include "datablock.h"
  4. #include "biglist.h"
  5. #include "user.h"
  6. #include "chatroom.h"
  7. #include "parameters.h"
  8. #include "stringbuilder.h"
  9. #include "message.h"
  10. #include "sha256.h"
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include "Debug.h"
  14. int test::runtest(const char *test)
  15. {
  16. datastring testname;
  17. testname = test;
  18. int result = 0;
  19. bool found = false;
  20. bool all = ((testname.length == 3) && (strcmp(testname.data,"all")==0));
  21. if ((all) || (testname == "datastring") || (testname == "1")) {
  22. result += test::datastring_test();
  23. found = true;
  24. }
  25. if ((all) || (testname == "datablock") || (testname == "2")) {
  26. result += test::datablock_test();
  27. found = true;
  28. }
  29. if ((all) || (testname == "biglist") || (testname == "3")) {
  30. result += test::biglist_test();
  31. found = true;
  32. }
  33. if ((all) || (testname == "user") || (testname == "4")) {
  34. result += test::user_test();
  35. found = true;
  36. }
  37. if ((all) || (testname == "parameters") || (testname == "5")) {
  38. result += test::parameters_test();
  39. found = true;
  40. }
  41. if ((all) || (testname == "stringbuilder") || (testname == "6")) {
  42. result += test::stringbuilder_test();
  43. found = true;
  44. }
  45. if ((all) || (testname == "sha256") || (testname == "7")) {
  46. result += test::sha256_test();
  47. found = true;
  48. }
  49. // The debug test should be the last test because it causes the program to crash.
  50. if ((all) || (testname == "Debug") || (testname == "8")) {
  51. result += test::Debug_test();
  52. found = true;
  53. }
  54. if (!found) {
  55. printf("Test for class %s wasn't found.\n",test);
  56. }
  57. return result;
  58. }
  59. int test::testcount()
  60. {
  61. return 8;
  62. }
  63. int test::datastring_test()
  64. {
  65. printf("datastring test: ");
  66. datastring test1;
  67. datastring test2;
  68. test1 = "Testing1";
  69. test2 = test1.substr(0,7);
  70. if (test2.data != test1.data) {
  71. printf("failed test 1.\n");
  72. return 1;
  73. }
  74. if (test2.length != 7) {
  75. printf("failed test 2.\n");
  76. return 1;
  77. }
  78. if (test2 != "Testing") {
  79. printf("failed test 3.\n");
  80. return 1;
  81. }
  82. if (!test1.null_terminated) {
  83. printf("failed test 4.\n");
  84. return 1;
  85. }
  86. if (test1.substr(0,4).null_terminated) {
  87. printf("failed test 5.\n");
  88. return 1;
  89. }
  90. if (test1 == test2) {
  91. printf("failed test 6.\n");
  92. return 1;
  93. }
  94. printf("passed.\n");
  95. return 0;
  96. }
  97. int test::datablock_test()
  98. {
  99. printf("datablock test: ");
  100. datablock block((char *)"Testing");
  101. if (block != "Testing") {
  102. printf("failed test 1.\n");
  103. return 1;
  104. }
  105. if (memcmp(block.data,"Testing",8)!=0) {
  106. printf("failed test 2.\n");
  107. return 1;
  108. }
  109. datablock *block2 = block.clone();
  110. if (block.usage != 2) {
  111. printf("failed test 3. block.usage=%d\n",block.usage);
  112. return 1;
  113. }
  114. idisposable::dereference((idisposable **)&block2);
  115. if (block.usage != 1) {
  116. printf("failed test 4.\n");
  117. return 1;
  118. }
  119. printf("passed.\n");
  120. return 0;
  121. }
  122. int test::biglist_test()
  123. {
  124. printf("biglist test: ");
  125. biglist<int> testing;
  126. testing.add(1);
  127. testing.add(2);
  128. testing.add(3);
  129. if (testing.length()!=3) {
  130. printf("failed test 1.\n");
  131. return 1;
  132. }
  133. if (!testing.find(2)) {
  134. printf("failed test 2.\n");
  135. return 1;
  136. }
  137. testing.remove(2);
  138. if (testing.find(2)) {
  139. printf("failed test 3.\n");
  140. return 1;
  141. }
  142. if (testing[0]->item!=1) {
  143. printf("failed test 4.\n");
  144. return 1;
  145. }
  146. printf("passed.\n");
  147. return 0;
  148. }
  149. int test::user_test()
  150. {
  151. printf("user test: ");
  152. user *testing = new user();
  153. datablock username("tester");
  154. datablock password("password");
  155. user *search;
  156. biglist<user *> users;
  157. testing->userid = 185;
  158. testing->username = username.clone();
  159. testing->password = password.clone();
  160. users.add(testing);
  161. search = user::find(&users,185);
  162. if (search== nullptr) {
  163. printf("failed test 1.\n");
  164. return 1;
  165. }
  166. if (search->userid != 185) {
  167. printf("failed test 2.\n");
  168. return 1;
  169. }
  170. search = user::find(&users,username);
  171. if (search== nullptr) {
  172. printf("failed test 3.\n");
  173. return 1;
  174. }
  175. if (search->userid != 185) {
  176. printf("failed test 4.\n");
  177. return 1;
  178. }
  179. search = user::find(&users,-34);
  180. if (search != nullptr) {
  181. printf("failed test 5.\n");
  182. return 1;
  183. }
  184. if (username.usage != 2) {
  185. printf("failed test 6.\n");
  186. return 1;
  187. }
  188. delete testing;
  189. testing = nullptr;
  190. if (username.usage != 1) {
  191. printf("failed test 7.\n");
  192. return 1;
  193. }
  194. printf("passed.\n");
  195. return 0;
  196. }
  197. int test::parameters_test()
  198. {
  199. printf("parameters test: ");
  200. parameters testing;
  201. datastring input;
  202. bool success = true;
  203. input = "4,test7,testing1250,3,abc";
  204. testing.string_parameter(input,success);
  205. if (!success) {
  206. printf("failed test 8.\n");
  207. return 1;
  208. }
  209. if (testing.string_parameters[0] != "test") {
  210. printf("failed test 3.\n");
  211. return 1;
  212. }
  213. testing.string_parameter(input,success);
  214. if (!success) {
  215. printf("failed test 9.\n");
  216. return 1;
  217. }
  218. testing.long_parameter(input,success);
  219. if (!success) {
  220. input.print();
  221. printf("failed test 10.\n");
  222. return 1;
  223. }
  224. testing.string_parameter(input,success);
  225. if (!success) {
  226. printf("failed test 7.\n");
  227. return 1;
  228. }
  229. if (input.length > 0) {
  230. printf("failed test 1.\n");
  231. return 1;
  232. }
  233. if (testing.parameter_count != 4) {
  234. printf("failed test 2.\n");
  235. return 1;
  236. }
  237. if (testing.string_parameters[1] != "testing") {
  238. printf("failed test 4.\n");
  239. return 1;
  240. }
  241. if (testing.long_parameters[2] != 1250) {
  242. printf("failed test 5.\n");
  243. return 1;
  244. }
  245. if (testing.string_parameters[3] != "abc") {
  246. printf("failed test 6.\n");
  247. return 1;
  248. }
  249. printf("passed.\n");
  250. return 0;
  251. }
  252. int test::stringbuilder_test()
  253. {
  254. stringbuilder testing;
  255. datastring twentyfive;
  256. message output;
  257. int twelve;
  258. int64_t thirteen;
  259. printf("stringbuilder test: ");
  260. // Test 1.
  261. twelve = 12;
  262. thirteen = 13;
  263. twentyfive = "twentyfive";
  264. testing += twelve;
  265. testing += "+";
  266. testing += thirteen;
  267. testing += "=";
  268. testing += twentyfive;
  269. output = testing;
  270. if (output.actual_message != "12+13=twentyfive")
  271. {
  272. printf("failed test 1.\n");
  273. return 1;
  274. }
  275. // Test 2.
  276. testing.clear();
  277. testing += "test(";
  278. testing.addparameter(twelve);
  279. testing.addparameter(5);
  280. testing.addparameter(twentyfive);
  281. testing += ")";
  282. output = testing;
  283. if (output.actual_message != "test(12,5,10,twentyfive)") {
  284. printf("failed test 2.\n");
  285. return 1;
  286. }
  287. printf("passed.\n");
  288. return 0;
  289. }
  290. int test::sha256_test()
  291. {
  292. printf("sha256 test: ");
  293. datablock input("This is a sha256 test.");
  294. datastring expected_output;
  295. datablock actual_output(SHA256_BLOCK_SIZE << 1);
  296. expected_output = "986fab2cb2d44c0ea7da9926c9f60bd07389bc775383d1e85f49efc4e01578af";
  297. input.sha_256(actual_output);
  298. if (actual_output != expected_output) {
  299. printf("failed.\n");
  300. return 1;
  301. }
  302. printf("passed.\n");
  303. return 0;
  304. }
  305. int test::Debug_test()
  306. {
  307. printf("Debug test.\n");
  308. // Start of error handling.
  309. error_signals::AddHandlers();
  310. pid_t tid = error_signals::GetThreadID();
  311. error_signals *error_thread = error_signals::GetThread(tid);
  312. if (error_thread == nullptr)
  313. {
  314. error_thread = error_signals::AddThread(tid);
  315. }
  316. volatile int val = 0;
  317. if (error_thread != nullptr)
  318. {
  319. error_thread->LineNumberStack = 0;
  320. val = setjmp(error_thread->position);
  321. }
  322. if (val != 0)
  323. {
  324. error_thread->DisplayErrorMessage(val);
  325. return 0;
  326. }
  327. // End of error handling.
  328. // Start of testing.
  329. Debug debug(__FILE__,__func__,__LINE__);
  330. test::Debug_test2();
  331. return 0;
  332. }
  333. void test::Debug_test2()
  334. {
  335. Debug debug(__FILE__,__func__,__LINE__);
  336. test::Debug_test3(2);
  337. debug = __LINE__;
  338. test::Debug_test3(0);
  339. debug = __LINE__;
  340. }
  341. int test::Debug_test3(int x)
  342. {
  343. Debug debug(__FILE__,__func__,__LINE__);
  344. int y;
  345. if (x < 0) {
  346. x = 1;
  347. }
  348. debug = __LINE__;
  349. y = 1/x;
  350. debug = __LINE__;
  351. return y;
  352. }