林永吉的Algorithm库
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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