upgraded StackTest, test OK!

This commit is contained in:
HuYingzhuo(hugo/hyzboy) 2023-08-09 17:14:54 +08:00
parent dbf6113742
commit a7e21c9ddf
2 changed files with 133 additions and 20 deletions

View File

@ -21,13 +21,6 @@ endmacro()
add_executable(HalfFloatTest datatype/HalfFloatTest.cpp) add_executable(HalfFloatTest datatype/HalfFloatTest.cpp)
cm_example_project("DataType" HalfFloatTest) cm_example_project("DataType" HalfFloatTest)
add_executable(LifetimeTest datatype/LifetimeTest.cpp)
#cm_example_project("DataType" LifetimeTest)
set_property(TARGET LifetimeTest PROPERTY FOLDER "CM/Examples/DataType")
add_executable(CollectionTest datatype/CollectionTest.cpp)
cm_example_project("DataType" CollectionTest)
add_executable(SplitStringTest datatype/SplitStringTest.cpp) add_executable(SplitStringTest datatype/SplitStringTest.cpp)
cm_example_project("DataType" SplitStringTest) cm_example_project("DataType" SplitStringTest)
@ -53,17 +46,26 @@ cm_example_project("DataType/RAM" RuntimeAssetManagerTest)
add_executable(Size2Test datatype/Size2Test.cpp) add_executable(Size2Test datatype/Size2Test.cpp)
cm_example_project("DataType" Size2Test) cm_example_project("DataType" Size2Test)
add_executable(DataArrayTest datatype/DataArrayTest.cpp)
cm_example_project("DataType" DataArrayTest)
add_executable(Uint2HexStrTest datatype/Uint2HexStrTest.cpp) add_executable(Uint2HexStrTest datatype/Uint2HexStrTest.cpp)
cm_example_project("DataType" Uint2HexStrTest) cm_example_project("DataType" Uint2HexStrTest)
####################################################################################################
add_executable(LifetimeTest datatype/LifetimeTest.cpp)
#cm_example_project("DataType" LifetimeTest)
set_property(TARGET LifetimeTest PROPERTY FOLDER "CM/Examples/DataType/DataArray")
add_executable(CollectionTest datatype/CollectionTest.cpp)
cm_example_project("DataType/DataArray" CollectionTest)
add_executable(DataArrayTest datatype/DataArrayTest.cpp)
cm_example_project("DataType/DataArray" DataArrayTest)
add_executable(StackTest datatype/StackTest.cpp) add_executable(StackTest datatype/StackTest.cpp)
cm_example_project("DataType" StackTest) set_property(TARGET StackTest PROPERTY FOLDER "CM/Examples/DataType/DataArray")
add_executable(QueueTest datatype/QueueTest.cpp) add_executable(QueueTest datatype/QueueTest.cpp)
set_property(TARGET QueueTest PROPERTY FOLDER "CM/Examples/DataType") set_property(TARGET QueueTest PROPERTY FOLDER "CM/Examples/DataType/DataArray")
#################################################################################################### ####################################################################################################

View File

@ -1,11 +1,15 @@
#include<hgl/type/Stack.H> #include<hgl/type/Stack.h>
#include<iostream> #include<iostream>
using namespace hgl; using namespace hgl;
using namespace std; using namespace std;
void TestStack() void TestStackOrdered()
{ {
cout<<endl;
cout<<"-----------------------------------------"<<endl;
cout<<"Stack Ordered Test"<<endl<<endl;
Stack<int> tab; Stack<int> tab;
int i; int i;
@ -30,17 +34,110 @@ void TestStack()
cout<<"Stack Count: "<<tab.GetCount()<<endl; cout<<"Stack Count: "<<tab.GetCount()<<endl;
} }
void TestStackUnordered()
{
cout<<endl;
cout<<"-----------------------------------------"<<endl;
cout<<"Stack Unordered Test"<<endl<<endl;
Stack<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);
}
}
}
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;
cout<<"-----------------------------------------"<<endl;
cout<<"Stack Struct Test"<<endl<<endl;
Stack<UserInfo> ui_queue;
for(uint i=0;i<sizeof(ui_array)/sizeof(UserInfo);i++)
ui_queue.Push(ui_array[i]);
cout<<"Stack Count: "<<ui_queue.GetCount()<<endl;
for(uint i=0;i<sizeof(ui_array)/sizeof(UserInfo);i++)
{
UserInfo ui;
ui_queue.Pop(ui);
cout<<i<<": "<<ui.name<<(ui.sex?" male ":" female ")<<ui.age<<" age."<<endl;
}
}
class StackTestObject class StackTestObject
{ {
int val; int val;
public: public:
StackTestObject(int v) StackTestObject(){val=-1;}
void Set(int v)
{ {
val=v; val=v;
cout<<"StackTestObject "<<val<<endl; cout<<"StackTestObject::Set "<<val<<endl;
} }
~StackTestObject() ~StackTestObject()
@ -51,18 +148,26 @@ public:
void TestObjectStack() void TestObjectStack()
{ {
cout<<endl;
cout<<"-----------------------------------------"<<endl;
cout<<"Stack Object Test"<<endl<<endl;
ObjectStack<StackTestObject> tab; ObjectStack<StackTestObject> tab;
int i; int i;
for(i=0;i<10;i++) for(i=0;i<10;i++)
{ {
tab.Push(new StackTestObject(i)); StackTestObject *obj=new StackTestObject;
obj->Set(i);
tab.Push(obj);
} }
cout<<"Stack Count: "<<tab.GetCount()<<endl; cout<<"Stack Count: "<<tab.GetCount()<<endl;
for(i=0;i<10;i++) for(i=0;i<5;i++) //只取出5个,剩几个给自动清理处理
{ {
StackTestObject *obj=tab.Pop(); StackTestObject *obj=tab.Pop();
@ -75,10 +180,16 @@ void TestObjectStack()
int os_main(int,os_char **) int os_main(int,os_char **)
{ {
TestStack(); srand(time(nullptr));
cout<<"------------------------"<<endl; //原生单个数据测试
TestStackOrdered();
TestStackUnordered();
//原生结构体测试
TestStackStruct();
//对象测试
TestObjectStack(); TestObjectStack();
return(0); return(0);