林永吉的Algorithm库
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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