xo-object2: allow 0-capacity DArrays + utest
This commit is contained in:
parent
52d72ac0e5
commit
293d805351
3 changed files with 47 additions and 10 deletions
|
|
@ -60,6 +60,9 @@ namespace xo {
|
|||
|
||||
/** create array containing elements @p args, using memory from @p mm.
|
||||
* Nullptr if space exhausted.
|
||||
*
|
||||
* Use:
|
||||
* Darray * v = DArray::array(mm, e1, e2, e3);
|
||||
**/
|
||||
template <typename... Args>
|
||||
requires (std::same_as<Args, obj<AGCObject>> && ...)
|
||||
|
|
|
|||
|
|
@ -17,21 +17,17 @@ namespace xo {
|
|||
DArray::empty(obj<AAllocator> mm,
|
||||
size_type cap)
|
||||
{
|
||||
assert(cap > 0);
|
||||
|
||||
DArray * result = nullptr;
|
||||
|
||||
if (cap > 0) {
|
||||
void * mem = mm.alloc(typeseq::id<DArray>(),
|
||||
sizeof(DArray) + cap * sizeof(obj<AGCObject>));
|
||||
void * mem = mm.alloc(typeseq::id<DArray>(),
|
||||
sizeof(DArray) + cap * sizeof(obj<AGCObject>));
|
||||
|
||||
result = new (mem) DArray();
|
||||
result = new (mem) DArray();
|
||||
|
||||
assert(result);
|
||||
assert(result);
|
||||
|
||||
result->capacity_ = cap;
|
||||
result->size_ = 0;
|
||||
}
|
||||
result->capacity_ = cap;
|
||||
result->size_ = 0;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -153,6 +153,44 @@ namespace xo {
|
|||
REQUIRE(arr->at(2).data() == e2.data());
|
||||
}
|
||||
|
||||
TEST_CASE("DArray-array", "[object2][DArray]")
|
||||
{
|
||||
ArenaConfig cfg { .name_ = "testarena",
|
||||
.size_ = 4*1024 };
|
||||
DArena arena = DArena::map(cfg);
|
||||
auto alloc = with_facet<AAllocator>::mkobj(&arena);
|
||||
|
||||
obj<AGCObject> e0 = DInteger::box<AGCObject>(alloc, 10);
|
||||
obj<AGCObject> e1 = DInteger::box<AGCObject>(alloc, 20);
|
||||
obj<AGCObject> e2 = DInteger::box<AGCObject>(alloc, 30);
|
||||
|
||||
DArray * arr = DArray::array(alloc, e0, e1, e2);
|
||||
|
||||
REQUIRE(arr != nullptr);
|
||||
REQUIRE(arr->size() == 3);
|
||||
REQUIRE(arr->capacity() == 3);
|
||||
REQUIRE(arr->is_empty() == false);
|
||||
|
||||
REQUIRE(arr->at(0).data() == e0.data());
|
||||
REQUIRE(arr->at(1).data() == e1.data());
|
||||
REQUIRE(arr->at(2).data() == e2.data());
|
||||
}
|
||||
|
||||
TEST_CASE("DArray-array-empty", "[object2][DArray]")
|
||||
{
|
||||
ArenaConfig cfg { .name_ = "testarena",
|
||||
.size_ = 4*1024 };
|
||||
DArena arena = DArena::map(cfg);
|
||||
auto alloc = with_facet<AAllocator>::mkobj(&arena);
|
||||
|
||||
DArray * arr = DArray::array(alloc);
|
||||
|
||||
REQUIRE(arr != nullptr);
|
||||
REQUIRE(arr->size() == 0);
|
||||
REQUIRE(arr->capacity() == 0);
|
||||
REQUIRE(arr->is_empty() == true);
|
||||
}
|
||||
|
||||
} /*namespace ut*/
|
||||
} /*namespace xo*/
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue