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

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