102 lines
3.4 KiB
C++
102 lines
3.4 KiB
C++
/** @file StringOps.test.cpp
|
|
*
|
|
* @author Roland Conybeare, Jan 2026
|
|
**/
|
|
|
|
#include <xo/object2/StringOps.hpp>
|
|
#include <xo/alloc2/arena/IAllocator_DArena.hpp>
|
|
#include <catch2/catch.hpp>
|
|
#include <cstring>
|
|
|
|
namespace xo {
|
|
using xo::scm::StringOps;
|
|
using xo::scm::DString;
|
|
using xo::mm::AAllocator;
|
|
using xo::mm::AGCObject;
|
|
using xo::mm::DArena;
|
|
using xo::mm::ArenaConfig;
|
|
using xo::facet::with_facet;
|
|
using xo::facet::obj;
|
|
|
|
namespace ut {
|
|
TEST_CASE("StringOps-empty", "[object2][StringOps]")
|
|
{
|
|
ArenaConfig cfg { .name_ = "testarena",
|
|
.size_ = 4*1024 };
|
|
DArena arena = DArena::map(cfg);
|
|
auto alloc = with_facet<AAllocator>::mkobj(&arena);
|
|
|
|
auto s = StringOps::empty(alloc, 16);
|
|
|
|
REQUIRE(s.data() != nullptr);
|
|
REQUIRE(s.data()->capacity() == 16);
|
|
REQUIRE(s.data()->size() == 0);
|
|
REQUIRE(s.data()->chars()[0] == '\0');
|
|
}
|
|
|
|
TEST_CASE("StringOps-empty-with-content", "[object2][StringOps]")
|
|
{
|
|
ArenaConfig cfg { .name_ = "testarena",
|
|
.size_ = 4*1024 };
|
|
DArena arena = DArena::map(cfg);
|
|
auto alloc = with_facet<AAllocator>::mkobj(&arena);
|
|
|
|
auto s = StringOps::empty(alloc, 32);
|
|
|
|
s.data()->sprintf("hello %s %d", "world", 42);
|
|
|
|
REQUIRE(s.data()->size() == 14);
|
|
REQUIRE(std::strcmp(s.data()->chars(), "hello world 42") == 0);
|
|
}
|
|
|
|
TEST_CASE("StringOps-from_cstr", "[object2][StringOps]")
|
|
{
|
|
ArenaConfig cfg { .name_ = "testarena",
|
|
.size_ = 4*1024 };
|
|
DArena arena = DArena::map(cfg);
|
|
auto alloc = with_facet<AAllocator>::mkobj(&arena);
|
|
|
|
const char * cstr = "hello world";
|
|
auto s = StringOps::from_cstr(alloc, cstr);
|
|
|
|
REQUIRE(s.data() != nullptr);
|
|
REQUIRE(s.data()->capacity() == 12);
|
|
REQUIRE(s.data()->size() == 11);
|
|
REQUIRE(std::strcmp(s.data()->chars(), cstr) == 0);
|
|
}
|
|
|
|
TEST_CASE("StringOps-clone", "[object2][StringOps]")
|
|
{
|
|
ArenaConfig cfg { .name_ = "testarena",
|
|
.size_ = 4*1024 };
|
|
DArena arena = DArena::map(cfg);
|
|
auto alloc = with_facet<AAllocator>::mkobj(&arena);
|
|
|
|
auto src = StringOps::from_cstr(alloc, "hello world");
|
|
auto copy = StringOps::clone(alloc, src);
|
|
|
|
REQUIRE(copy.data() != nullptr);
|
|
REQUIRE(copy.data() != src.data());
|
|
REQUIRE(copy.data()->size() == src.data()->size());
|
|
REQUIRE(copy.data()->capacity() == src.data()->capacity());
|
|
REQUIRE(std::strcmp(copy.data()->chars(), src.data()->chars()) == 0);
|
|
}
|
|
|
|
TEST_CASE("StringOps-printf", "[object2][StringOps]")
|
|
{
|
|
ArenaConfig cfg { .name_ = "testarena",
|
|
.size_ = 4*1024 };
|
|
DArena arena = DArena::map(cfg);
|
|
auto alloc = with_facet<AAllocator>::mkobj(&arena);
|
|
|
|
auto s = StringOps::printf(alloc, 32, "hello %s %d", "world", 42);
|
|
|
|
REQUIRE(s.data() != nullptr);
|
|
REQUIRE(s.data()->capacity() == 32);
|
|
REQUIRE(s.data()->size() == 14);
|
|
REQUIRE(std::strcmp(s.data()->chars(), "hello world 42") == 0);
|
|
}
|
|
} /*namespace ut*/
|
|
} /*namespace xo*/
|
|
|
|
/* end StringOps.test.cpp */
|