diff --git a/base/test/unittest/common/utils_file_test.cpp b/base/test/unittest/common/utils_file_test.cpp index 21c9c73415bc830dcff120f7896714677cdb6a74..0ac9d607dbab240c1aa022367a704a5225a61530 100644 --- a/base/test/unittest/common/utils_file_test.cpp +++ b/base/test/unittest/common/utils_file_test.cpp @@ -73,6 +73,21 @@ int RemoveTestFile(const std::string& path) return unlink(path.c_str()); } +void OpenAndLoadFileContent(string& content, string& loadResult) +{ + string filename = UtilsFileTest::FILE_PATH; + int fd = open(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + bool ret = SaveStringToFd(fd, content); + close(fd); + EXPECT_EQ(ret, true); + + fd = open(filename.c_str(), O_RDONLY); + ret = LoadStringFromFd(fd, loadResult); + close(fd); + RemoveTestFile(filename); + EXPECT_EQ(ret, true); +} + /* * @tc.name: testLoadStringFromFile001 * @tc.desc: Test loading an existed file 'meminfo' @@ -389,18 +404,8 @@ HWTEST_F(UtilsFileTest, testSaveStringToFd001, TestSize.Level0) HWTEST_F(UtilsFileTest, testSaveStringToFd002, TestSize.Level0) { string content; - string filename = FILE_PATH; - int fd = open(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); - bool ret = SaveStringToFd(fd, content); - close(fd); - EXPECT_EQ(ret, true); - string loadResult; - fd = open(filename.c_str(), O_RDONLY); - ret = LoadStringFromFd(fd, loadResult); - close(fd); - RemoveTestFile(filename); - EXPECT_EQ(ret, true); + OpenAndLoadFileContent(content, loadResult); EXPECT_EQ(loadResult, ""); } @@ -411,18 +416,8 @@ HWTEST_F(UtilsFileTest, testSaveStringToFd002, TestSize.Level0) HWTEST_F(UtilsFileTest, testSaveStringToFd003, TestSize.Level0) { string content = CONTENT_STR; - string filename = FILE_PATH; - int fd = open(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); - bool ret = SaveStringToFd(fd, content); - close(fd); - EXPECT_EQ(ret, true); - string loadResult; - fd = open(filename.c_str(), O_RDONLY); - ret = LoadStringFromFd(fd, loadResult); - close(fd); - RemoveTestFile(filename); - EXPECT_EQ(ret, true); + OpenAndLoadFileContent(content, loadResult); EXPECT_EQ(loadResult, content); } diff --git a/base/test/unittest/common/utils_safe_map_test.cpp b/base/test/unittest/common/utils_safe_map_test.cpp index 9c78dbfb778d35370384ff70ce55c6a44dfb1666..ff308a2950aae8de718c0eff7b1f8a1513658c11 100644 --- a/base/test/unittest/common/utils_safe_map_test.cpp +++ b/base/test/unittest/common/utils_safe_map_test.cpp @@ -17,8 +17,8 @@ #include #include #include -#include #include +#include #include // std::chrono::seconds using namespace testing::ext; using namespace std; @@ -28,6 +28,36 @@ namespace { class UtilsSafeMap : public testing::Test { }; +//线程数 +const int THREAD_NUM = 100; +//数据长度 +const int DATA_NUM = 10; +//线程休眠秒数3 +const int SLEEP_SECOND_3 = 3; +//线程休眠秒数4 +const int SLEEP_SECOND_4 = 4; +//线程休眠秒数10 +const int SLEEP_SECOND_10 = 10; +void TestThreadRunResult(std::thread (&threads)[THREAD_NUM], + vector &result, + std::vector> &vcfi) +{ + std::this_thread::sleep_for(std::chrono::seconds(SLEEP_SECOND_4)); + for (auto& t : threads) { + t.join(); + } + + for (auto& t : vcfi) { + result.push_back(t.get()); + } + + std::sort(result.begin(), result.end()); + + for (int i = 0; i < THREAD_NUM; ++i) { + ASSERT_EQ(i, result[i]); + } +} + /* * @tc.name: testUtilsCopyAndAssign001 * @tc.desc: single thread test the normal feature insert and erase and EnsureInsert @@ -226,8 +256,6 @@ HWTEST_F(UtilsSafeMap, testUtilsNormalFeatureIterate001, TestSize.Level0) * @tc.name: testUtilsConcurrentIterate001 * @tc.desc: 100 threads test in iterate operation to rewrite a SafeMap. */ -const int THREAD_NUM = 100; -const int DATA_NUM = 10; HWTEST_F(UtilsSafeMap, testUtilsConcurrentIterate001, TestSize.Level0) { SafeMap demoData; @@ -255,7 +283,7 @@ HWTEST_F(UtilsSafeMap, testUtilsConcurrentIterate001, TestSize.Level0) threads[i] = std::thread(lamfuncIterate, std::ref(demoData), i, system_clock::from_time_t(timeT)); } - std::this_thread::sleep_for(std::chrono::seconds(3)); + std::this_thread::sleep_for(std::chrono::seconds(SLEEP_SECOND_3)); for (auto& t : threads) { t.join(); } @@ -300,7 +328,7 @@ HWTEST_F(UtilsSafeMap, testUtilsConcurrentWriteAndRead001, TestSize.Level0) checkThread[i] = std::thread(lamfuncCheck, std::ref(demoData), key, system_clock::from_time_t(timeT)); } - std::this_thread::sleep_for(std::chrono::seconds(3)); + std::this_thread::sleep_for(std::chrono::seconds(SLEEP_SECOND_3)); for (auto& t : threads) { t.join(); } @@ -323,22 +351,22 @@ HWTEST_F(UtilsSafeMap, testUtilsConcurrentWriteAndFind001, TestSize.Level0) std::vector> vcfi; ASSERT_NO_THROW({ - auto lamfuncInsert = [](SafeMap& data, const string& key, - const int& value, const std::chrono::system_clock::time_point& absTime) { - std::this_thread::sleep_until(absTime); - data.EnsureInsert(key, value); - }; - auto lamfuncCheckLoop = [](SafeMap& data, const string& key, std::chrono::system_clock::time_point absTime) { std::this_thread::sleep_until(absTime); thread_local int i = -1; while (!data.Find(key, i)) { - std::this_thread::sleep_for(std::chrono::microseconds(10)); + std::this_thread::sleep_for(std::chrono::microseconds(SLEEP_SECOND_10)); } return i; }; + auto lamfuncInsert = [](SafeMap& data, const string& key, + const int& value, const std::chrono::system_clock::time_point& absTime) { + std::this_thread::sleep_until(absTime); + data.EnsureInsert(key, value); + }; + using std::chrono::system_clock; std::time_t timeT = system_clock::to_time_t(system_clock::now()); @@ -352,53 +380,12 @@ HWTEST_F(UtilsSafeMap, testUtilsConcurrentWriteAndFind001, TestSize.Level0) std::ref(demoData), key + std::to_string(i), system_clock::from_time_t(timeT))); } - std::this_thread::sleep_for(std::chrono::seconds(4)); - for (auto& t : threads) { - t.join(); - } - vector result; - for (auto& t : vcfi) { - result.push_back(t.get()); - } - - std::sort(result.begin(), result.end()); - - for (int i = 0; i < THREAD_NUM; ++i) { - ASSERT_EQ(i, result[i]); - } + TestThreadRunResult(threads, result, vcfi); }); } -static void ResultCompare(std::vector>& vcfi, SafeMap& demoData) -{ - vector result; - for (auto& t : vcfi) { - result.push_back(t.get()); - } - - std::sort(result.begin(), result.end()); - - for (int i = 0; i < THREAD_NUM; ++i) { - ASSERT_EQ(i, result[i]); - } - - int t = 0; - result.clear(); - for (int i = 0; i < THREAD_NUM; ++i) { - t = -1; - ASSERT_TRUE(demoData.Find("A" + std::to_string(i), t)); - result.push_back(t); - } - - std::sort(result.begin(), result.end()); - - for (int i = 0; i < THREAD_NUM; ++i) { - ASSERT_EQ(i + 1, result[i]); - } -} - /* * @tc.name: testUtilsConcurrentWriteAndFindAndSet001 * @tc.desc: 100 threads test in writein to the corresponding key of the map, @@ -422,7 +409,7 @@ HWTEST_F(UtilsSafeMap, testUtilsConcurrentWriteAndFindAndSet001, TestSize.Level0 std::this_thread::sleep_until(absTime); thread_local int i = -1; while (!data.FindOldAndSetNew(key, i, newvalue)) { - std::this_thread::sleep_for(std::chrono::microseconds(10)); + std::this_thread::sleep_for(std::chrono::microseconds(SLEEP_SECOND_10)); } return i; }; @@ -440,12 +427,23 @@ HWTEST_F(UtilsSafeMap, testUtilsConcurrentWriteAndFindAndSet001, TestSize.Level0 std::ref(demoData), key + std::to_string(i), i + 1, system_clock::from_time_t(timeT))); } - std::this_thread::sleep_for(std::chrono::seconds(4)); - for (auto& t : threads) { - t.join(); + vector result; + + TestThreadRunResult(threads, result, vcfi); + + int t = 0; + result.clear(); + for (int i = 0; i < THREAD_NUM; ++i) { + t = -1; + ASSERT_TRUE(demoData.Find("A" + std::to_string(i), t)); + result.push_back(t); } - ResultCompare(vcfi, demoData); + std::sort(result.begin(), result.end()); + + for (int i = 0; i < THREAD_NUM; ++i) { + ASSERT_EQ(i + 1, result[i]); + } }); } } // namespace diff --git a/base/test/unittest/common/utils_singleton_test.cpp b/base/test/unittest/common/utils_singleton_test.cpp index 8461612a958e56d84d57ec48b61d3cf57ecd310e..d572b1c399f50d5241ad70a60672c4275f48acdb 100644 --- a/base/test/unittest/common/utils_singleton_test.cpp +++ b/base/test/unittest/common/utils_singleton_test.cpp @@ -31,8 +31,8 @@ public: } }; -DelayedSingletonDeclearTest::~DelayedSingletonDeclearTest() {}; DelayedSingletonDeclearTest::DelayedSingletonDeclearTest() {}; +DelayedSingletonDeclearTest::~DelayedSingletonDeclearTest() {}; class SingletonDeclearTest { DECLARE_SINGLETON(SingletonDeclearTest); diff --git a/base/test/unittest/common/utils_sorted_vector_test.cpp b/base/test/unittest/common/utils_sorted_vector_test.cpp index 8316b5465b1db0c01daaea68bf722eb1a231cb1e..86404b411e0489adb5e7cf273d047f696a3d8d70 100644 --- a/base/test/unittest/common/utils_sorted_vector_test.cpp +++ b/base/test/unittest/common/utils_sorted_vector_test.cpp @@ -24,16 +24,33 @@ namespace { class UtilsSortedVector : public testing::Test { }; +template +void PushItem(SortedVector& svec, std::vector& vec, int pushTimes) +{ + for (int i = 0; i < pushTimes; i++) { + vec.push_back(i); + } + + for (int i = (pushTimes / 2) - 1; i >= 0; i--) { + svec.Add(i); + } + + for (int i = 0; i < pushTimes; i++) { + svec.Add(i); + } +} + HWTEST_F(UtilsSortedVector, testDefaultConsAndAddAndSort, TestSize.Level0) { SortedVector svec; std::vector vec; - - for (int i = 0; i < 10; i++) { + //默认添加次数 + int defaultPushTimes = 10; + for (int i = 0; i < defaultPushTimes; i++) { vec.push_back(i); } - for (int i = 9; i >= 0; i--) { + for (int i = defaultPushTimes - 1; i >= 0; i--) { svec.Add(i); } @@ -49,12 +66,13 @@ HWTEST_F(UtilsSortedVector, testConsFromSortedAllowDup, TestSize.Level0) { SortedVector svec; std::vector vec; - - for (int i = 0; i < 10; i++) { + //默认添加次数 + int defaultPushTimes = 10; + for (int i = 0; i < defaultPushTimes; i++) { vec.push_back(i); } - for (int i = 9; i >= 0; i--) { + for (int i = defaultPushTimes - 1; i >= 0; i--) { svec.Add(i); } @@ -72,26 +90,20 @@ HWTEST_F(UtilsSortedVector, testConsFromSortedNotAllowDuplicate, TestSize.Level0 { SortedVector svec; std::vector vec; + //默认添加次数 + int defaultPushTimes = 20; + //期望的SortedVector的大小 + int exceptSortedVectorSize = 30; - for (int i = 0; i < 20; i++) { - vec.push_back(i); - } + PushItem(svec, vec, defaultPushTimes); - for (int i = 9; i >= 0; i--) { - svec.Add(i); - } - - for (int i = 0; i < 20; i++) { - svec.Add(i); - } - - ASSERT_EQ(static_cast(30), svec.Size()); + ASSERT_EQ(static_cast(exceptSortedVectorSize), svec.Size()); SortedVector newSvec(svec); - ASSERT_EQ(static_cast(20), newSvec.Size()); + ASSERT_EQ(static_cast(defaultPushTimes), newSvec.Size()); - for (int i = 0; i < 20; i++) { + for (int i = 0; i < defaultPushTimes; i++) { ASSERT_EQ(vec[i], newSvec[i]); } } @@ -100,33 +112,24 @@ HWTEST_F(UtilsSortedVector, testConsFromSortedNotAllowToAlloworNotAllow, TestSiz { SortedVector svec; std::vector vec; + //默认添加次数 + int defaultPushTimes = 20; + PushItem(svec, vec, defaultPushTimes); - for (int i = 0; i < 20; i++) { - vec.push_back(i); - } - - for (int i = 9; i >= 0; i--) { - svec.Add(i); - } - - for (int i = 0; i < 20; i++) { - svec.Add(i); - } - - ASSERT_EQ(static_cast(20), svec.Size()); + ASSERT_EQ(static_cast(defaultPushTimes), svec.Size()); SortedVector newSvecTrue(svec); - ASSERT_EQ(static_cast(20), newSvecTrue.Size()); + ASSERT_EQ(static_cast(defaultPushTimes), newSvecTrue.Size()); - for (int i = 0; i < 20; i++) { + for (int i = 0; i < defaultPushTimes; i++) { ASSERT_EQ(vec[i], newSvecTrue[i]); } SortedVector newSvecFalse(svec); - ASSERT_EQ(static_cast(20), newSvecFalse.Size()); + ASSERT_EQ(static_cast(defaultPushTimes), newSvecFalse.Size()); - for (int i = 0; i < 20; i++) { + for (int i = 0; i < defaultPushTimes; i++) { ASSERT_EQ(vec[i], newSvecFalse[i]); } } @@ -135,12 +138,13 @@ HWTEST_F(UtilsSortedVector, testoperatoreq, TestSize.Level0) { SortedVector svec; std::vector vec; - - for (int i = 0; i < 10; i++) { + //默认添加次数 + int defaultPushTimes = 10; + for (int i = 0; i < defaultPushTimes; i++) { vec.push_back(i); } - for (int i = 9; i >= 0; i--) { + for (int i = defaultPushTimes - 1; i >= 0; i--) { svec.Add(i); } @@ -158,25 +162,18 @@ HWTEST_F(UtilsSortedVector, testOperatorEqAllowToNotAllow, TestSize.Level0) { SortedVector svec; std::vector vec; + //默认添加次数 + int defaultPushTimes = 20; + //期望的SortedVector的大小 + int exceptSortedVectorSize = 30; + PushItem(svec, vec, defaultPushTimes); - for (int i = 0; i < 20; i++) { - vec.push_back(i); - } - - for (int i = 9; i >= 0; i--) { - svec.Add(i); - } - - for (int i = 0; i < 20; i++) { - svec.Add(i); - } - - ASSERT_EQ(static_cast(30), svec.Size()); + ASSERT_EQ(static_cast(exceptSortedVectorSize), svec.Size()); SortedVector newSvec = svec; - ASSERT_EQ(static_cast(20), newSvec.Size()); + ASSERT_EQ(static_cast(defaultPushTimes), newSvec.Size()); - for (int i = 0; i < 20; i++) { + for (int i = 0; i < defaultPushTimes; i++) { ASSERT_EQ(vec[i], newSvec[i]); } } @@ -185,25 +182,16 @@ HWTEST_F(UtilsSortedVector, testOperatorEqNotAllowToAllowOrNotAllow, TestSize.Le { SortedVector svec; std::vector vec; + //默认添加次数 + int defaultPushTimes = 20; + PushItem(svec, vec, defaultPushTimes); - for (int i = 0; i < 20; i++) { - vec.push_back(i); - } - - for (int i = 9; i >= 0; i--) { - svec.Add(i); - } - - for (int i = 0; i < 20; i++) { - svec.Add(i); - } - - ASSERT_EQ(static_cast(20), svec.Size()); + ASSERT_EQ(static_cast(defaultPushTimes), svec.Size()); SortedVector newSvecFalse = svec; SortedVector newSvecTrue = svec; - ASSERT_EQ(static_cast(20), newSvecFalse.Size()); - ASSERT_EQ(static_cast(20), newSvecTrue.Size()); - for (int i = 0; i < 20; i++) { + ASSERT_EQ(static_cast(defaultPushTimes), newSvecFalse.Size()); + ASSERT_EQ(static_cast(defaultPushTimes), newSvecTrue.Size()); + for (int i = 0; i < defaultPushTimes; i++) { ASSERT_EQ(vec[i], newSvecFalse[i]); ASSERT_EQ(vec[i], newSvecTrue[i]); } @@ -212,26 +200,29 @@ HWTEST_F(UtilsSortedVector, testOperatorEqNotAllowToAllowOrNotAllow, TestSize.Le HWTEST_F(UtilsSortedVector, testOperatorEqAssignmentTwice, TestSize.Level0) { SortedVector svec; - + //默认添加次数 + int defaultPushTimes = 20; + //期望的SortedVector的大小 + int exceptSortedVectorSize = 30; std::vector vec; - for (int i = 20; i < 30; i++) { + for (int i = defaultPushTimes; i < exceptSortedVectorSize; i++) { vec.push_back(i); } - for (int i = 0; i < 20; i++) { + for (int i = 0; i < defaultPushTimes; i++) { svec.Add(i); } SortedVector newSvecFalse; - for (int i = 20; i < 30; i++) { + for (int i = defaultPushTimes; i < exceptSortedVectorSize; i++) { newSvecFalse.Add(i); } svec = newSvecFalse; - ASSERT_EQ(static_cast(10), svec.Size()); - for (int i = 0; i < 10; i++) { + ASSERT_EQ(static_cast(defaultPushTimes / 2), svec.Size()); + for (int i = 0; i < defaultPushTimes / 2; i++) { ASSERT_EQ(vec[i], svec[i]); } } @@ -240,12 +231,13 @@ HWTEST_F(UtilsSortedVector, testoperatorconsteq, TestSize.Level0) { SortedVector svec; std::vector vec; - - for (int i = 0; i < 10; i++) { + //默认添加次数 + int defaultPushTimes = 10; + for (int i = 0; i < defaultPushTimes; i++) { vec.push_back(i); } - for (int i = 9; i >= 0; i--) { + for (int i = defaultPushTimes - 1; i >= 0; i--) { svec.Add(i); } @@ -262,15 +254,16 @@ HWTEST_F(UtilsSortedVector, testoperatorconsteq, TestSize.Level0) HWTEST_F(UtilsSortedVector, testsizeclearIsEmpty, TestSize.Level0) { SortedVector svec; - + //默认添加次数 + int defaultPushTimes = 10; ASSERT_TRUE(svec.IsEmpty()); ASSERT_EQ(svec.Size(), static_cast(0)); - for (int i = 9; i >= 0; i--) { + for (int i = defaultPushTimes - 1; i >= 0; i--) { svec.Add(i); } - ASSERT_EQ(svec.Size(), static_cast(10)); + ASSERT_EQ(svec.Size(), static_cast(defaultPushTimes)); ASSERT_FALSE(svec.IsEmpty()); @@ -283,24 +276,28 @@ HWTEST_F(UtilsSortedVector, testsizeclearIsEmpty, TestSize.Level0) HWTEST_F(UtilsSortedVector, testCapasityandSetcapasity, TestSize.Level0) { SortedVector svec; - - for (int i = 9; i >= 0; i--) { + //默认添加次数 + int defaultPushTimes = 10; + //最大容量 + int maxCapcity = 1000; + for (int i = defaultPushTimes - 1; i >= 0; i--) { svec.Add(i); } - svec.SetCapcity(1000); + svec.SetCapcity(maxCapcity); - ASSERT_EQ(svec.Capacity(), static_cast(1000)); + ASSERT_EQ(svec.Capacity(), static_cast(maxCapcity)); - ASSERT_LT(svec.SetCapcity(500), static_cast(0)); - ASSERT_EQ(svec.Capacity(), static_cast(1000)); + ASSERT_LT(svec.SetCapcity(maxCapcity / 2), static_cast(0)); + ASSERT_EQ(svec.Capacity(), static_cast(maxCapcity)); } HWTEST_F(UtilsSortedVector, testconstArray, TestSize.Level0) { SortedVector svec; - - for (int i = 9; i >= 0; i--) { + //默认添加次数 + int defaultPushTimes = 10; + for (int i = defaultPushTimes - 1; i >= 0; i--) { svec.Add(i); } @@ -320,8 +317,9 @@ HWTEST_F(UtilsSortedVector, testconstArray, TestSize.Level0) HWTEST_F(UtilsSortedVector, testeditArray, TestSize.Level0) { SortedVector svec; - - for (int i = 9; i >= 0; i--) { + //默认添加次数 + int defaultPushTimes = 10; + for (int i = defaultPushTimes - 1; i >= 0; i--) { svec.Add(i); } @@ -366,8 +364,9 @@ HWTEST_F(UtilsSortedVector, testeditArray, TestSize.Level0) HWTEST_F(UtilsSortedVector, testIndexOf, TestSize.Level0) { SortedVector svec; - - for (int i = 9; i >= 0; i--) { + //默认添加次数 + int defaultPushTimes = 10; + for (int i = defaultPushTimes - 1; i >= 0; i--) { svec.Add(i); } @@ -379,14 +378,15 @@ HWTEST_F(UtilsSortedVector, testIndexOf, TestSize.Level0) ASSERT_EQ(i, svec.IndexOf(i)); } - ASSERT_EQ(-1, svec.IndexOf(10)); + ASSERT_EQ(-1, svec.IndexOf(defaultPushTimes)); } HWTEST_F(UtilsSortedVector, testOrderof, TestSize.Level0) { SortedVector svec; - - for (int i = 9; i >= 0; i--) { + //默认添加次数 + int defaultPushTimes = 10; + for (int i = defaultPushTimes - 1; i >= 0; i--) { svec.Add(i); } @@ -399,19 +399,20 @@ HWTEST_F(UtilsSortedVector, testOrderof, TestSize.Level0) ASSERT_EQ(static_cast(0), svec.OrderOf(-2)); ASSERT_EQ(static_cast(0), svec.OrderOf(-1)); - ASSERT_EQ(static_cast(10), svec.OrderOf(9)); - ASSERT_EQ(static_cast(10), svec.OrderOf(10)); + ASSERT_EQ(static_cast(defaultPushTimes), svec.OrderOf(defaultPushTimes - 1)); + ASSERT_EQ(static_cast(defaultPushTimes), svec.OrderOf(defaultPushTimes)); } HWTEST_F(UtilsSortedVector, testoperatorAccess, TestSize.Level0) { SortedVector svec; - - for (int i = 9; i >= 0; i--) { + //默认添加次数 + int defaultPushTimes = 10; + for (int i = defaultPushTimes - 1; i >= 0; i--) { svec.Add(i); } - for (int i = 0; i <= 9; i++) { + for (int i = 0; i <= defaultPushTimes - 1; i++) { ASSERT_EQ(i, svec[i]); } } @@ -419,27 +420,29 @@ HWTEST_F(UtilsSortedVector, testoperatorAccess, TestSize.Level0) HWTEST_F(UtilsSortedVector, testBack, TestSize.Level0) { SortedVector svec; - - for (int i = 9; i >= 0; i--) { + //默认添加次数 + int defaultPushTimes = 10; + for (int i = defaultPushTimes - 1; i >= 0; i--) { svec.Add(i); } - ASSERT_EQ(9, svec.Back()); + ASSERT_EQ(defaultPushTimes - 1, svec.Back()); } HWTEST_F(UtilsSortedVector, testMirrorItemAt, TestSize.Level0) { SortedVector svec; - - for (int i = 9; i >= 0; i--) { + //默认添加次数 + int defaultPushTimes = 10; + for (int i = defaultPushTimes - 1; i >= 0; i--) { svec.Add(i); } - for (ssize_t i = -1; i >= -10; i--) { - ASSERT_EQ(i + 10, svec.MirrorItemAt(i)); + for (ssize_t i = -1; i >= -defaultPushTimes; i--) { + ASSERT_EQ(i + defaultPushTimes, svec.MirrorItemAt(i)); } - for (ssize_t i = 0; i <= 9; i++) { + for (ssize_t i = 0; i <= defaultPushTimes - 1; i++) { ASSERT_EQ(i, svec.MirrorItemAt(i)); } } @@ -447,12 +450,13 @@ HWTEST_F(UtilsSortedVector, testMirrorItemAt, TestSize.Level0) HWTEST_F(UtilsSortedVector, testEditItemAt, TestSize.Level0) { SortedVector svec; - - for (int i = 9; i >= 0; i--) { + //默认添加次数 + int defaultPushTimes = 10; + for (int i = defaultPushTimes - 1; i >= 0; i--) { svec.Add(i); } - for (ssize_t i = 0; i <= 9; i++) { + for (ssize_t i = 0; i <= defaultPushTimes - 1; i++) { svec.EditItemAt(i) += 1; ASSERT_EQ(i + 1, svec.EditItemAt(i)); } @@ -461,14 +465,15 @@ HWTEST_F(UtilsSortedVector, testEditItemAt, TestSize.Level0) HWTEST_F(UtilsSortedVector, testCopyCtorFromVector, TestSize.Level0) { std::vector vec; - - for (int i = 9; i >= 0; i--) { + //默认添加次数 + int defaultPushTimes = 10; + for (int i = defaultPushTimes - 1; i >= 0; i--) { vec.push_back(i); } SortedVector svec(vec); - for (ssize_t i = 0; i <= 9; i++) { + for (ssize_t i = 0; i <= defaultPushTimes - 1; i++) { ASSERT_EQ(i, svec[i]); } } @@ -476,21 +481,26 @@ HWTEST_F(UtilsSortedVector, testCopyCtorFromVector, TestSize.Level0) HWTEST_F(UtilsSortedVector, testConsFromVectorToNotAllowDuplicate, TestSize.Level0) { std::vector vec; - - for (int i = 9; i >= 0; i--) { + //第一次添加次数10 + int firstPushTimes = 10; + //第二次添加次数20 + int secondPushTimes = 20; + //期望的SortedVector的大小 + int exceptSortedVectorSize = 30; + for (int i = firstPushTimes - 1; i >= 0; i--) { vec.push_back(i); } - for (int i = 19; i >= 0; i--) { + for (int i = secondPushTimes - 1; i >= 0; i--) { vec.push_back(i); } - ASSERT_EQ(static_cast(30), vec.size()); + ASSERT_EQ(static_cast(exceptSortedVectorSize), vec.size()); SortedVector svec(vec); - ASSERT_EQ(static_cast(20), svec.Size()); + ASSERT_EQ(static_cast(secondPushTimes), svec.Size()); - for (ssize_t i = 0; i <= 19; i++) { + for (ssize_t i = 0; i <= secondPushTimes - 1; i++) { ASSERT_EQ(i, svec[i]); } } @@ -498,19 +508,21 @@ HWTEST_F(UtilsSortedVector, testConsFromVectorToNotAllowDuplicate, TestSize.Leve HWTEST_F(UtilsSortedVector, testMergevector, TestSize.Level0) { SortedVector svec; + //默认添加次数 + int defaultPushTimes = 10; - for (int i = 9; i >= 0; i--) { + for (int i = defaultPushTimes - 1; i >= 0; i--) { svec.Add(i); } std::vector vec; - for (int i = 10; i < 20; i++) { + for (int i = defaultPushTimes; i < defaultPushTimes * 2; i++) { vec.push_back(i); } svec.Merge(vec); - for (ssize_t i = 0; i < 20; i++) { + for (ssize_t i = 0; i < defaultPushTimes * 2; i++) { ASSERT_EQ(i, svec[i]); } } @@ -519,24 +531,30 @@ HWTEST_F(UtilsSortedVector, testMergevectorNoduplicate, TestSize.Level0) { SortedVector svec; - - for (int i = 9; i >= 0; i--) { + //第一次添加次数10 + int firstPushTimes = 10; + //第二次添加次数 + int secondPushTimes = 20; + //期望的SortedVector的大小 + int exceptSortedVectorSize = 30; + + for (int i = firstPushTimes - 1; i >= 0; i--) { svec.Add(i); } std::vector vec; - for (int i = 0; i < 20; i++) { + for (int i = 0; i < secondPushTimes; i++) { vec.push_back(i); } - for (int i = 10; i < 30; i++) { + for (int i = firstPushTimes; i < exceptSortedVectorSize; i++) { vec.push_back(i); } svec.Merge(vec); - ASSERT_EQ(svec.Size(), static_cast(30)); + ASSERT_EQ(svec.Size(), static_cast(exceptSortedVectorSize)); - for (ssize_t i = 0; i < 30; i++) { + for (ssize_t i = 0; i < exceptSortedVectorSize; i++) { ASSERT_EQ(i, svec[i]); } } @@ -545,15 +563,17 @@ HWTEST_F(UtilsSortedVector, testMergesortedvectorNoduplicate, TestSize.Level0) { SortedVector svec; SortedVector svec2; - for (int i = 9; i >= 0; i--) { + //默认添加次数 + int defaultPushTimes = 10; + for (int i = defaultPushTimes - 1; i >= 0; i--) { svec.Add(i); svec2.Add(i); } svec.Merge(svec2); - ASSERT_EQ(static_cast(10), svec.Size()); + ASSERT_EQ(static_cast(defaultPushTimes), svec.Size()); // 0,1,2,3,4,5,6,7,8,9 - for (ssize_t i = 0; i < 10; i++) { + for (ssize_t i = 0; i < defaultPushTimes; i++) { ASSERT_EQ(i, svec[i]); } } @@ -562,7 +582,9 @@ HWTEST_F(UtilsSortedVector, testMergesortedvector, TestSize.Level0) { SortedVector svec; SortedVector svec2; - for (int i = 9; i >= 0; i--) { + //默认添加次数 + int defaultPushTimes = 10; + for (int i = defaultPushTimes - 1; i >= 0; i--) { svec.Add(i); svec2.Add(i); } @@ -570,7 +592,7 @@ HWTEST_F(UtilsSortedVector, testMergesortedvector, TestSize.Level0) svec.Merge(svec2); // 0,0,1,1,2,2,3,3... - for (ssize_t i = 0; i < 20; i++) { + for (ssize_t i = 0; i < defaultPushTimes * 2; i++) { ASSERT_EQ(i / 2, svec[i]); } } @@ -578,20 +600,21 @@ HWTEST_F(UtilsSortedVector, testMergesortedvector, TestSize.Level0) HWTEST_F(UtilsSortedVector, testAddNotAllowDuplicate, TestSize.Level0) { SortedVector svec; - - for (int i = 9; i >= 0; i--) { + //默认添加次数 + int defaultPushTimes = 10; + for (int i = defaultPushTimes - 1; i >= 0; i--) { ASSERT_NE(svec.Add(i), static_cast(-1)); } - ASSERT_EQ(static_cast(10), svec.Size()); + ASSERT_EQ(static_cast(defaultPushTimes), svec.Size()); // duplicate - for (int i = 9; i >= 0; i--) { + for (int i = defaultPushTimes - 1; i >= 0; i--) { ASSERT_EQ(svec.Add(i), static_cast(-1)); - ASSERT_EQ(static_cast(10), svec.Size()); + ASSERT_EQ(static_cast(defaultPushTimes), svec.Size()); } - for (ssize_t i = 0; i < 10; i++) { + for (ssize_t i = 0; i < defaultPushTimes; i++) { ASSERT_EQ(i, svec[i]); } } diff --git a/base/test/unittest/common/utils_thread_test.cpp b/base/test/unittest/common/utils_thread_test.cpp index 7da8ede8bd66eedb0faa66d2256bdaaf14738172..6ab485e2a90dc5bc0cfb4c4f906f0fd0cb55c60b 100644 --- a/base/test/unittest/common/utils_thread_test.cpp +++ b/base/test/unittest/common/utils_thread_test.cpp @@ -115,11 +115,25 @@ bool TestThread::Run() prctl(PR_GET_NAME, threadName, 0, 0); name_ = threadName; - if (runFunc_ != nullptr) { - return (*runFunc_)(data_); - } + return (runFunc_ != nullptr) ? (*runFunc_)(data_) : false; +} - return false; +void TestThreadRun(std::unique_ptr &test, int times) +{ + pthread_t thread = test->GetThread(); + + // pthread_equal return non-zero if equal + EXPECT_EQ(pthread_equal(thread, -1) != 0, (test->IsRunning() ? false : true)); + + // ReadyToWork return false, RUN will not be called! + EXPECT_EQ(test->priority_, DEFAULT_PRIO); + EXPECT_EQ(test->name_, DEFAULT_THREAD_NAME); + + EXPECT_EQ(test->data_, 0); + EXPECT_EQ(times, 0); + test->NotifyExitSync(); + sleep(1); + EXPECT_EQ(pthread_equal(test->GetThread(), -1) != 0, (test->IsRunning() ? false : true)); } /* @@ -189,20 +203,7 @@ HWTEST_F(UtilsThreadTest, testThread003, TestSize.Level0) ThreadStatus status = test->Start("test_thread_03", THREAD_PROI_LOW, 1024); EXPECT_EQ(status == ThreadStatus::OK, true); - pthread_t thread = test->GetThread(); - - // pthread_equal return non-zero if equal - EXPECT_EQ(pthread_equal(thread , -1) != 0, (test->IsRunning() ? false : true)); - - // ReadyToWork return false, RUN will not be called! - EXPECT_EQ(test->priority_, DEFAULT_PRIO); - EXPECT_EQ(test->name_, DEFAULT_THREAD_NAME); - - EXPECT_EQ(test->data_, 0); - EXPECT_EQ(times, 0); - test->NotifyExitSync(); - sleep(1); - EXPECT_EQ(pthread_equal(test->GetThread(), -1) != 0, (test->IsRunning() ? false : true)); + TestThreadRun(test, times); } /* @@ -244,20 +245,7 @@ HWTEST_F(UtilsThreadTest, testThread005, TestSize.Level0) ThreadStatus status = test->Start("test_thread_05", THREAD_PROI_LOW, 1024); EXPECT_EQ(status == ThreadStatus::OK, true); - pthread_t thread = test->GetThread(); - - // pthread_equal return non-zero if equal - EXPECT_EQ(pthread_equal(thread , -1) != 0, (test->IsRunning() ? false : true)); - - // ReadyToWork return false, RUN will not be called! - EXPECT_EQ(test->priority_, DEFAULT_PRIO); - EXPECT_EQ(test->name_, DEFAULT_THREAD_NAME); - - EXPECT_EQ(test->data_, 0); - EXPECT_EQ(times, 0); - test->NotifyExitSync(); - sleep(1); - EXPECT_EQ(pthread_equal(test->GetThread(), -1) != 0, (test->IsRunning() ? false : true)); + TestThreadRun(test, times); } /* @@ -365,11 +353,7 @@ bool TestThread2::Run() prctl(PR_GET_NAME, threadName, 0, 0); name_ = threadName; - if (runFunc_ != nullptr) { - return (*runFunc_)(data_); - } - - return false; + return (runFunc_ != nullptr) ? (*runFunc_)(data_) : false; } /*