diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /intl/icu_capi/cpp/examples/bidi | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'intl/icu_capi/cpp/examples/bidi')
-rw-r--r-- | intl/icu_capi/cpp/examples/bidi/.gitignore | 2 | ||||
-rw-r--r-- | intl/icu_capi/cpp/examples/bidi/Makefile | 28 | ||||
-rw-r--r-- | intl/icu_capi/cpp/examples/bidi/test.cpp | 93 |
3 files changed, 123 insertions, 0 deletions
diff --git a/intl/icu_capi/cpp/examples/bidi/.gitignore b/intl/icu_capi/cpp/examples/bidi/.gitignore new file mode 100644 index 0000000000..cb34c546b3 --- /dev/null +++ b/intl/icu_capi/cpp/examples/bidi/.gitignore @@ -0,0 +1,2 @@ +a.out +a.out.dSYM diff --git a/intl/icu_capi/cpp/examples/bidi/Makefile b/intl/icu_capi/cpp/examples/bidi/Makefile new file mode 100644 index 0000000000..594ca66c37 --- /dev/null +++ b/intl/icu_capi/cpp/examples/bidi/Makefile @@ -0,0 +1,28 @@ +# This file is part of ICU4X. For terms of use, please see the file +# called LICENSE at the top level of the ICU4X source tree +# (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +.DEFAULT_GOAL := test +.PHONY: build test +FORCE: + +ALL_HEADERS := $(wildcard ../../include/*.hpp) $(wildcard ../../../c/include/*.h) + +CXX?=g++ + +$(ALL_HEADERS): + +../../../../../target/debug/libicu_capi_staticlib.a: FORCE + cargo build -p icu_capi_staticlib --features compiled_data + +a.out: ../../../../../target/debug/libicu_capi_staticlib.a $(ALL_HEADERS) test.cpp + $(CXX) -std=c++17 test.cpp ../../../../../target/debug/libicu_capi_staticlib.a -ldl -lpthread -lm -g + +build: a.out + +test: build + ./a.out + +clean: + git clean -xf * + rm -f ../../../../../target/debug/libicu_capi_staticlib.a diff --git a/intl/icu_capi/cpp/examples/bidi/test.cpp b/intl/icu_capi/cpp/examples/bidi/test.cpp new file mode 100644 index 0000000000..629cf752ce --- /dev/null +++ b/intl/icu_capi/cpp/examples/bidi/test.cpp @@ -0,0 +1,93 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +#include "../../include/ICU4XDataProvider.hpp" +#include "../../include/ICU4XBidi.hpp" +#include "../../include/ICU4XLogger.hpp" + +#include <iostream> + +int main() { + ICU4XDataProvider dp = ICU4XDataProvider::create_compiled(); + auto bidi = ICU4XBidi::create(dp).ok().value(); + + // Written char-by-char to avoid messing up certain text editors. + std::string_view str = + "א" + "ב" + "ג" + "a" + "b" + "c" + "\n" + "a" + "b" + "c" + "א" + "ב" + "ג"; + + // reordered string is same for both lines + std::string_view reordered_expected = + "a" + "b" + "c" + "ג" + "ב" + "א"; + auto bidi_info = bidi.for_text(str, ICU4XBidi::level_ltr()); + auto n_para = bidi_info.paragraph_count(); + if (n_para != 2) { + std::cout << "Expected 2 paragraphs, found " << n_para << std::endl; + return 1; + } + + auto para = bidi_info.paragraph_at(0).value(); + + auto size = para.size(); + if (size != 10) { + std::cout << "Expected paragraph of size 10, found " << size << std::endl; + return 1; + } + + // The first paragraph's first strongly directional character is RTL + uint8_t level = para.level_at(0); + std::cout << "Level of first paragraph at index 0 is " << unsigned(level) << std::endl; + if (!ICU4XBidi::level_is_rtl(level)) { + std::cout << "Expected level at index 0 to be RTL" << std::endl; + return 1; + } + + + std::string reordered = para.reorder_line(0, 9).ok().value(); + std::cout << "Reordered paragraph: " << reordered << std::endl; + + if (reordered != reordered) { + std::cout << "Found incorrect reordering, expected: " << reordered << std::endl; + } + + para.set_paragraph_in_text(1).ok().value(); + + size = para.size(); + if (size != 9) { + std::cout << "Expected paragraph of size 9, found " << size << std::endl; + return 1; + } + + // The second paragraph's first strongly directional character is LTR + level = para.level_at(0); + std::cout << "Level of second paragraph at index 0 is " << unsigned(level) << std::endl; + if (!ICU4XBidi::level_is_ltr(level)) { + std::cout << "Expected level at index 0 to be LTR" << std::endl; + return 1; + } + reordered = para.reorder_line(10, 19).ok().value(); + std::cout << "Reordered paragraph: " << reordered << std::endl; + + if (reordered != reordered) { + std::cout << "Found incorrect reordering, expected: " << reordered << std::endl; + } + + return 0; +} |