林永吉的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ů.

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