40# include <gtest/gtest.h>
53using namespace testing;
77 if (delim.find(c) == string::npos)
79 else if (not token.empty())
85 if (not token.empty())
104 for (
size_t i = 0; i <
expected.size(); ++i)
121 for (
size_t i = 0; i <
expected.size(); ++i)
131 string s =
concat(s1, blank, s2, s3);
132 ASSERT_EQ(s,
"hello world!");
135TEST(StringUtils, vector_to_string)
137 vector<int> v = {1, 2, 3};
143TEST(StringUtils, array_to_string)
154TEST(StringUtils, trim_variants)
157 string s =
"\t abc \n";
158 EXPECT_EQ(
trim(s),
"abc");
159 EXPECT_EQ(s,
"\t abc \n");
163 string s =
"\t abc \n";
170 EXPECT_EQ(
trim(s),
"");
175TEST(StringUtils, contains_string_view)
177 EXPECT_TRUE(
contains(
"hello world",
"world"));
178 EXPECT_FALSE(
contains(
"hello",
"xyz"));
182TEST(StringUtils, double_formatting)
186 auto s =
to_str(1.0 / 3.0);
187 EXPECT_FALSE(s.empty());
190TEST(StringUtils, case_conversion_ascii)
197 EXPECT_EQ(s,
"hello");
199 EXPECT_EQ(s,
"HELLO");
202TEST(StringUtils, case_conversion_non_ascii_does_not_crash)
205 s.push_back(
static_cast<char>(0xFF));
208 ASSERT_EQ(lower.size(), 2u);
209 EXPECT_EQ(lower[1],
'a');
212TEST(StringUtils, only_alpha_and_removals)
218 EXPECT_EQ(
only_alpha(
"Hello, World! 123"),
"helloworld123");
219 EXPECT_EQ(
only_alpha(
"0123456789"),
"0123456789");
231 EXPECT_EQ(
join(
l,
","),
"");
235 EXPECT_EQ(
join(
l,
","),
"1,2,3");
236 EXPECT_EQ(
join(
l,
" - "),
"1 - 2 - 3");
239TEST(StringUtils, numeric_parsers)
265TEST(StringUtils, prefix_and_remove_prefix)
268 EXPECT_FALSE(
is_prefix(
"foo",
"foobar"));
270 string s =
"prefix_value";
272 EXPECT_EQ(s,
"value");
278 EXPECT_EQ(
to_name(
"hello"),
"Hello");
279 EXPECT_EQ(
to_name(
"Hello"),
"Hello");
286 EXPECT_TRUE(parts.is_empty());
291 ASSERT_EQ(parts.size(), 3u);
292 EXPECT_EQ(parts.nth(0),
"camel");
293 EXPECT_EQ(parts.nth(1),
"Case");
294 EXPECT_EQ(parts.nth(2),
"String");
298TEST(StringUtils, split_and_split_string)
301 auto v =
split(
"a,b,,c",
',');
302 ASSERT_EQ(v.size(), 4u);
303 EXPECT_EQ(v[0],
"a");
304 EXPECT_EQ(v[1],
"b");
306 EXPECT_EQ(v[3],
"c");
311 ASSERT_EQ(
l.
size(), 3u);
312 EXPECT_EQ(
l.
nth(0),
"a");
313 EXPECT_EQ(
l.
nth(1),
"b");
314 EXPECT_EQ(
l.
nth(2),
"c");
319 ASSERT_EQ(
l.
size(), 4u);
320 EXPECT_EQ(
l.
nth(0),
"a");
321 EXPECT_EQ(
l.
nth(1),
"b");
322 EXPECT_EQ(
l.
nth(2),
"c");
323 EXPECT_EQ(
l.
nth(3),
"d");
328 ASSERT_EQ(
l.
size(), 1u);
329 EXPECT_EQ(
l.
nth(0),
"abc");
333TEST(StringUtilsCritical, split_string_edge_cases)
337 EXPECT_TRUE(
out.is_empty());
342 EXPECT_TRUE(
out.is_empty());
355 auto list_out =
split_to_list(
"alpha||beta,gamma;;;delta",
"|,;");
356 auto array_out =
split_to_array(
"alpha||beta,gamma;;;delta",
"|,;");
382 EXPECT_THROW(
split_pos(s, 5), range_error);
387 EXPECT_THROW(
split_n(
"abc", 0), range_error);
388 EXPECT_THROW(
split_n(
"abc", 4), range_error);
391 ASSERT_EQ(
l.
size(), 4u);
392 EXPECT_EQ(
l.
nth(0),
"a");
393 EXPECT_EQ(
l.
nth(1),
"b");
394 EXPECT_EQ(
l.
nth(2),
"c");
395 EXPECT_EQ(
l.
nth(3),
"def");
407 ASSERT_EQ(
out.size(), 2u);
408 ASSERT_EQ(
out.nth(0).size(), 2u);
409 ASSERT_EQ(
out.nth(1).size(), 2u);
410 EXPECT_EQ(
out.nth(1).nth(0), 3);
411 EXPECT_EQ(
out.nth(1).nth(1), 0);
414TEST(StringUtils, format_string_no_underflow)
424 ASSERT_EQ(formatted.size(), 2u);
425 ASSERT_EQ(formatted.nth(0).size(), 2u);
434 ASSERT_EQ(csv.size(), 1u);
435 EXPECT_EQ(csv.nth(0).nth(0),
"a,");
436 EXPECT_EQ(csv.nth(0).nth(1),
"b,");
437 EXPECT_EQ(csv.nth(0).nth(2),
"c");
440TEST(StringUtils, text_wrapping_and_shift)
442 const string text =
"one two three four five";
450 EXPECT_EQ(shifted,
" a\n b");
453TEST(StringUtils, build_pars_list_variadic)
459TEST(StringUtils, build_pars_list_container)
474 EXPECT_EQ(s,
"xxxxxx");
478 EXPECT_TRUE(e.empty());
484 ASSERT_EQ(a.size(), 4u);
485 EXPECT_EQ(a[0],
"a");
486 EXPECT_EQ(a[1],
"b");
487 EXPECT_EQ(a[2],
"c");
488 EXPECT_EQ(a[3],
"d");
491TEST(StringUtils, format_string_computed_lens)
500 ASSERT_EQ(formatted.size(), 2u);
501 ASSERT_EQ(formatted.nth(0).size(), 2u);
502 ASSERT_EQ(formatted.nth(1).size(), 2u);
505TEST(StringUtilsCritical, format_string_exact_alignment_and_stream_output)
516 ASSERT_EQ(by_max.size(), 3u);
517 EXPECT_EQ(by_max.nth(0).nth(0),
"id ");
518 EXPECT_EQ(by_max.nth(0).nth(1),
"value ");
519 EXPECT_EQ(by_max.nth(1).nth(0),
" 7 ");
520 EXPECT_EQ(by_max.nth(1).nth(1),
" abc ");
521 EXPECT_EQ(by_max.nth(2).nth(0),
"42 ");
522 EXPECT_EQ(by_max.nth(2).nth(1),
" x ");
528 ASSERT_EQ(by_lens.size(), 3u);
529 EXPECT_EQ(by_lens.nth(0).nth(0),
" id ");
530 EXPECT_EQ(by_lens.nth(0).nth(1),
" value ");
531 EXPECT_EQ(by_lens.nth(1).nth(0),
" 7 ");
532 EXPECT_EQ(by_lens.nth(1).nth(1),
" abc ");
533 EXPECT_EQ(by_lens.nth(2).nth(0),
" 42 ");
534 EXPECT_EQ(by_lens.nth(2).nth(1),
" x ");
536 std::ostringstream
out;
538 EXPECT_EQ(
out.str(),
" id value \n 7 abc \n 42 x \n");
541TEST(StringUtils, to_string_matrix_and_lines)
558TEST(StringUtilsCritical, justify_text_exact_wrapping_and_margins)
560 const auto justified =
justify_text(
"a bb c ddd e", 7, 2);
561 EXPECT_EQ(justified,
" a bb c\n ddd e");
564 ASSERT_EQ(lines.size(), 2u);
565 EXPECT_EQ(lines.nth(0).size(), 9u);
566 EXPECT_EQ(lines.nth(1),
" ddd e");
568 EXPECT_EQ(
justify_text(
"encyclopedia x", 5),
"encyclopedia\nx");
569 EXPECT_EQ(
justify_text(
" one\t two\nthree ", 6),
"one\ntwo\nthree");
572TEST(StringUtilsCritical, numeric_parsing_strictness)
588 EXPECT_NEAR(
safe_atof(
"-1.25e2"), -125.0, 1e-12);
589 EXPECT_NEAR(
safe_atof(
" +3.5"), 3.5, 1e-12);
591 EXPECT_THROW(
safe_atof(
""), runtime_error);
592 EXPECT_THROW(
safe_atof(
"abc"), runtime_error);
593 EXPECT_THROW(
safe_atof(
"1.2x"), runtime_error);
594 EXPECT_THROW(
safe_atof(
"3.5 "), runtime_error);
595 EXPECT_THROW(
safe_atof(
"1e309"), runtime_error);
596 EXPECT_THROW(
safe_atof(
"1e-5000"), runtime_error);
597 EXPECT_THROW(
safe_atof(
"nan"), runtime_error);
598 EXPECT_THROW(
safe_atof(
"inf"), runtime_error);
601TEST(StringUtils, split_text_into_words_and_lines)
604 ASSERT_EQ(
w.size(), 3u);
605 EXPECT_EQ(
w.nth(0),
"a");
606 EXPECT_EQ(
w.nth(1),
"b");
607 EXPECT_EQ(
w.nth(2),
"c");
610 ASSERT_EQ(
l.
size(), 2u);
611 EXPECT_EQ(
l.
nth(0),
"a");
612 EXPECT_EQ(
l.
nth(1),
"b");
615TEST(StringUtils, justify_and_align_except_first)
617 const string text =
"one two three four five";
627TEST(StringUtils, numeric_boundaries)
629 const auto max_st = std::numeric_limits<size_t>::max();
630 EXPECT_TRUE(
is_size_t(::std::to_string(max_st)));
631 EXPECT_FALSE(
is_size_t(::std::to_string(max_st) +
"0"));
633 const auto max_l = std::numeric_limits<long>::max();
634 const auto min_l = std::numeric_limits<long>::min();
635 EXPECT_TRUE(
is_long(::std::to_string(max_l)));
636 EXPECT_TRUE(
is_long(::std::to_string(min_l)));
637 EXPECT_FALSE(
is_long(::std::to_string(max_l) +
"0"));
642 const char *v = std::getenv(
"ALEPH_STRESS");
643 if (v ==
nullptr or *v ==
'\0')
647 const long m = std::strtol(v, &end, 10);
648 if (end == v or *end !=
'\0')
662 std::uniform_int_distribution<int> byte_dist(0, 255);
665 for (
size_t i = 0; i < len; ++i)
666 s.push_back(
static_cast<char>(byte_dist(
rng)));
672 static constexpr char alphabet[] =
673 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
674 std::uniform_int_distribution<int> pick(0,
int(
sizeof(alphabet) - 2));
677 for (
size_t i = 0; i < len; ++i)
678 s.push_back(alphabet[pick(
rng)]);
682TEST(StringUtilsStress, split_pos_recomposes)
684 std::mt19937
rng(12345);
688 for (
size_t pos = 0; pos <= s.size(); ++pos)
691 EXPECT_EQ(p.first + p.second, s);
692 EXPECT_EQ(p.first.size(), pos);
697TEST(StringUtilsStress, split_n_recomposes)
699 std::mt19937
rng(54321);
703 const size_t n = 1 + (size_t(iter) % std::min<size_t>(16, s.size()));
705 ASSERT_EQ(parts.size(), n);
707 parts.for_each([&](
const string &x) { recomposed += x; });
708 EXPECT_EQ(recomposed, s);
710 const size_t base = s.size() / n;
711 for (
size_t i = 0; i + 1 < n; ++i)
712 EXPECT_EQ(parts.nth(i).size(), base);
716TEST(StringUtilsStress, split_string_tokens_have_no_delims)
718 std::mt19937
rng(999);
719 static const string delims =
" _-";
723 const size_t tokens = 1 + (size_t(iter) % 12);
724 for (
size_t i = 0; i < tokens; ++i)
727 s.push_back(delims[
size_t(iter +
int(i)) % delims.size()]);
732 ASSERT_FALSE(
out.is_empty());
733 out.for_each([&](
const string &t)
735 EXPECT_FALSE(t.empty());
736 for (
char c : delims)
737 EXPECT_EQ(t.find(c), string::npos);
742TEST(StringUtilsStress, split_string_matches_reference_parser)
744 std::mt19937
rng(1337);
745 static const string delim_pool =
" ,;|/_-\t\n";
746 static const string token_pool =
747 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
749 std::uniform_int_distribution<int> delim_size_dist(0, 4);
750 std::uniform_int_distribution<int> delim_pick(0,
int(delim_pool.size() - 1));
751 std::uniform_int_distribution<int> token_pick(0,
int(token_pool.size() - 1));
752 std::uniform_int_distribution<int> text_len_dist(0, 128);
753 std::bernoulli_distribution use_delim(0.30);
758 const int dsz = delim_size_dist(
rng);
759 for (
int i = 0; i < dsz; ++i)
760 delims.push_back(delim_pool[
size_t(delim_pick(
rng))]);
763 const int len = text_len_dist(
rng);
764 text.reserve(
size_t(len));
765 for (
int i = 0; i < len; ++i)
766 if (not delims.empty()
and use_delim(
rng))
767 text.push_back(delims[
size_t(delim_pick(
rng)) % delims.size()]);
769 text.push_back(token_pool[
size_t(token_pick(
rng))]);
780TEST(StringUtilsStress, case_conversion_no_crash_and_preserves_length)
782 std::mt19937
rng(2024);
788 EXPECT_EQ(lo.size(), s.size());
789 EXPECT_EQ(up.size(), s.size());
793TEST(StringUtilsStress, build_pars_list_round_trip_for_ints)
795 std::mt19937
rng(77);
796 std::uniform_int_distribution<int> dist(-100000, 100000);
800 const int a = dist(
rng);
801 const int b = dist(
rng);
802 const int c = dist(
rng);
805 const auto expected = ::std::to_string(a) +
", " + ::std::to_string(b) +
", " + ::std::to_string(c);
String manipulation utilities.
static string random_ascii_token(std::mt19937 &rng, size_t len)
static string random_string(std::mt19937 &rng, size_t len)
static DynList< string > reference_split_string(const string &s, const string &delim)
Splits a string into non-empty tokens using a set of delimiter characters.
static void expect_tokens_eq(const DynList< string > &expected, const DynList< string > &got)
Asserts that two DynList<string> contain the same tokens in the same order.
static int stress_multiplier()
size_t size_t int32_t * out
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & append(const T &data)
Append a copy of data
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
size_t size() const noexcept
Return the number of items of the list, in constant time.
Minimal std::expected-style result type for C++20.
Type & nth(const size_t n)
Return the n-th item of the container.
Key * append(const Key &key)
Alias for insert() (copy version).
Main namespace for Aleph-w library functions.
std::string tolower(const char *str)
Convert a C std::string to lower-case.
bool is_prefix(const std::string &str, const std::string &prefix)
Check whether prefix is a prefix of str.
std::string remove_symbols(const std::string &str, const std::string &symbols)
Remove any character appearing in symbols.
std::string to_upper(const std::string &str)
Convert a std::string to upper-case (byte-wise).
DynList< std::string > split_text_into_lines(const std::string &text)
Split a text into lines by "\n".
std::string justify_line_except_first(const std::string &text, const size_t width, const size_t left_margin=0)
Justify all lines except the first one.
DynList< std::string > split_text_into_words(const std::string &text)
Split a text into whitespace-separated words.
Array< std::string > split_to_array(const std::string &s, const std::string &delim)
Split a std::string into an Aleph::Array<std::string>.
std::pair< std::string, std::string > split_pos(const std::string &str, const size_t pos)
Split a std::string at a fixed position.
std::string remove_prefix(std::string &str, const std::string &prefix)
Remove prefix from str if present.
std::string concat(const Args &...args)
Concatenate multiple arguments into a single std::string.
std::string & trim_in_place(std::string &s)
Trim a std::string in-place (leading + trailing whitespace removed).
std::string align_text_to_left(const std::string &text, const size_t page_width, const size_t left_margin=0)
Align text to the left by wrapping lines at page_width.
bool contains(const std::string_view &str, const std::string_view &substr)
Check if substr appears inside str.
std::string to_Pascalcase(const std::string &str)
Convert an identifier-like std::string to PascalCase.
DynList< DynList< std::string > > format_string_csv(const DynList< DynList< std::string > > &mat)
Produce a CSV-like matrix (commas added to all but last element in each row).
bool is_long(const std::string &str)
Check whether a std::string fully parses as a long.
DynList< std::string > split_n(const std::string &str, const size_t n)
Split a std::string into n parts.
and
Check uniqueness with explicit hash + equality functors.
std::string to_name(const std::string &str)
Uppercase the first character of str and return the resulting copy.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
bool is_float(const std::string &str)
Check whether a std::string fully parses as a finite float.
bool is_size_t(const std::string &str)
Check whether a std::string fully parses as a non-negative size_t.
std::string trim(const std::string &s)
Return a trimmed copy of a std::string (leading + trailing whitespace removed).
DynList< std::string > split_camel_case(const char *const str)
Split a camelCase / PascalCase std::string into tokens.
void build_pars_list(std::string &unused)
Base case for build_pars_list(std::string&, ...).
std::string remove_spaces(const std::string &str)
Remove all whitespace characters from a std::string.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
std::string to_str(const double d)
Convert double to a std::string with maximum round-trip precision.
std::string justify_text(const std::string &text, const size_t width, const size_t left_margin=0)
Justify a text to a target width.
bool is_double(const std::string &str)
Check whether a std::string fully parses as a finite double.
DynList< std::string > split_to_list(const std::string &s, const std::string &delim)
Split a std::string into an Aleph::DynList<std::string>.
std::string shift_lines_to_left(const std::string &str, const size_t n)
Indent every line in a multi-line std::string by n spaces.
DynList< DynList< T > > complete_rows(DynList< DynList< T > > &m)
Pad all rows of a matrix to the maximum row length.
std::string align_text_to_left_except_first(const std::string &text, const size_t width, const size_t left_margin=0)
Align all lines except the first one.
std::string toupper(const char *str)
Convert a C std::string to upper-case.
std::ostream & join(const C &c, const std::string &sep, std::ostream &out)
Join elements of an Aleph-style container into a stream.
std::vector< std::string > & split(const std::string &s, const char delim, std::vector< std::string > &elems)
Split a std::string by a single delimiter character.
DynList< DynList< std::string > > format_string(const DynList< size_t > &lens, const DynList< DynList< std::string > > &mat)
double safe_atof(const std::string &s)
Convert a std::string to double and throw on parse errors.
std::string to_lower(const std::string &str)
Convert a std::string to lower-case (byte-wise).
void fill_string(std::string &str, char sym)
Fill all the content of std::string with a defined char.
std::string & mutable_tolower(std::string &str)
Convert a std::string to lower-case in-place.
std::string & mutable_toupper(std::string &str)
Convert a std::string to upper-case in-place.
std::string only_alpha(const std::string &str)
Extract alphanumeric ASCII characters and normalize letters to lower-case.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)