完成List测试用例
This commit is contained in:
parent
8d25718d5b
commit
df308cbd5a
|
@ -68,11 +68,6 @@ protected:
|
|||
virtual bool Release();
|
||||
};
|
||||
|
||||
// 数组长度
|
||||
#define ArrayLength(arr) (sizeof(arr)/sizeof(arr[0]))
|
||||
// 数组清零,固定长度
|
||||
//#define ArrayZero(arr) memset(arr, 0, sizeof(arr))
|
||||
|
||||
// 使用常量数组来定义一个指针数组
|
||||
//#define CArray(arr) (Array(arr, ArrayLength(arr)))
|
||||
//#define SArray(obj) (Array(&obj, sizeof(obj)))
|
||||
|
|
|
@ -4,7 +4,9 @@
|
|||
|
||||
List::List(int size)
|
||||
{
|
||||
_Arr = Arr;
|
||||
_Count = 0;
|
||||
_Capacity = ArrayLength(Arr) >> 2;
|
||||
}
|
||||
|
||||
//List::List(T* items, uint count) { Set(items, count); }
|
||||
|
@ -14,26 +16,34 @@ int List::Count() const { return _Count; }
|
|||
// 添加单个元素
|
||||
void List::Add(void* item)
|
||||
{
|
||||
//Push(item);
|
||||
CheckCapacity(_Count + 1);
|
||||
|
||||
_Arr[_Count++] = item;
|
||||
}
|
||||
|
||||
/*// 添加多个元素
|
||||
void List::Add(T* items, int count)
|
||||
// 添加多个元素
|
||||
void List::Add(void** items, uint count)
|
||||
{
|
||||
for(int i=0; i<count; i++) Push(*items++);
|
||||
}*/
|
||||
//assert(items, "items");
|
||||
//assert(count, "count");
|
||||
if(!items || !count) return;
|
||||
|
||||
CheckCapacity(_Count + count);
|
||||
|
||||
while(count--) _Arr[_Count++] = items++;
|
||||
}
|
||||
|
||||
// 删除指定位置元素
|
||||
void List::RemoveAt(int index)
|
||||
void List::RemoveAt(uint index)
|
||||
{
|
||||
int len = _Count;
|
||||
if(len <= 0 || index >= len) return;
|
||||
|
||||
// 复制元素
|
||||
//if(index < len - 1) memmove(&_Arr[index], &_Arr[index + 1], (len - index - 1) * sizeof(T));
|
||||
if(index < len - 1)
|
||||
// 复制元素,最后一个不用复制
|
||||
int remain = len - 1 - index;
|
||||
if(remain)
|
||||
{
|
||||
len = (len - index - 1) * sizeof(void*);
|
||||
len = remain * sizeof(void*);
|
||||
Buffer(&_Arr[index], len).Copy(0, &_Arr[index + 1], len);
|
||||
}
|
||||
_Count--;
|
||||
|
@ -42,8 +52,18 @@ void List::RemoveAt(int index)
|
|||
// 删除指定元素
|
||||
void List::Remove(const void* item)
|
||||
{
|
||||
//int index = FindIndex(item);
|
||||
//if(index >= 0) RemoveAt(index);
|
||||
int index = FindIndex(item);
|
||||
if(index >= 0) RemoveAt(index);
|
||||
}
|
||||
|
||||
int List::FindIndex(const void* item)
|
||||
{
|
||||
for(int i=0; i<_Count; i++)
|
||||
{
|
||||
if(_Arr[i] == item) return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 释放所有指针指向的内存
|
||||
|
@ -51,7 +71,7 @@ List& List::DeleteAll()
|
|||
{
|
||||
for(int i=0; i < _Count; i++)
|
||||
{
|
||||
if(_Arr[i]) delete _Arr[i];
|
||||
if(_Arr[i]) delete (int*)_Arr[i];
|
||||
}
|
||||
|
||||
return *this;
|
||||
|
@ -67,3 +87,22 @@ const void** List::ToArray() const
|
|||
{
|
||||
return _Arr;
|
||||
}*/
|
||||
|
||||
// 重载索引运算符[],返回指定元素的第一个
|
||||
void* List::operator[](int i) const
|
||||
{
|
||||
if(i<0 || i>=_Count) return nullptr;
|
||||
|
||||
return _Arr[i];
|
||||
}
|
||||
|
||||
void*& List::operator[](int i)
|
||||
{
|
||||
if(i<0 || i>=_Count)
|
||||
{
|
||||
static void* dummy;
|
||||
return dummy;
|
||||
}
|
||||
|
||||
return _Arr[i];
|
||||
}
|
||||
|
|
19
Core/List.h
19
Core/List.h
|
@ -1,7 +1,7 @@
|
|||
#ifndef _List_H_
|
||||
#define _List_H_
|
||||
|
||||
// 变长列表
|
||||
// 变长列表。仅用于存储指针
|
||||
class List
|
||||
{
|
||||
public:
|
||||
|
@ -14,28 +14,37 @@ public:
|
|||
void Add(void* item);
|
||||
|
||||
// 添加多个元素
|
||||
void Add(void* items, int count);
|
||||
void Add(void** items, uint count);
|
||||
|
||||
// 删除指定位置元素
|
||||
void RemoveAt(int index);
|
||||
void RemoveAt(uint index);
|
||||
|
||||
// 删除指定元素
|
||||
void Remove(const void* item);
|
||||
|
||||
void Clear();
|
||||
|
||||
// 查找指定项。不存在时返回-1
|
||||
int FindIndex(const void* item);
|
||||
|
||||
// 释放所有指针指向的内存
|
||||
List& DeleteAll();
|
||||
|
||||
// 返回内部指针
|
||||
//const void** ToArray() const;
|
||||
|
||||
// 重载索引运算符[],返回指定元素的第一个
|
||||
void* operator[](int i) const;
|
||||
void*& operator[](int i);
|
||||
|
||||
private:
|
||||
void** _Arr;
|
||||
int _Count;
|
||||
int _Capacity;
|
||||
uint _Count;
|
||||
uint _Capacity;
|
||||
|
||||
void* Arr[0x10];
|
||||
|
||||
bool CheckCapacity(int count);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -49,4 +49,9 @@ public:
|
|||
const String Name() const; // 名称
|
||||
};
|
||||
|
||||
// 数组长度
|
||||
#define ArrayLength(arr) (sizeof(arr)/sizeof(arr[0]))
|
||||
// 数组清零,固定长度
|
||||
//#define ArrayZero(arr) memset(arr, 0, sizeof(arr))
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include "Sys.h"
|
||||
#include "List.h"
|
||||
|
||||
#if DEBUG
|
||||
static void TestAssign()
|
||||
|
@ -92,28 +93,49 @@ void TestList()
|
|||
|
||||
//不同长度的原始数据
|
||||
byte buf1[] = {1,2,3,4,5};
|
||||
//byte buf2[] = {6,7,8,9};
|
||||
byte buf2[] = {6,7,8,9};
|
||||
byte buf3[] = {10,11,12,13,14,15,16,17,18,19,20};
|
||||
|
||||
Array arr1(buf1, sizeof(buf1));
|
||||
arr1.Show(true);
|
||||
List list;
|
||||
list.Add(buf1);
|
||||
list.Add(buf2);
|
||||
list.Add(buf3);
|
||||
|
||||
assert(arr1.GetBuffer() == (byte*)buf1 && arr1.Length()== sizeof(buf1),"Array(void* data, int len)");
|
||||
assert(arr1[0] == 1, " byte& operator[](int i)");
|
||||
assert(list.Count() == 3, "Count()");
|
||||
assert(list[0] == buf1 && list[1] == buf2 && list[2] == buf3, "void Add(void* item)");
|
||||
|
||||
arr1.Clear();
|
||||
assert(arr1[1] == 0, "virtual void Clear()");
|
||||
// 添加
|
||||
list.Add(buf2);
|
||||
list.Add(buf3);
|
||||
assert(list.Count() == 5, "Count()");
|
||||
assert(list[3] == buf2 && list[4] == buf3, "void Add(void* item)");
|
||||
|
||||
arr1.Set(buf1, sizeof(buf1));
|
||||
assert(arr1== buf1, "bool Set(void* data, int len)");
|
||||
// 查找
|
||||
int idx = list.FindIndex(buf2);
|
||||
assert(idx == 1, "int FindIndex(const void* item)");
|
||||
|
||||
arr1.SetItem(buf3, 0, sizeof(buf3));
|
||||
arr1.Show(true);
|
||||
assert(arr1[arr1.Length() - 1] == 10 && arr1.Length() == sizeof(buf3) , "bool SetItem(const void* data, int index, int count);");
|
||||
// 删除倒数第二个。后面前移
|
||||
list.RemoveAt(list.Count()); // 无效
|
||||
list.RemoveAt(list.Count() - 2);
|
||||
assert(list.Count() == 4, "Count()");
|
||||
assert(list[3] == buf3, "void RemoveAt(uint index)");
|
||||
|
||||
TestAssign();
|
||||
TestAssign2();
|
||||
TestCopy();
|
||||
// 删除具体项。后面前移
|
||||
list.Remove(buf2);
|
||||
assert(list.Count() == 3, "Count()");
|
||||
assert(list[1] == buf3 && list[2] == buf3, "void Remove(const void* item)");
|
||||
|
||||
// 删除具体项。找不到的情况
|
||||
list.Remove(buf2);
|
||||
assert(list.Count() == 3, "Count()");
|
||||
|
||||
// 查找。找不到的情况
|
||||
idx = list.FindIndex(buf2);
|
||||
assert(idx == -1, "int FindIndex(const void* item)");
|
||||
|
||||
//TestAssign();
|
||||
//TestAssign2();
|
||||
//TestCopy();
|
||||
|
||||
debug_printf("TestList测试完毕......\r\n");
|
||||
|
||||
|
|
Loading…
Reference in New Issue