From 4042911531dae81aaf6d99dab5929102b2599147 Mon Sep 17 00:00:00 2001 From: wujianlin Date: Sat, 21 Sep 2024 19:11:16 +0800 Subject: [PATCH] synchronizing code Issue:https://gitee.com/openharmony/commonlibrary_c_utils/issues/IASOEC?from=project-issue Signed-off-by: wujianlin --- .../event_benchmark_test.cpp | 78 ++-- .../mapped_benchmark_test.cpp | 70 ++-- .../unique_fd_benchmark_test.cpp | 36 +- .../refbase_fuzzer/refbase_fuzzer.cpp | 81 ++-- .../unittest/common/utils_parcel_test.cpp | 386 ++++++++---------- .../common/utils_safe_block_queue_test.cpp | 204 +++++---- .../utils_safe_block_queue_tracking.cpp | 129 +++--- .../unittest/common/utils_safe_map_test.cpp | 53 ++- 8 files changed, 481 insertions(+), 556 deletions(-) diff --git a/base/test/benchmarktest/event_benchmark_test/event_benchmark_test.cpp b/base/test/benchmarktest/event_benchmark_test/event_benchmark_test.cpp index 33bfb12..fb47554 100644 --- a/base/test/benchmarktest/event_benchmark_test/event_benchmark_test.cpp +++ b/base/test/benchmarktest/event_benchmark_test/event_benchmark_test.cpp @@ -205,49 +205,6 @@ BENCHMARK_F(BenchmarkEventTest, testIOEventHandler002)(benchmark::State& state) BENCHMARK_LOGD("EventTest testIOEventHandler002 end."); } -static void AddHandler(std::shared_ptr& reactor, std::shared_ptr& handler1, - std::shared_ptr& handler2, std::shared_ptr& handler3, - benchmark::State& state) -{ - AssertEqual(reactor->AddHandler(handler1.get()), EVENT_SYS_ERR_OK, - "reactor->AddHandler(handler1.get()) did not equal EVENT_SYS_ERR_OK as expected.", state); - AssertEqual(reactor->AddHandler(handler2.get()), EVENT_SYS_ERR_OK, - "reactor->AddHandler(handler2.get()) did not equal EVENT_SYS_ERR_OK as expected.", state); - AssertUnequal(reactor->AddHandler(handler3.get()), EVENT_SYS_ERR_OK, - "reactor->AddHandler(handler3.get()) was not different from EVENT_SYS_ERR_OK as expected.", state); - AssertUnequal(reactor->AddHandler(nullptr), EVENT_SYS_ERR_OK, - "reactor->AddHandler(nullptr) was not different from EVENT_SYS_ERR_OK as expected.", state); -} - -static void RemoveHandler(std::shared_ptr& reactor, std::shared_ptr& handler2, - std::shared_ptr& handler3, std::shared_ptr& handler4, - benchmark::State& state) -{ - AssertUnequal(reactor->RemoveHandler(nullptr), EVENT_SYS_ERR_OK, - "reactor->RemoveHandler(nullptr) was not different from EVENT_SYS_ERR_OK as expected.", state); - AssertUnequal(reactor->RemoveHandler(handler3.get()), EVENT_SYS_ERR_OK, - "reactor->RemoveHandler(handler3.get()) was not different from EVENT_SYS_ERR_OK as expected.", state); - AssertUnequal(reactor->RemoveHandler(handler4.get()), EVENT_SYS_ERR_OK, - "reactor->RemoveHandler(handler4.get()) was not different from EVENT_SYS_ERR_OK as expected.", state); - AssertEqual(reactor->RemoveHandler(handler2.get()), EVENT_SYS_ERR_OK, - "reactor->RemoveHandler(handler2.get()) did not equal EVENT_SYS_ERR_OK as expected.", state); -} - -static void UpdateHandler(std::shared_ptr& reactor, std::shared_ptr& handler1, - std::shared_ptr& handler3, std::shared_ptr& handler4, - benchmark::State& state) -{ - // Update handler - AssertUnequal(reactor->UpdateHandler(nullptr), EVENT_SYS_ERR_OK, - "reactor->UpdateHandler(nullptr) was not different from EVENT_SYS_ERR_OK as expected.", state); - AssertUnequal(reactor->UpdateHandler(handler3.get()), EVENT_SYS_ERR_OK, - "reactor->UpdateHandler(handler3.get()) was not different from EVENT_SYS_ERR_OK as expected.", state); - AssertEqual(reactor->UpdateHandler(handler1.get()), EVENT_SYS_ERR_OK, - "reactor->UpdateHandler(handler1.get()) did not equal EVENT_SYS_ERR_OK as expected.", state); - AssertEqual(reactor->UpdateHandler(handler4.get()), EVENT_SYS_ERR_OK, - "reactor->UpdateHandler(handler4.get()) did not equal EVENT_SYS_ERR_OK as expected.", state); -} - /* * @tc.name: testIOEventReactor001 * @tc.desc: test basic interfaces of IOEventReactor. @@ -273,7 +230,14 @@ BENCHMARK_F(BenchmarkEventTest, testIOEventReactor001)(benchmark::State& state) "reactor->SetUp() did not equal EVENT_SYS_ERR_OK as expected.", state); // 3. Add handler - AddHandler(reactor, handler1, handler2, handler3, state); + AssertEqual(reactor->AddHandler(handler1.get()), EVENT_SYS_ERR_OK, + "reactor->AddHandler(handler1.get()) did not equal EVENT_SYS_ERR_OK as expected.", state); + AssertEqual(reactor->AddHandler(handler2.get()), EVENT_SYS_ERR_OK, + "reactor->AddHandler(handler2.get()) did not equal EVENT_SYS_ERR_OK as expected.", state); + AssertUnequal(reactor->AddHandler(handler3.get()), EVENT_SYS_ERR_OK, + "reactor->AddHandler(handler3.get()) was not different from EVENT_SYS_ERR_OK as expected.", state); + AssertUnequal(reactor->AddHandler(nullptr), EVENT_SYS_ERR_OK, + "reactor->AddHandler(nullptr) was not different from EVENT_SYS_ERR_OK as expected.", state); // 4. Add handler from the handler side. AssertUnequal(handler1->Start(reactor.get()), true, @@ -282,16 +246,30 @@ BENCHMARK_F(BenchmarkEventTest, testIOEventReactor001)(benchmark::State& state) "handler3->Start(reactor.get()) was not different from false as expected.", state); // 5. Remove handler - RemoveHandler(reactor, handler2, handler3, handler4, state); + AssertUnequal(reactor->RemoveHandler(nullptr), EVENT_SYS_ERR_OK, + "reactor->RemoveHandler(nullptr) was not different from EVENT_SYS_ERR_OK as expected.", state); + AssertUnequal(reactor->RemoveHandler(handler3.get()), EVENT_SYS_ERR_OK, + "reactor->RemoveHandler(handler3.get()) was not different from EVENT_SYS_ERR_OK as expected.", state); + AssertUnequal(reactor->RemoveHandler(handler4.get()), EVENT_SYS_ERR_OK, + "reactor->RemoveHandler(handler4.get()) was not different from EVENT_SYS_ERR_OK as expected.", state); + AssertEqual(reactor->RemoveHandler(handler2.get()), EVENT_SYS_ERR_OK, + "reactor->RemoveHandler(handler2.get()) did not equal EVENT_SYS_ERR_OK as expected.", state); // 6. Remove handler from the handler side. AssertUnequal(handler2->Stop(reactor.get()), true, "handler2->Stop(reactor.get()) was not different from false as expected.", state); // 7. Update handler - UpdateHandler(reactor, handler1, handler3, handler4, state); - - // Update handler from the handler side. + AssertUnequal(reactor->UpdateHandler(nullptr), EVENT_SYS_ERR_OK, + "reactor->UpdateHandler(nullptr) was not different from EVENT_SYS_ERR_OK as expected.", state); + AssertUnequal(reactor->UpdateHandler(handler3.get()), EVENT_SYS_ERR_OK, + "reactor->UpdateHandler(handler3.get()) was not different from EVENT_SYS_ERR_OK as expected.", state); + AssertEqual(reactor->UpdateHandler(handler1.get()), EVENT_SYS_ERR_OK, + "reactor->UpdateHandler(handler1.get()) did not equal EVENT_SYS_ERR_OK as expected.", state); + AssertEqual(reactor->UpdateHandler(handler4.get()), EVENT_SYS_ERR_OK, + "reactor->UpdateHandler(handler4.get()) did not equal EVENT_SYS_ERR_OK as expected.", state); + + // 8. Update handler from the handler side. AssertEqual(handler2->Update(reactor.get()), true, "handler2->Update(reactor.get()) was not different from false as expected.", state); AssertUnequal(handler3->Update(reactor.get()), true, @@ -378,7 +356,8 @@ bool TimerFdHandler::Initialize(uint32_t interval) newValue.it_interval.tv_nsec = (interval % MILLI_TO_BASE) * MILLI_TO_NANO; if (timerfd_settime(GetFd(), TFD_TIMER_ABSTIME, &newValue, nullptr) == INVALID_FD) { - BENCHMARK_LOGD("Set timerFd failed-%{public}s timer_fd:%{public}d", strerror(errno), GetFd()); + BENCHMARK_LOGD("Set timerFd failed-%{public}s timer_fd:%{public}d, next_time:%{public}lld, "\ + "interval:%{public}lld", strerror(errno), GetFd(), newValue.it_value.tv_sec, newValue.it_interval.tv_sec); return false; } @@ -1153,6 +1132,7 @@ BENCHMARK_F(BenchmarkEventTest, testNewTimer003)(benchmark::State& state) class A { public: + // Set Timer with default timeoutMs 100ms. explicit A(int data) : data_(data), timer_("ATimer", 100) {} ~A() = default; bool Init(); diff --git a/base/test/benchmarktest/mapped_benchmark_test/mapped_benchmark_test.cpp b/base/test/benchmarktest/mapped_benchmark_test/mapped_benchmark_test.cpp index b5abd45..e918cf7 100644 --- a/base/test/benchmarktest/mapped_benchmark_test/mapped_benchmark_test.cpp +++ b/base/test/benchmarktest/mapped_benchmark_test/mapped_benchmark_test.cpp @@ -52,6 +52,8 @@ public: } else { BENCHMARK_LOGD("Create test dir Failed: %{public}s", dir.c_str()); } + + BENCHMARK_LOGD("Page size: %{public}lld", MappedFile::PageSize()); } void TearDown(const ::benchmark::State& state) override @@ -72,11 +74,17 @@ void PrintStatus(MappedFile& mf) BENCHMARK_LOGD("Mapped Region Start: %{public}p\n" "Mapped Region End: %{public}p\n" "View start: %{public}p\n" - "View End: %{public}p\n", + "View End: %{public}p\n" + "View Size: %{public}lld\n" + "File Offset Start: %{public}lld\n" + "File Offset End: %{public}lld", reinterpret_cast(mf.RegionStart()), reinterpret_cast(mf.RegionEnd()), reinterpret_cast(mf.Begin()), - reinterpret_cast(mf.End())); + reinterpret_cast(mf.End()), + mf.Size(), + mf.StartOffset(), + mf.EndOffset()); } bool CreateTestFile(const std::string& path, const std::string& content) @@ -446,24 +454,6 @@ BENCHMARK_F(BenchmarkMappedFileTest, testReMap001)(benchmark::State& state) BENCHMARK_LOGD("MappedFileTest testReMap001 end."); } -static void ChangeParamsWhenUnmapped(MappedFile& mf, const int sizeIncrement, std::string& filename, - std::string& filename1, benchmark::State& state) -{ - AssertTrue((mf.ChangeSize(mf.Size() + sizeIncrement)), - "mf.ChangeSize(mf.Size() + sizeIncrement) did not equal true as expected.", state); - AssertTrue((mf.ChangeSize(MappedFile::DEFAULT_LENGTH)), - "mf.ChangeSize(MappedFile::DEFAULT_LENGTH) did not equal true as expected.", state); - AssertTrue((mf.ChangeOffset(mf.PageSize())), - "mf.ChangeOffset(mf.PageSize()) did not equal true as expected.", state); - AssertTrue((mf.ChangeOffset(0)), "mf.ChangeOffset(0) did not equal true as expected.", state); - AssertTrue((mf.ChangePath(filename1)), "mf.ChangePath(filename1) did not equal true as expected.", state); - AssertTrue((mf.ChangePath(filename)), "mf.ChangePath(filename) did not equal true as expected.", state); - AssertTrue((mf.ChangeHint(reinterpret_cast(0x89000))), - "mf.ChangeHint(reinterpret_cast(0x89000)) did not equal true as expected.", state); - AssertTrue((mf.ChangeMode(MapMode::READ_ONLY)), - "mf.ChangeMode(MapMode::READ_ONLY) did not equal true as expected.", state); -} - /* * @tc.name: testReMap002 * @tc.desc: Test remapping via changing params. @@ -485,7 +475,19 @@ BENCHMARK_F(BenchmarkMappedFileTest, testReMap002)(benchmark::State& state) MappedFile mf(filename); // Change params when unmapped. - ChangeParamsWhenUnmapped(mf, sizeIncrement, filename, filename1, state); + AssertTrue((mf.ChangeSize(mf.Size() + sizeIncrement)), + "mf.ChangeSize(mf.Size() + sizeIncrement) did not equal true as expected.", state); + AssertTrue((mf.ChangeSize(MappedFile::DEFAULT_LENGTH)), + "mf.ChangeSize(MappedFile::DEFAULT_LENGTH) did not equal true as expected.", state); + AssertTrue((mf.ChangeOffset(mf.PageSize())), + "mf.ChangeOffset(mf.PageSize()) did not equal true as expected.", state); + AssertTrue((mf.ChangeOffset(0)), "mf.ChangeOffset(0) did not equal true as expected.", state); + AssertTrue((mf.ChangePath(filename1)), "mf.ChangePath(filename1) did not equal true as expected.", state); + AssertTrue((mf.ChangePath(filename)), "mf.ChangePath(filename) did not equal true as expected.", state); + AssertTrue((mf.ChangeHint(reinterpret_cast(0x89000))), + "mf.ChangeHint(reinterpret_cast(0x89000)) did not equal true as expected.", state); + AssertTrue((mf.ChangeMode(MapMode::READ_ONLY)), + "mf.ChangeMode(MapMode::READ_ONLY) did not equal true as expected.", state); // 2. map file AssertEqual(mf.Map(), MAPPED_FILE_ERR_OK, "mf.Map() did not equal MAPPED_FILE_ERR_OK as expected.", state); @@ -790,21 +792,6 @@ void KeepAPageAndReachBottom(off_t& endOff, const off_t orig, MappedFile& mf, be PrintStatus(mf); } -static void Remapping(MappedFile& mf, off_t& endOff, off_t& curSize, benchmark::State& state) -{ - // 7. this turn will trigger a remapping - endOff = mf.EndOffset(); - AssertEqual(mf.TurnNext(), MAPPED_FILE_ERR_OK, - "mf.TurnNext() did not equal MAPPED_FILE_ERR_OK as expected.", state); - AssertTrue((mf.IsMapped()), "mf.IsMapped() did not equal true as expected.", state); - AssertEqual(mf.StartOffset(), endOff + 1, "mf.StartOffset() did not equal endOff + 1 as expected.", state); - AssertEqual(mf.Size(), curSize, "mf.Size() did not equal curSize as expected.", state); - AssertEqual(mf.RegionStart(), mf.Begin(), "mf.RegionStart() did not equal mf.Begin() as expected.", state); - AssertEqual(static_cast(mf.RegionEnd() - mf.RegionStart()) + 1LL, mf.PageSize(), - "static_cast(mf.RegionEnd() - mf.RegionStart()) + 1LL did not equal mf.PageSize().", state); - PrintStatus(mf); -} - /* * @tc.name: testTurnNext001 * @tc.desc: Test TurnNext() when `IsMapped()`. @@ -853,8 +840,17 @@ BENCHMARK_F(BenchmarkMappedFileTest, testTurnNext001)(benchmark::State& state) KeepAPageAndReachBottom(endOff, orig, mf, state); // 7. this turn will trigger a remapping + endOff = mf.EndOffset(); off_t curSize = mf.Size(); - Remapping(mf, endOff, curSize, state); + AssertEqual(mf.TurnNext(), MAPPED_FILE_ERR_OK, + "mf.TurnNext() did not equal MAPPED_FILE_ERR_OK as expected.", state); + AssertTrue((mf.IsMapped()), "mf.IsMapped() did not equal true as expected.", state); + AssertEqual(mf.StartOffset(), endOff + 1, "mf.StartOffset() did not equal endOff + 1 as expected.", state); + AssertEqual(mf.Size(), curSize, "mf.Size() did not equal curSize as expected.", state); + AssertEqual(mf.RegionStart(), mf.Begin(), "mf.RegionStart() did not equal mf.Begin() as expected.", state); + AssertEqual(static_cast(mf.RegionEnd() - mf.RegionStart()) + 1LL, mf.PageSize(), + "static_cast(mf.RegionEnd() - mf.RegionStart()) + 1LL did not equal mf.PageSize().", state); + PrintStatus(mf); // 8. keep turnNext within a page for (off_t cnt = 1; cnt < (MappedFile::PageSize() / 100LL / curSize); cnt++) { diff --git a/base/test/benchmarktest/unique_fd_benchmark_test/unique_fd_benchmark_test.cpp b/base/test/benchmarktest/unique_fd_benchmark_test/unique_fd_benchmark_test.cpp index 4fddad3..dc6e9ee 100644 --- a/base/test/benchmarktest/unique_fd_benchmark_test/unique_fd_benchmark_test.cpp +++ b/base/test/benchmarktest/unique_fd_benchmark_test/unique_fd_benchmark_test.cpp @@ -27,7 +27,7 @@ using namespace std; namespace OHOS { namespace { -static const char *TEST_FILE_NAME = "testfilename.test"; +static const char *g_testfilename = "testfilename.test"; class BenchmarkUniqueFd : public benchmark::Fixture { public: @@ -42,7 +42,7 @@ public: void SetUp(const ::benchmark::State& state) override { ofstream outfile; - outfile.open(TEST_FILE_NAME, ios::out | ios::trunc); + outfile.open(g_testfilename, ios::out | ios::trunc); outfile << "testdata\n" << std::endl; outfile.close(); @@ -51,11 +51,11 @@ public: void TearDown(::benchmark::State& state) override { ifstream inputfile; - inputfile.open(TEST_FILE_NAME, ios::in); + inputfile.open(g_testfilename, ios::in); std::string testStr; inputfile >> testStr; inputfile.close(); - AssertEqual(remove(TEST_FILE_NAME), 0, "remove(TEST_FILE_NAME) did not equal 0 as expected.", state); + AssertEqual(remove(g_testfilename), 0, "remove(testfilename) did not equal 0 as expected.", state); } protected: @@ -85,7 +85,7 @@ BENCHMARK_F(BenchmarkUniqueFd, testUtilsUniqueCtroFromInt)(benchmark::State& sta BENCHMARK_LOGD("UniqueFd testUtilsUniqueCtroFromInt start."); const int expectedValue = -1; while (state.KeepRunning()) { - UniqueFd ufd2(open(TEST_FILE_NAME, O_RDWR, FILE_PERMISSION_READ_WRITE)); + UniqueFd ufd2(open(g_testfilename, O_RDWR, FILE_PERMISSION_READ_WRITE)); AssertUnequal(ufd2, expectedValue, "ufd2 was not different from expectedValue as expected.", state); } BENCHMARK_LOGD("UniqueFd testUtilsUniqueCtroFromInt end."); @@ -95,7 +95,7 @@ BENCHMARK_F(BenchmarkUniqueFd, testUtilsUniqueFdeqcompare)(benchmark::State& sta { BENCHMARK_LOGD("UniqueFd testUtilsUniqueFdeqcompare start."); while (state.KeepRunning()) { - int fd = open(TEST_FILE_NAME, O_RDWR, FILE_PERMISSION_READ_WRITE); + int fd = open(g_testfilename, O_RDWR, FILE_PERMISSION_READ_WRITE); UniqueFd ufd2(fd); AssertEqual(fd, ufd2, "fd did not equal ufd2 as expected.", state); AssertEqual(ufd2, fd, "ufd2 did not equal fd as expected.", state); @@ -107,7 +107,7 @@ BENCHMARK_F(BenchmarkUniqueFd, testUtilsUniqueFdeqcompareNl)(benchmark::State& s { BENCHMARK_LOGD("UniqueFd testUtilsUniqueFdeqcompareNl start."); while (state.KeepRunning()) { - int fd = open(TEST_FILE_NAME, O_RDWR, FILE_PERMISSION_READ_WRITE); + int fd = open(g_testfilename, O_RDWR, FILE_PERMISSION_READ_WRITE); UniqueFd ufd2(fd); AssertGreaterThanOrEqual(ufd2, 0, "ufd2 >= 0 did not equal true as expected.", state); AssertLessThanOrEqual(0, ufd2, "0 <= ufd2 did not equal true as expected.", state); @@ -119,7 +119,7 @@ BENCHMARK_F(BenchmarkUniqueFd, testUtilsUniqueFdeqcompareBg)(benchmark::State& s { BENCHMARK_LOGD("UniqueFd testUtilsUniqueFdeqcompareBg start."); while (state.KeepRunning()) { - int fd = open(TEST_FILE_NAME, O_RDWR, FILE_PERMISSION_READ_WRITE); + int fd = open(g_testfilename, O_RDWR, FILE_PERMISSION_READ_WRITE); UniqueFd ufd2(fd); AssertGreaterThan(ufd2, MIN_VALID_FD, "ufd2 > MIN_VALID_FD did not equal true as expected.", state); AssertLessThan(MIN_VALID_FD, ufd2, "MIN_VALID_FD < ufd2 did not equal true as expected.", state); @@ -131,7 +131,7 @@ BENCHMARK_F(BenchmarkUniqueFd, testUtilsUniqueFdeqcompareNb)(benchmark::State& s { BENCHMARK_LOGD("UniqueFd testUtilsUniqueFdeqcompareNb start."); while (state.KeepRunning()) { - int fd = open(TEST_FILE_NAME, O_RDWR, FILE_PERMISSION_READ_WRITE); + int fd = open(g_testfilename, O_RDWR, FILE_PERMISSION_READ_WRITE); UniqueFd ufd2(fd); AssertLessThanOrEqual(ufd2, MAX_VALID_FD, "ufd2 <= MAX_VALID_FD did not equal true as expected.", state); AssertGreaterThanOrEqual(MAX_VALID_FD, ufd2, "MAX_VALID_FD >= ufd2 did not equal true as expected.", state); @@ -143,7 +143,7 @@ BENCHMARK_F(BenchmarkUniqueFd, testUtilsUniqueFdeqcompareLess)(benchmark::State& { BENCHMARK_LOGD("UniqueFd testUtilsUniqueFdeqcompareLess start."); while (state.KeepRunning()) { - int fd = open(TEST_FILE_NAME, O_RDWR, FILE_PERMISSION_READ_WRITE); + int fd = open(g_testfilename, O_RDWR, FILE_PERMISSION_READ_WRITE); UniqueFd ufd2(fd); AssertLessThan(ufd2, MAX_VALID_FD, "ufd2 < MAX_VALID_FD did not equal true as expected.", state); AssertGreaterThan(MAX_VALID_FD, ufd2, "MAX_VALID_FD > ufd2 did not equal true as expected.", state); @@ -155,7 +155,7 @@ BENCHMARK_F(BenchmarkUniqueFd, testUtilsUniqueFdeqcompareNeq)(benchmark::State& { BENCHMARK_LOGD("UniqueFd testUtilsUniqueFdeqcompareNeq start."); while (state.KeepRunning()) { - int fd = open(TEST_FILE_NAME, O_RDWR, FILE_PERMISSION_READ_WRITE); + int fd = open(g_testfilename, O_RDWR, FILE_PERMISSION_READ_WRITE); UniqueFd ufd2(fd); AssertUnequal(ufd2, MAX_VALID_FD, "ufd2 != MAX_VALID_FD did not equal true as expected.", state); AssertUnequal(MAX_VALID_FD, ufd2, "MAX_VALID_FD != ufd2 did not equal true as expected.", state); @@ -185,7 +185,7 @@ BENCHMARK_F(BenchmarkUniqueFd, testUtilsUniqueFdDefineDeletor)(benchmark::State& const int invalidFileDescriptor = -1; while (state.KeepRunning()) { NewDeleter::iflag = 0; - int fd = open(TEST_FILE_NAME, O_RDWR); + int fd = open(g_testfilename, O_RDWR); { UniqueFdAddDeletor ufd2(fd); @@ -205,7 +205,7 @@ BENCHMARK_F(BenchmarkUniqueFd, testUtilsUniqueFdDefineDeletorCloseStatus)(benchm BENCHMARK_LOGD("UniqueFd testUtilsUniqueFdDefineDeletorCloseStatus start."); const int errorReturn = -1; while (state.KeepRunning()) { - int fd = open(TEST_FILE_NAME, O_RDWR); + int fd = open(g_testfilename, O_RDWR); { UniqueFdAddDeletor ufd2(fd); @@ -223,7 +223,7 @@ BENCHMARK_F(BenchmarkUniqueFd, testUtilsUniqueFdRelease)(benchmark::State& state BENCHMARK_LOGD("UniqueFd testUtilsUniqueFdRelease start."); const int expectedValue = -1; while (state.KeepRunning()) { - UniqueFd ufd(open(TEST_FILE_NAME, O_RDWR, FILE_PERMISSION_READ_WRITE)); + UniqueFd ufd(open(g_testfilename, O_RDWR, FILE_PERMISSION_READ_WRITE)); AssertUnequal(ufd, expectedValue, "ufd was not different from expectedValue as expected.", state); int fd = ufd.Release(); @@ -238,7 +238,7 @@ BENCHMARK_F(BenchmarkUniqueFd, testUtilsUniqueFdOperatorInt)(benchmark::State& s BENCHMARK_LOGD("UniqueFd testUtilsUniqueFdOperatorInt start."); const int expectedValue = -1; while (state.KeepRunning()) { - UniqueFd ufd(open(TEST_FILE_NAME, O_RDWR, FILE_PERMISSION_READ_WRITE)); + UniqueFd ufd(open(g_testfilename, O_RDWR, FILE_PERMISSION_READ_WRITE)); AssertUnequal(ufd, expectedValue, "ufd was not different from expectedValue as expected.", state); int fd = ufd; @@ -252,7 +252,7 @@ BENCHMARK_F(BenchmarkUniqueFd, testUtilsUniqueFdGet)(benchmark::State& state) BENCHMARK_LOGD("UniqueFd testUtilsUniqueFdGet start."); const int expectedValue = -1; while (state.KeepRunning()) { - UniqueFd ufd(open(TEST_FILE_NAME, O_RDWR, FILE_PERMISSION_READ_WRITE)); + UniqueFd ufd(open(g_testfilename, O_RDWR, FILE_PERMISSION_READ_WRITE)); AssertUnequal(ufd, expectedValue, "ufd was not different from expectedValue as expected.", state); int fd = ufd.Get(); @@ -266,7 +266,7 @@ BENCHMARK_F(BenchmarkUniqueFd, testUtilsUniqueFdMoveConstructor)(benchmark::Stat BENCHMARK_LOGD("UniqueFd testUtilsUniqueFdMoveConstructor start."); const int expectedValue = -1; while (state.KeepRunning()) { - UniqueFd ufd(open(TEST_FILE_NAME, O_RDWR, FILE_PERMISSION_READ_WRITE)); + UniqueFd ufd(open(g_testfilename, O_RDWR, FILE_PERMISSION_READ_WRITE)); AssertUnequal(ufd, expectedValue, "ufd was not different from expectedValue as expected.", state); UniqueFd ufd1(std::move(ufd)); @@ -281,7 +281,7 @@ BENCHMARK_F(BenchmarkUniqueFd, testUtilsUniqueFdMoveAssignment)(benchmark::State BENCHMARK_LOGD("UniqueFd testUtilsUniqueFdMoveAssignment start."); const int expectedValue = -1; while (state.KeepRunning()) { - UniqueFd ufd(open(TEST_FILE_NAME, O_RDWR, FILE_PERMISSION_READ_WRITE)); + UniqueFd ufd(open(g_testfilename, O_RDWR, FILE_PERMISSION_READ_WRITE)); AssertUnequal(ufd, expectedValue, "ufd was not different from expectedValue as expected.", state); UniqueFd ufd1; diff --git a/base/test/fuzztest/refbase_fuzzer/refbase_fuzzer.cpp b/base/test/fuzztest/refbase_fuzzer/refbase_fuzzer.cpp index 8e8b00b..fb7f764 100644 --- a/base/test/fuzztest/refbase_fuzzer/refbase_fuzzer.cpp +++ b/base/test/fuzztest/refbase_fuzzer/refbase_fuzzer.cpp @@ -250,9 +250,39 @@ const std::vector> }, }; -// Clean up WeakRefCounter -void CleanUpWeakRefCounter(SingleThreadRefCounts& state, WeakRefCounter* newWeakRef) +void TestLoop(const std::vector& fuzzOps) { + SingleThreadRefCounts state; + uint8_t lockedOpSize = readOrIncOperations.size(); + uint8_t totalOperationTypes = lockedOpSize + decOperations.size(); + WeakRefCounter* newWeakRef = nullptr; + + for (auto op : fuzzOps) { + auto opVal = op % totalOperationTypes; + if (opVal >= lockedOpSize) { + decOperations[opVal % lockedOpSize](&state, newWeakRef); + } else { + bool shouldLock = state.strongCount == 0 && state.weakCount == 0 && state.weakRefCount == 0; + if (shouldLock) { + g_strongLock.LockRead(); + if (g_refDeleted) { + if (state.weakRefCounterExists) { + FUZZ_LOGI("thread = %{public}u, delete newWeakRef", GetThreadId()); + delete newWeakRef; + } + FUZZ_LOGI("thread = %{public}u return", GetThreadId()); + g_strongLock.UnLockRead(); + return; + } + } + readOrIncOperations[opVal](&state, newWeakRef); + if (shouldLock) { + g_strongLock.UnLockRead(); + } + } + } + + // Clean up WeakRefCounter if (state.weakRefCounterExists) { FUZZ_LOGI("thread = %{public}u, delete newWeakRef", GetThreadId()); delete newWeakRef; @@ -270,11 +300,8 @@ void CleanUpWeakRefCounter(SingleThreadRefCounts& state, WeakRefCounter* newWeak } } } -} -// Clean up any weak references -void CleanUpWeakCounter(SingleThreadRefCounts& state) -{ + // Clean up any weak references for (; state.weakCount > 0; state.weakCount--) { bool shouldLock = state.strongCount == 0 && state.weakCount == 1; if (shouldLock) { @@ -287,11 +314,8 @@ void CleanUpWeakCounter(SingleThreadRefCounts& state) g_strongLock.UnLockWrite(); } } -} -// Clean up any strong references -void CleanUpStrongCounter(SingleThreadRefCounts& state) -{ + // Clean up any strong references for (; state.strongCount > 0; state.strongCount--) { bool shouldLock = state.strongCount == 1; if (shouldLock) { @@ -306,43 +330,6 @@ void CleanUpStrongCounter(SingleThreadRefCounts& state) } } -void TestLoop(const std::vector& fuzzOps) -{ - SingleThreadRefCounts state; - uint8_t lockedOpSize = readOrIncOperations.size(); - uint8_t totalOperationTypes = lockedOpSize + decOperations.size(); - WeakRefCounter* newWeakRef = nullptr; - - for (auto op : fuzzOps) { - auto opVal = op % totalOperationTypes; - if (opVal >= lockedOpSize) { - decOperations[opVal % lockedOpSize](&state, newWeakRef); - } else { - bool shouldLock = state.strongCount == 0 && state.weakCount == 0 && state.weakRefCount == 0; - if (shouldLock) { - g_strongLock.LockRead(); - if (g_refDeleted) { - if (state.weakRefCounterExists) { - FUZZ_LOGI("thread = %{public}u, delete newWeakRef", GetThreadId()); - delete newWeakRef; - } - FUZZ_LOGI("thread = %{public}u return", GetThreadId()); - g_strongLock.UnLockRead(); - return; - } - } - readOrIncOperations[opVal](&state, newWeakRef); - if (shouldLock) { - g_strongLock.UnLockRead(); - } - } - } - - CleanUpWeakRefCounter(state, newWeakRef); - CleanUpWeakCounter(state); - CleanUpStrongCounter(state); -} - void RefbaseTestFunc(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider) { FUZZ_LOGI("RefbaseTestFunc start"); diff --git a/base/test/unittest/common/utils_parcel_test.cpp b/base/test/unittest/common/utils_parcel_test.cpp index 0961b58..adc8195 100644 --- a/base/test/unittest/common/utils_parcel_test.cpp +++ b/base/test/unittest/common/utils_parcel_test.cpp @@ -303,7 +303,12 @@ HWTEST_F(UtilsParcelTest, test_parcel_readvec_001, TestSize.Level0) EXPECT_EQ(result, false); } -static void ReadvecTestTwoFunc01() +/** + * @tc.name: test_parcel_readvec_002 + * @tc.desc: test parcel read vector failed with invlalid vector length -1. + * @tc.type: FUNC + */ +HWTEST_F(UtilsParcelTest, test_parcel_readvec_002, TestSize.Level0) { Parcel parcel1; parcel1.WriteInt32(-1); @@ -336,99 +341,87 @@ static void ReadvecTestTwoFunc01() val4.push_back(x4); result = parcel4.ReadInt32Vector(&val4); EXPECT_EQ(result, false); -} - -static void ReadvecTestTwoFunc02() -{ - Parcel parcel1; - parcel1.WriteInt32(-1); - std::vector val1; - int64_t x1 = 1; - val1.push_back(x1); - bool result = parcel1.ReadInt64Vector(&val1); + + Parcel parcel5; + parcel5.WriteInt32(-1); + std::vector val5; + int64_t x5 = 1; + val5.push_back(x5); + result = parcel5.ReadInt64Vector(&val5); EXPECT_EQ(result, false); - Parcel parcel2; - parcel2.WriteInt32(-1); - std::vector val2; - uint8_t x2 = 1; - val2.push_back(x2); - result = parcel2.ReadUInt8Vector(&val2); + Parcel parcel6; + parcel6.WriteInt32(-1); + std::vector val6; + uint8_t x6 = 1; + val6.push_back(x6); + result = parcel6.ReadUInt8Vector(&val6); EXPECT_EQ(result, false); - Parcel parcel3; - parcel3.WriteInt32(-1); - std::vector val3; - uint16_t x3 = 1; - val3.push_back(x3); - result = parcel3.ReadUInt16Vector(&val3); + Parcel parcel7; + parcel7.WriteInt32(-1); + std::vector val7; + uint16_t x7 = 1; + val7.push_back(x7); + result = parcel7.ReadUInt16Vector(&val7); EXPECT_EQ(result, false); - Parcel parcel4; - parcel4.WriteInt32(-1); - std::vector val4; - uint32_t x4 = 1; - val4.push_back(x4); - result = parcel4.ReadUInt32Vector(&val4); + Parcel parcel8; + parcel8.WriteInt32(-1); + std::vector val8; + uint32_t x8 = 1; + val8.push_back(x8); + result = parcel8.ReadUInt32Vector(&val8); EXPECT_EQ(result, false); -} -static void ReadvecTestTwoFunc03() -{ - Parcel parcel1; - parcel1.WriteInt32(-1); - std::vector val1; - uint64_t x1 = 1; - val1.push_back(x1); - bool result = parcel1.ReadUInt64Vector(&val1); + Parcel parcel9; + parcel9.WriteInt32(-1); + std::vector val9; + uint64_t x9 = 1; + val9.push_back(x9); + result = parcel9.ReadUInt64Vector(&val9); EXPECT_EQ(result, false); - Parcel parcel2; - parcel2.WriteInt32(-1); - std::vector val2; - float x2 = 1; - val2.push_back(x2); - result = parcel2.ReadFloatVector(&val2); + Parcel parcel10; + parcel10.WriteInt32(-1); + std::vector val10; + float x10 = 1; + val10.push_back(x10); + result = parcel10.ReadFloatVector(&val10); EXPECT_EQ(result, false); - Parcel parcel3; - parcel3.WriteInt32(-1); - std::vector val3; - double x3 = 1; - val3.push_back(x3); - result = parcel3.ReadDoubleVector(&val3); + Parcel parcel11; + parcel11.WriteInt32(-1); + std::vector val11; + double x11 = 1; + val11.push_back(x11); + result = parcel11.ReadDoubleVector(&val11); EXPECT_EQ(result, false); - Parcel parcel4; - parcel4.WriteInt32(-1); - std::vector val4; - std::string x4 = "test"; - val4.push_back(x4); - result = parcel4.ReadStringVector(&val4); + Parcel parcel12; + parcel12.WriteInt32(-1); + std::vector val12; + std::string x12 = "test"; + val12.push_back(x12); + result = parcel12.ReadStringVector(&val12); EXPECT_EQ(result, false); - Parcel parcel5; - parcel5.WriteInt32(-1); - std::vector val5; - std::u16string x5 = u"test"; - val5.push_back(x5); - result = parcel5.ReadString16Vector(&val5); + Parcel parcel13; + parcel13.WriteInt32(-1); + std::vector val13; + std::u16string x13 = u"test"; + val13.push_back(x13); + result = parcel13.ReadString16Vector(&val13); EXPECT_EQ(result, false); } /** - * @tc.name: test_parcel_readvec_002 - * @tc.desc: test parcel read vector failed with invlalid vector length -1. + * @tc.name: test_parcel_readvec_003 + * @tc.desc: test parcel read vector failed with invlalid vector length + * std::vector::max_size(). * @tc.type: FUNC */ -HWTEST_F(UtilsParcelTest, test_parcel_readvec_002, TestSize.Level0) -{ - ReadvecTestTwoFunc01(); - ReadvecTestTwoFunc02(); - ReadvecTestTwoFunc03(); -} - -static void ReadvecTestThreeFunc01() +HWTEST_F(UtilsParcelTest, test_parcel_readvec_003, TestSize.Level0) { Parcel parcel1; std::vector val1; @@ -461,101 +454,90 @@ static void ReadvecTestThreeFunc01() parcel4.WriteInt32(val4.max_size()); result = parcel4.ReadInt32Vector(&val4); EXPECT_EQ(result, false); -} -static void ReadvecTestThreeFunc02() -{ - Parcel parcel1; - std::vector val1; - int64_t x1 = 1; - val1.push_back(x1); - parcel1.WriteInt32(val1.max_size()); - bool result = parcel1.ReadInt64Vector(&val1); + Parcel parcel5; + std::vector val5; + int64_t x5 = 1; + val5.push_back(x5); + parcel5.WriteInt32(val5.max_size()); + result = parcel5.ReadInt64Vector(&val5); EXPECT_EQ(result, false); - Parcel parcel2; - std::vector val2; - uint8_t x2 = 1; - val2.push_back(x2); - parcel2.WriteInt32(val2.max_size()); - result = parcel2.ReadUInt8Vector(&val2); + Parcel parcel6; + std::vector val6; + uint8_t x6 = 1; + val6.push_back(x6); + parcel6.WriteInt32(val6.max_size()); + result = parcel6.ReadUInt8Vector(&val6); EXPECT_EQ(result, false); - Parcel parcel3; - std::vector val3; - uint16_t x3 = 1; - val3.push_back(x3); - parcel3.WriteInt32(val3.max_size()); - result = parcel3.ReadUInt16Vector(&val3); + Parcel parcel7; + std::vector val7; + uint16_t x7 = 1; + val7.push_back(x7); + parcel7.WriteInt32(val7.max_size()); + result = parcel7.ReadUInt16Vector(&val7); EXPECT_EQ(result, false); - Parcel parcel4; - std::vector val4; - uint32_t x4 = 1; - val4.push_back(x4); - parcel4.WriteInt32(val4.max_size()); - result = parcel4.ReadUInt32Vector(&val4); + Parcel parcel8; + std::vector val8; + uint32_t x8 = 1; + val8.push_back(x8); + parcel8.WriteInt32(val8.max_size()); + result = parcel8.ReadUInt32Vector(&val8); EXPECT_EQ(result, false); -} -static void ReadvecTestThreeFunc03() -{ - Parcel parcel1; - std::vector val1; - uint64_t x1 = 1; - val1.push_back(x1); - parcel1.WriteInt32(val1.max_size()); - bool result = parcel1.ReadUInt64Vector(&val1); + Parcel parcel9; + std::vector val9; + uint64_t x9 = 1; + val9.push_back(x9); + parcel9.WriteInt32(val9.max_size()); + result = parcel9.ReadUInt64Vector(&val9); EXPECT_EQ(result, false); - Parcel parcel2; - std::vector val2; - float x2 = 1; - val2.push_back(x2); - parcel2.WriteInt32(val2.max_size()); - result = parcel2.ReadFloatVector(&val2); + Parcel parcel10; + std::vector val10; + float x10 = 1; + val10.push_back(x10); + parcel10.WriteInt32(val10.max_size()); + result = parcel10.ReadFloatVector(&val10); EXPECT_EQ(result, false); - Parcel parcel3; - std::vector val3; - double x3 = 1; - val3.push_back(x3); - parcel3.WriteInt32(val3.max_size()); - result = parcel3.ReadDoubleVector(&val3); + Parcel parcel11; + std::vector val11; + double x11 = 1; + val11.push_back(x11); + parcel11.WriteInt32(val11.max_size()); + result = parcel11.ReadDoubleVector(&val11); EXPECT_EQ(result, false); - Parcel parcel4; - std::vector val4; - std::string x4 = "test"; - val4.push_back(x4); - parcel4.WriteInt32(val4.max_size()); - result = parcel4.ReadStringVector(&val4); + Parcel parcel12; + std::vector val12; + std::string x12 = "test"; + val12.push_back(x12); + parcel12.WriteInt32(val12.max_size()); + result = parcel12.ReadStringVector(&val12); EXPECT_EQ(result, false); - Parcel parcel5; - std::vector val5; - std::u16string x5 = u"test"; - val5.push_back(x5); - parcel5.WriteInt32(val5.max_size()); - result = parcel5.ReadString16Vector(&val5); + Parcel parcel13; + std::vector val13; + std::u16string x13 = u"test"; + val13.push_back(x13); + parcel13.WriteInt32(val13.max_size()); + result = parcel13.ReadString16Vector(&val13); EXPECT_EQ(result, false); } /** - * @tc.name: test_parcel_readvec_003 - * @tc.desc: test parcel read vector failed with invlalid vector length - * std::vector::max_size(). + * @tc.name: test_parcel_writevec_001 + * @tc.desc: test parcel write vector failed with writting data out of the + * maximum capacity. * @tc.type: FUNC */ -HWTEST_F(UtilsParcelTest, test_parcel_readvec_003, TestSize.Level0) +HWTEST_F(UtilsParcelTest, test_parcel_writevec_001, TestSize.Level0) { - ReadvecTestThreeFunc01(); - ReadvecTestThreeFunc02(); - ReadvecTestThreeFunc03(); -} + size_t cap = DEFAULT_CPACITY; -static void WritevecTestOneFunc01(const size_t cap) -{ Parcel parcel(nullptr); std::vector val1; bool x1 = true; @@ -591,106 +573,85 @@ static void WritevecTestOneFunc01(const size_t cap) } result = parcel.WriteInt32Vector(val4); EXPECT_EQ(result, false); -} -static void WritevecTestOneFunc02(const size_t cap) -{ - Parcel parcel(nullptr); - std::vector val1; - int64_t x1 = 1; + parcel.FlushBuffer(); + std::vector val5; + int64_t x5 = 1; for (int i = 0; i < cap / sizeof(int64_t); i++) { - val1.push_back(x1); + val5.push_back(x5); } - bool result = parcel.WriteInt64Vector(val1); + result = parcel.WriteInt64Vector(val5); EXPECT_EQ(result, false); parcel.FlushBuffer(); - std::vector val2; - uint8_t x2 = 1; + std::vector val6; + uint8_t x6 = 1; for (int i = 0; i < cap / sizeof(uint8_t); i++) { - val2.push_back(x2); + val6.push_back(x6); } - result = parcel.WriteUInt8Vector(val2); + result = parcel.WriteUInt8Vector(val6); EXPECT_EQ(result, false); parcel.FlushBuffer(); - std::vector val3; - uint16_t x3 = 1; + std::vector val7; + uint16_t x7 = 1; for (int i = 0; i < cap / sizeof(uint16_t); i++) { - val3.push_back(x3); + val7.push_back(x7); } - result = parcel.WriteUInt16Vector(val3); + result = parcel.WriteUInt16Vector(val7); EXPECT_EQ(result, false); parcel.FlushBuffer(); - std::vector val4; - uint32_t x4 = 1; + std::vector val8; + uint32_t x8 = 1; for (int i = 0; i < cap / sizeof(uint32_t); i++) { - val4.push_back(x4); + val8.push_back(x8); } - result = parcel.WriteUInt32Vector(val4); + result = parcel.WriteUInt32Vector(val8); EXPECT_EQ(result, false); -} -static void WritevecTestOneFunc03(const size_t cap) -{ - Parcel parcel(nullptr); - std::vector val1; - uint64_t x1 = 1; + parcel.FlushBuffer(); + std::vector val9; + uint64_t x9 = 1; for (int i = 0; i < cap / sizeof(uint64_t); i++) { - val1.push_back(x1); + val9.push_back(x9); } - bool result = parcel.WriteUInt64Vector(val1); + result = parcel.WriteUInt64Vector(val9); EXPECT_EQ(result, false); parcel.FlushBuffer(); - std::vector val2; - float x2 = 1; + std::vector val10; + float x10 = 1; for (int i = 0; i < cap / sizeof(float); i++) { - val2.push_back(x2); + val10.push_back(x10); } - result = parcel.WriteFloatVector(val2); + result = parcel.WriteFloatVector(val10); EXPECT_EQ(result, false); parcel.FlushBuffer(); - std::vector val3; - double x3 = 1; + std::vector val11; + double x11 = 1; for (int i = 0; i < cap / sizeof(double); i++) { - val3.push_back(x3); + val11.push_back(x11); } - result = parcel.WriteDoubleVector(val3); + result = parcel.WriteDoubleVector(val11); EXPECT_EQ(result, false); parcel.FlushBuffer(); - std::vector val4; - std::string x4((cap / sizeof(char)), 't'); - val4.push_back(x4); - result = parcel.WriteStringVector(val4); + std::vector val12; + std::string x12((cap / sizeof(char)), 't'); + val12.push_back(x12); + result = parcel.WriteStringVector(val12); EXPECT_EQ(result, false); parcel.FlushBuffer(); - std::vector val5; - std::u16string x5((cap / sizeof(char16_t)), u't'); - val5.push_back(x5); - result = parcel.WriteString16Vector(val5); + std::vector val13; + std::u16string x13((cap / sizeof(char16_t)), u't'); + val13.push_back(x13); + result = parcel.WriteString16Vector(val13); EXPECT_EQ(result, false); } -/** - * @tc.name: test_parcel_writevec_001 - * @tc.desc: test parcel write vector failed with writting data out of the - * maximum capacity. - * @tc.type: FUNC - */ -HWTEST_F(UtilsParcelTest, test_parcel_writevec_001, TestSize.Level0) -{ - size_t cap = DEFAULT_CPACITY; - - WritevecTestOneFunc01(cap); - WritevecTestOneFunc02(cap); - WritevecTestOneFunc03(cap); -} - /** * @tc.name: test_parcel_SetMaxCapacity_001 * @tc.desc: test parcel primary type read write. @@ -1433,7 +1394,7 @@ void WriteVectorTestData(Parcel &parcel, const VectorTestData &data) EXPECT_EQ(result, true); } -void ReadVectorTestDataFunc01(Parcel &parcel, const VectorTestData &data) +void ReadVectorTestData(Parcel &parcel, const VectorTestData &data) { vector boolread; vector int8read; @@ -1441,6 +1402,11 @@ void ReadVectorTestDataFunc01(Parcel &parcel, const VectorTestData &data) vector int32read; vector int64read; + vector uint8read; + vector uint16read; + vector uint32read; + vector uint64read; + bool result = parcel.ReadBoolVector(&boolread); EXPECT_EQ(result, true); for (size_t i = 0; i < data.booltest.size(); i++) { @@ -1470,16 +1436,8 @@ void ReadVectorTestDataFunc01(Parcel &parcel, const VectorTestData &data) for (size_t i = 0; i < data.int64test.size(); i++) { EXPECT_EQ(data.int64test[i], int64read[i]); } -} -void ReadVectorTestDataFunc02(Parcel &parcel, const VectorTestData &data) -{ - vector uint8read; - vector uint16read; - vector uint32read; - vector uint64read; - - bool result = parcel.ReadUInt8Vector(&uint8read); + result = parcel.ReadUInt8Vector(&uint8read); EXPECT_EQ(result, true); for (size_t i = 0; i < data.uint8test.size(); i++) { EXPECT_EQ(data.uint8test[i], uint8read[i]); @@ -1504,12 +1462,6 @@ void ReadVectorTestDataFunc02(Parcel &parcel, const VectorTestData &data) } } -void ReadVectorTestData(Parcel &parcel, const VectorTestData &data) -{ - ReadVectorTestDataFunc01(parcel, data); - ReadVectorTestDataFunc02(parcel, data); -} - /** * @tc.name: test_parcel_WriteAndReadVector_001 * @tc.desc: test vector parcel read and write. diff --git a/base/test/unittest/common/utils_safe_block_queue_test.cpp b/base/test/unittest/common/utils_safe_block_queue_test.cpp index 4cb44db..01c2d08 100644 --- a/base/test/unittest/common/utils_safe_block_queue_test.cpp +++ b/base/test/unittest/common/utils_safe_block_queue_test.cpp @@ -44,13 +44,13 @@ public: putStatus = false; getStatus = false; } + static SafeBlockQueue shareQueue; bool putStatus; bool getStatus; - static SafeBlockQueue shareQueue; - void Put(int j) + void Put(int i) { - shareQueue.Push(j); + shareQueue.Push(i); putStatus = true; } @@ -68,8 +68,8 @@ void PutHandleThreadData(DemoThreadData& q, int i) q.Put(i); } -void GetThreadDatePushedStatus(std::array& demoDatas, unsigned int& pushedIn, - unsigned int& unpushedIn) +void GetThreadDatePushedStatus(std::array& demoDatas, + unsigned int& pushedIn, unsigned int& unpushedIn) { pushedIn = 0; unpushedIn = 0; @@ -143,23 +143,6 @@ HWTEST_F(UtilsSafeBlockQueue, testGet001, TestSize.Level0) ASSERT_EQ(t, 0); } -static void ThreadsJoin(std::thread (&threads)[THREAD_NUM]) -{ - for (auto& t : threads) { - t.join(); - } -} - -static void CheckFullQueueStatus(std::array& demoDatas, unsigned int& pushedIn, - unsigned int& unpushedIn, unsigned int& getedOut, unsigned int& ungetedOut) -{ - ASSERT_TRUE(DemoThreadData::shareQueue.IsFull()); - GetThreadDatePushedStatus(demoDatas, pushedIn, unpushedIn); - GetThreadDateGetedStatus(demoDatas, getedOut, ungetedOut); - ASSERT_EQ(pushedIn, THREAD_NUM); - ASSERT_EQ(getedOut, THREAD_NUM - QUEUE_SLOTS); -} - /* * @tc.name: testMutilthreadPutAndBlock001 * @tc.desc: Multiple threads put until blocking runs, one thread gets, all threads finish running normally @@ -179,7 +162,10 @@ HWTEST_F(UtilsSafeBlockQueue, testMutilthreadPutAndBlock001, TestSize.Level0) std::this_thread::sleep_for(std::chrono::seconds(2)); ASSERT_TRUE(DemoThreadData::shareQueue.IsFull()); - unsigned int pushedIn = 0, unpushedIn = 0, getedOut = 0, ungetedOut = 0; + unsigned int pushedIn = 0; + unsigned int unpushedIn = 0; + unsigned int getedOut = 0; + unsigned int ungetedOut = 0; GetThreadDatePushedStatus(demoDatas, pushedIn, unpushedIn); ASSERT_EQ(pushedIn, QUEUE_SLOTS); @@ -192,8 +178,15 @@ HWTEST_F(UtilsSafeBlockQueue, testMutilthreadPutAndBlock001, TestSize.Level0) std::this_thread::sleep_for(std::chrono::seconds(2)); // queue is full and some threads is blocked and is not joined - CheckFullQueueStatus(demoDatas, pushedIn, unpushedIn, getedOut, ungetedOut); - ThreadsJoin(threads); + ASSERT_TRUE(DemoThreadData::shareQueue.IsFull()); + GetThreadDatePushedStatus(demoDatas, pushedIn, unpushedIn); + GetThreadDateGetedStatus(demoDatas, getedOut, ungetedOut); + ASSERT_EQ(pushedIn, THREAD_NUM); + ASSERT_EQ(getedOut, THREAD_NUM - QUEUE_SLOTS); + + for (auto& t : threads) { + t.join(); + } while (!DemoThreadData::shareQueue.IsEmpty()) { demoDatas[0].Get(); @@ -202,16 +195,6 @@ HWTEST_F(UtilsSafeBlockQueue, testMutilthreadPutAndBlock001, TestSize.Level0) // here means all thread end ok or if some operation blocked and the testcase blocked } -static std::time_t GetTimeAddTwoSeconds() -{ - using std::chrono::system_clock; - std::time_t timeT = system_clock::to_time_t(system_clock::now()); - cout << "start time: " << timeT << endl; - const int twoSec = 2; - timeT += twoSec; - return timeT; -} - /* * @tc.name: testMutilthreadConcurrentPutAndBlockInblankqueue001 * @tc.desc: Multi-threaded put() on the empty queue. When n threads are waiting to reach a certain @@ -226,7 +209,10 @@ HWTEST_F(UtilsSafeBlockQueue, testMutilthreadConcurrentPutAndBlockInblankqueue00 using std::chrono::system_clock; - std::time_t timeT = GetTimeAddTwoSeconds(); + std::time_t timeT = system_clock::to_time_t(system_clock::now()); + cout << "start time: " << timeT << endl; + timeT += 2; + // 2. start thread ASSERT_TRUE(DemoThreadData::shareQueue.IsEmpty()); for (unsigned int i = 0; i < THREAD_NUM; i++) { @@ -253,8 +239,15 @@ HWTEST_F(UtilsSafeBlockQueue, testMutilthreadConcurrentPutAndBlockInblankqueue00 std::this_thread::sleep_for(std::chrono::seconds(2)); // queue is full and some threads is blocked and is not joined - CheckFullQueueStatus(demoDatas, pushedIn, unpushedIn, getedOut, ungetedOut); - ThreadsJoin(threads); + ASSERT_TRUE(DemoThreadData::shareQueue.IsFull()); + GetThreadDatePushedStatus(demoDatas, pushedIn, unpushedIn); + GetThreadDateGetedStatus(demoDatas, getedOut, ungetedOut); + ASSERT_EQ(pushedIn, THREAD_NUM); + ASSERT_EQ(getedOut, THREAD_NUM - QUEUE_SLOTS); + + for (auto& t : threads) { + t.join(); + } while (!DemoThreadData::shareQueue.IsEmpty()) { demoDatas[0].Get(); @@ -262,22 +255,6 @@ HWTEST_F(UtilsSafeBlockQueue, testMutilthreadConcurrentPutAndBlockInblankqueue00 // here means all thread end ok or if some operation blocked and the testcase blocked } -static void QueuePushInfull() -{ - for (unsigned int i = 0; i < QUEUE_SLOTS; i++) { - int t = i; - DemoThreadData::shareQueue.Push(t); - } -} - -static void QueuePushInnotfull(const unsigned int remain) -{ - for (unsigned int i = 0; i < QUEUE_SLOTS - remain; i++) { - int t = i; - DemoThreadData::shareQueue.Push(t); - } -} - /* * @tc.name: testMutilthreadConcurrentPutAndBlockInfullqueue001 * @tc.desc: Multi-threaded put() on the full queue. When n threads are waiting to reach a certain @@ -292,9 +269,14 @@ HWTEST_F(UtilsSafeBlockQueue, testMutilthreadConcurrentPutAndBlockInfullqueue001 using std::chrono::system_clock; - std::time_t timeT = GetTimeAddTwoSeconds(); + std::time_t timeT = system_clock::to_time_t(system_clock::now()); + cout << "start time: " << timeT << endl; + timeT += 2; ASSERT_TRUE(DemoThreadData::shareQueue.IsEmpty()); - QueuePushInfull(); + for (unsigned int i = 0; i < QUEUE_SLOTS; i++) { + int t = i; + DemoThreadData::shareQueue.Push(t); + } ASSERT_TRUE(DemoThreadData::shareQueue.IsFull()); // 2. start thread put in full queue @@ -320,7 +302,10 @@ HWTEST_F(UtilsSafeBlockQueue, testMutilthreadConcurrentPutAndBlockInfullqueue001 ASSERT_EQ(unpushedIn, THREAD_NUM - (i + 1)); } - ThreadsJoin(threads); + for (auto& t : threads) { + t.join(); + } + while (!DemoThreadData::shareQueue.IsEmpty()) { demoDatas[0].Get(); } @@ -340,7 +325,9 @@ HWTEST_F(UtilsSafeBlockQueue, testMutilthreadConcurrentGetAndBlockInblankqueue00 using std::chrono::system_clock; - std::time_t timeT = GetTimeAddTwoSeconds(); + std::time_t timeT = system_clock::to_time_t(system_clock::now()); + cout << "start time: " << timeT << endl; + timeT += 2; ASSERT_TRUE(DemoThreadData::shareQueue.IsEmpty()); // 2. start thread put in empty queue @@ -370,7 +357,10 @@ HWTEST_F(UtilsSafeBlockQueue, testMutilthreadConcurrentGetAndBlockInblankqueue00 ASSERT_EQ(ungetedOut, THREAD_NUM - (i + 1)); } - ThreadsJoin(threads); + for (auto& t : threads) { + t.join(); + } + while (!DemoThreadData::shareQueue.IsEmpty()) { demoDatas[0].Get(); } @@ -389,7 +379,9 @@ HWTEST_F(UtilsSafeBlockQueue, testMutilthreadConcurrentGetAndBlockInfullqueue001 using std::chrono::system_clock; - std::time_t timeT = GetTimeAddTwoSeconds(); + std::time_t timeT = system_clock::to_time_t(system_clock::now()); + cout << "start time: " << timeT << endl; + timeT += 2; ASSERT_TRUE(DemoThreadData::shareQueue.IsEmpty()); int t = 1; for (unsigned int i = 0; i < QUEUE_SLOTS; i++) { @@ -425,7 +417,10 @@ HWTEST_F(UtilsSafeBlockQueue, testMutilthreadConcurrentGetAndBlockInfullqueue001 ASSERT_EQ(getedOut, THREAD_NUM); ASSERT_EQ(ungetedOut, static_cast(0)); - ThreadsJoin(threads); + for (auto& t : threads) { + t.join(); + } + while (!DemoThreadData::shareQueue.IsEmpty()) { demoDatas[0].Get(); } @@ -446,9 +441,14 @@ HWTEST_F(UtilsSafeBlockQueue, testMutilthreadConcurrentGetAndBlockInnotfullqueue using std::chrono::system_clock; const unsigned int REMAIN_SLOTS = 5; - std::time_t timeT = GetTimeAddTwoSeconds(); + std::time_t timeT = system_clock::to_time_t(system_clock::now()); + cout << "start time: " << timeT << endl; + timeT += 2; ASSERT_TRUE(DemoThreadData::shareQueue.IsEmpty()); - QueuePushInnotfull(REMAIN_SLOTS); + for (unsigned int i = 0; i < QUEUE_SLOTS - REMAIN_SLOTS; i++) { + int t = i; + DemoThreadData::shareQueue.Push(t); + } // 2. start thread put in not full queue for (unsigned int i = 0; i < THREAD_NUM; i++) { @@ -475,7 +475,10 @@ HWTEST_F(UtilsSafeBlockQueue, testMutilthreadConcurrentGetAndBlockInnotfullqueue ASSERT_EQ(getedOut, THREAD_NUM); ASSERT_EQ(ungetedOut, static_cast(0)); - ThreadsJoin(threads); + for (auto& t : threads) { + t.join(); + } + while (!DemoThreadData::shareQueue.IsEmpty()) { demoDatas[0].Get(); } @@ -496,9 +499,14 @@ HWTEST_F(UtilsSafeBlockQueue, testMutilthreadConcurrentPutAndBlockInnotfullqueue using std::chrono::system_clock; const unsigned int REMAIN_SLOTS = 5; - std::time_t timeT = GetTimeAddTwoSeconds(); + std::time_t timeT = system_clock::to_time_t(system_clock::now()); + cout << "start time: " << timeT << endl; + timeT += 2; ASSERT_TRUE(DemoThreadData::shareQueue.IsEmpty()); - QueuePushInnotfull(REMAIN_SLOTS); + for (unsigned int i = 0; i < QUEUE_SLOTS - REMAIN_SLOTS; i++) { + int t = i; + DemoThreadData::shareQueue.Push(t); + } // 2. start thread put in not full queue for (unsigned int i = 0; i < THREAD_NUM; i++) { @@ -524,25 +532,15 @@ HWTEST_F(UtilsSafeBlockQueue, testMutilthreadConcurrentPutAndBlockInnotfullqueue ASSERT_EQ(putedin, THREAD_NUM); ASSERT_EQ(unputedin, static_cast(0)); - ThreadsJoin(threads); + for (auto& t : threads) { + t.join(); + } + while (!DemoThreadData::shareQueue.IsEmpty()) { demoDatas[0].Get(); } } -static void CheckQueueStatus(std::array& demoDatas) -{ - unsigned int getedOut = 0; - unsigned int ungetedOut = 0; - unsigned int pushedIn = 0; - unsigned int unpushedIn = 0; - GetThreadDateGetedStatus(demoDatas, getedOut, ungetedOut); - GetThreadDatePushedStatus(demoDatas, pushedIn, unpushedIn); - - ASSERT_EQ(pushedIn, THREAD_NUM); - ASSERT_EQ(getedOut, THREAD_NUM); -} - /* * @tc.name: testMutilthreadConcurrentGetAndPopInblankqueue001 * @tc.desc: Multi-threaded put() and Multi-threaded get() on the empty queue. When all threads are waiting to reach @@ -559,7 +557,9 @@ HWTEST_F(UtilsSafeBlockQueue, testMutilthreadConcurrentGetAndPopInblankqueue001, using std::chrono::system_clock; - std::time_t timeT = GetTimeAddTwoSeconds(); + std::time_t timeT = system_clock::to_time_t(system_clock::now()); + cout << "start time: " << timeT << endl; + timeT += 2; ASSERT_TRUE(DemoThreadData::shareQueue.IsEmpty()); // 2. start thread put and get in empty queue @@ -577,10 +577,23 @@ HWTEST_F(UtilsSafeBlockQueue, testMutilthreadConcurrentGetAndPopInblankqueue001, std::this_thread::sleep_for(std::chrono::seconds(3)); ASSERT_TRUE(DemoThreadData::shareQueue.IsEmpty()); - CheckQueueStatus(demoDatas); + unsigned int getedOut = 0; + unsigned int ungetedOut = 0; + unsigned int pushedIn = 0; + unsigned int unpushedIn = 0; + GetThreadDateGetedStatus(demoDatas, getedOut, ungetedOut); + GetThreadDatePushedStatus(demoDatas, pushedIn, unpushedIn); + + ASSERT_EQ(pushedIn, THREAD_NUM); + ASSERT_EQ(getedOut, THREAD_NUM); + + for (auto& t : threadsout) { + t.join(); + } - ThreadsJoin(threadsout); - ThreadsJoin(threadsin); + for (auto& t : threadsin) { + t.join(); + } while (!DemoThreadData::shareQueue.IsEmpty()) { demoDatas[0].Get(); @@ -604,7 +617,9 @@ HWTEST_F(UtilsSafeBlockQueue, testMutilthreadConcurrentGetAndPopInfullqueue001, using std::chrono::system_clock; - std::time_t timeT = GetTimeAddTwoSeconds(); + std::time_t timeT = system_clock::to_time_t(system_clock::now()); + cout << "start time: " << timeT << endl; + timeT += 2; ASSERT_TRUE(DemoThreadData::shareQueue.IsEmpty()); int t = 1; for (unsigned int i = 0; i < QUEUE_SLOTS; i++) { @@ -625,10 +640,23 @@ HWTEST_F(UtilsSafeBlockQueue, testMutilthreadConcurrentGetAndPopInfullqueue001, std::this_thread::sleep_for(std::chrono::seconds(3)); ASSERT_TRUE(DemoThreadData::shareQueue.IsFull()); - CheckQueueStatus(demoDatas); + unsigned int getedOut = 0; + unsigned int ungetedOut = 0; + unsigned int pushedIn = 0; + unsigned int unpushedIn = 0; + GetThreadDateGetedStatus(demoDatas, getedOut, ungetedOut); + GetThreadDatePushedStatus(demoDatas, pushedIn, unpushedIn); - ThreadsJoin(threadsout); - ThreadsJoin(threadsin); + ASSERT_EQ(pushedIn, THREAD_NUM); + ASSERT_EQ(getedOut, THREAD_NUM); + + for (auto& t : threadsout) { + t.join(); + } + + for (auto& t : threadsin) { + t.join(); + } while (!DemoThreadData::shareQueue.IsEmpty()) { demoDatas[0].Get(); diff --git a/base/test/unittest/common/utils_safe_block_queue_tracking.cpp b/base/test/unittest/common/utils_safe_block_queue_tracking.cpp index 05e35b7..e5ed313 100644 --- a/base/test/unittest/common/utils_safe_block_queue_tracking.cpp +++ b/base/test/unittest/common/utils_safe_block_queue_tracking.cpp @@ -40,10 +40,16 @@ public: joinStatus = false; } - bool putStatus; - bool getStatus; static SafeBlockQueueTracking shareQueue; static bool joinStatus; + bool putStatus; + bool getStatus; + + void Put(int i) + { + shareQueue.Push(i); + putStatus = true; + } void Get() { @@ -51,10 +57,11 @@ public: getStatus = true; } - void Put(int j) + void GetAndOneTaskDone() { - shareQueue.Push(j); - putStatus = true; + shareQueue.Pop(); + getStatus = true; + shareQueue.OneTaskDone(); } void Join() @@ -62,13 +69,6 @@ public: shareQueue.Join(); joinStatus = true; } - - void GetAndOneTaskDone() - { - shareQueue.Pop(); - getStatus = true; - shareQueue.OneTaskDone(); - } }; SafeBlockQueueTracking DemoThreadData::shareQueue(QUEUE_SLOTS); @@ -200,25 +200,6 @@ HWTEST_F(UtilsSafeBlockQueueTracking, testMutilthreadPutAndBlock001, TestSize.Le demoDatas[0].joinStatus = false; } -static void CheckFullQueueStatus(std::array& demoDatas, unsigned int& pushedIn, - unsigned int& unpushedIn, unsigned int& getedOut, unsigned int& ungetedOut) -{ - ASSERT_TRUE(DemoThreadData::shareQueue.IsFull()); - GetThreadDatePushedStatus(demoDatas, pushedIn, unpushedIn); - GetThreadDateGetedStatus(demoDatas, getedOut, ungetedOut); - ASSERT_EQ(pushedIn, THREAD_NUM); - ASSERT_EQ(getedOut, THREAD_NUM - QUEUE_SLOTS); -} - -static std::time_t GetTimeAddTwoSeconds() -{ - using std::chrono::system_clock; - std::time_t timeT = system_clock::to_time_t(system_clock::now()); - const int twoSec = 2; - timeT += twoSec; - return timeT; -} - /* * @tc.name: testMutilthreadConcurrentPutAndBlockInblankqueue001 * @tc.desc: Multi-threaded put() on the empty queue. When n threads are waiting to reach a certain @@ -233,7 +214,8 @@ HWTEST_F(UtilsSafeBlockQueueTracking, testMutilthreadConcurrentPutAndBlockInblan using std::chrono::system_clock; - std::time_t timeT = GetTimeAddTwoSeconds(); + std::time_t timeT = system_clock::to_time_t(system_clock::now()); + timeT += 2; // 2. start thread ASSERT_TRUE(DemoThreadData::shareQueue.IsEmpty()); @@ -267,7 +249,11 @@ HWTEST_F(UtilsSafeBlockQueueTracking, testMutilthreadConcurrentPutAndBlockInblan std::this_thread::sleep_for(std::chrono::seconds(2)); // queue is full and some threads is blocked and is not joined - CheckFullQueueStatus(demoDatas, pushedIn, unpushedIn, getedOut, ungetedOut); + ASSERT_TRUE(DemoThreadData::shareQueue.IsFull()); + GetThreadDatePushedStatus(demoDatas, pushedIn, unpushedIn); + GetThreadDateGetedStatus(demoDatas, getedOut, ungetedOut); + ASSERT_EQ(pushedIn, THREAD_NUM); + ASSERT_EQ(getedOut, THREAD_NUM - QUEUE_SLOTS); for (auto& t : threads) { t.join(); @@ -283,22 +269,6 @@ HWTEST_F(UtilsSafeBlockQueueTracking, testMutilthreadConcurrentPutAndBlockInblan joinThread.join(); } -static void QueuePushInfull() -{ - for (unsigned int i = 0; i < QUEUE_SLOTS; i++) { - int t = i; - DemoThreadData::shareQueue.Push(t); - } -} - -static void QueuePushInnotfull(const unsigned int remain) -{ - for (unsigned int i = 0; i < QUEUE_SLOTS - remain; i++) { - int t = i; - DemoThreadData::shareQueue.Push(t); - } -} - /* * @tc.name: testMutilthreadConcurrentPutAndBlockInfullqueue001 * @tc.desc: Multi-threaded put() on the full queue. When n threads are waiting to reach a certain @@ -313,9 +283,13 @@ HWTEST_F(UtilsSafeBlockQueueTracking, testMutilthreadConcurrentPutAndBlockInfull using std::chrono::system_clock; - std::time_t timeT = GetTimeAddTwoSeconds(); + std::time_t timeT = system_clock::to_time_t(system_clock::now()); + timeT += 2; ASSERT_TRUE(DemoThreadData::shareQueue.IsEmpty()); - QueuePushInfull(); + for (unsigned int i = 0; i < QUEUE_SLOTS; i++) { + int t = i; + DemoThreadData::shareQueue.Push(t); + } ASSERT_TRUE(DemoThreadData::shareQueue.IsFull()); // 2. start thread put in full queue @@ -372,7 +346,8 @@ HWTEST_F(UtilsSafeBlockQueueTracking, testMutilthreadConcurrentGetAndBlockInblan using std::chrono::system_clock; - std::time_t timeT = GetTimeAddTwoSeconds(); + std::time_t timeT = system_clock::to_time_t(system_clock::now()); + timeT += 2; ASSERT_TRUE(DemoThreadData::shareQueue.IsEmpty()); // 2. start thread put in empty queue @@ -418,14 +393,6 @@ HWTEST_F(UtilsSafeBlockQueueTracking, testMutilthreadConcurrentGetAndBlockInblan joinThread.join(); } -static void QueuePushFullEquivalent(const int Equivalent) -{ - for (unsigned int i = 0; i < QUEUE_SLOTS; i++) { - DemoThreadData::shareQueue.Push(Equivalent); - } - ASSERT_TRUE(DemoThreadData::shareQueue.IsFull()); -} - /* * @tc.name: testMutilthreadConcurrentGetAndBlockInfullqueue001 * @tc.desc: Multi-threaded get() on the full queue. When n threads are waiting to reach a certain @@ -440,10 +407,14 @@ HWTEST_F(UtilsSafeBlockQueueTracking, testMutilthreadConcurrentGetAndBlockInfull using std::chrono::system_clock; - std::time_t timeT = GetTimeAddTwoSeconds(); + std::time_t timeT = system_clock::to_time_t(system_clock::now()); + timeT += 2; ASSERT_TRUE(DemoThreadData::shareQueue.IsEmpty()); int t = 1; - QueuePushFullEquivalent(t); + for (unsigned int i = 0; i < QUEUE_SLOTS; i++) { + DemoThreadData::shareQueue.Push(t); + } + ASSERT_TRUE(DemoThreadData::shareQueue.IsFull()); // start thread to join demoDatas[0].joinStatus = false; @@ -463,7 +434,8 @@ HWTEST_F(UtilsSafeBlockQueueTracking, testMutilthreadConcurrentGetAndBlockInfull ASSERT_TRUE(DemoThreadData::shareQueue.IsEmpty()); ASSERT_TRUE(demoDatas[0].joinStatus); - unsigned int getedOut = 0, ungetedOut = 0; + unsigned int getedOut = 0; + unsigned int ungetedOut = 0; GetThreadDateGetedStatus(demoDatas, getedOut, ungetedOut); ASSERT_EQ(getedOut, QUEUE_SLOTS); @@ -516,9 +488,13 @@ HWTEST_F(UtilsSafeBlockQueueTracking, testMutilthreadConcurrentGetAndBlockInnotf using std::chrono::system_clock; const unsigned int REMAIN_SLOTS = 5; - std::time_t timeT = GetTimeAddTwoSeconds(); + std::time_t timeT = system_clock::to_time_t(system_clock::now()); + timeT += 2; ASSERT_TRUE(DemoThreadData::shareQueue.IsEmpty()); - QueuePushInnotfull(REMAIN_SLOTS); + for (unsigned int i = 0; i < QUEUE_SLOTS - REMAIN_SLOTS; i++) { + int t = i; + DemoThreadData::shareQueue.Push(t); + } // start thread to join demoDatas[0].joinStatus = false; @@ -572,16 +548,20 @@ HWTEST_F(UtilsSafeBlockQueueTracking, testMutilthreadConcurrentGetAndBlockInnotf HWTEST_F(UtilsSafeBlockQueueTracking, testMutilthreadConcurrentPutAndBlockInnotfullqueue001, TestSize.Level0) { // 1. prepare - std::array demoDatas; std::thread threads[THREAD_NUM]; + std::array demoDatas; demoDatas.fill(DemoThreadData()); using std::chrono::system_clock; const unsigned int REMAIN_SLOTS = 5; - std::time_t timeT = GetTimeAddTwoSeconds(); + std::time_t timeT = system_clock::to_time_t(system_clock::now()); + timeT += 2; ASSERT_TRUE(DemoThreadData::shareQueue.IsEmpty()); - QueuePushInnotfull(REMAIN_SLOTS); + for (unsigned int i = 0; i < QUEUE_SLOTS - REMAIN_SLOTS; i++) { + int t = i; + DemoThreadData::shareQueue.Push(t); + } // start thread to join demoDatas[0].joinStatus = false; @@ -645,10 +625,14 @@ HWTEST_F(UtilsSafeBlockQueueTracking, testMutilthreadConcurrentGetAndPopInfullqu using std::chrono::system_clock; - std::time_t timeT = GetTimeAddTwoSeconds(); + std::time_t timeT = system_clock::to_time_t(system_clock::now()); + timeT += 2; ASSERT_TRUE(DemoThreadData::shareQueue.IsEmpty()); int t = 1; - QueuePushFullEquivalent(t); + for (unsigned int i = 0; i < QUEUE_SLOTS; i++) { + DemoThreadData::shareQueue.Push(t); + } + ASSERT_TRUE(DemoThreadData::shareQueue.IsFull()); // start thread to join demoDatas[0].joinStatus = false; @@ -670,7 +654,10 @@ HWTEST_F(UtilsSafeBlockQueueTracking, testMutilthreadConcurrentGetAndPopInfullqu std::this_thread::sleep_for(std::chrono::seconds(3)); ASSERT_TRUE(DemoThreadData::shareQueue.IsFull()); - unsigned int getedOut = 0, ungetedOut = 0, pushedIn = 0, unpushedIn = 0; + unsigned int getedOut = 0; + unsigned int ungetedOut = 0; + unsigned int pushedIn = 0; + unsigned int unpushedIn = 0; GetThreadDateGetedStatus(demoDatas, getedOut, ungetedOut); GetThreadDatePushedStatus(demoDatas, pushedIn, unpushedIn); diff --git a/base/test/unittest/common/utils_safe_map_test.cpp b/base/test/unittest/common/utils_safe_map_test.cpp index 9c78dbf..0b90909 100644 --- a/base/test/unittest/common/utils_safe_map_test.cpp +++ b/base/test/unittest/common/utils_safe_map_test.cpp @@ -371,34 +371,6 @@ HWTEST_F(UtilsSafeMap, testUtilsConcurrentWriteAndFind001, TestSize.Level0) }); } -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, @@ -445,7 +417,30 @@ HWTEST_F(UtilsSafeMap, testUtilsConcurrentWriteAndFindAndSet001, TestSize.Level0 t.join(); } - ResultCompare(vcfi, 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]); + } }); } } // namespace -- Gitee