|
- #include "pch.h"
- #include "CppUnitTest.h"
- #include "test.h"
- #include <assert.h>
- #include <chrono>
- #include <ctime>
- #include <cstdlib>
- using namespace Microsoft::VisualStudio::CppUnitTestFramework;
-
- void GenerateRandomArray(vector<int>& 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<int> arr = { 5,8,4,3,9,1 };
- const vector<int> 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<int> 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<int> 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<std::chrono::milliseconds>(end - start);
- Logger::WriteMessage(("Time: " + std::to_string(duration.count()) + "ms").c_str());
- Assert::IsTrue(duration.count() < 10000); // 设定阈值
- }
- };
- }
|