added PoolTest.cpp

This commit is contained in:
HuYingzhuo(hugo/hyzboy) 2023-08-10 12:02:03 +08:00
parent a7e21c9ddf
commit fe28916336
5 changed files with 204 additions and 82 deletions

View File

@ -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)

156
datatype/PoolTest.cpp Normal file
View File

@ -0,0 +1,156 @@
#include<hgl/type/Pool.h>
#include<iostream>
#include"UserInfo.h"
using namespace hgl;
using namespace std;
void ShowUserInfoArray(const DataArray<UserInfo> &ua)
{
cout<<"user info array "<<ua.GetCount()<<" ";
bool first=true;
for(auto &ui:ua)
{
if(first){cout<<'['<<ui.name;first=false;}
else cout<<','<<ui.name;
}
cout<<"]"<<endl;
};
void StructPoolTest()
{
Pool<UserInfo> pool;
//添加所有的人物数据到池中
{
for(auto ui:ui_array)
pool.Append(ui);
}
ShowUserInfoArray(pool.GetActiveArray());
//随机释放几个(仅闲置,并不释放内存)
{
int count=10+rand()%10;
for(int i=0;i<count;i++)
pool.Release(ui_array[rand()%26]);
}
ShowUserInfoArray(pool.GetActiveArray());
cout<<"idle count: "<<pool.GetInactiveCount()<<endl;
//取出所有闲置数据
{
int i=0;
UserInfo ui;
while(pool.Get(ui))
cout<<i++<<": get "<<ui.name<<" from idle."<<endl;
}
}
class UserInfoClass
{
UserInfo info;
public:
UserInfoClass()=default;
void Set(const UserInfo &ui)
{
memcpy(&info,&ui,sizeof(UserInfo));
cout<<"UserInfoClass::Set("<<info.name<<")"<<endl;
}
~UserInfoClass()
{
cout<<"~UserInfoClass("<<info.name<<")"<<endl;
}
const char *GetName()const{return info.name;}
};
void ShowUserInfoArray(const DataArray<UserInfoClass *> &ua)
{
cout<<"user info array "<<ua.GetCount()<<" [";
bool first=true;
for(auto *uic:ua)
{
if(first){cout<<'['<<uic->GetName();first=false;}
else cout<<','<<uic->GetName();
}
cout<<"]"<<endl;
};
void ObjectPoolTest()
{
ObjectPool<UserInfoClass> pool;
List<UserInfoClass *> 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: "<<pool.GetInactiveCount()<<endl;
//取出闲置列表中的一半数据
{
int i=0;
UserInfoClass *uic;
for(int i=0;i<release_list.GetCount()/2;i++) //取闲置列表中的一半数据
if(pool.Get(uic))
cout<<i<<": get "<<uic->GetName()<<" from idle."<<endl;
}
//剩下的一半留给自动释放
cout<<"finished current function, and then auto release."<<endl;
//原本是自动,我们这里手写调用一下,方便输出调试信息。
//以下代码等同ObjectPool::~ObjectPool()
{
cout<<"clear active "<<pool.GetActiveCount()<<endl;
pool.ClearActive();
cout<<"clear inactive "<<pool.GetInactiveCount()<<endl;
pool.ClearInactive();
}
}
void main()
{
srand(time(nullptr));
StructPoolTest();
ObjectPoolTest();
}

View File

@ -1,5 +1,6 @@
#include<hgl/type/Queue.h>
#include<iostream>
#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<<endl;

View File

@ -1,5 +1,6 @@
#include<hgl/type/Stack.h>
#include<iostream>
#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<<endl;

41
datatype/UserInfo.h Normal file
View File

@ -0,0 +1,41 @@
#pragma once
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}
};