林永吉的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ů.

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