summaryrefslogtreecommitdiffstats
path: root/intl/icu_capi/cpp/examples/locale
diff options
context:
space:
mode:
Diffstat (limited to 'intl/icu_capi/cpp/examples/locale')
-rw-r--r--intl/icu_capi/cpp/examples/locale/.gitignore2
-rw-r--r--intl/icu_capi/cpp/examples/locale/Makefile28
-rw-r--r--intl/icu_capi/cpp/examples/locale/test.cpp107
3 files changed, 137 insertions, 0 deletions
diff --git a/intl/icu_capi/cpp/examples/locale/.gitignore b/intl/icu_capi/cpp/examples/locale/.gitignore
new file mode 100644
index 0000000000..cb34c546b3
--- /dev/null
+++ b/intl/icu_capi/cpp/examples/locale/.gitignore
@@ -0,0 +1,2 @@
+a.out
+a.out.dSYM
diff --git a/intl/icu_capi/cpp/examples/locale/Makefile b/intl/icu_capi/cpp/examples/locale/Makefile
new file mode 100644
index 0000000000..57a9c06943
--- /dev/null
+++ b/intl/icu_capi/cpp/examples/locale/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
+
+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/locale/test.cpp b/intl/icu_capi/cpp/examples/locale/test.cpp
new file mode 100644
index 0000000000..b2bba0ceff
--- /dev/null
+++ b/intl/icu_capi/cpp/examples/locale/test.cpp
@@ -0,0 +1,107 @@
+// 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/ICU4XLocale.hpp"
+#include "../../include/ICU4XLogger.hpp"
+
+#include <iostream>
+
+static bool test_locale(ICU4XLocale &locale, std::string_view expectedString,
+ const char *message) {
+ std::string actualString = locale.to_string().ok().value();
+ std::cout << message << ": \"" << actualString << "\"" << std::endl;
+ if (actualString != expectedString) {
+ std::cout << "Locale did not match expected: \"" << expectedString << "\""
+ << std::endl;
+ return false;
+ }
+ return true;
+}
+
+static bool test_string(std::string_view actualString,
+ std::string_view expectedString, const char *message) {
+ std::cout << message << ": \"" << actualString << "\"" << std::endl;
+ if (actualString != expectedString) {
+ std::cout << "String did not match expected: \"" << expectedString << "\""
+ << std::endl;
+ return false;
+ }
+ return true;
+}
+
+int main() {
+ ICU4XLogger::init_simple_logger();
+ ICU4XLocale locale = ICU4XLocale::create_from_string("es-ES").ok().value();
+ if (!test_locale(locale, "es-ES", "Created a locale")) {
+ return 1;
+ }
+
+ locale.set_language("en").ok();
+ if (!test_locale(locale, "en-ES", "The language can be updated")) {
+ return 1;
+ }
+
+ locale.set_region("US").ok();
+ if (!test_locale(locale, "en-US", "The region can be updated")) {
+ return 1;
+ }
+
+ locale.set_script("Latn").ok();
+ if (!test_locale(locale, "en-Latn-US", "The script can be updated")) {
+ return 1;
+ }
+
+ if (!test_string(locale.language().ok().value(), "en",
+ "The language can be accessed")) {
+ return 1;
+ }
+ if (!test_string(locale.region().ok().value(), "US",
+ "The region can be accessed")) {
+ return 1;
+ }
+ if (!test_string(locale.script().ok().value(), "Latn",
+ "The script can be accessed")) {
+ return 1;
+ }
+
+ locale.set_language("").ok();
+ if (!test_locale(locale, "und-Latn-US", "Removed the language")) {
+ return 1;
+ }
+
+ locale.set_region("").ok();
+ if (locale.region().is_ok()) {
+ std::cout << "Expected region to be an err" << std::endl;
+ return 1;
+ }
+ if (!test_locale(locale, "und-Latn", "Removed the region")) {
+ return 1;
+ }
+
+ locale.set_script("").ok();
+ if (locale.script().is_ok()) {
+ std::cout << "Expected script to be an err" << std::endl;
+ return 1;
+ }
+ if (!test_locale(locale, "und", "Removed the script")) {
+ return 1;
+ }
+
+ locale = ICU4XLocale::create_from_string("en-US-u-hc-h12").ok().value();
+ if (!test_string(locale.get_unicode_extension("hc").ok().value(), "h12",
+ "The unicode extension can be accessed")) {
+ return 1;
+ }
+ if (!test_string(locale.basename().ok().value(), "en-US",
+ "The basename can be accessed")) {
+ return 1;
+ }
+
+ locale = ICU4XLocale::create_und();
+ if (!test_locale(locale, "und", "Created an undefined locale")) {
+ return 1;
+ }
+
+ return 0;
+}