林永吉的Algorithm库
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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