林永吉的Algorithm库
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

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