From 39d14f2e420c2581ed872960d96a11e7831ab2d6 Mon Sep 17 00:00:00 2001 From: JIU JIALIN <2339061402@qq.com> Date: Fri, 11 Jul 2025 16:56:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=B8=80=E4=B8=AA=E6=96=B0?= =?UTF-8?q?=E7=9A=84=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- UnitTest1.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 UnitTest1.cpp 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); // 设定阈值 + } + }; +}