/** @file DString.test.cpp * * @author Roland Conybeare, Jan 2026 **/ #include #include #include #include #include #include namespace xo { using xo::scm::DString; using xo::mm::AAllocator; using xo::mm::DArena; using xo::mm::ArenaConfig; using xo::facet::with_facet; using xo::facet::obj; namespace ut { TEST_CASE("DString-empty", "[object2][DString]") { ArenaConfig cfg { .name_ = "testarena", .size_ = 4*1024 }; DArena arena = DArena::map(cfg); auto alloc = with_facet::mkobj(&arena); DString * s = DString::empty(alloc, 16); REQUIRE(s != nullptr); REQUIRE(s->capacity() == 16); 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::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); } TEST_CASE("DString-assign", "[object2][DString]") { ArenaConfig cfg { .name_ = "testarena", .size_ = 4*1024 }; DArena arena = DArena::map(cfg); auto alloc = with_facet::mkobj(&arena); DString * src = DString::from_cstr(alloc, "hello"); DString * dst = DString::empty(alloc, 16); dst->assign(*src); REQUIRE(dst->size() == 5); REQUIRE(std::strcmp(dst->chars(), "hello") == 0); } TEST_CASE("DString-assign-truncate", "[object2][DString]") { ArenaConfig cfg { .name_ = "testarena", .size_ = 4*1024 }; DArena arena = DArena::map(cfg); auto alloc = with_facet::mkobj(&arena); DString * src = DString::from_cstr(alloc, "hello world"); DString * dst = DString::empty(alloc, 6); dst->assign(*src); REQUIRE(dst->size() == 5); REQUIRE(std::strcmp(dst->chars(), "hello") == 0); } TEST_CASE("DString-data", "[object2][DString]") { ArenaConfig cfg { .name_ = "testarena", .size_ = 4*1024 }; DArena arena = DArena::map(cfg); auto alloc = with_facet::mkobj(&arena); DString * s = DString::empty(alloc, 16); char * p = s->data(); std::strcpy(p, "test"); s->fixup_size(); REQUIRE(s->size() == 4); REQUIRE(std::strcmp(s->chars(), "test") == 0); } TEST_CASE("DString-operator-bracket", "[object2][DString]") { ArenaConfig cfg { .name_ = "testarena", .size_ = 4*1024 }; DArena arena = DArena::map(cfg); auto alloc = with_facet::mkobj(&arena); DString * s = DString::from_cstr(alloc, "hello"); REQUIRE((*s)[0] == 'h'); REQUIRE((*s)[4] == 'o'); (*s)[0] = 'H'; REQUIRE(std::strcmp(s->chars(), "Hello") == 0); } TEST_CASE("DString-clear", "[object2][DString]") { ArenaConfig cfg { .name_ = "testarena", .size_ = 4*1024 }; DArena arena = DArena::map(cfg); auto alloc = with_facet::mkobj(&arena); DString * s = DString::from_cstr(alloc, "hello"); REQUIRE(s->size() == 5); s->clear(); REQUIRE(s->size() == 0); REQUIRE(s->chars()[0] == '\0'); REQUIRE(s->capacity() == 6); } TEST_CASE("DString-fixup_size", "[object2][DString]") { ArenaConfig cfg { .name_ = "testarena", .size_ = 4*1024 }; DArena arena = DArena::map(cfg); auto alloc = with_facet::mkobj(&arena); DString * s = DString::empty(alloc, 16); char * p = s->data(); p[0] = 'a'; p[1] = 'b'; p[2] = 'c'; p[3] = '\0'; REQUIRE(s->size() == 0); auto new_size = s->fixup_size(); REQUIRE(new_size == 3); REQUIRE(s->size() == 3); } TEST_CASE("DString-string_view", "[object2][DString]") { ArenaConfig cfg { .name_ = "testarena", .size_ = 4*1024 }; DArena arena = DArena::map(cfg); auto alloc = with_facet::mkobj(&arena); DString * s = DString::from_cstr(alloc, "hello"); std::string_view sv = *s; REQUIRE(sv == "hello"); REQUIRE(sv.size() == 5); } TEST_CASE("DString-cstr-conversion", "[object2][DString]") { ArenaConfig cfg { .name_ = "testarena", .size_ = 4*1024 }; DArena arena = DArena::map(cfg); auto alloc = with_facet::mkobj(&arena); DString * s = DString::from_cstr(alloc, "hello"); const char * cstr = *s; REQUIRE(std::strcmp(cstr, "hello") == 0); } TEST_CASE("DString-begin-end", "[object2][DString]") { ArenaConfig cfg { .name_ = "testarena", .size_ = 4*1024 }; DArena arena = DArena::map(cfg); auto alloc = with_facet::mkobj(&arena); DString * s = DString::from_cstr(alloc, "hello"); REQUIRE(s->begin() == s->chars()); REQUIRE(s->end() == s->chars() + 5); REQUIRE(s->end() - s->begin() == 5); *s->begin() = 'H'; REQUIRE(std::strcmp(s->chars(), "Hello") == 0); } TEST_CASE("DString-cbegin-cend", "[object2][DString]") { ArenaConfig cfg { .name_ = "testarena", .size_ = 4*1024 }; DArena arena = DArena::map(cfg); auto alloc = with_facet::mkobj(&arena); DString * s = DString::from_cstr(alloc, "hello"); REQUIRE(s->cbegin() == s->chars()); REQUIRE(s->cend() == s->chars() + 5); REQUIRE(s->cend() - s->cbegin() == 5); } TEST_CASE("DString-range-for", "[object2][DString]") { ArenaConfig cfg { .name_ = "testarena", .size_ = 4*1024 }; DArena arena = DArena::map(cfg); auto alloc = with_facet::mkobj(&arena); DString * s = DString::from_cstr(alloc, "hello"); std::string result; for (char c : *s) { result += c; } REQUIRE(result == "hello"); } TEST_CASE("DString-iterator-modify", "[object2][DString]") { ArenaConfig cfg { .name_ = "testarena", .size_ = 4*1024 }; DArena arena = DArena::map(cfg); auto alloc = with_facet::mkobj(&arena); DString * s = DString::from_cstr(alloc, "hello"); for (auto it = s->begin(); it != s->end(); ++it) { *it = std::toupper(*it); } REQUIRE(std::strcmp(s->chars(), "HELLO") == 0); } } /*namespace ut*/ } /*namespace xo*/ /* end DString.test.cpp */