From 72f06fc2577ca1fe4a0ef99c1df7096b50d827b3 Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Tue, 1 Aug 2023 19:36:38 +0800 Subject: [PATCH] added QueueTest.cpp and LifetimeTest.cpp --- datatype/LifetimeTest.cpp | 184 ++++++++++++++++++++++++++++++++++++++ datatype/QueueTest.cpp | 85 ++++++++++++++++++ 2 files changed, 269 insertions(+) create mode 100644 datatype/LifetimeTest.cpp create mode 100644 datatype/QueueTest.cpp diff --git a/datatype/LifetimeTest.cpp b/datatype/LifetimeTest.cpp new file mode 100644 index 0000000..3aab504 --- /dev/null +++ b/datatype/LifetimeTest.cpp @@ -0,0 +1,184 @@ +#include + +#include +#include + +using namespace hgl; + +template class TestArray +{ + TCB *life_cb; + + T *items; + int count; + int alloc_count; + +private: + + void Realloc(int new_count) + { + if(new_count *cb) + { + life_cb=cb; + + items=nullptr; + count=0; + } + + ~TestArray() + { + if(items)free(items); + } + + T *Create() + { + int off=count; + + ++count; + + Realloc(count); + + T *p=items+off; + + life_cb->Create(p); + + return p; + } + + void Add(T *data,const int num) + { + int off=count; + + count+=num; + + Realloc(count); + + life_cb->Copy(items+off,data,num); + } + + void Add(T data) + { + Add(&data,1); + } +}; + +template class TestRawArray:public TestArray> +{ + RawLifetimeCallback life_cb; + +public: + + TestRawArray():TestArray>(&life_cb){} + ~TestRawArray()=default; +}; + +template class TestObject +{ + int value; + +public: + + TestObject(){value=0;} + ~TestObject() + { + std::cout<<"~TestObject("< class TestObjectArray:public Test> +{ + DefaultObjectLifetimeCallback life_cb;; + +public: + + TestObjectArray():TestArray>(&life_cb){} + ~TestObjectArray()=default; +}; + +void main() +{ + //原生类型测试 + { + TestRawArray ta_int; + + ta_int.Add(1); + ta_int.Add(2); + ta_int.Add(3); + + *(ta_int.Create())=4; + + int two[2]={9,0}; + + ta_int.Add(two,2); + + std::cout<<"TestArray: "; + + for(const int val:ta_int) + std::cout< ta_ds; + + DataStruct ds{1,2}; + + ta_ds.Add(ds); + + DataStruct *p=ta_ds.Create(); + + *p={3,4}; + + DataStruct ds2[2]{{5,6},{7,8}}; + + ta_ds.Add(ds2,2); + + std::cout<<"TestArray>: "; + + for(const DataStruct &ds:ta_ds) + std::cout<<"{"<> ta_obj; + + ta_obj.Add(); + ta_obj.Add(); + ta_obj.Add(); + + std::cout<<"TestArray>: "; + + for(const TestObject &obj:ta_obj) + std::cout<<"{"< +#include + +using namespace hgl; +using namespace std; + +void TestQueue() +{ + Queue tab; + int i; + + for(i=0;i<10;i++) + { + cout<<"push "< tab; + + int i; + + for(i=0;i<10;i++) + { + tab.Push(new QueueTestObject(i)); + } + + cout<<"Queue Count: "<