完成List测试用例

This commit is contained in:
Stone 2016-06-03 03:37:15 +00:00
parent 8d25718d5b
commit df308cbd5a
5 changed files with 108 additions and 38 deletions

View File

@ -68,11 +68,6 @@ protected:
virtual bool Release(); 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 CArray(arr) (Array(arr, ArrayLength(arr)))
//#define SArray(obj) (Array(&obj, sizeof(obj))) //#define SArray(obj) (Array(&obj, sizeof(obj)))

View File

@ -4,7 +4,9 @@
List::List(int size) List::List(int size)
{ {
_Arr = Arr;
_Count = 0; _Count = 0;
_Capacity = ArrayLength(Arr) >> 2;
} }
//List::List(T* items, uint count) { Set(items, count); } //List::List(T* items, uint count) { Set(items, count); }
@ -14,26 +16,34 @@ int List::Count() const { return _Count; }
// 添加单个元素 // 添加单个元素
void List::Add(void* item) 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; int len = _Count;
if(len <= 0 || index >= len) return; if(len <= 0 || index >= len) return;
// 复制元素 // 复制元素,最后一个不用复制
//if(index < len - 1) memmove(&_Arr[index], &_Arr[index + 1], (len - index - 1) * sizeof(T)); int remain = len - 1 - index;
if(index < len - 1) if(remain)
{ {
len = (len - index - 1) * sizeof(void*); len = remain * sizeof(void*);
Buffer(&_Arr[index], len).Copy(0, &_Arr[index + 1], len); Buffer(&_Arr[index], len).Copy(0, &_Arr[index + 1], len);
} }
_Count--; _Count--;
@ -42,8 +52,18 @@ void List::RemoveAt(int index)
// 删除指定元素 // 删除指定元素
void List::Remove(const void* item) void List::Remove(const void* item)
{ {
//int index = FindIndex(item); int index = FindIndex(item);
//if(index >= 0) RemoveAt(index); 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++) for(int i=0; i < _Count; i++)
{ {
if(_Arr[i]) delete _Arr[i]; if(_Arr[i]) delete (int*)_Arr[i];
} }
return *this; return *this;
@ -67,3 +87,22 @@ const void** List::ToArray() const
{ {
return _Arr; 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];
}

View File

@ -1,7 +1,7 @@
#ifndef _List_H_ #ifndef _List_H_
#define _List_H_ #define _List_H_
// 变长列表 // 变长列表。仅用于存储指针
class List class List
{ {
public: public:
@ -14,15 +14,18 @@ public:
void Add(void* item); 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 Remove(const void* item);
void Clear(); void Clear();
// 查找指定项。不存在时返回-1
int FindIndex(const void* item);
// 释放所有指针指向的内存 // 释放所有指针指向的内存
List& DeleteAll(); List& DeleteAll();
@ -30,12 +33,18 @@ public:
// 返回内部指针 // 返回内部指针
//const void** ToArray() const; //const void** ToArray() const;
// 重载索引运算符[],返回指定元素的第一个
void* operator[](int i) const;
void*& operator[](int i);
private: private:
void** _Arr; void** _Arr;
int _Count; uint _Count;
int _Capacity; uint _Capacity;
void* Arr[0x10]; void* Arr[0x10];
bool CheckCapacity(int count);
}; };
#endif #endif

View File

@ -49,4 +49,9 @@ public:
const String Name() const; // 名称 const String Name() const; // 名称
}; };
// 数组长度
#define ArrayLength(arr) (sizeof(arr)/sizeof(arr[0]))
// 数组清零,固定长度
//#define ArrayZero(arr) memset(arr, 0, sizeof(arr))
#endif #endif

View File

@ -1,4 +1,5 @@
#include "Sys.h" #include "Sys.h"
#include "List.h"
#if DEBUG #if DEBUG
static void TestAssign() static void TestAssign()
@ -92,28 +93,49 @@ void TestList()
//不同长度的原始数据 //不同长度的原始数据
byte buf1[] = {1,2,3,4,5}; 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}; byte buf3[] = {10,11,12,13,14,15,16,17,18,19,20};
Array arr1(buf1, sizeof(buf1)); List list;
arr1.Show(true); list.Add(buf1);
list.Add(buf2);
list.Add(buf3);
assert(arr1.GetBuffer() == (byte*)buf1 && arr1.Length()== sizeof(buf1),"Array(void* data, int len)"); assert(list.Count() == 3, "Count()");
assert(arr1[0] == 1, " byte& operator[](int i)"); 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); list.RemoveAt(list.Count()); // 无效
assert(arr1[arr1.Length() - 1] == 10 && arr1.Length() == sizeof(buf3) , "bool SetItem(const void* data, int index, int count);"); list.RemoveAt(list.Count() - 2);
assert(list.Count() == 4, "Count()");
assert(list[3] == buf3, "void RemoveAt(uint index)");
TestAssign(); // 删除具体项。后面前移
TestAssign2(); list.Remove(buf2);
TestCopy(); 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"); debug_printf("TestList测试完毕......\r\n");