林永吉的Algorithm库
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

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