林永吉的Algorithm库
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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