林永吉的Algorithm库
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

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