added DataChainTest2.cpp and then it tested a bug in DataChain class.

This commit is contained in:
hyzboy 2024-03-25 00:34:45 +08:00
parent 1a2a6a1be3
commit b5153faf6b
3 changed files with 141 additions and 7 deletions

View File

@ -76,6 +76,9 @@ set_example_project_folder("DataType/DataArray" StackPoolTest)
add_executable(DataChainTest datatype/DataChainTest.cpp) add_executable(DataChainTest datatype/DataChainTest.cpp)
cm_example_project("DataType/DataArray" DataChainTest) cm_example_project("DataType/DataArray" DataChainTest)
add_executable(DataChainTest2 datatype/DataChainTest2.cpp)
cm_example_project("DataType/DataArray" DataChainTest2)
#################################################################################################### ####################################################################################################
add_executable(1_ActiveIDManagerTest datatype/ActiveIDManagerTest.cpp) add_executable(1_ActiveIDManagerTest datatype/ActiveIDManagerTest.cpp)

View File

@ -58,16 +58,16 @@ int os_main(int,os_char **)
dc.Release(un[0]); dc.Release(un[0]);
out_data_chain(&dc); out_data_chain(&dc);
cout<<"Release 5 "; cout<<"Release 7 ";
dc.Release(un[5]); dc.Release(un[7]);
out_data_chain(&dc); out_data_chain(&dc);
cout<<"Release 9 "; cout<<"Release 9 ";
dc.Release(un[9]); dc.Release(un[9]);
out_data_chain(&dc); out_data_chain(&dc);
cout<<"Release 6 "; cout<<"Release 4 ";
dc.Release(un[6]); dc.Release(un[4]);
out_data_chain(&dc); out_data_chain(&dc);
cout<<"---------------------------------------------------------------------------------"<<endl; cout<<"---------------------------------------------------------------------------------"<<endl;
@ -75,13 +75,14 @@ int os_main(int,os_char **)
DataChain::UserNode *ud20=dc.Acquire(20); DataChain::UserNode *ud20=dc.Acquire(20);
out_data_chain(&dc); out_data_chain(&dc);
cout<<"Release 2 ";
dc.Release(un[2]);
out_data_chain(&dc);
cout<<"Release 3 "; cout<<"Release 3 ";
dc.Release(un[3]); dc.Release(un[3]);
out_data_chain(&dc); out_data_chain(&dc);
cout<<"Release 2 ";
dc.Release(un[2]);
out_data_chain(&dc);
cout<<"Acquire 15 "; cout<<"Acquire 15 ";
DataChain::UserNode *ud15=dc.Acquire(15); DataChain::UserNode *ud15=dc.Acquire(15);
out_data_chain(&dc); out_data_chain(&dc);

130
datatype/DataChainTest2.cpp Normal file
View File

@ -0,0 +1,130 @@
#include<hgl/type/DataChain.h>
#include<hgl/type/List.h>
#include<iostream>
#include<iomanip>
#include<random>
using namespace std;
using namespace hgl;
void out_data_chain(DataChain *dc)
{
DataChain::ChainNode *start =dc->GetStartNode();
DataChain::ChainNode *end =dc->GetEndNode();
DataChain::ChainNode *node =start;
cout<<"Data Chain: ";
while(node)
{
cout<<"["<<setw(3)<<node->start<<","<<setw(3)<<node->count<<"]";
if(node==end)
break;
node=node->next;
}
cout<<endl;
}
int os_main(int,os_char **)
{
constexpr const int BLOCK_SIZE=100;
DataChain dc(BLOCK_SIZE); ///数据链管理器
cout<<"DataChain Test"<<endl;
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<int> dis(1,BLOCK_SIZE);
uniform_int_distribution<int> dis_un_count(BLOCK_SIZE/10,BLOCK_SIZE/4);
uniform_int_distribution<int> dis_block_count(1,BLOCK_SIZE/10);
List<DataChain::UserNode *> user_node_list;
int free_count=dc.GetFreeCount();
do
{
int ubc=hgl_min(dis_block_count(gen),free_count);
DataChain::UserNode *un=dc.Acquire(ubc);
if(!un) //失败了
{
cout<<"Acquire UserNode Error! FreeCount "<<free_count<<endl;
break;
}
else
{
free_count-=ubc;
cout<<"Acquire UserNode: ["<<setw(3)<<un->GetStart()<<","<<setw(3)<<un->GetCount()<<"] ";
}
out_data_chain(&dc);
user_node_list.Add(un);
}while(free_count>0);
bernoulli_distribution dis_op(0.5); //使用伯努利分布随机数产生操作类型50%的概率是Acquire50%的概率是Release
const int top_op_count=dis_un_count(gen); //最上层操作次数
int op_run_count=0; //执行成功的次数
do
{
bool op=dis_op(gen); //这次是acquire还是release
int op_count=dis_un_count(gen); //操作数量
if(op) //acquire
{
op_count=hgl_min(op_count,dc.GetFreeCount());
if(op_count<=0)
continue;
DataChain::UserNode *un=dc.Acquire(op_count);
if(un)
{
cout<<"Acquire ["<<setw(3)<<un->GetStart()<<","<<setw(3)<<un->GetCount()<<"] ";
user_node_list.Add(un);
}
else
{
cout<<"Acquire "<<op_count<<" blocks failed!"<<endl;
continue;
}
}
else //release
{
int pos=dis(gen)%user_node_list.GetCount();
DataChain::UserNode *un;
user_node_list.Get(pos,un);
if(dc.Release(un))
{
cout<<"Release ["<<setw(3)<<un->GetStart()<<","<<setw(3)<<un->GetCount()<<"] ";
user_node_list.Delete(pos);
++op_run_count;
}
else
{
cout<<"Release UserNode "<<pos<<" failed!"<<endl;
continue;
}
}
out_data_chain(&dc);
++op_run_count;
}while(op_run_count<top_op_count); //执行成功次数满了才能退出,失败的次数不算
return 0;
}