From fe289163361f4aa37f8c2eaf4310338eed1cd98c Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Thu, 10 Aug 2023 12:02:03 +0800 Subject: [PATCH] added PoolTest.cpp --- CMakeLists.txt | 7 +- datatype/PoolTest.cpp | 156 +++++++++++++++++++++++++++++++++++++++++ datatype/QueueTest.cpp | 41 +---------- datatype/StackTest.cpp | 41 +---------- datatype/UserInfo.h | 41 +++++++++++ 5 files changed, 204 insertions(+), 82 deletions(-) create mode 100644 datatype/PoolTest.cpp create mode 100644 datatype/UserInfo.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 547864c..bbed65f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,10 +56,10 @@ add_executable(LifetimeTest datatype/LifetimeTest.cpp) set_property(TARGET LifetimeTest PROPERTY FOLDER "CM/Examples/DataType/DataArray") add_executable(CollectionTest datatype/CollectionTest.cpp) -cm_example_project("DataType/DataArray" CollectionTest) +set_property(TARGET CollectionTest PROPERTY FOLDER "CM/Examples/DataType/DataArray") add_executable(DataArrayTest datatype/DataArrayTest.cpp) -cm_example_project("DataType/DataArray" DataArrayTest) +set_property(TARGET DataArrayTest PROPERTY FOLDER "CM/Examples/DataType/DataArray") add_executable(StackTest datatype/StackTest.cpp) set_property(TARGET StackTest PROPERTY FOLDER "CM/Examples/DataType/DataArray") @@ -67,6 +67,9 @@ set_property(TARGET StackTest PROPERTY FOLDER "CM/Examples/DataType/DataArray") add_executable(QueueTest datatype/QueueTest.cpp) set_property(TARGET QueueTest PROPERTY FOLDER "CM/Examples/DataType/DataArray") +add_executable(PoolTest datatype/PoolTest.cpp) +set_property(TARGET PoolTest PROPERTY FOLDER "CM/Examples/DataType/DataArray") + #################################################################################################### add_executable(FixFilenameTest filesystem/FixFilenameTest.cpp) diff --git a/datatype/PoolTest.cpp b/datatype/PoolTest.cpp new file mode 100644 index 0000000..24f7c66 --- /dev/null +++ b/datatype/PoolTest.cpp @@ -0,0 +1,156 @@ +#include +#include +#include"UserInfo.h" + +using namespace hgl; +using namespace std; + +void ShowUserInfoArray(const DataArray &ua) +{ + cout<<"user info array "< pool; + + //添加所有的人物数据到池中 + { + for(auto ui:ui_array) + pool.Append(ui); + } + + ShowUserInfoArray(pool.GetActiveArray()); + + //随机释放几个(仅闲置,并不释放内存) + { + int count=10+rand()%10; + + for(int i=0;i &ua) +{ + cout<<"user info array "<GetName();first=false;} + else cout<<','<GetName(); + } + + cout<<"]"< pool; + + List release_list; + + //添加所有的人物数据到池中 + { + for(auto ui:ui_array) + { + UserInfoClass *uic=new UserInfoClass; + + uic->Set(ui); + + pool.Append(uic); + + if(rand()%3==1) //有1/3的概率,将这个数据放入释放列表 + release_list.Add(uic); + } + } + + ShowUserInfoArray(pool.GetActiveArray()); + + //释放刚刚记录的几个(仅闲置,并不释放内存) + pool.Release(release_list.GetData(),release_list.GetCount()); + + ShowUserInfoArray(pool.GetActiveArray()); + + cout<<"idle count: "<GetName()<<" from idle."< #include +#include"UserInfo.h" using namespace hgl; using namespace std; @@ -62,46 +63,6 @@ void TestQueueUnordered() } } -struct UserInfo -{ - char name[32]; - bool sex; - int age; -}; - -static UserInfo ui_array[]= -{ - //注: 以下人物信息由Github Copilot自动创建 - //ps: The following list of character information is automatically created by Github Copilot - - {"Adloph",true,18}, - {"Bella",false,19}, - {"Cindy",false,20}, - {"David",true,21}, - {"Elsa",false,22}, - {"Frank",true,23}, - {"Gina",false,24}, - {"Helen",false,25}, - {"Ivan",true,26}, - {"Jack",true,27}, - {"Kitty",false,28}, - {"Lily",false,29}, - {"Mike",true,30}, - {"Nancy",false,31}, - {"Owen",true,32}, - {"Peter",true,33}, - {"Queen",false,34}, - {"Robert",true,35}, - {"Sunny",false,36}, - {"Tom",true,37}, - {"Uma",false,38}, - {"Vivian",false,39}, - {"Wendy",false,40}, - {"Xavier",true,41}, - {"Yoyo",false,42}, - {"Zack",true,43} -}; - void TestQueueStruct() { cout< #include +#include"UserInfo.h" using namespace hgl; using namespace std; @@ -62,46 +63,6 @@ void TestStackUnordered() } } -struct UserInfo -{ - char name[32]; - bool sex; - int age; -}; - -static UserInfo ui_array[]= -{ - //注: 以下人物信息由Github Copilot自动创建 - //ps: The following list of character information is automatically created by Github Copilot - - {"Adloph",true,18}, - {"Bella",false,19}, - {"Cindy",false,20}, - {"David",true,21}, - {"Elsa",false,22}, - {"Frank",true,23}, - {"Gina",false,24}, - {"Helen",false,25}, - {"Ivan",true,26}, - {"Jack",true,27}, - {"Kitty",false,28}, - {"Lily",false,29}, - {"Mike",true,30}, - {"Nancy",false,31}, - {"Owen",true,32}, - {"Peter",true,33}, - {"Queen",false,34}, - {"Robert",true,35}, - {"Sunny",false,36}, - {"Tom",true,37}, - {"Uma",false,38}, - {"Vivian",false,39}, - {"Wendy",false,40}, - {"Xavier",true,41}, - {"Yoyo",false,42}, - {"Zack",true,43} -}; - void TestStackStruct() { cout<