xo-object2: + StringOps + utest
This commit is contained in:
parent
f02e550f8c
commit
3349cea289
4 changed files with 92 additions and 1 deletions
36
xo-object2/include/xo/object2/StringOps.hpp
Normal file
36
xo-object2/include/xo/object2/StringOps.hpp
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/** @file StringOps.hpp
|
||||
*
|
||||
* @author Roland Conybeare, Jan 2026
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "string/IGCObject_DString.hpp"
|
||||
#include "DString.hpp"
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/** @brief string functions
|
||||
*
|
||||
* note: separate from DString
|
||||
**/
|
||||
struct StringOps {
|
||||
using AGCObject = xo::mm::AGCObject;
|
||||
using AAllocator = xo::mm::AAllocator;
|
||||
using size_type = DString::size_type;
|
||||
|
||||
template <typename AFacet = AGCObject>
|
||||
static obj<AFacet,DString> empty(obj<AAllocator> mm,
|
||||
size_type cap);
|
||||
};
|
||||
|
||||
template <typename AFacet>
|
||||
obj<AFacet,DString>
|
||||
StringOps::empty(obj<AAllocator> mm, size_type cap)
|
||||
{
|
||||
return obj<AFacet,DString>(DString::empty(mm, cap));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* end StringOps.hpp */
|
||||
|
|
@ -4,6 +4,7 @@ set(UTEST_EXE utest.object2)
|
|||
set(UTEST_SRCS
|
||||
object2_utest_main.cpp
|
||||
DString.test.cpp
|
||||
StringOps.test.cpp
|
||||
X1Collector.test.cpp
|
||||
Printable.test.cpp
|
||||
)
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
* @author Roland Conybeare, Jan 2026
|
||||
**/
|
||||
|
||||
#include <xo/object2/DString.hpp>
|
||||
#include <xo/object2/StringOps.hpp>
|
||||
#include <xo/alloc2/arena/IAllocator_DArena.hpp>
|
||||
#include <catch2/catch.hpp>
|
||||
#include <cctype>
|
||||
|
|
|
|||
54
xo-object2/utest/StringOps.test.cpp
Normal file
54
xo-object2/utest/StringOps.test.cpp
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
/** @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);
|
||||
|
||||
obj<AGCObject, DString> 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);
|
||||
|
||||
obj<AGCObject, DString> 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);
|
||||
}
|
||||
} /*namespace ut*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end StringOps.test.cpp */
|
||||
Loading…
Add table
Add a link
Reference in a new issue