158 lines
2.7 KiB
C++
158 lines
2.7 KiB
C++
#include<hgl/type/Queue.h>
|
|
#include<iostream>
|
|
#include"UserInfo.h"
|
|
|
|
using namespace hgl;
|
|
using namespace std;
|
|
|
|
void TestQueueOrdered()
|
|
{
|
|
cout<<endl;
|
|
cout<<"-----------------------------------------"<<endl;
|
|
cout<<"Queue Ordered Test"<<endl<<endl;
|
|
|
|
Queue<int> tab;
|
|
int i;
|
|
|
|
for(i=0;i<10;i++)
|
|
{
|
|
cout<<"push "<<i<<endl;
|
|
|
|
tab.Push(i);
|
|
}
|
|
|
|
cout<<"Queue Count: "<<tab.GetCount()<<endl;
|
|
|
|
for(i=0;i<10;i++)
|
|
{
|
|
int val;
|
|
|
|
tab.Pop(val);
|
|
|
|
cout<<"pop "<<val<<endl;
|
|
}
|
|
|
|
cout<<"Queue Count: "<<tab.GetCount()<<endl;
|
|
}
|
|
|
|
void TestQueueUnordered()
|
|
{
|
|
cout<<endl;
|
|
cout<<"-----------------------------------------"<<endl;
|
|
cout<<"Queue Unordered Test"<<endl<<endl;
|
|
|
|
Queue<int> tab;
|
|
int i;
|
|
int val;
|
|
|
|
for(i=0;i<20;i++)
|
|
{
|
|
if(rand()&1)
|
|
{
|
|
if(tab.Pop(val))
|
|
cout<<"pop "<<val<<endl;
|
|
else
|
|
cout<<"pop error"<<endl;
|
|
}
|
|
else
|
|
{
|
|
cout<<"push "<<i<<endl;
|
|
|
|
tab.Push(i);
|
|
}
|
|
}
|
|
}
|
|
|
|
void TestQueueStruct()
|
|
{
|
|
cout<<endl;
|
|
cout<<"-----------------------------------------"<<endl;
|
|
cout<<"Queue Struct Test"<<endl<<endl;
|
|
|
|
Queue<UserInfo> ui_queue;
|
|
|
|
for(uint i=0;i<sizeof(user_info_array)/sizeof(UserInfo);i++)
|
|
ui_queue.Push(user_info_array[i]);
|
|
|
|
cout<<"Queue Count: "<<ui_queue.GetCount()<<endl;
|
|
|
|
for(uint i=0;i<sizeof(user_info_array)/sizeof(UserInfo);i++)
|
|
{
|
|
UserInfo ui;
|
|
|
|
ui_queue.Pop(ui);
|
|
|
|
cout<<i<<": "<<ui.name<<(ui.sex?" male ":" female ")<<ui.age<<" age."<<endl;
|
|
}
|
|
}
|
|
|
|
class QueueTestObject
|
|
{
|
|
int val;
|
|
|
|
public:
|
|
|
|
QueueTestObject(){val=-1;}
|
|
|
|
void Set(int v)
|
|
{
|
|
val=v;
|
|
|
|
cout<<"QueueTestObject::Set "<<val<<endl;
|
|
}
|
|
|
|
~QueueTestObject()
|
|
{
|
|
cout<<"~QueueTestObject "<<val<<endl;
|
|
}
|
|
};
|
|
|
|
void TestObjectQueue()
|
|
{
|
|
cout<<endl;
|
|
cout<<"-----------------------------------------"<<endl;
|
|
cout<<"Queue Object Test"<<endl<<endl;
|
|
|
|
ObjectQueue<QueueTestObject> tab;
|
|
|
|
int i;
|
|
|
|
for(i=0;i<10;i++)
|
|
{
|
|
QueueTestObject *obj=new QueueTestObject;
|
|
|
|
obj->Set(i);
|
|
|
|
tab.Push(obj);
|
|
}
|
|
|
|
cout<<"Queue Count: "<<tab.GetCount()<<endl;
|
|
|
|
for(i=0;i<5;i++) //只取出5个,剩几个给自动清理处理
|
|
{
|
|
QueueTestObject *obj;
|
|
|
|
if(tab.Pop(obj))
|
|
delete obj;
|
|
}
|
|
|
|
cout<<"Queue Count: "<<tab.GetCount()<<endl;
|
|
}
|
|
|
|
int os_main(int,os_char **)
|
|
{
|
|
srand(time(nullptr));
|
|
|
|
//原生单个数据测试
|
|
TestQueueOrdered();
|
|
TestQueueUnordered();
|
|
|
|
//原生结构体测试
|
|
TestQueueStruct();
|
|
|
|
//对象测试
|
|
TestObjectQueue();
|
|
|
|
return(0);
|
|
}
|