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

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