diff --git a/base/test/benchmarktest/BUILD.gn b/base/test/benchmarktest/BUILD.gn index 7fa6c81c03a6ee63ae479f192cc2d5b63cf42b59..f42ae72331605e7edbbb35f469387356d4545069 100644 --- a/base/test/benchmarktest/BUILD.gn +++ b/base/test/benchmarktest/BUILD.gn @@ -23,6 +23,9 @@ group("benchmarktest") { "parcel_benchmark_test:ParcelTest", "refbase_benchmark_test:RefbaseTest", "rwlock_benchmark_test:RwlockTest", + "safe_map_benchmark_test:SafeMapTest", + "singleton_benchmark_test:SingletonTest", + "sorted_vector_benchmark_test:SortedVectorTest", "string_benchmark_test:StringTest", "timer_benchmark_test:TimerTest", ] diff --git a/base/test/benchmarktest/safe_map_benchmark_test/BUILD.gn b/base/test/benchmarktest/safe_map_benchmark_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..a148b5c16f2c50aee1c530cbd595759e96d8acb4 --- /dev/null +++ b/base/test/benchmarktest/safe_map_benchmark_test/BUILD.gn @@ -0,0 +1,35 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") + +module_output_path = "commonlibrary_c_utils/safe_map" + +ohos_benchmarktest("SafeMapTest") { + module_out_path = module_output_path + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "safe_map_benchmark_test.cpp" ] + + deps = [ "//third_party/benchmark:benchmark" ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog_base", + ] +} diff --git a/base/test/benchmarktest/safe_map_benchmark_test/safe_map_benchmark_test.cpp b/base/test/benchmarktest/safe_map_benchmark_test/safe_map_benchmark_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3df4bf0d7f790662152ade803f94ac42b44d532 --- /dev/null +++ b/base/test/benchmarktest/safe_map_benchmark_test/safe_map_benchmark_test.cpp @@ -0,0 +1,618 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "safe_map.h" +#include +#include +#include +#include +#include +#include "../log.h" +#include "../assert.h" +using namespace std; +using std::chrono::system_clock; + +namespace OHOS { +namespace { + +class BenchmarkSafeMap : public benchmark::Fixture { +public: + BenchmarkSafeMap() + { + Iterations(iterations); + Repetitions(repetitions); + ReportAggregatesOnly(); + } + + ~BenchmarkSafeMap() override = default; + void SetUp(const ::benchmark::State& state) override + { + } + + void TearDown(const ::benchmark::State& state) override + { + } + +protected: + const int32_t repetitions = 3; + const int32_t iterations = 50; +}; + +const int INSERT_ONE = 1; +const int INSERT_TWO = 2; +const int INSERT_THREE = 3; +const int INSERT_FOUR = 4; +const int INSERT_FIVE = 5; +const int INSERT_SIX = 6; + +/* + * @tc.name: testUtilsCopyAndAssign001 + * @tc.desc: single thread test the normal feature insert and erase and EnsureInsert + */ +BENCHMARK_F(BenchmarkSafeMap, testUtilsCopyAndAssign001)(benchmark::State& state) +{ + BENCHMARK_LOGD("SafeMap testUtilsCopyAndAssign001 start."); + while (state.KeepRunning()) { + SafeMap demoData; + // insert new + demoData.Insert("A", INSERT_ONE); + AssertFalse(demoData.IsEmpty(), "demoData.IsEmpty() did not equal false as expected.", state); + AssertEqual(demoData.Size(), INSERT_ONE, "demoData.Size() did not equal INSERT_ONE as expected.", state); + + SafeMap newdemo = demoData; + int tar = -1; + AssertTrue(newdemo.Find("A", tar), "newdemo.Find(\"A\", tar) did not equal true as expected.", state); + AssertEqual(INSERT_ONE, tar, "INSERT_ONE did not equal tar as expected.", state); + + tar = -1; + SafeMap newdemo2; + newdemo2 = demoData; + AssertTrue(newdemo2.Find("A", tar), "newdemo2.Find(\"A\", tar) did not equal true as expected.", state); + AssertEqual(INSERT_ONE, tar, "INSERT_ONE did not equal tar as expected.", state); + } + BENCHMARK_LOGD("SafeMap testUtilsCopyAndAssign001 end."); +} + +/* + * @tc.name: testUtilsoperator001 + * @tc.desc: SafeMap + */ +BENCHMARK_F(BenchmarkSafeMap, testUtilsoperator001)(benchmark::State& state) +{ + BENCHMARK_LOGD("SafeMap testUtilsoperator001 start."); + while (state.KeepRunning()) { + SafeMap demoData; + // insert new + demoData.Insert("A", INSERT_ONE); + AssertFalse(demoData.IsEmpty(), "demoData.IsEmpty() did not equal false as expected.", state); + AssertEqual(demoData.Size(), INSERT_ONE, "demoData.Size() did not equal INSERT_ONE as expected.", state); + + int valueA; + bool foundA = demoData.Find("A", valueA); + AssertTrue(foundA, "demoData.Find(\"A\", valueA) did not return true as expected.", state); + AssertEqual(valueA, INSERT_ONE, "Value retrieved did not equal INSERT_ONE as expected.", state); + + SafeMap newdemo = demoData; + int valueNewDemoA; + bool foundNewDemoA = newdemo.Find("A", valueNewDemoA); + AssertTrue(foundNewDemoA, "newdemo.Find(\"A\", valueNewDemoA) did not return true as expected.", state); + AssertEqual(valueNewDemoA, INSERT_ONE, "Value retrieved did not equal INSERT_ONE as expected.", state); + + int tar = -1; + newdemo.Insert("B", INSERT_SIX); + bool foundB = newdemo.Find("B", tar); + AssertTrue(foundB, "newdemo.Find(\"B\", tar) did not return true as expected.", state); + AssertEqual(INSERT_SIX, tar, "INSERT_SIX did not equal tar as expected.", state); + + SafeMap newdemo2; + newdemo2 = newdemo; + int valueNewDemo2A; + bool foundNewDemo2A = newdemo2.Find("A", valueNewDemo2A); + AssertTrue(foundNewDemo2A, "newdemo2.Find(\"A\", valueNewDemo2A) did not return true as expected.", state); + AssertEqual(valueNewDemo2A, INSERT_ONE, "Value retrieved did not equal INSERT_ONE as expected.", state); + } + BENCHMARK_LOGD("SafeMap testUtilsoperator001 end."); +} + +/* + * @tc.name: testUtilsNormalFeatureInsert001 + * @tc.desc: SafeMap + */ +BENCHMARK_F(BenchmarkSafeMap, testUtilsNormalFeatureInsert001)(benchmark::State& state) +{ + BENCHMARK_LOGD("SafeMap testUtilsNormalFeatureInsert001 start."); + while (state.KeepRunning()) { + SafeMap demoData; + AssertTrue(demoData.IsEmpty(), "demoData.IsEmpty() did not equal true as expected.", state); + + // insert new + demoData.Insert("A", INSERT_ONE); + AssertFalse(demoData.IsEmpty(), "demoData.IsEmpty() did not equal false as expected.", state); + AssertEqual(demoData.Size(), INSERT_ONE, "demoData.Size() did not equal INSERT_ONE as expected.", state); + + // insert copy one should fail + AssertFalse(demoData.Insert("A", INSERT_TWO), + "demoData.Insert(\"A\", INSERT_TWO) did not equal false as expected.", state); + AssertEqual(demoData.Size(), INSERT_ONE, "demoData.Size() did not equal INSERT_ONE as expected.", state); + } + BENCHMARK_LOGD("SafeMap testUtilsNormalFeatureInsert001 end."); +} + +/* + * @tc.name: testUtilsNormalFeatureEnsureInsert001 + * @tc.desc: SafeMap + */ +BENCHMARK_F(BenchmarkSafeMap, testUtilsNormalFeatureEnsureInsert001)(benchmark::State& state) +{ + BENCHMARK_LOGD("SafeMap testUtilsNormalFeatureEnsureInsert001 start."); + while (state.KeepRunning()) { + SafeMap demoData; + AssertTrue(demoData.IsEmpty(), "demoData.IsEmpty() did not equal true as expected.", state); + + demoData.Insert("A", INSERT_ONE); + demoData.EnsureInsert("B", INSERT_TWO); + + AssertFalse(demoData.IsEmpty(), "demoData.IsEmpty() did not equal false as expected.", state); + AssertEqual(demoData.Size(), INSERT_TWO, "demoData.Size() did not equal INSERT_TWO as expected.", state); + + // insert copy one and new one + demoData.EnsureInsert("B", INSERT_FIVE); + demoData.EnsureInsert("C", INSERT_SIX); + AssertEqual(demoData.Size(), INSERT_THREE, "demoData.Size() did not equal INSERT_THREE as expected.", state); + } + BENCHMARK_LOGD("SafeMap testUtilsNormalFeatureEnsureInsert001 end."); +} + +/* + * @tc.name: testUtilsNormalFeatureFind001 + * @tc.desc: SafeMap + */ +BENCHMARK_F(BenchmarkSafeMap, testUtilsNormalFeatureFind001)(benchmark::State& state) +{ + BENCHMARK_LOGD("SafeMap testUtilsNormalFeatureFind001 start."); + while (state.KeepRunning()) { + SafeMap demoData; + AssertTrue(demoData.IsEmpty(), "demoData.IsEmpty() did not equal true as expected.", state); + + demoData.Insert("A", INSERT_ONE); + demoData.Insert("B", 10000); + demoData.EnsureInsert("B", INSERT_TWO); + demoData.EnsureInsert("C", INSERT_SIX); + + AssertFalse(demoData.IsEmpty(), "demoData.IsEmpty() did not equal false as expected.", state); + AssertEqual(demoData.Size(), INSERT_THREE, "demoData.Size() did not equal INSERT_THREE as expected.", state); + + int i = 0; + AssertTrue(demoData.Find("A", i), "demoData.Find(\"A\", i) did not equal true as expected.", state); + AssertEqual(i, INSERT_ONE, "i did not equal INSERT_ONE as expected.", state); + AssertTrue(demoData.Find("B", i), "demoData.Find(\"B\", i) did not equal true as expected.", state); + AssertEqual(i, INSERT_TWO, "i did not equal INSERT_TWO as expected.", state); + + AssertTrue(demoData.Find("C", i), "demoData.Find(\"C\", i) did not equal true as expected.", state); + AssertEqual(i, INSERT_SIX, "i did not equal INSERT_SIX as expected.", state); + } + BENCHMARK_LOGD("SafeMap testUtilsNormalFeatureFind001 end."); +} + +/* + * @tc.name: testUtilsNormalFeatureFindAndSet001 + * @tc.desc: SafeMap + */ +BENCHMARK_F(BenchmarkSafeMap, testUtilsNormalFeatureFindAndSet001)(benchmark::State& state) +{ + BENCHMARK_LOGD("SafeMap testUtilsNormalFeatureFindAndSet001 start."); + while (state.KeepRunning()) { + SafeMap demoData; + AssertTrue(demoData.IsEmpty(), "demoData.IsEmpty() did not equal true as expected.", state); + + demoData.Insert("A", INSERT_ONE); + demoData.EnsureInsert("B", INSERT_TWO); + + int oldvalue = 0; + int newvalue = 3; + AssertTrue(demoData.FindOldAndSetNew("A", oldvalue, newvalue), + "demoData.FindOldAndSetNew(\"A\", oldvalue, newvalue) did not equal true as expected.", state); + + // old value + AssertEqual(oldvalue, INSERT_ONE, "oldvalue did not equal INSERT_ONE as expected.", state); + + newvalue = 4; + AssertTrue(demoData.FindOldAndSetNew("B", oldvalue, newvalue), + "demoData.FindOldAndSetNew(\"B\", oldvalue, newvalue) did not equal true as expected.", state); + + // old value + AssertEqual(oldvalue, INSERT_TWO, "oldvalue did not equal INSERT_TWO as expected.", state); + + int i = -1; + AssertTrue(demoData.Find("A", i), "demoData.Find(\"A\", i) did not equal true as expected.", state); + + // new value + AssertEqual(i, INSERT_THREE, "i did not equal INSERT_THREE as expected.", state); + AssertTrue(demoData.Find("B", i), "demoData.Find(\"B\", i) did not equal true as expected.", state); + + // new value + AssertEqual(i, INSERT_FOUR, "i did not equal INSERT_FOUR as expected.", state); + } + BENCHMARK_LOGD("SafeMap testUtilsNormalFeatureFindAndSet001 end."); +} + +/* + * @tc.name: testUtilsNormalFeatureEraseAndClear001 + * @tc.desc: SafeMap + */ +BENCHMARK_F(BenchmarkSafeMap, testUtilsNormalFeatureEraseAndClear001)(benchmark::State& state) +{ + BENCHMARK_LOGD("SafeMap testUtilsNormalFeatureEraseAndClear001 start."); + while (state.KeepRunning()) { + SafeMap demoData; + AssertTrue(demoData.IsEmpty(), "demoData.IsEmpty() did not equal true as expected.", state); + + demoData.Insert("A", INSERT_ONE); + demoData.EnsureInsert("B", INSERT_TWO); + + AssertEqual(demoData.Size(), INSERT_TWO, "demoData.Size() did not equal INSERT_TWO as expected.", state); + demoData.Erase("A"); + AssertEqual(demoData.Size(), INSERT_ONE, "demoData.Size() did not equal INSERT_ONE as expected.", state); + + demoData.Clear(); + AssertEqual(demoData.Size(), 0, "demoData.Size() did not equal 0 as expected.", state); + } + BENCHMARK_LOGD("SafeMap testUtilsNormalFeatureEraseAndClear001 end."); +} + +/* + * @tc.name: testUtilsNormalFeatureIterate001 + * @tc.desc: Using Iterate to change the second parameter of SafeMap + */ +void callback(const std::string str, int& value) +{ + value++; +} + +BENCHMARK_F(BenchmarkSafeMap, testUtilsNormalFeatureIterate001)(benchmark::State& state) +{ + BENCHMARK_LOGD("SafeMap testUtilsNormalFeatureIterate001 start."); + while (state.KeepRunning()) { + SafeMap demoData; + AssertTrue(demoData.IsEmpty(), "demoData.IsEmpty() did not equal true as expected.", state); + + demoData.Insert("A", INSERT_ONE); + demoData.Insert("B", INSERT_TWO); + demoData.Insert("C", INSERT_THREE); + demoData.Insert("D", INSERT_FOUR); + demoData.Iterate(callback); + + AssertEqual(demoData.Size(), INSERT_FOUR, "demoData.Size() did not equal INSERT_FOUR as expected.", state); + + int valueA; + bool foundA = demoData.Find("A", valueA); + AssertTrue(foundA, "Key \"A\" was not found as expected.", state); + AssertEqual(valueA, INSERT_TWO, "Value for key \"A\" did not equal INSERT_TWO as expected.", state); + + int valueB; + bool foundB = demoData.Find("B", valueB); + AssertTrue(foundB, "Key \"B\" was not found as expected.", state); + AssertEqual(valueB, INSERT_THREE, "Value for key \"B\" did not equal INSERT_THREE as expected.", state); + + int valueC; + bool foundC = demoData.Find("C", valueC); + AssertTrue(foundC, "Key \"C\" was not found as expected.", state); + AssertEqual(valueC, INSERT_FOUR, "Value for key \"C\" did not equal INSERT_FOUR as expected.", state); + + int valueD; + bool foundD = demoData.Find("D", valueD); + AssertTrue(foundD, "Key \"D\" was not found as expected.", state); + AssertEqual(valueD, INSERT_FIVE, "Value for key \"D\" did not equal INSERT_FIVE as expected.", state); + } + BENCHMARK_LOGD("SafeMap testUtilsNormalFeatureIterate001 end."); +} + +/* + * @tc.name: testSafeMapConstructor001 + * @tc.desc: SafeMapConstructor + */ +BENCHMARK_F(BenchmarkSafeMap, testSafeMapConstructor001)(benchmark::State& state) +{ + BENCHMARK_LOGD("SafeMap testSafeMapConstructor001 start."); + while (state.KeepRunning()) { + SafeMap demoData; + + bool result = demoData.Insert("ONE", INSERT_ONE); + AssertEqual(result, true, "result did not equal true as expected.", state); + result = demoData.Insert("TWO", INSERT_TWO); + AssertEqual(result, true, "result did not equal true as expected.", state); + + SafeMap demoData1(demoData); + int valueOne; + int valueTwo; + bool foundOne = demoData1.Find("ONE", valueOne); + bool foundTwo = demoData1.Find("TWO", valueTwo); + AssertTrue(foundOne && foundTwo, "Not all keys were found as expected.", state); + AssertEqual(valueOne, INSERT_ONE, "Value for key \"ONE\" did not match as expected.", state); + AssertEqual(valueTwo, INSERT_TWO, "Value for key \"TWO\" did not match as expected.", state); + } + BENCHMARK_LOGD("SafeMap testSafeMapConstructor001 end."); +} + +/* + * @tc.name: testSafeMapOperator001 + * @tc.desc: SafeMapConstructor + */ +BENCHMARK_F(BenchmarkSafeMap, testSafeMapOperator001)(benchmark::State& state) +{ + BENCHMARK_LOGD("SafeMap testSafeMapOperator001 start."); + while (state.KeepRunning()) { + SafeMap demoData; + + bool result = demoData.Insert("ONE", INSERT_ONE); + AssertEqual(result, true, "result did not equal true as expected.", state); + result = demoData.Insert("TWO", INSERT_TWO); + AssertEqual(result, true, "result did not equal true as expected.", state); + + int valueOne; + int valueTwo; + bool foundOne = demoData.Find("ONE", valueOne); + bool foundTwo = demoData.Find("TWO", valueTwo); + AssertTrue(foundOne, "Key \"ONE\" was not found as expected.", state); + AssertTrue(foundTwo, "Key \"TWO\" was not found as expected.", state); + AssertEqual(valueOne, INSERT_ONE, "Value for key \"ONE\" did not equal INSERT_ONE as expected.", state); + AssertEqual(valueTwo, INSERT_TWO, "Value for key \"TWO\" did not equal INSERT_TWO as expected.", state); + } + BENCHMARK_LOGD("SafeMap testSafeMapOperator001 end."); +} + +/* + * @tc.name: testSafeMapOperator002 + * @tc.desc: SafeMapConstructor + */ +BENCHMARK_F(BenchmarkSafeMap, testSafeMapOperator002)(benchmark::State& state) +{ + BENCHMARK_LOGD("SafeMap testSafeMapOperator002 start."); + while (state.KeepRunning()) { + SafeMap demoData; + + bool result = demoData.Insert("ONE", INSERT_ONE); + AssertEqual(result, true, "result did not equal true as expected.", state); + + SafeMap demoData1; + result = demoData1.Insert("TWO", INSERT_ONE); + AssertEqual(result, true, "result did not equal true as expected.", state); + + int valueDemoData; + int valueDemoData1; + bool foundDemoData = demoData.Find("ONE", valueDemoData); + bool foundDemoData1 = demoData1.Find("TWO", valueDemoData1); + BENCHMARK_LOGD("SafeMap data1:%{public}d data2:%{public}d", foundDemoData, foundDemoData1); + AssertEqual(valueDemoData, valueDemoData1, + "Values for keys \"ONE\" in demoData and demoData1 did not match as expected.", state); + } + BENCHMARK_LOGD("SafeMap testSafeMapOperator002 end."); +} + +/* + * @tc.name: testUtilsConcurrentIterate001 + * @tc.desc: 10 threads test in iterate operation to rewrite a SafeMap. + */ +const int SLEEP_FOR_FIFTY_MILLISECOND = 50; +const int THREAD_NUM = 10; +const int DATA_NUM = 5; +BENCHMARK_F(BenchmarkSafeMap, testUtilsConcurrentIterate001)(benchmark::State& state) +{ + BENCHMARK_LOGD("SafeMap testUtilsConcurrentIterate001 start."); + while (state.KeepRunning()) { + SafeMap demoData; + for (int i = 0; i < DATA_NUM; i++) { + demoData.Insert("A" + std::to_string(i), 0); + } + std::thread threads[THREAD_NUM]; + + auto lamfuncIterate = [](SafeMap& data, const int& cnt, + std::chrono::time_point absTime) { + auto callback_it = [cnt](const string data, int& value) { + value = cnt; + }; + std::this_thread::sleep_until(absTime); + data.Iterate(callback_it); + }; + + auto timeT = std::chrono::high_resolution_clock::now(); + timeT += std::chrono::milliseconds(SLEEP_FOR_FIFTY_MILLISECOND); + + for (int i = 0; i < THREAD_NUM; ++i) { + threads[i] = std::thread(lamfuncIterate, std::ref(demoData), i, timeT); + } + + for (auto& t : threads) { + t.join(); + } + + for (int i = 0; i < DATA_NUM - 1; i++) { + int valueCurrent; + int valueNext; + bool foundCurrent = demoData.Find("A" + std::to_string(i), valueCurrent); + bool foundNext = demoData.Find("A" + std::to_string(i + 1), valueNext); + + AssertTrue(foundCurrent && foundNext, "Not all keys were found as expected.", state); + AssertEqual(valueCurrent, valueNext, + ("Values for keys \"A" + std::to_string(i) + "\" and \"A" + std::to_string(i + 1) + + "\" did not match as expected.").c_str(), state); + } + } + BENCHMARK_LOGD("SafeMap testUtilsConcurrentIterate001 end."); +} + +/* + * @tc.name: testUtilsConcurrentWriteAndRead001 + * @tc.desc: 100 threads test in writein to the same key of the map, while read at same time and no throw + */ +BENCHMARK_F(BenchmarkSafeMap, testUtilsConcurrentWriteAndRead001)(benchmark::State& state) +{ + BENCHMARK_LOGD("SafeMap testUtilsConcurrentWriteAndRead001 start."); + SafeMap demoData; + std::thread threads[THREAD_NUM]; + std::thread checkThread[THREAD_NUM]; + while (state.KeepRunning()) { + auto lamfuncInsert = [](SafeMap& data, const string& key, + const int& value, std::chrono::time_point absTime) { + std::this_thread::sleep_until(absTime); + data.EnsureInsert(key, value); + }; + + auto lamfuncCheck = [](SafeMap& data, const string& key, + std::chrono::time_point absTime) { + std::this_thread::sleep_until(absTime); + thread_local int i = -1; + data.Find(key, i); + }; + + auto timeT = std::chrono::high_resolution_clock::now(); + timeT += std::chrono::milliseconds(SLEEP_FOR_FIFTY_MILLISECOND); + string key("A"); + + for (int i = 0; i < THREAD_NUM; ++i) { + threads[i] = std::thread(lamfuncInsert, std::ref(demoData), key, i, timeT); + checkThread[i] = std::thread(lamfuncCheck, std::ref(demoData), key, timeT); + } + + for (auto& t : threads) { + t.join(); + } + + for (auto& t : checkThread) { + t.join(); + } + } + BENCHMARK_LOGD("SafeMap testUtilsConcurrentWriteAndRead001 end."); +} + +void ClearAllContainer(SafeMap& demoData, std::vector>& vcfi, vector& result) +{ + demoData.Clear(); + result.clear(); + vcfi.clear(); +} + +/* + * @tc.name: testUtilsConcurrentWriteAndFind001 + * @tc.desc: 100 threads test in writein to the corresponding key of the map, + * while read at same time and check the results + */ +BENCHMARK_F(BenchmarkSafeMap, testUtilsConcurrentWriteAndFind001)(benchmark::State& state) +{ + BENCHMARK_LOGD("SafeMap testUtilsConcurrentWriteAndFind001 start."); + SafeMap demoData; + std::thread threads[THREAD_NUM]; + std::vector> vcfi; + while (state.KeepRunning()) { + auto lamfuncInsert = [](SafeMap& data, const string& key, + const int& value, std::chrono::time_point absTime) { + std::this_thread::sleep_until(absTime); + data.EnsureInsert(key, value); + }; + auto lamfuncCheckLoop = [](SafeMap& data, const string& key, + std::chrono::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)); + } + return i; + }; + auto timeT = std::chrono::high_resolution_clock::now(); + timeT += std::chrono::milliseconds(SLEEP_FOR_FIFTY_MILLISECOND); + string key("A"); + for (int i = 0; i < THREAD_NUM; ++i) { + threads[i] = std::thread(lamfuncInsert, std::ref(demoData), + key + std::to_string(i), i, timeT); + vcfi.push_back(std::async(std::launch::async, lamfuncCheckLoop, + std::ref(demoData), key + std::to_string(i), timeT)); + } + 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) { + AssertEqual(i, result[i], "tmp did not equal result[i+10] as expected.", state); + } + ClearAllContainer(demoData, vcfi, result); + } + BENCHMARK_LOGD("SafeMap testUtilsConcurrentWriteAndFind001 end."); +} + +/* + * @tc.name: testUtilsConcurrentWriteAndFindAndSet001 + * @tc.desc: 100 threads test in writein to the corresponding key of the map, + * while findandfix at same time and check the results + */ +BENCHMARK_F(BenchmarkSafeMap, testUtilsConcurrentWriteAndFindAndSet001)(benchmark::State& state) +{ + SafeMap demoData; + std::thread threads[THREAD_NUM]; + std::vector> vcfi; + while (state.KeepRunning()) { + auto lamfuncInsert = [](SafeMap& data, const string& key, + const int& value, std::chrono::time_point absTime) { + std::this_thread::sleep_until(absTime); + data.EnsureInsert(key, value); + }; + auto lamfuncCheckLoop = [](SafeMap& data, const string& key, + const int& newvalue, std::chrono::time_point absTime) { + 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)); + } + return i; + }; + auto timeT = std::chrono::high_resolution_clock::now(); + timeT += std::chrono::milliseconds(SLEEP_FOR_FIFTY_MILLISECOND); + string key("A"); + for (int i = 0; i < THREAD_NUM; ++i) { + threads[i] = std::thread(lamfuncInsert, std::ref(demoData), + key + std::to_string(i), i, timeT); + vcfi.push_back(std::async(std::launch::async, lamfuncCheckLoop, + std::ref(demoData), key + std::to_string(i), i + 1, timeT)); + } + 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) { + AssertEqual(i, result[i], "i did not equal result[i] as expected.", state); + } + result.clear(); + for (int i = 0; i < THREAD_NUM; ++i) { + int t = -1; + AssertTrue((demoData.Find("A" + std::to_string(i), t)), "demoData.Find did not equal true.", state); + result.push_back(t); + } + std::sort(result.begin(), result.end()); + for (int i = 0; i < THREAD_NUM; ++i) { + AssertEqual(i + 1, result[i], "i + 1 did not equal result[i] as expected.", state); + } + ClearAllContainer(demoData, vcfi, result); + } +} +} // namespace +} // namespace OHOS +// Run the benchmark +BENCHMARK_MAIN(); diff --git a/base/test/benchmarktest/singleton_benchmark_test/BUILD.gn b/base/test/benchmarktest/singleton_benchmark_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..e1b4d0b65817e865cefa40724e2fa6848d1d54b9 --- /dev/null +++ b/base/test/benchmarktest/singleton_benchmark_test/BUILD.gn @@ -0,0 +1,35 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") + +module_output_path = "commonlibrary_c_utils/singleton" + +ohos_benchmarktest("SingletonTest") { + module_out_path = module_output_path + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "singleton_benchmark_test.cpp" ] + + deps = [ "//third_party/benchmark:benchmark" ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog_base", + ] +} diff --git a/base/test/benchmarktest/singleton_benchmark_test/singleton_benchmark_test.cpp b/base/test/benchmarktest/singleton_benchmark_test/singleton_benchmark_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dfd89db6ff5f9f411c9093485e9ecf43496c033c --- /dev/null +++ b/base/test/benchmarktest/singleton_benchmark_test/singleton_benchmark_test.cpp @@ -0,0 +1,226 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "singleton.h" +#include +#include +#include +#include "../log.h" +#include "../assert.h" +using namespace std; + +namespace OHOS { +namespace { + +static constexpr long DELAYEDSINGLETON_SP1_USE_COUNT = 2; +static constexpr long DELAYEDSINGLETON_SP2_USE_COUNT = 3; + +class BenchmarkSingletonTest : public benchmark::Fixture { +public: + BenchmarkSingletonTest() + { + Iterations(iterations); + Repetitions(repetitions); + ReportAggregatesOnly(); + } + + ~BenchmarkSingletonTest() override = default; + void SetUp(const ::benchmark::State& state) override + { + } + + void TearDown(const ::benchmark::State& state) override + { + } + +protected: + const int32_t repetitions = 3; + const int32_t iterations = 1000; +}; + +class DelayedSingletonDeclearTest { + DECLARE_DELAYED_SINGLETON(DelayedSingletonDeclearTest); +public: + void* GetObjAddr() + { + BENCHMARK_LOGD("SingletonTest DelayedSingletonDeclearTest void* GetObjAddr is called."); + return static_cast(this); + } +}; + +DelayedSingletonDeclearTest::~DelayedSingletonDeclearTest() {}; +DelayedSingletonDeclearTest::DelayedSingletonDeclearTest() {}; + +class SingletonDeclearTest { + DECLARE_SINGLETON(SingletonDeclearTest); +public: + void* GetObjAddr() + { + BENCHMARK_LOGD("SingletonTest SingletonDeclearTest void* GetObjAddr is called."); + return static_cast(this); + } +}; + +SingletonDeclearTest::~SingletonDeclearTest() {}; +SingletonDeclearTest::SingletonDeclearTest() {}; + +class SingletonTest : public Singleton { +public: + void* GetObjAddr() + { + BENCHMARK_LOGD("SingletonTest SingletonTest void* GetObjAddr is called."); + return static_cast(this); + } +}; + +class DelayedSingletonTest : public DelayedSingleton { +public: + void* GetObjAddr() + { + BENCHMARK_LOGD("SingletonTest DelayedSingletonTest void* GetObjAddr is called."); + return static_cast(this); + } +}; + + +class DelayedRefSingletonDeclearTest { + DECLARE_DELAYED_REF_SINGLETON(DelayedRefSingletonDeclearTest); +public: + void* GetObjAddr() + { + BENCHMARK_LOGD("SingletonTest DelayedRefSingletonDeclearTest void* GetObjAddr is called."); + return static_cast(this); + } +}; + +DelayedRefSingletonDeclearTest::DelayedRefSingletonDeclearTest() {}; +DelayedRefSingletonDeclearTest::~DelayedRefSingletonDeclearTest() {}; + +class DelayedRefSingletonTest : public DelayedRefSingleton { +public: + void* GetObjAddr() + { + BENCHMARK_LOGD("SingletonTest DelayedRefSingletonTest void* GetObjAddr is called."); + return static_cast(this); + } +}; + +BENCHMARK_F(BenchmarkSingletonTest, test_DelayedSingletonDeclearTest)(benchmark::State& state) +{ + BENCHMARK_LOGD("SingletonTest test_DelayedSingletonDeclearTest start."); + while (state.KeepRunning()) { + shared_ptr sp1 = DelayedSingleton::GetInstance(); + AssertEqual(sp1.use_count(), DELAYEDSINGLETON_SP1_USE_COUNT, + "sp1.use_count() did not equal DELAYEDSINGLETON_SP1_USE_COUNT as expected.", state); + + shared_ptr sp2 = DelayedSingleton::GetInstance(); + AssertEqual(sp1->GetObjAddr(), sp2->GetObjAddr(), + "sp1->GetObjAddr() did not equal sp2->GetObjAddr() as expected.", state); + AssertEqual(sp1.get(), sp2.get(), "sp1.get() did not equal sp2.get() as expected.", state); + AssertEqual(sp2.use_count(), DELAYEDSINGLETON_SP2_USE_COUNT, + "sp2.use_count() did not equal DELAYEDSINGLETON_SP2_USE_COUNT as expected.", state); + } + BENCHMARK_LOGD("SingletonTest test_DelayedSingletonDeclearTest end."); +} + +BENCHMARK_F(BenchmarkSingletonTest, test_SingletonDeclearTest)(benchmark::State& state) +{ + BENCHMARK_LOGD("SingletonTest test_SingletonDeclearTest start."); + while (state.KeepRunning()) { + SingletonDeclearTest &st1 = Singleton::GetInstance(); + SingletonDeclearTest &st2 = Singleton::GetInstance(); + AssertEqual(st1.GetObjAddr(), st2.GetObjAddr(), + "st1.GetObjAddr() did not equal st2.GetObjAddr() as expected.", state); + } + BENCHMARK_LOGD("SingletonTest test_SingletonDeclearTest end."); +} + +BENCHMARK_F(BenchmarkSingletonTest, test_SingletonTest)(benchmark::State& state) +{ + BENCHMARK_LOGD("SingletonTest test_SingletonTest start."); + while (state.KeepRunning()) { + SingletonTest &st1 = SingletonTest::GetInstance(); + SingletonTest &st2 = SingletonTest::GetInstance(); + AssertEqual(st1.GetObjAddr(), st2.GetObjAddr(), + "st1.GetObjAddr() did not equal st2.GetObjAddr() as expected.", state); + } + BENCHMARK_LOGD("SingletonTest test_SingletonTest end."); +} + +BENCHMARK_F(BenchmarkSingletonTest, test_DelayedSingletonTest)(benchmark::State& state) +{ + BENCHMARK_LOGD("SingletonTest test_DelayedSingletonTest start."); + while (state.KeepRunning()) { + shared_ptr sp1 = DelayedSingletonTest::GetInstance(); + AssertEqual(sp1.use_count(), DELAYEDSINGLETON_SP1_USE_COUNT, + "sp1.use_count() did not equal DELAYEDSINGLETON_SP1_USE_COUNT as expected.", state); + + shared_ptr sp2 = DelayedSingletonTest::GetInstance(); + AssertEqual(sp1->GetObjAddr(), sp2->GetObjAddr(), + "sp1->GetObjAddr() did not equal sp2->GetObjAddr() as expected.", state); + AssertEqual(sp1.get(), sp2.get(), "sp1.get() did not equal sp2.get() as expected.", state); + AssertEqual(sp2.use_count(), DELAYEDSINGLETON_SP2_USE_COUNT, + "sp2.use_count() did not equal DELAYEDSINGLETON_SP2_USE_COUNT as expected.", state); + } + BENCHMARK_LOGD("SingletonTest test_DelayedSingletonTest end."); +} + +BENCHMARK_F(BenchmarkSingletonTest, test_DelayedRefSingletonTest)(benchmark::State& state) +{ + BENCHMARK_LOGD("SingletonTest test_DelayedRefSingletonTest start."); + while (state.KeepRunning()) { + DelayedRefSingletonTest& p1 = DelayedRefSingletonTest::GetInstance(); + DelayedRefSingletonTest& p2 = DelayedRefSingletonTest::GetInstance(); + AssertEqual(p1.GetObjAddr(), p2.GetObjAddr(), + "p1.GetObjAddr() did not equal p2.GetObjAddr() as expected.", state); + } + BENCHMARK_LOGD("SingletonTest test_DelayedRefSingletonTest end."); +} + +BENCHMARK_F(BenchmarkSingletonTest, test_DelayedRefSingletonDeclearTest)(benchmark::State& state) +{ + BENCHMARK_LOGD("SingletonTest test_DelayedRefSingletonDeclearTest start."); + while (state.KeepRunning()) { + DelayedRefSingletonDeclearTest& p1 = DelayedRefSingleton::GetInstance(); + DelayedRefSingletonDeclearTest& p2 = DelayedRefSingleton::GetInstance(); + AssertEqual(p1.GetObjAddr(), p2.GetObjAddr(), + "p1.GetObjAddr() did not equal p2.GetObjAddr() as expected.", state); + } + BENCHMARK_LOGD("SingletonTest test_DelayedRefSingletonDeclearTest end."); +} + +/** + * @tc.name: test_DelayedSingletonDestroyTest + * @tc.desc: test Singleton Destroy Instance + * @tc.type: FUNC + */ +BENCHMARK_F(BenchmarkSingletonTest, test_DelayedSingletonDestroyTest)(benchmark::State& state) +{ + BENCHMARK_LOGD("SingletonTest test_DelayedSingletonDestroyTest start."); + while (state.KeepRunning()) { + shared_ptr sp1 = DelayedSingleton::GetInstance(); + AssertUnequal(sp1, nullptr, "sp1 equal nullptr as expected.", state); + + sp1.reset(); + DelayedSingleton::DestroyInstance(); + AssertEqual(sp1, nullptr, "sp1 not equal nullptr as expected.", state); + } + BENCHMARK_LOGD("SingletonTest test_DelayedSingletonDestroyTest end."); +} +} // namespace +} // namespace OHOS +// Run the benchmark +BENCHMARK_MAIN(); \ No newline at end of file diff --git a/base/test/benchmarktest/sorted_vector_benchmark_test/BUILD.gn b/base/test/benchmarktest/sorted_vector_benchmark_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..e7e5ba69ec835f79c4f47c1935a84187df874792 --- /dev/null +++ b/base/test/benchmarktest/sorted_vector_benchmark_test/BUILD.gn @@ -0,0 +1,35 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") + +module_output_path = "commonlibrary_c_utils/sorted_vector" + +ohos_benchmarktest("SortedVectorTest") { + module_out_path = module_output_path + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "sorted_vector_benchmark_test.cpp" ] + + deps = [ "//third_party/benchmark:benchmark" ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog_base", + ] +} diff --git a/base/test/benchmarktest/sorted_vector_benchmark_test/sorted_vector_benchmark_test.cpp b/base/test/benchmarktest/sorted_vector_benchmark_test/sorted_vector_benchmark_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c65ba2da9db151d3630d78bf8fff5a0a38a75e9d --- /dev/null +++ b/base/test/benchmarktest/sorted_vector_benchmark_test/sorted_vector_benchmark_test.cpp @@ -0,0 +1,828 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "sorted_vector.h" +#include +#include "../log.h" +#include "../assert.h" +using namespace std; + +namespace OHOS { +namespace { + +static constexpr ssize_t SVECTOR_START_POS_ASSIGN = 0; +static constexpr ssize_t SVECTOR_END_POS_ASSIGN = 9; +static constexpr ssize_t SVECTOR_START_POS_VALUE = 0; +static constexpr ssize_t SVECTOR_END_POS_VALUE = 10; +static constexpr ssize_t SVECTOR_INVALID_VALUE = -1; +const int VECTOR_SIZE_TEN = 10; +const int VECTOR_SIZE_TWENTY = 20; +const int ELEMENT_END_VALUE = 30; +const size_t EXPECTED_SIZE_AFTER_DUPLICATES = 30; + +class BenchmarkSortedVector : public benchmark::Fixture { +public: + BenchmarkSortedVector() + { + Iterations(iterations); + Repetitions(repetitions); + ReportAggregatesOnly(); + } + + ~BenchmarkSortedVector() override = default; + void SetUp(const ::benchmark::State& state) override + { + } + + void TearDown(const ::benchmark::State& state) override + { + } + +protected: + const int32_t repetitions = 3; + const int32_t iterations = 1000; +}; + +BENCHMARK_F(BenchmarkSortedVector, testDefaultConsAndAddAndSort)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testDefaultConsAndAddAndSort start."); + while (state.KeepRunning()) { + SortedVector svec; + std::vector vec; + for (int i = 0; i < VECTOR_SIZE_TEN; i++) { + vec.push_back(i); + } + + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + + auto it = svec.Begin(); + auto it2 = vec.begin(); + for (; (it != svec.End()) and (it2 != vec.end()); it2++, it++) { + AssertEqual(*it, *it2, "*it did not equal *it2 as expected.", state); + } + } + BENCHMARK_LOGD("SortedVector testDefaultConsAndAddAndSort end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testConsFromSortedAllowDup)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testConsFromSortedAllowDup start."); + while (state.KeepRunning()) { + SortedVector svec; + std::vector vec; + for (int i = 0; i < VECTOR_SIZE_TEN; i++) { + vec.push_back(i); + } + + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + + SortedVector newSvec(svec); + auto it = newSvec.Begin(); + auto it2 = vec.begin(); + for (; (it != newSvec.End()) and (it2 != vec.end()); it2++, it++) { + AssertEqual(*it, *it2, "*it did not equal *it2 as expected.", state); + } + } + BENCHMARK_LOGD("SortedVector testConsFromSortedAllowDup end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testConsFromSortedNotAllowDuplicate)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testConsFromSortedNotAllowDuplicate start."); + while (state.KeepRunning()) { + SortedVector svec; + std::vector vec; + for (int i = 0; i < VECTOR_SIZE_TWENTY; i++) { + vec.push_back(i); + } + + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + + for (int i = 0; i < VECTOR_SIZE_TWENTY; i++) { + svec.Add(i); + } + + AssertEqual(static_cast(EXPECTED_SIZE_AFTER_DUPLICATES), svec.Size(), + "static_cast(EXPECTED_SIZE_AFTER_DUPLICATES) did not equal svec.Size() as expected.", state); + + SortedVector newSvec(svec); + AssertEqual(static_cast(VECTOR_SIZE_TWENTY), newSvec.Size(), + "static_cast(VECTOR_SIZE_TWENTY) did not equal newSvec.Size() as expected.", state); + for (int i = 0; i < VECTOR_SIZE_TWENTY; i++) { + AssertEqual(vec[i], newSvec[i], "vec[i] did not equal newSvec[i] as expected.", state); + } + } + BENCHMARK_LOGD("SortedVector testConsFromSortedNotAllowDuplicate end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testConsFromSortedNotAllowToAlloworNotAllow)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testConsFromSortedNotAllowToAlloworNotAllow start."); + while (state.KeepRunning()) { + SortedVector svec; + std::vector vec; + for (int i = 0; i < VECTOR_SIZE_TWENTY; i++) { + vec.push_back(i); + } + + for (int i = SVECTOR_START_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + + for (int i = 0; i < VECTOR_SIZE_TWENTY; i++) { + svec.Add(i); + } + + AssertEqual(static_cast(VECTOR_SIZE_TWENTY), svec.Size(), + "static_cast(VECTOR_SIZE_TWENTY) did not equal svec.Size() as expected.", state); + + SortedVector newSvecTrue(svec); + AssertEqual(static_cast(VECTOR_SIZE_TWENTY), newSvecTrue.Size(), + "static_cast(VECTOR_SIZE_TWENTY) did not equal newSvecTrue.Size() as expected.", state); + for (int i = 0; i < VECTOR_SIZE_TWENTY; i++) { + AssertEqual(vec[i], newSvecTrue[i], "vec[i] did not equal newSvecTrue[i] as expected.", state); + } + + SortedVector newSvecFalse(svec); + AssertEqual(static_cast(VECTOR_SIZE_TWENTY), newSvecFalse.Size(), + "static_cast(VECTOR_SIZE_TWENTY) did not equal newSvecFalse.Size() as expected.", state); + for (int i = 0; i < VECTOR_SIZE_TWENTY; i++) { + AssertEqual(vec[i], newSvecFalse[i], "vec[i] did not equal newSvecFalse[i] as expected.", state); + } + } + BENCHMARK_LOGD("SortedVector testConsFromSortedNotAllowToAlloworNotAllow end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testoperatoreq)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testoperatoreq start."); + while (state.KeepRunning()) { + SortedVector svec; + std::vector vec; + for (int i = 0; i < VECTOR_SIZE_TEN; i++) { + vec.push_back(i); + } + + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + + SortedVector newSvec = svec; + auto it = newSvec.Begin(); + auto it2 = svec.Begin(); + for (; (it != newSvec.End()) and (it2 != svec.End()); it2++, it++) { + AssertEqual(*it, *it2, "*it did not equal *it2 as expected.", state); + } + } + BENCHMARK_LOGD("SortedVector testoperatoreq end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testOperatorEqAllowToNotAllow)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testOperatorEqAllowToNotAllow start."); + while (state.KeepRunning()) { + SortedVector svec; + std::vector vec; + for (int i = 0; i < VECTOR_SIZE_TWENTY; i++) { + vec.push_back(i); + } + + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + + for (int i = 0; i < VECTOR_SIZE_TWENTY; i++) { + svec.Add(i); + } + + AssertEqual(static_cast(EXPECTED_SIZE_AFTER_DUPLICATES), svec.Size(), + "static_cast(EXPECTED_SIZE_AFTER_DUPLICATES) did not equal svec.Size() as expected.", state); + + SortedVector newSvec = svec; + AssertEqual(static_cast(VECTOR_SIZE_TWENTY), newSvec.Size(), + "static_cast(VECTOR_SIZE_TWENTY) did not equal newSvec.Size() as expected.", state); + for (int i = 0; i < VECTOR_SIZE_TWENTY; i++) { + AssertEqual(vec[i], newSvec[i], "vec[i] did not equal newSvec[i] as expected.", state); + } + } + BENCHMARK_LOGD("SortedVector testOperatorEqAllowToNotAllow end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testOperatorEqNotAllowToAllowOrNotAllow)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testOperatorEqNotAllowToAllowOrNotAllow start."); + while (state.KeepRunning()) { + SortedVector svec; + std::vector vec; + for (int i = 0; i < VECTOR_SIZE_TWENTY; i++) { + vec.push_back(i); + } + + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + + for (int i = 0; i < VECTOR_SIZE_TWENTY; i++) { + svec.Add(i); + } + + AssertEqual(static_cast(VECTOR_SIZE_TWENTY), svec.Size(), + "static_cast(VECTOR_SIZE_TWENTY) did not equal svec.Size() as expected.", state); + + SortedVector newSvecFalse = svec; + SortedVector newSvecTrue = svec; + AssertEqual(static_cast(VECTOR_SIZE_TWENTY), newSvecFalse.Size(), + "static_cast(VECTOR_SIZE_TWENTY) did not equal newSvecFalse.Size() as expected.", state); + AssertEqual(static_cast(VECTOR_SIZE_TWENTY), newSvecTrue.Size(), + "static_cast(VECTOR_SIZE_TWENTY) did not equal newSvecTrue.Size() as expected.", state); + for (int i = 0; i < VECTOR_SIZE_TWENTY; i++) { + AssertEqual(vec[i], newSvecFalse[i], "vec[i] did not equal newSvecFalse[i] as expected.", state); + AssertEqual(vec[i], newSvecTrue[i], "vec[i] did not equal newSvecTrue[i] as expected.", state); + } + } + BENCHMARK_LOGD("SortedVector testOperatorEqNotAllowToAllowOrNotAllow end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testOperatorEqAssignmentTwice)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testOperatorEqAssignmentTwice start."); + while (state.KeepRunning()) { + SortedVector svec; + std::vector vec; + for (int i = VECTOR_SIZE_TWENTY; i < ELEMENT_END_VALUE; i++) { + vec.push_back(i); + } + + for (int i = 0; i < VECTOR_SIZE_TWENTY; i++) { + svec.Add(i); + } + + SortedVector newSvecFalse; + for (int i = VECTOR_SIZE_TWENTY; i < ELEMENT_END_VALUE; i++) { + newSvecFalse.Add(i); + } + + svec = newSvecFalse; + AssertEqual(static_cast(VECTOR_SIZE_TEN), svec.Size(), + "static_cast(VECTOR_SIZE_TEN) did not equal svec.Size() as expected.", state); + for (int i = 0; i < VECTOR_SIZE_TEN; i++) { + AssertEqual(vec[i], svec[i], "vec[i] did not equal svec[i] as expected.", state); + } + } + BENCHMARK_LOGD("SortedVector testOperatorEqAssignmentTwice end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testoperatorconsteq)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testoperatorconsteq start."); + while (state.KeepRunning()) { + SortedVector svec; + std::vector vec; + for (int i = 0; i < VECTOR_SIZE_TEN; i++) { + vec.push_back(i); + } + + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + + const SortedVector newSvec = svec; + auto it = newSvec.Begin(); + auto it2 = svec.Begin(); + for (; (it != newSvec.End()) and (it2 != svec.End()); it2++, it++) { + AssertEqual(*it, *it2, "*it did not equal *it2 as expected.", state); + } + } + BENCHMARK_LOGD("SortedVector testoperatorconsteq end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testsizeclearIsEmpty)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testsizeclearIsEmpty start."); + const size_t expectedSizeEmpty = 0; + const size_t expectedSizeAfterAdd = 10; + while (state.KeepRunning()) { + SortedVector svec; + AssertTrue(svec.IsEmpty(), "svec.IsEmpty() did not equal true as expected.", state); + AssertEqual(svec.Size(), static_cast(expectedSizeEmpty), + "svec.Size() did not equal static_cast(expectedSizeEmpty) as expected.", state); + + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + AssertEqual(svec.Size(), static_cast(expectedSizeAfterAdd), + "svec.Size() did not equal static_cast(expectedSizeAfterAdd) as expected.", state); + AssertFalse(svec.IsEmpty(), "svec.IsEmpty() did not equal false as expected.", state); + + svec.Clear(); + AssertTrue(svec.IsEmpty(), "svec.IsEmpty() did not equal true as expected.", state); + AssertEqual(svec.Size(), static_cast(expectedSizeEmpty), + "svec.Size() did not equal static_cast(expectedSizeEmpty) as expected.", state); + } + BENCHMARK_LOGD("SortedVector testsizeclearIsEmpty end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testCapasityandSetcapasity)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testCapasityandSetcapasity start."); + const size_t initialCapacity = 1000; + const size_t reducedCapacity = 500; + while (state.KeepRunning()) { + SortedVector svec; + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + + svec.SetCapcity(initialCapacity); + AssertEqual(svec.Capacity(), static_cast(initialCapacity), + "svec.Capacity() did not equal static_cast(initialCapacity) as expected.", state); + AssertLessThan(svec.SetCapcity(reducedCapacity), static_cast(0), + "svec.SetCapcity(reducedCapacity) was not less than static_cast(0) as expected.", state); + AssertEqual(svec.Capacity(), static_cast(initialCapacity), + "svec.Capacity() did not equal static_cast(initialCapacity) as expected.", state); + } + BENCHMARK_LOGD("SortedVector testCapasityandSetcapasity end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testconstArray)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testconstArray start."); + while (state.KeepRunning()) { + SortedVector svec; + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + + const int* pi = svec.Array(); + int arraySize = svec.Size(); + auto it = svec.Begin(); + int i = 0; + for (; (i < arraySize) && (it != svec.End()); ++it, ++i) { + AssertEqual(pi[i], *it, "pi[i] did not equal *it as expected.", state); + } + + AssertEqual(i, arraySize, "i did not equal arraySize as expected.", state); + AssertEqual(it, svec.End(), "it did not equal svec.End() as expected.", state); + } + BENCHMARK_LOGD("SortedVector testconstArray end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testeditArray)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testeditArray start."); + while (state.KeepRunning()) { + SortedVector svec; + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + + int* pi = svec.EditArray(); + int arraySize = svec.Size(); + // compare and equal + auto it = svec.Begin(); + int i = 0; + for (; (i < arraySize) && (it != svec.End()); ++it, ++i) { + AssertEqual(pi[i], *it, "pi[i] did not equal *it as expected.", state); + } + // size equal + AssertEqual(i, arraySize, "i did not equal arraySize as expected.", state); + AssertEqual(it, svec.End(), "it did not equal svec.End() as expected.", state); + + // fix value + for (int t = 0; t < arraySize; ++t) { + pi[t] += 1; + } + + // compare and equal add 1 + SortedVector copyvec; + it = copyvec.Begin(); + i = 0; + for (; (i < arraySize) && (it != copyvec.End()); ++it, ++i) { + AssertEqual(pi[i], *it + 1, "pi[i] did not equal *it + 1 as expected.", state); + } + + // all renewed + it = svec.Begin(); + i = 0; + for (; (i < arraySize) && (it != svec.End()); ++it, ++i) { + AssertEqual(pi[i], *it, "pi[i] did not equal *it as expected.", state); + } + } + BENCHMARK_LOGD("SortedVector testeditArray end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testIndexOf)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testIndexOf start."); + const int searchValueTen = 10; + const int invalidIndex = -1; + while (state.KeepRunning()) { + SortedVector svec; + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + + int arraySize = svec.Size(); + auto it = svec.Begin(); + int i = 0; + for (; (i < arraySize) && (it != svec.End()); ++it, ++i) { + AssertEqual(i, svec.IndexOf(i), "i did not equal svec.IndexOf(i) as expected.", state); + } + AssertEqual(invalidIndex, svec.IndexOf(searchValueTen), + "invalidIndex did not equal svec.IndexOf(searchValueTen) as expected.", state); + } + BENCHMARK_LOGD("SortedVector testIndexOf end."); +} + + +BENCHMARK_F(BenchmarkSortedVector, testOrderof)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testOrderof start."); + const int searchValueNegTwo = -2; + const int searchValueNegOne = -1; + const int searchValueNine = 9; + const int searchValueTen = 10; + const size_t orderNotFound = 0; + const size_t expectedOrderTen = 10; + while (state.KeepRunning()) { + SortedVector svec; + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + + size_t count = svec.Size(); + size_t order = 0; + for (size_t i = 0; i < count; i++) { + order = svec.OrderOf(i); + AssertEqual((i + 1), order, "i + 1 did not equal order as expected.", state); + } + + AssertEqual(orderNotFound, svec.OrderOf(searchValueNegTwo), + "orderNotFound did not equal svec.OrderOf(searchValueNegTwo) as expected.", state); + AssertEqual(orderNotFound, svec.OrderOf(searchValueNegOne), + "orderNotFound did not equal svec.OrderOf(searchValueNegOne) as expected.", state); + AssertEqual(expectedOrderTen, svec.OrderOf(searchValueNine), + "expectedOrderTen did not equal svec.OrderOf(searchValueNine) as expected.", state); + AssertEqual(expectedOrderTen, svec.OrderOf(searchValueTen), + "expectedOrderTen did not equal svec.OrderOf(searchValueTen) as expected.", state); + } + BENCHMARK_LOGD("SortedVector testOrderof end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testoperatorAccess)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testoperatorAccess start."); + while (state.KeepRunning()) { + SortedVector svec; + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + + for (int i = SVECTOR_START_POS_ASSIGN; i <= SVECTOR_END_POS_ASSIGN; i++) { + AssertEqual(i, svec[i], "i did not equal svec[i] as expected.", state); + } + } + BENCHMARK_LOGD("SortedVector testoperatorAccess end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testBack)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testBack start."); + while (state.KeepRunning()) { + SortedVector svec; + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + + AssertEqual(SVECTOR_END_POS_ASSIGN, svec.Back(), + "SVECTOR_END_POS_ASSIGN did not equal svec.Back() as expected.", state); + } + BENCHMARK_LOGD("SortedVector testBack end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testMirrorItemAt)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testMirrorItemAt start."); + const ssize_t mirrorStartIndex = -1; // Indicating the start index for mirror item test + const ssize_t mirrorEndIndex = -10; // Indicating the end index for mirror item test + const ssize_t mirrorOffset = 10; // Indicating the offset for calculating the mirror item + while (state.KeepRunning()) { + SortedVector svec; + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + + for (ssize_t i = mirrorStartIndex; i >= mirrorEndIndex; i--) { + AssertEqual((i + mirrorOffset), svec.MirrorItemAt(i), + "i + mirrorOffset did not equal svec.MirrorItemAt(i) as expected.", state); + } + + for (ssize_t i = SVECTOR_START_POS_ASSIGN; i <= SVECTOR_END_POS_ASSIGN; i++) { + AssertEqual(i, svec.MirrorItemAt(i), "i did not equal svec.MirrorItemAt(i) as expected.", state); + } + } + BENCHMARK_LOGD("SortedVector testMirrorItemAt end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testEditItemAt)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testEditItemAt start."); + while (state.KeepRunning()) { + SortedVector svec; + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + + for (ssize_t i = SVECTOR_START_POS_ASSIGN; i <= SVECTOR_END_POS_ASSIGN; i++) { + svec.EditItemAt(i) += 1; + AssertEqual((i + 1), svec.EditItemAt(i), "i + 1 did not equal svec.EditItemAt(i) as expected.", state); + } + } + BENCHMARK_LOGD("SortedVector testEditItemAt end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testCopyCtorFromVector)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testCopyCtorFromVector start."); + while (state.KeepRunning()) { + std::vector vec; + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + vec.push_back(i); + } + + SortedVector svec(vec); + for (ssize_t i = SVECTOR_START_POS_ASSIGN; i <= SVECTOR_END_POS_ASSIGN; i++) { + AssertEqual(i, svec[i], "i did not equal svec[i] as expected.", state); + } + } + BENCHMARK_LOGD("SortedVector testCopyCtorFromVector end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testConsFromVectorToNotAllowDuplicate)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testConsFromVectorToNotAllowDuplicate start."); + static constexpr size_t consFromVectorVectorSize = 30; + static constexpr size_t consFromVectorSortedVectorSize = 20; + while (state.KeepRunning()) { + std::vector vec; + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + vec.push_back(i); + } + + for (int i = 19; i >= 0; i--) { + vec.push_back(i); + } + + AssertEqual(static_cast(consFromVectorVectorSize), vec.size(), + "static_cast(consFromVectorVectorSize) did not equal vec.size() as expected.", state); + + SortedVector svec(vec); + AssertEqual(static_cast(consFromVectorSortedVectorSize), svec.Size(), + "static_cast(consFromVectorSortedVectorSize) did not equal svec.Size() as expected.", state); + for (ssize_t i = 0; i <= 19; i++) { + AssertEqual(i, svec[i], "i did not equal svec[i] as expected.", state); + } + } + BENCHMARK_LOGD("SortedVector testConsFromVectorToNotAllowDuplicate end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testMergevector)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testMergevector start."); + static constexpr int mergeStart = 10; // Represents the starting position for adding elements to std::vector + static constexpr int mergeEnd = 20; // Represents the ending position for adding elements to + // std::vector and the expected size of SortedVector + while (state.KeepRunning()) { + SortedVector svec; + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + + std::vector vec; + for (int i = mergeStart; i < mergeEnd; i++) { + vec.push_back(i); + } + + svec.Merge(vec); + + for (ssize_t i = 0; i < mergeEnd; i++) { + AssertEqual(i, svec[i], "i did not equal svec[i] as expected.", state); + } + } + BENCHMARK_LOGD("SortedVector testMergevector end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testMergevectorNoduplicate)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testMergevectorNoduplicate start."); + static constexpr int initialMergeStart = 0; // Represents the starting position for + // the first range of elements added to std::vector + static constexpr int initialMergeEnd = 20; // Represents the ending position for + // the first range of elements added to std::vector + static constexpr int extendedMergeStart = 10; // Represents the starting position for + // the second range of elements added to std::vector + static constexpr int extendedMergeEnd = 30; // Represents the ending position for + // the second range of elements added to std::vector + // and the expected size of SortedVector after merge + while (state.KeepRunning()) { + SortedVector svec; + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + + std::vector vec; + for (int i = initialMergeStart; i < initialMergeEnd; i++) { + vec.push_back(i); + } + + for (int i = extendedMergeStart; i < extendedMergeEnd; i++) { + vec.push_back(i); + } + + svec.Merge(vec); + + AssertEqual(svec.Size(), static_cast(extendedMergeEnd), + "svec.Size() did not equal static_cast(extendedMergeEnd) as expected.", state); + for (ssize_t i = initialMergeStart; i < extendedMergeEnd; i++) { + AssertEqual(i, svec[i], "i did not equal svec[i] as expected.", state); + } + } + BENCHMARK_LOGD("SortedVector testMergevectorNoduplicate end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testMergesortedvectorNoduplicate)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testMergesortedvectorNoduplicate start."); + const size_t expectedSizeAfterMerge = 10; // Expected size of the vector after merging + // two vectors without duplicates + const ssize_t startValueForElements = 0; // Start value of elements to be added to the vector + const ssize_t endValueForElements = 9; // End value of elements to be added to the vector + while (state.KeepRunning()) { + SortedVector svec; + SortedVector svec2; + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + svec2.Add(i); + } + + svec.Merge(svec2); + + AssertEqual(static_cast(expectedSizeAfterMerge), svec.Size(), + "Expected size after merge did not equal svec.Size().", state); + // 0,1,2,3,4,5,6,7,8,9 + for (ssize_t i = startValueForElements; i <= endValueForElements; i++) { + AssertEqual(i, svec[i], "Element value did not match expected value.", state); + } + } + BENCHMARK_LOGD("SortedVector testMergesortedvectorNoduplicate end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testMergesortedvector)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testMergesortedvector start."); + static constexpr ssize_t expectedMergedSize = 20; // Represents the expected size of SortedVector after merge + static constexpr int duplicateFactor = 2; // Represents the factor of duplication for each element in SortedVector + while (state.KeepRunning()) { + SortedVector svec; + SortedVector svec2; + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + svec2.Add(i); + } + + svec.Merge(svec2); + + // 0,0,1,1,2,2,3,3... + for (ssize_t i = 0; i < expectedMergedSize; i++) { + AssertEqual((i / duplicateFactor), svec[i], + "(i / duplicateFactor) did not equal svec[i] as expected.", state); + } + } + BENCHMARK_LOGD("SortedVector testMergesortedvector end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testAddNotAllowDuplicate)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testAddNotAllowDuplicate start."); + static constexpr int addNotAllowedSvectorSize = 10; + while (state.KeepRunning()) { + SortedVector svec; + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + AssertUnequal(svec.Add(i), static_cast(SVECTOR_INVALID_VALUE), + "svec.Add(i) was not different from static_cast(-1) as expected.", state); + } + AssertEqual(static_cast(addNotAllowedSvectorSize), svec.Size(), + "static_cast(addNotAllowedSvectorSize) did not equal svec.Size() as expected.", state); + + // duplicate + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + AssertEqual(svec.Add(i), static_cast(SVECTOR_INVALID_VALUE), + "svec.Add(i) did not equal static_cast(-1) as expected.", state); + AssertEqual(static_cast(addNotAllowedSvectorSize), svec.Size(), + "static_cast(addNotAllowedSvectorSize) did not equal svec.Size() as expected.", state); + } + + for (ssize_t i = SVECTOR_START_POS_VALUE; i < SVECTOR_END_POS_VALUE; i++) { + AssertEqual(i, svec[i], "i did not equal svec[i] as expected.", state); + } + } + BENCHMARK_LOGD("SortedVector testAddNotAllowDuplicate end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testConsVectorAllowDuplicate)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testConsVectorAllowDuplicate start."); + while (state.KeepRunning()) { + SortedVector svec; + std::vector vec; + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + vec.push_back(i); + svec.Add(i); + } + + SortedVector newSvec(vec); + auto it = newSvec.Begin(); + auto it2 = svec.Begin(); + for (; (it != newSvec.End()) and (it2 != vec.end()); it2++, it++) { + AssertEqual(*it, *it2, "*it did not equal *it2 as expected.", state); + } + } + BENCHMARK_LOGD("SortedVector testConsVectorAllowDuplicate end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testFront)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testFront start."); + static constexpr int frontSvectorFrontValue = 0; + while (state.KeepRunning()) { + SortedVector svec; + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + + AssertEqual(svec.Front(), frontSvectorFrontValue, "svec.Front() did not equal 0 as expected.", state); + } + BENCHMARK_LOGD("SortedVector testFront end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testPopBack)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testPopBack start."); + static constexpr int popBackSvectorBackValue = 8; + static constexpr size_t popBackSvectorSize = 9; + while (state.KeepRunning()) { + SortedVector svec; + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + + svec.PopBack(); + AssertEqual(svec.Back(), popBackSvectorBackValue, "svec.Back() did not equal 8 as expected.", state); + AssertEqual(svec.Size(), popBackSvectorSize, "svec.Size() did not equal 9 as expected.", state); + } + BENCHMARK_LOGD("SortedVector testPopBack end."); +} + +BENCHMARK_F(BenchmarkSortedVector, testErase)(benchmark::State& state) +{ + BENCHMARK_LOGD("SortedVector testErase start."); + const size_t erasePosition = 5; + const int expectedValueAtErasePosition = 6; + const size_t expectedSizeAfterErase = 9; + while (state.KeepRunning()) { + SortedVector svec; + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + + svec.Erase(erasePosition); + AssertEqual(svec[erasePosition], expectedValueAtErasePosition, + "svec[erasePosition] did not equal expectedValueAtErasePosition as expected.", state); + AssertEqual(svec.Size(), expectedSizeAfterErase, + "svec.Size() did not equal expectedSizeAfterErase as expected.", state); + } + BENCHMARK_LOGD("SortedVector testErase end."); +} +} // namespace +} // namespace OHOS +// Run the benchmark +BENCHMARK_MAIN(); \ No newline at end of file