// SPDX-License-Identifier: GPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® // Word wrap and ellipsis over a fixed-advance measure (every code point // ten pixels), so the expected byte offsets and widths are exact. import std; import ImsdMessages; using namespace ImsdMessages; namespace { std::int32_t failures = 0; void Check(bool ok, std::string_view what) { std::println("{} {}", ok ? "PASS" : "FAIL", what); if (!ok) ++failures; } void CheckEqual(std::string_view actual, std::string_view expected, std::string_view what) { const bool ok = actual == expected; if (ok) std::println("PASS {}", what); else std::println("FAIL {} — expected \"{}\", got \"{}\"", what, expected, actual); if (!ok) ++failures; } } std::string Show(const std::vector& lines) { std::string out; for (const WrappedLine& l : lines) out += std::format("[{},{}){} ", l.begin, l.end, l.width); return out; } void CheckLines(const std::vector& actual, const std::vector& expected, std::string_view what) { const bool ok = actual == expected; if (ok) std::println("PASS {}", what); else std::println("FAIL {} — expected {} got {}", what, Show(expected), Show(actual)); if (!ok) ++failures; } int main() { WordWrapper wrap([](std::uint32_t, float) { return 10.0f; }); CheckLines(wrap.Wrap("hello world", 10, 200), { { 0, 11, 110 } }, "fits on one line"); CheckLines(wrap.Wrap("hello world", 10, 60), { { 0, 5, 50 }, { 6, 11, 50 } }, "breaks at the space, the space in neither line"); CheckLines(wrap.Wrap("hello world", 10, 60), { { 0, 5, 50 }, { 7, 12, 50 } }, "a run of spaces is skipped at the break"); CheckLines(wrap.Wrap("ab cdef", 10, 50), { { 0, 2, 20 }, { 4, 8, 40 } }, "a run of spaces before the overflow goes to neither line"); CheckLines(wrap.Wrap("abcdef ghi", 10, 60), { { 0, 6, 60 }, { 7, 10, 30 } }, "a split forced at a space starts the next line after it"); CheckLines(wrap.Wrap("abcdefghij", 10, 35), { { 0, 3, 30 }, { 3, 6, 30 }, { 6, 9, 30 }, { 9, 10, 10 } }, "a long word splits between code points"); CheckLines(wrap.Wrap("a\nb", 10, 100), { { 0, 1, 10 }, { 2, 3, 10 } }, "a newline is a hard break"); CheckLines(wrap.Wrap("a\n\nb", 10, 100), { { 0, 1, 10 }, { 2, 2, 0 }, { 3, 4, 10 } }, "an empty line between two newlines"); CheckLines(wrap.Wrap("ab\n", 10, 50), { { 0, 2, 20 }, { 3, 3, 0 } }, "a text ending in a newline has an empty last line"); CheckLines(wrap.Wrap("", 10, 100), { { 0, 0, 0 } }, "empty text is one empty line"); CheckLines(wrap.Wrap("well-known word", 10, 60), { { 0, 5, 50 }, { 5, 10, 50 }, { 11, 15, 40 } }, "a hyphen is a break opportunity that stays on its line"); CheckLines(wrap.Wrap("héllo wörld", 10, 60), { { 0, 6, 50 }, { 7, 13, 50 } }, "multi-byte code points count once"); CheckLines(wrap.Wrap("x", 10, 5), { { 0, 1, 10 } }, "a single code point wider than the line is kept"); CheckLines(wrap.Wrap("ab cd ef", 10, 50), { { 0, 5, 50 }, { 6, 8, 20 } }, "a space that overflows ends the line after the words that fit"); CheckLines(wrap.Wrap("abcde ", 10, 50), { { 0, 5, 50 } }, "a trailing space that overflows opens no empty line"); CheckLines(wrap.Wrap("abcd ", 10, 50), { { 0, 4, 40 } }, "nor does a run of them"); CheckLines(wrap.Wrap(" \nabc", 10, 30), { { 0, 3, 30 }, { 6, 9, 30 } }, "a run of spaces wider than the line, before a newline: the split forced at a space opens no blank line either"); CheckLines(wrap.Wrap("x\n ", 10, 30), { { 0, 1, 10 }, { 2, 5, 30 } }, "nor at the text's end"); CheckLines(wrap.Wrap("abcde \nx", 10, 50), { { 0, 5, 50 }, { 7, 8, 10 } }, "a space that overflows before a newline leaves no blank line between the two"); CheckLines(wrap.Wrap("abcde \n", 10, 50), { { 0, 5, 50 }, { 7, 7, 0 } }, "a text ending in a newline keeps its empty last line"); CheckLines(wrap.Wrap(" abcdef", 10, 30), { { 0, 3, 30 }, { 3, 6, 30 }, { 6, 7, 10 } }, "a leading space is no break: no empty first line, the space stays on its line"); CheckLines(wrap.Wrap("x\n abcd", 10, 30), { { 0, 1, 10 }, { 2, 5, 30 }, { 5, 7, 20 } }, "nor is a space right after a newline"); CheckLines(wrap.Wrap(" abcdef", 10, 30), { { 0, 3, 30 }, { 3, 6, 30 }, { 6, 8, 20 } }, "nor is a run of them: the line opens with its first word, the spaces before it on that line"); CheckLines(wrap.Wrap("x\n abcd", 10, 30), { { 0, 1, 10 }, { 2, 5, 30 }, { 5, 8, 30 } }, "the same after a newline"); const std::vector& first = wrap.Wrap("cached text", 10, 60); const std::vector& again = wrap.Wrap("cached text", 10, 60); Check(&first == &again, "the same text, width and size hit the cache"); const std::size_t before = wrap.CacheSize(); wrap.Wrap("cached text", 10, 70); Check(wrap.CacheSize() == before + 1, "another width is another entry"); CheckEqual(wrap.Ellipsize("hello world", 10, 200), "hello world", "ellipsis: fits"); CheckEqual(wrap.Ellipsize("hello world", 10, 60), "hello…", "ellipsis: cut with room for the mark"); CheckEqual(wrap.Ellipsize("hello world", 10, 110), "hello world", "ellipsis: exactly fits"); CheckEqual(wrap.Ellipsize("x", 10, 5), "…", "ellipsis: nothing fits"); CheckEqual(wrap.Ellipsize("line one\nline two", 10, 200), "line one…", "ellipsis: a preview stops at the first newline"); std::size_t i = 0; Check(DecodeUtf8("é", i) == 0xE9 && i == 2, "DecodeUtf8 of a two-byte sequence"); i = 0; Check(DecodeUtf8("\xff", i) == 0xFFFD && i == 1, "DecodeUtf8 of a stray byte consumes it"); return failures == 0 ? 0 : 1; }