diff --git a/include/xo/object2/DArray.hpp b/include/xo/object2/DArray.hpp index afa4769..0dc13cd 100644 --- a/include/xo/object2/DArray.hpp +++ b/include/xo/object2/DArray.hpp @@ -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 requires (std::same_as> && ...) diff --git a/src/object2/DArray.cpp b/src/object2/DArray.cpp index 392404c..1b4138c 100644 --- a/src/object2/DArray.cpp +++ b/src/object2/DArray.cpp @@ -17,21 +17,17 @@ namespace xo { DArray::empty(obj mm, size_type cap) { - assert(cap > 0); - DArray * result = nullptr; - if (cap > 0) { - void * mem = mm.alloc(typeseq::id(), - sizeof(DArray) + cap * sizeof(obj)); + void * mem = mm.alloc(typeseq::id(), + sizeof(DArray) + cap * sizeof(obj)); - 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; } diff --git a/utest/DArray.test.cpp b/utest/DArray.test.cpp index aa718d4..84afbe1 100644 --- a/utest/DArray.test.cpp +++ b/utest/DArray.test.cpp @@ -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::mkobj(&arena); + + obj e0 = DInteger::box(alloc, 10); + obj e1 = DInteger::box(alloc, 20); + obj e2 = DInteger::box(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::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*/