Some @safe + unittest

This commit is contained in:
Grim Maple 2022-11-05 21:59:16 +03:00
parent cebe137ecd
commit d86ef672e6
1 changed files with 36 additions and 8 deletions

View File

@ -241,26 +241,26 @@ struct ObjectList(T) {
protected T[] _list; protected T[] _list;
protected int _count; protected int _count;
/** returns count of items */ /** returns count of items */
@property int count() const { return _count; } @property int count() @safe @nogc const { return _count; }
alias length = count; alias length = count;
/** get item by index */ /** get item by index */
inout(T) get(int index) inout { inout(T) get(int index) @safe inout {
assert(index >= 0 && index < _count, "child index out of range"); assert(index >= 0 && index < _count, "child index out of range");
return _list[index]; return _list[index];
} }
/// get item by index /// get item by index
T opIndex(int index) { T opIndex(int index) @safe {
return get(index); return get(index);
} }
/** add item to list */ /** add item to list */
T add(T item) { T add(T item) @safe {
if (_list.length <= _count) // resize if (_list.length <= _count) // resize
_list.length = _list.length < 4 ? 4 : _list.length * 2; _list.length = _list.length < 4 ? 4 : _list.length * 2;
_list[_count++] = item; _list[_count++] = item;
return item; return item;
} }
/** add item to list */ /** add item to list */
T insert(T item, int index = -1) { T insert(T item, int index = -1) @safe {
if (index > _count || index < 0) if (index > _count || index < 0)
index = _count; index = _count;
if (_list.length <= _count) // resize if (_list.length <= _count) // resize
@ -272,7 +272,7 @@ struct ObjectList(T) {
return item; return item;
} }
/** find child index for item, return -1 if not found */ /** find child index for item, return -1 if not found */
int indexOf(T item) { int indexOf(T item) @safe @nogc const{
if (item is null) if (item is null)
return -1; return -1;
for (int i = 0; i < _count; i++) for (int i = 0; i < _count; i++)
@ -290,7 +290,7 @@ struct ObjectList(T) {
} }
} }
/** remove item from list, return removed item */ /** remove item from list, return removed item */
T remove(int index) { T remove(int index) @safe {
assert(index >= 0 && index < _count, "child index out of range"); assert(index >= 0 && index < _count, "child index out of range");
T item = _list[index]; T item = _list[index];
for (int i = index; i < _count - 1; i++) for (int i = index; i < _count - 1; i++)
@ -343,7 +343,7 @@ struct ObjectList(T) {
T[] asArray() { T[] asArray() {
if (!_count) if (!_count)
return null; return null;
return _list[0.._count]; return _list[0.._count];
} }
/// destructor destroys all items /// destructor destroys all items
~this() { ~this() {
@ -351,3 +351,31 @@ struct ObjectList(T) {
} }
} }
unittest
{
class Dummy
{
this() { }
this(int v) { a = v; }
int a = 11;
}
Dummy test = new Dummy(123);
ObjectList!Dummy d;
d.add(new Dummy());
assert(d.count == 1);
d.add(new Dummy(13));
assert(d.count == 2);
int count = 0;
foreach(a; d)
count++;
assert(count == 2);
d.remove(0);
assert(d[0].a == 13);
d.insert(new Dummy(44), 0);
d.insert(new Dummy(0), 1);
assert(d[0].a == 44 && d[1].a == 0);
d.insert(test);
assert(d.indexOf(test) == d.count() - 1);
d.replace(new Dummy(456), 0);
assert(d[0].a == 456);
}