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

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