diff --git a/JJL.c b/JJL.c index a26b925..82865e4 100644 --- a/JJL.c +++ b/JJL.c @@ -5,6 +5,11 @@ int add(int a,int b) return a + b; } +int sub(int a,int b) +{ + return a-b; +} + int main() { printf("hello world!"); diff --git a/UnitTest1.cpp b/UnitTest1.cpp new file mode 100644 index 0000000..ea56c80 --- /dev/null +++ b/UnitTest1.cpp @@ -0,0 +1,57 @@ +#include "pch.h" +#include "CppUnitTest.h" +#include "test.h" +#include +#include +#include +#include +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +void GenerateRandomArray(vector& a,int sum) +{ + srand(time(0)); // 使用当前时间作为种子 + for (int i = 0; i < sum; i++) { + a.push_back(i); + } +} + + +namespace UnitTest1 +{ + TEST_CLASS(UnitTest1) + { + public: + + TEST_METHOD(TestMethod1) + { + vector arr = { 5,8,4,3,9,1 }; + const vector expect = { 1,3,4,5,8,9 }; + selectionSort(arr); + for(int i= 0;i < arr.size();i++) + { + Assert::AreEqual(expect.at(i), arr.at(i)); + } + } + TEST_METHOD(TestEmptyArr) + { + vector arr = {}; + selectionSort(arr); + Assert::IsTrue(true); + + } + BEGIN_TEST_METHOD_ATTRIBUTE(TestRunTime) + TEST_METHOD_ATTRIBUTE(L"RUN","TIME") + END_TEST_METHOD_ATTRIBUTE(TestRunTime) + TEST_METHOD(TestRunTime) + { + vector arr; + GenerateRandomArray(arr, 10000); // 生成随机数组 + auto start = std::chrono::high_resolution_clock::now(); + selectionSort(arr); + auto end = std::chrono::high_resolution_clock::now(); + auto duration = std::chrono::duration_cast(end - start); + Logger::WriteMessage(("Time: " + std::to_string(duration.count()) + "ms").c_str()); + Assert::IsTrue(duration.count() < 10000); // 设定阈值 + } + }; +}