林永吉的Algorithm库
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

439 řádky
16 KiB

  1. #include "pch.h"
  2. #include "CppUnitTest.h"
  3. using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  4. #define FileName_1 "../Alogrithm/config/1_ContainsNearbyDuplicate.ini"
  5. #define FileName_2 "../Alogrithm/config/2_ExcelSheetColumnTiTle.ini"
  6. #define FileName_3 "../Alogrithm/config/3_bool IsUgly.ini"
  7. #define FileName_4 "../Alogrithm/config/4_IsPalindrome.ini"
  8. #define FileName_5 "../Alogrithm/config/5_MinDepth.ini"
  9. #define FileName_6 "../Alogrithm/config/6_ContainsDuplicate.ini"
  10. #define FileName_7 "../Alogrithm/config/7_MaxDepth.ini"
  11. #define FileName_8 "../Alogrithm/config/8_HammingWeight.ini"
  12. #define FileName_9 "../Alogrithm/config/9_AddBinary.ini"
  13. #define FileName_10 "../Alogrithm/config/10_BinaryTreePaths.ini"
  14. #define FileName_11 "../Alogrithm/config/11_CanWinNim.ini"
  15. #define FileName_12 "../Alogrithm/config/12_IsValid.ini"
  16. #define FileName_13 "../Alogrithm/config/13_MyAtoi.ini"
  17. #define FileName_14 "../Alogrithm/config/14_SingleNumber.ini"
  18. #define FileName_15 "../Alogrithm/config/15_WordPattern.ini"
  19. #define FileName_16 "../Alogrithm/config/16_ReverseBits.ini"
  20. #define FileName_17 "../Alogrithm/config/17_WordBreak.ini"
  21. #define FileName_18 "../Alogrithm/config/18_PlusOne.ini"
  22. #define FileName_19 "../Alogrithm/config/19_MySqrt.ini"
  23. #define FileName_20 "../Alogrithm/config/20_MoveZeroes.ini"
  24. #define FileName_21 "../Alogrithm/config/21_Reverse.ini"
  25. #define FileName_22 "../Alogrithm/config/22_Rotate.ini"
  26. namespace UnitTest
  27. {
  28. TEST_CLASS(UnitTest_1)
  29. {
  30. TEST_METHOD(TestMethode1)
  31. {
  32. char Section_Name[100][10] = { 0 };
  33. int Section_Count = CalcCount(100, Section_Name, FileName_1);
  34. int array_count = 0;
  35. int* Section_Value;
  36. CString Na, Nb, nExpect;
  37. for (int i = 0; i < Section_Count; i++) {
  38. GetPrivateProfileString(Section_Name[i], "array", " ", Na.GetBuffer(200), 200, FileName_1);
  39. GetPrivateProfileString(Section_Name[i], "key", " ", Nb.GetBuffer(20), 20, FileName_1);
  40. GetPrivateProfileString(Section_Name[i], "output", " ", nExpect.GetBuffer(20), 20, FileName_1);
  41. Section_Value = str_device(Na, &array_count);
  42. bool nReal = ContainsNearbyDuplicate(Section_Value, array_count, _ttoi(Nb));
  43. Assert::AreEqual(nReal, CstrToBool(nExpect));
  44. }
  45. }
  46. };
  47. TEST_CLASS(UnitTest_2)
  48. {
  49. TEST_METHOD(TestMethode1)
  50. {
  51. char Section_Name[100][10] = { 0 };
  52. int Section_Count = CalcCount(100, Section_Name, FileName_2);
  53. CString Na, nExpect;
  54. for (int i = 0; i < Section_Count; i++) {
  55. GetPrivateProfileString(Section_Name[i], "Na", " ", Na.GetBuffer(20), 20, FileName_2);
  56. GetPrivateProfileString(Section_Name[i], "nExpect", " ", nExpect.GetBuffer(100), 100, FileName_2);
  57. char* nReal = ExcelSheetColumnTitle(_ttoi(Na));
  58. Assert::AreEqual(nReal, nExpect);
  59. }
  60. }
  61. };
  62. TEST_CLASS(UnitTest_3)
  63. {
  64. TEST_METHOD(TestMethode3)
  65. {
  66. char Section_Name[100][10] = { 0 };
  67. int Section_Count = CalcCount(100, Section_Name, FileName_3);
  68. CString Na, nExpect;
  69. for (int i = 0; i < Section_Count; i++) {
  70. GetPrivateProfileString(Section_Name[i], "Na", " ", Na.GetBuffer(20), 20, FileName_3);
  71. GetPrivateProfileString(Section_Name[i], "nExpect", " ", nExpect.GetBuffer(20), 20, FileName_3);
  72. bool nReal = IsUgly(_ttoi(Na));
  73. Assert::AreEqual(nReal, CstrToBool(nExpect));
  74. }
  75. }
  76. };
  77. TEST_CLASS(UnitTest_4)
  78. {
  79. TEST_METHOD(TestMethode1)
  80. {
  81. char Section_Name[100][10] = { 0 };
  82. int Section_Count = CalcCount(100, Section_Name, FileName_4);
  83. CString Na, nExpect;
  84. for (int i = 0; i < Section_Count; i++) {
  85. GetPrivateProfileString(Section_Name[i], "Na", " ", Na.GetBuffer(20), 20, FileName_4);
  86. GetPrivateProfileString(Section_Name[i], "nExpect", " ", nExpect.GetBuffer(20), 20, FileName_4);
  87. bool nReal = IsPalindrome(_ttoi(Na));
  88. Assert::AreEqual(nReal, CstrToBool(nExpect));
  89. }
  90. }
  91. };
  92. TEST_CLASS(UnitTest_5)
  93. {
  94. TEST_METHOD(TestMethode1)
  95. {
  96. char Section_Name[100][10] = { 0 };
  97. int Section_Count = CalcCount(100, Section_Name, FileName_5);
  98. CString Na, nExpect;
  99. for (int i = 0; i < Section_Count; i++) {
  100. GetPrivateProfileString(Section_Name[i], "input", " ", Na.GetBuffer(200), 200, FileName_5);
  101. GetPrivateProfileString(Section_Name[i], "output", " ", nExpect.GetBuffer(20), 20, FileName_5);
  102. char return_str[100][50] = { 0 };
  103. int return_count = str_device2(Na, return_str);
  104. TreeNode* root = CreatBitTree(return_str, return_count);
  105. int nReal = MinDepth(root);
  106. Assert::AreEqual(nReal,_ttoi(nExpect));
  107. free_tree(root);
  108. }
  109. }
  110. };
  111. TEST_CLASS(UnitTest_6)
  112. {
  113. TEST_METHOD(TestMethode1)
  114. {
  115. char Section_Name[100][10] = { 0 };
  116. int Section_Count = CalcCount(100, Section_Name, FileName_6);
  117. int array_count = 0;
  118. int* Section_Value;
  119. CString Na, Nb, nExpect;
  120. for (int i = 0; i < Section_Count; i++) {
  121. GetPrivateProfileString(Section_Name[i], "input", " ", Na.GetBuffer(200), 200, FileName_6);
  122. GetPrivateProfileString(Section_Name[i], "output", " ", nExpect.GetBuffer(20), 20, FileName_6);
  123. Section_Value = str_device(Na, &array_count);
  124. bool nReal = containsDuplicate(Section_Value, array_count);
  125. Assert::AreEqual(nReal, CstrToBool(nExpect));
  126. }
  127. }
  128. };
  129. TEST_CLASS(UnitTest_7)
  130. {
  131. TEST_METHOD(TestMethode1)
  132. {
  133. char Section_Name[100][10] = { 0 };
  134. int Section_Count = CalcCount(100, Section_Name, FileName_7);
  135. CString Na, nExpect;
  136. for (int i = 0; i < Section_Count; i++) {
  137. GetPrivateProfileString(Section_Name[i], "input", " ", Na.GetBuffer(200), 200, FileName_7);
  138. GetPrivateProfileString(Section_Name[i], "output", " ", nExpect.GetBuffer(20), 20, FileName_7);
  139. char return_str[100][50] = { 0 };
  140. int return_count = str_device2(Na, return_str);
  141. TreeNode2* root = CreatBitTree2(return_str, return_count);
  142. int nReal = MaxDepth(root);
  143. Assert::AreEqual(nReal, _ttoi(nExpect));
  144. free_tree2(root);
  145. }
  146. }
  147. };
  148. TEST_CLASS(UnitTest_8)
  149. {
  150. TEST_METHOD(TestMethode1)
  151. {
  152. char Section_Name[100][10] = { 0 };
  153. int Section_Count = CalcCount(100, Section_Name, FileName_8);
  154. CString Na, nExpect;
  155. for (int i = 0; i < Section_Count; i++) {
  156. GetPrivateProfileString(Section_Name[i], "input", " ", Na.GetBuffer(20), 20, FileName_8);
  157. GetPrivateProfileString(Section_Name[i], "output", " ", nExpect.GetBuffer(100), 100, FileName_8);
  158. int nReal = HammingWeight(_ttoi(Na));
  159. Assert::AreEqual(nReal, _ttoi(nExpect));
  160. }
  161. }
  162. };
  163. TEST_CLASS(UnitTest_9)
  164. {
  165. TEST_METHOD(TestMethode1)
  166. {
  167. char Section_Name[100][10] = { 0 };
  168. int Section_Count = CalcCount(100, Section_Name, FileName_9);
  169. CString input1,input2, output;
  170. for (int i = 0; i < Section_Count; i++) {
  171. GetPrivateProfileString(Section_Name[i], "input1", " ", input1.GetBuffer(100), 100, FileName_9);
  172. GetPrivateProfileString(Section_Name[i], "input2", " ", input2.GetBuffer(100), 100, FileName_9);
  173. GetPrivateProfileString(Section_Name[i], "output", " ", output.GetBuffer(200), 200, FileName_9);
  174. char p1[100];
  175. char p2[100];
  176. strcpy(p1, input1);
  177. strcpy(p2, input2);
  178. char* nReal = AddBinary(p1,p2);
  179. Assert::AreEqual(nReal, output);
  180. }
  181. }
  182. };
  183. TEST_CLASS(UnitTest_10)
  184. {
  185. TEST_METHOD(TestMethode1)
  186. {
  187. char Section_Name[100][10] = { 0 };
  188. int Section_Count = CalcCount(100, Section_Name, FileName_10);
  189. CString Na, nExpect;
  190. for (int i = 0; i < Section_Count; i++) {
  191. GetPrivateProfileString(Section_Name[i], "input", " ", Na.GetBuffer(200), 200, FileName_10);
  192. GetPrivateProfileString(Section_Name[i], "output", " ", nExpect.GetBuffer(500), 500, FileName_10);
  193. char return_str[100][50] = { 0 };
  194. int return_count = str_device2(Na, return_str);
  195. TreeNode3* root = CreatBitTree3(return_str, return_count);
  196. int returnSize = 0;
  197. char** returnStr=NULL;
  198. returnStr = binaryTreePaths(root, &returnSize);
  199. char nReal[500] = { 0 };
  200. for (int i = 0; i < returnSize; i++)
  201. {
  202. strcat(nReal, returnStr[i]);
  203. if (i != (returnSize - 1))
  204. {
  205. nReal[strlen(nReal)] = ',';
  206. }
  207. }
  208. Assert::AreEqual(nReal,nExpect);
  209. free_tree3(root);
  210. }
  211. }
  212. };
  213. TEST_CLASS(UnitTest_11)
  214. {
  215. TEST_METHOD(TestMethode1)
  216. {
  217. char Section_Name[100][10] = { 0 };
  218. int Section_Count = CalcCount(100, Section_Name, FileName_11);
  219. CString input,output;
  220. for (int i = 0; i < Section_Count; i++) {
  221. GetPrivateProfileString(Section_Name[i], "input", " ", input.GetBuffer(20), 20, FileName_11);
  222. GetPrivateProfileString(Section_Name[i], "output", " ", output.GetBuffer(20), 20, FileName_11);
  223. bool nReal = CanWinNim(_ttoi(input));
  224. Assert::AreEqual(nReal, CstrToBool(output));
  225. }
  226. }
  227. };
  228. TEST_CLASS(UnitTest_12)
  229. {
  230. TEST_METHOD(TestMethode1)
  231. {
  232. char Section_Name[100][10] = { 0 };
  233. int Section_Count = CalcCount(100, Section_Name, FileName_12);
  234. char input_char[1024] = { 0 };
  235. CString input,output;
  236. for (int i = 0; i < Section_Count; i++) {
  237. GetPrivateProfileString(Section_Name[i], "input", " ", input.GetBuffer(200), 200, FileName_12);
  238. GetPrivateProfileString(Section_Name[i], "output", " ", output.GetBuffer(20), 20, FileName_12);
  239. strcpy(input_char, input);
  240. bool nReal = IsValid(input_char);
  241. Assert::AreEqual(nReal, CstrToBool(output));
  242. }
  243. }
  244. };
  245. TEST_CLASS(UnitTest_13)
  246. {
  247. TEST_METHOD(TestMethode1)
  248. {
  249. char Section_Name[100][10] = { 0 };
  250. int Section_Count = CalcCount(100, Section_Name, FileName_13);
  251. char input_char[1024] = { 0 };
  252. CString input, output;
  253. for (int i = 0; i < Section_Count; i++) {
  254. GetPrivateProfileString(Section_Name[i], "input", " ", input.GetBuffer(200), 200, FileName_13);
  255. GetPrivateProfileString(Section_Name[i], "output", " ", output.GetBuffer(20), 20, FileName_13);
  256. strcpy(input_char, input);
  257. int nReal = MyAtoi(input_char);
  258. Assert::AreEqual(nReal, _ttoi(output));
  259. }
  260. }
  261. };
  262. TEST_CLASS(UnitTest_14)
  263. {
  264. TEST_METHOD(TestMethode1)
  265. {
  266. char Section_Name[100][10] = { 0 };
  267. int Section_Count = CalcCount(100, Section_Name, FileName_14);
  268. int array_count = 0;
  269. int* Section_Value;
  270. CString input,output;
  271. for (int i = 0; i < Section_Count; i++) {
  272. GetPrivateProfileString(Section_Name[i], "input", " ", input.GetBuffer(200), 200, FileName_14);
  273. GetPrivateProfileString(Section_Name[i], "output", " ", output.GetBuffer(20), 20, FileName_14);
  274. Section_Value = str_device(input, &array_count);
  275. int nReal = SingleNumber(Section_Value, array_count);
  276. Assert::AreEqual(nReal, _ttoi(output));
  277. }
  278. }
  279. };
  280. TEST_CLASS(UnitTest_15)
  281. {
  282. TEST_METHOD(TestMethode1)
  283. {
  284. char Section_Name[100][10] = { 0 };
  285. int Section_Count = CalcCount(100, Section_Name, FileName_15);
  286. char Pattern_char[1024] = { 0 };
  287. char Str_char[1024] = { 0 };
  288. CString Pattern, Str, Output;
  289. for (int i = 0; i < Section_Count; i++) {
  290. GetPrivateProfileString(Section_Name[i], "Pattern", " ", Pattern.GetBuffer(200), 200, FileName_15);
  291. GetPrivateProfileString(Section_Name[i], "Str", " ", Str.GetBuffer(400), 400, FileName_15);
  292. GetPrivateProfileString(Section_Name[i], "Output", " ", Output.GetBuffer(20), 20, FileName_15);
  293. strcpy(Pattern_char, Pattern);
  294. strcpy(Str_char, Str);
  295. bool nReal = WordPattern(Pattern_char, Str_char);
  296. Assert::AreEqual(nReal, CstrToBool(Output));
  297. }
  298. }
  299. };
  300. TEST_CLASS(UnitTest_16)
  301. {
  302. TEST_METHOD(TestMethode1)
  303. {
  304. char Section_Name[100][10] = { 0 };
  305. int Section_Count = CalcCount(100, Section_Name, FileName_16);
  306. CString input,output;
  307. for (int i = 0; i < Section_Count; i++) {
  308. GetPrivateProfileString(Section_Name[i], "Input", " ", input.GetBuffer(200), 200, FileName_16);
  309. GetPrivateProfileString(Section_Name[i], "Output", " ", output.GetBuffer(200), 200, FileName_16);
  310. uint32_t nReal = ReverseBits((uint32_t)_atoi64(input));
  311. Assert::AreEqual(nReal, (uint32_t)_atoi64(output));
  312. }
  313. }
  314. };
  315. TEST_CLASS(UnitTest_17)
  316. {
  317. TEST_METHOD(TestMethode1)
  318. {
  319. char Section_Name[100][10] = { 0 };
  320. int Section_Count = CalcCount(100, Section_Name, FileName_17);
  321. CString Input1, Input2, Output;
  322. char Str_char[1024] = { 0 };
  323. char* WordDic[100];
  324. for (int i = 0; i < Section_Count; i++) {
  325. GetPrivateProfileString(Section_Name[i], "Input1", " ", Input1.GetBuffer(200), 200, FileName_17);
  326. GetPrivateProfileString(Section_Name[i], "Input2", " ", Input2.GetBuffer(500), 500, FileName_17);
  327. GetPrivateProfileString(Section_Name[i], "Output", " ", Output.GetBuffer(20), 20, FileName_17);
  328. strcpy(Str_char, Input1);
  329. int Wordic_count = str_device3(Input2, WordDic);
  330. bool nReal = WordBreak(Str_char, WordDic, Wordic_count);
  331. Assert::AreEqual(nReal, CstrToBool(Output));
  332. }
  333. }
  334. };
  335. TEST_CLASS(UnitTest_18)
  336. {
  337. TEST_METHOD(TestMethode1)
  338. {
  339. char Section_Name[100][10] = { 0 };
  340. int Section_Count = CalcCount(100, Section_Name, FileName_18);
  341. int input_count = 0, output_count = 0, returnSize = 0;
  342. int* input_value;
  343. int* output_value;
  344. CString input,output;
  345. for (int i = 0; i < Section_Count; i++) {
  346. GetPrivateProfileString(Section_Name[i], "Input", " ", input.GetBuffer(200), 200, FileName_18);
  347. GetPrivateProfileString(Section_Name[i], "Output", " ", output.GetBuffer(200), 200, FileName_18);
  348. input_value = str_device(input, &input_count);
  349. output_value = str_device(output, &output_count);
  350. int* nReal = PlusOne(input_value, input_count, &returnSize);
  351. for (int i = 0; i < input_count; i++) {
  352. Assert::AreEqual(nReal[i], output_value[i]);
  353. }
  354. }
  355. }
  356. };
  357. TEST_CLASS(UnitTest_19)
  358. {
  359. TEST_METHOD(TestMethode1)
  360. {
  361. char Section_Name[100][10] = { 0 };
  362. int Section_Count = CalcCount(100, Section_Name, FileName_19);
  363. CString input,output;
  364. for (int i = 0; i < Section_Count; i++) {
  365. GetPrivateProfileString(Section_Name[i], "Input", " ", input.GetBuffer(100), 100, FileName_19);
  366. GetPrivateProfileString(Section_Name[i], "Output", " ", output.GetBuffer(100), 100, FileName_19);
  367. int nReal = MySqrt(_ttoi(input));
  368. Assert::AreEqual(nReal, _ttoi(output));
  369. }
  370. }
  371. };
  372. TEST_CLASS(UnitTest_20)
  373. {
  374. TEST_METHOD(TestMethode1)
  375. {
  376. char Section_Name[100][10] = { 0 };
  377. int Section_Count = CalcCount(100, Section_Name, FileName_20);
  378. int input_count = 0, output_count = 0, returnSize = 0;
  379. int* input_value;
  380. int* output_value;
  381. CString input, output;
  382. for (int i = 0; i < Section_Count; i++) {
  383. GetPrivateProfileString(Section_Name[i], "Input", " ", input.GetBuffer(200), 200, FileName_20);
  384. GetPrivateProfileString(Section_Name[i], "Output", " ", output.GetBuffer(200), 200, FileName_20);
  385. input_value = str_device(input, &input_count);
  386. output_value = str_device(output, &output_count);
  387. MoveZeroes(input_value, input_count);
  388. for (int i = 0; i < input_count; i++) {
  389. Assert::AreEqual(input_value[i], output_value[i]);
  390. }
  391. }
  392. }
  393. };
  394. TEST_CLASS(UnitTest_21)
  395. {
  396. TEST_METHOD(TestMethode1)
  397. {
  398. char Section_Name[100][10] = { 0 };
  399. int Section_Count = CalcCount(100, Section_Name, FileName_21);
  400. CString input, output;
  401. for (int i = 0; i < Section_Count; i++) {
  402. GetPrivateProfileString(Section_Name[i], "Input", " ", input.GetBuffer(100), 100, FileName_21);
  403. GetPrivateProfileString(Section_Name[i], "Output", " ", output.GetBuffer(100), 100, FileName_21);
  404. int nReal = Reverse(_ttoi(input));
  405. Assert::AreEqual(nReal, _ttoi(output));
  406. }
  407. }
  408. };
  409. TEST_CLASS(UnitTest_22)
  410. {
  411. TEST_METHOD(TestMethode1)
  412. {
  413. char Section_Name[100][10] = { 0 };
  414. int Section_Count = CalcCount(100, Section_Name, FileName_22);
  415. int input_count = 0, output_count = 0, returnSize = 0;
  416. int* input_value;
  417. int* output_value;
  418. CString input, output,k;
  419. for (int i = 0; i < Section_Count; i++) {
  420. GetPrivateProfileString(Section_Name[i], "Input", " ", input.GetBuffer(200), 200, FileName_22);
  421. GetPrivateProfileString(Section_Name[i], "k", " ", k.GetBuffer(20), 20, FileName_22);
  422. GetPrivateProfileString(Section_Name[i], "Output", " ", output.GetBuffer(200), 200, FileName_22);
  423. input_value = str_device(input, &input_count);
  424. output_value = str_device(output, &output_count);
  425. Rotate1(input_value, input_count, _ttoi(k));
  426. for (int i = 0; i < input_count; i++) {
  427. Assert::AreEqual(input_value[i], output_value[i]);
  428. }
  429. }
  430. }
  431. };
  432. }