林永吉的Algorithm库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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