xo-object2: add DString::from_cstr + utest
This commit is contained in:
parent
8d577129ee
commit
6b216a7e84
2 changed files with 36 additions and 0 deletions
|
|
@ -4,6 +4,7 @@
|
|||
**/
|
||||
|
||||
#include "DString.hpp"
|
||||
#include <cstring>
|
||||
|
||||
namespace xo {
|
||||
using xo::facet::typeseq;
|
||||
|
|
@ -31,6 +32,24 @@ namespace xo {
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
DString *
|
||||
DString::from_cstr(obj<AAllocator> mm,
|
||||
const char * cstr)
|
||||
{
|
||||
size_type len = std::strlen(cstr);
|
||||
size_type cap = len + 1;
|
||||
|
||||
void * mem = mm.alloc(typeseq::id<DString>(),
|
||||
sizeof(DString) + cap);
|
||||
|
||||
DString * result = new (mem) DString();
|
||||
result->capacity_ = cap;
|
||||
result->size_ = len;
|
||||
std::memcpy(result->chars_, cstr, cap);
|
||||
|
||||
return result;
|
||||
}
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
#include <xo/object2/DString.hpp>
|
||||
#include <xo/alloc2/arena/IAllocator_DArena.hpp>
|
||||
#include <catch2/catch.hpp>
|
||||
#include <cstring>
|
||||
|
||||
namespace xo {
|
||||
using xo::scm::DString;
|
||||
|
|
@ -30,6 +31,22 @@ namespace xo {
|
|||
REQUIRE(s->size() == 0);
|
||||
REQUIRE(s->chars()[0] == '\0');
|
||||
}
|
||||
|
||||
TEST_CASE("DString-from_cstr", "[object2][DString]")
|
||||
{
|
||||
ArenaConfig cfg { .name_ = "testarena",
|
||||
.size_ = 4*1024 };
|
||||
DArena arena = DArena::map(cfg);
|
||||
auto alloc = with_facet<AAllocator>::mkobj(&arena);
|
||||
|
||||
const char * cstr = "hello world";
|
||||
DString * s = DString::from_cstr(alloc, cstr);
|
||||
|
||||
REQUIRE(s != nullptr);
|
||||
REQUIRE(s->capacity() == 12);
|
||||
REQUIRE(s->size() == 11);
|
||||
REQUIRE(std::strcmp(s->chars(), cstr) == 0);
|
||||
}
|
||||
} /*namespace ut*/
|
||||
} /*namespace xo*/
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue