Files
Library/tests/standalone/Text/Str.cpp
T

194 lines
6.9 KiB
C++

#include <Void/Text/Str.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
#include <vector>
using namespace Void::Str;
// ── trim ──────────────────────────────────────────────────────────────────────
Void::Testing::Test str_trim_left()
{
Void::Testing::expect_eq(trim_left(" hello"), std::string_view{"hello"});
Void::Testing::expect_eq(trim_left("hello"), std::string_view{"hello"});
}
Void::Testing::Test str_trim_right()
{
Void::Testing::expect_eq(trim_right("hello "), std::string_view{"hello"});
}
Void::Testing::Test str_trim_both()
{
Void::Testing::expect_eq(trim(" hello "), std::string_view{"hello"});
Void::Testing::expect_eq(trim(""), std::string_view{""});
}
// ── case ──────────────────────────────────────────────────────────────────────
Void::Testing::Test str_to_lower()
{
Void::Testing::expect_eq(to_lower("Hello World"), std::string{"hello world"});
}
Void::Testing::Test str_to_upper()
{
Void::Testing::expect_eq(to_upper("hello"), std::string{"HELLO"});
}
// ── predicates ────────────────────────────────────────────────────────────────
Void::Testing::Test str_starts_with()
{
Void::Testing::expect(starts_with("foobar", "foo"));
Void::Testing::expect(!starts_with("foobar", "bar"));
}
Void::Testing::Test str_ends_with()
{
Void::Testing::expect(ends_with("foobar", "bar"));
Void::Testing::expect(!ends_with("foobar", "foo"));
}
Void::Testing::Test str_contains()
{
Void::Testing::expect(contains("hello world", "world"));
Void::Testing::expect(!contains("hello", "xyz"));
}
Void::Testing::Test str_is_alpha()
{
Void::Testing::expect(is_alpha("abc"));
Void::Testing::expect(!is_alpha("ab1"));
Void::Testing::expect(!is_alpha(""));
}
Void::Testing::Test str_is_digit()
{
Void::Testing::expect(is_digit("123"));
Void::Testing::expect(!is_digit("12a"));
}
Void::Testing::Test str_is_alnum()
{
Void::Testing::expect(is_alnum("abc123"));
Void::Testing::expect(!is_alnum("abc!"));
}
// ── split / join ──────────────────────────────────────────────────────────────
Void::Testing::Test str_split_basic()
{
const auto parts = split("a,b,c", ",");
Void::Testing::expect_eq(parts.size(), std::size_t{3});
Void::Testing::expect_eq(parts[0], std::string_view{"a"});
Void::Testing::expect_eq(parts[2], std::string_view{"c"});
}
Void::Testing::Test str_split_no_delim()
{
const auto parts = split("hello", ",");
Void::Testing::expect_eq(parts.size(), std::size_t{1});
Void::Testing::expect_eq(parts[0], std::string_view{"hello"});
}
Void::Testing::Test str_split_once()
{
const auto parts = split_once("key=value=extra", "=");
Void::Testing::expect_eq(parts.size(), std::size_t{2});
Void::Testing::expect_eq(parts[0], std::string_view{"key"});
Void::Testing::expect_eq(parts[1], std::string_view{"value=extra"});
}
Void::Testing::Test str_join()
{
const std::vector<std::string> v{"a", "b", "c"};
Void::Testing::expect_eq(join(v, ", "), std::string{"a, b, c"});
}
// ── replace ───────────────────────────────────────────────────────────────────
Void::Testing::Test str_replace_all()
{
Void::Testing::expect_eq(replace_all("aabaa", "aa", "x"), std::string{"xbx"});
}
Void::Testing::Test str_replace_all_no_match()
{
Void::Testing::expect_eq(replace_all("hello", "xyz", "!"), std::string{"hello"});
}
// ── repeat ────────────────────────────────────────────────────────────────────
Void::Testing::Test str_repeat()
{
Void::Testing::expect_eq(repeat("ab", 3), std::string{"ababab"});
Void::Testing::expect_eq(repeat("x", 0), std::string{""});
}
// ── count ─────────────────────────────────────────────────────────────────────
Void::Testing::Test str_count_occurrences()
{
Void::Testing::expect_eq(count("ababab", "ab"), std::size_t{3});
Void::Testing::expect_eq(count("hello", "xyz"), std::size_t{0});
}
// ── pad ───────────────────────────────────────────────────────────────────────
Void::Testing::Test str_pad_left()
{
Void::Testing::expect_eq(pad_left("hi", 5), std::string{" hi"});
Void::Testing::expect_eq(pad_left("hello", 3), std::string{"hello"});
}
Void::Testing::Test str_pad_right()
{
Void::Testing::expect_eq(pad_right("hi", 5), std::string{"hi "});
}
// ── numeric conversion ────────────────────────────────────────────────────────
Void::Testing::Test str_to_int_valid()
{
int v = 0;
Void::Testing::expect(to_int("42", v));
Void::Testing::expect_eq(v, 42);
}
Void::Testing::Test str_to_int_invalid()
{
int v = 0;
Void::Testing::expect(!to_int("abc", v));
Void::Testing::expect(!to_int("12x", v));
}
Void::Testing::Test str_to_double_valid()
{
double v = 0.0;
Void::Testing::expect(to_double("3.14", v));
Void::Testing::expect(v > 3.13 && v < 3.15);
}
Void::Testing::Test str_ascii_helpers_preserve_high_bytes_and_nuls()
{
static_assert(trim_ascii(" \t\r\nabc\n ") == "abc");
static_assert(is_alpha_ascii("AbCd"));
static_assert(!is_alpha_ascii("\xC3\xA9"));
static_assert(is_digit_ascii("0123456789"));
static_assert(is_alnum_ascii("Az09"));
static_assert(iequals_ascii("HeLlO\xFF", "hElLo\xFF"));
static_assert(!iequals_ascii("abc", "abcd"));
const std::string mixed{" \xC3\xA9""Ab\xFF""Z\0x\n", 10};
Void::Testing::expect_eq(trim_left_ascii(mixed).size(), std::size_t{9});
Void::Testing::expect_eq(trim_right_ascii(mixed).size(), std::size_t{9});
Void::Testing::expect_eq(trim_ascii(mixed).size(), std::size_t{8});
const std::string lower = to_lower_ascii("A\x80\xFFZ");
Void::Testing::expect_eq(lower, std::string{"a\x80\xFFz"});
const std::string upper = to_upper_ascii("a\x80\xFFz");
Void::Testing::expect_eq(upper, std::string{"A\x80\xFFZ"});
}