From 15357cba15042e17efca8ad54a9217c99be6bc41 Mon Sep 17 00:00:00 2001 From: wujianlin Date: Mon, 22 Jan 2024 07:32:33 +0000 Subject: [PATCH] Optimize test cases and encapsulate cleanup capabilities as functions Issue:https://gitee.com/openharmony/commonlibrary_c_utils/issues/I8Y01U?from=project-issue Signed-off-by: wujianlin --- .../refbase_fuzzer/refbase_fuzzer.cpp | 82 +++++++++++-------- 1 file changed, 48 insertions(+), 34 deletions(-) diff --git a/base/test/fuzztest/refbase_fuzzer/refbase_fuzzer.cpp b/base/test/fuzztest/refbase_fuzzer/refbase_fuzzer.cpp index ec2ce49..071cbc6 100644 --- a/base/test/fuzztest/refbase_fuzzer/refbase_fuzzer.cpp +++ b/base/test/fuzztest/refbase_fuzzer/refbase_fuzzer.cpp @@ -250,39 +250,9 @@ const std::vector> }, }; -void TestLoop(const std::vector& fuzzOps) +// Clean up WeakRefCounter +void CleanUpWeakRefCounter(SingleThreadRefCounts& state, WeakRefCounter* newWeakRef) { - 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; @@ -300,8 +270,11 @@ void TestLoop(const std::vector& fuzzOps) } } } +} - // Clean up any weak references +// Clean up any weak references +void CleanUpWeakCounter(SingleThreadRefCounts& state) +{ for (; state.weakCount > 0; state.weakCount--) { bool shouldLock = state.strongCount == 0 && state.weakCount == 1; if (shouldLock) { @@ -314,8 +287,11 @@ void TestLoop(const std::vector& fuzzOps) g_strongLock.UnLockWrite(); } } +} - // Clean up any strong references +// Clean up any strong references +void CleanUpStrongCounter(SingleThreadRefCounts& state) +{ for (; state.strongCount > 0; state.strongCount--) { bool shouldLock = state.strongCount == 1; if (shouldLock) { @@ -330,6 +306,43 @@ void TestLoop(const std::vector& fuzzOps) } } +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"); @@ -353,6 +366,7 @@ void RefbaseTestFunc(const uint8_t* data, size_t size, FuzzedDataProvider* dataP if (!g_refModified && !g_refDeleted) { delete g_ref; + g_ref = nullptr; } FUZZ_LOGI("RefbaseTestFunc end"); } -- Gitee