林永吉的Algorithm库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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