林永吉的Algorithm库
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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