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

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