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/c/examples/locale | |
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/c/examples/locale')
-rw-r--r-- | intl/icu_capi/c/examples/locale/.gitignore | 2 | ||||
-rw-r--r-- | intl/icu_capi/c/examples/locale/Makefile | 26 | ||||
-rw-r--r-- | intl/icu_capi/c/examples/locale/test.c | 267 |
3 files changed, 295 insertions, 0 deletions
diff --git a/intl/icu_capi/c/examples/locale/.gitignore b/intl/icu_capi/c/examples/locale/.gitignore new file mode 100644 index 0000000000..cb34c546b3 --- /dev/null +++ b/intl/icu_capi/c/examples/locale/.gitignore @@ -0,0 +1,2 @@ +a.out +a.out.dSYM diff --git a/intl/icu_capi/c/examples/locale/Makefile b/intl/icu_capi/c/examples/locale/Makefile new file mode 100644 index 0000000000..b068183f41 --- /dev/null +++ b/intl/icu_capi/c/examples/locale/Makefile @@ -0,0 +1,26 @@ +# 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/*.h) + +$(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.c + gcc test.c ../../../../../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/c/examples/locale/test.c b/intl/icu_capi/c/examples/locale/test.c new file mode 100644 index 0000000000..65f1fbad36 --- /dev/null +++ b/intl/icu_capi/c/examples/locale/test.c @@ -0,0 +1,267 @@ +// 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/ICU4XLocaleCanonicalizer.h" +#include "../../include/ICU4XLocaleExpander.h" +#include "../../include/ICU4XLogger.h" +#include <string.h> +#include <stdio.h> + +/** + * A helper for testing the locale with nice error messages. + */ +bool test_locale(ICU4XLocale* locale, const char* message, const char* expected) { + char output[40]; + + // Test setters + DiplomatWriteable write = diplomat_simple_writeable(output, 40); + diplomat_result_void_ICU4XError result = ICU4XLocale_to_string(locale, &write); + if (!result.is_ok) { + return 1; + } + printf("Output for %s is \"%s\"\n", message, output); + if (strcmp(output, expected) != 0) { + printf("Output does not match expected output!\n"); + return false; + } + return true; +} + +ICU4XLocale* get_locale(const char* localeText) { + diplomat_result_box_ICU4XLocale_ICU4XError locale_result = ICU4XLocale_create_from_string(localeText, strlen(localeText)); + if (!locale_result.is_ok) { + printf("Could not create locale from: %s", localeText); + } + return locale_result.ok; +} + + +int main() { + ICU4XLogger_init_simple_logger(); + char output[40]; + + // Test creating a locale. + DiplomatWriteable write = diplomat_simple_writeable(output, 40); + diplomat_result_box_ICU4XLocale_ICU4XError locale_result = ICU4XLocale_create_from_string("ar", 2); + if (!locale_result.is_ok) { + return 1; + } + ICU4XLocale* locale = locale_result.ok; + diplomat_result_void_ICU4XError result = ICU4XLocale_to_string(locale, &write); + if (!result.is_ok) { + return 1; + } + printf("Output for a basic locale is %s\n", output); + const char* expected = u8"ar"; + if (strcmp(output, expected) != 0) { + printf("Output does not match expected output!\n"); + return 1; + } + ICU4XLocale_destroy(locale); + + // Test some accessors. + write = diplomat_simple_writeable(output, 40); + locale_result = ICU4XLocale_create_from_string("fr-FR-u-hc-h23", 14); + if (!locale_result.is_ok) { + return 1; + } + locale = locale_result.ok; + result = ICU4XLocale_language(locale, &write); + if (!result.is_ok) { + return 1; + } + printf("Output for the language is %s\n", output); + expected = u8"fr"; + if (strcmp(output, expected) != 0) { + printf("Output does not match expected output!\n"); + return 1; + } + + write = diplomat_simple_writeable(output, 40); + result = ICU4XLocale_region(locale, &write); + if (!result.is_ok) { + return 1; + } + printf("Output for the region is %s\n", output); + expected = u8"FR"; + if (strcmp(output, expected) != 0) { + printf("Output does not match expected output!\n"); + return 1; + } + + write = diplomat_simple_writeable(output, 40); + result = ICU4XLocale_get_unicode_extension(locale, "hc", 2, &write); + if (!result.is_ok) { + return 1; + } + printf("Output for the extension is %s\n", output); + expected = u8"h23"; + if (strcmp(output, expected) != 0) { + printf("Output does not match expected output!\n"); + return 1; + } + + result = ICU4XLocale_get_unicode_extension(locale, "ca", 2, &write); + if (!(!result.is_ok && result.err == ICU4XError_LocaleUndefinedSubtagError)) { + return 1; + } + + // Test setting the language + write = diplomat_simple_writeable(output, 40); + const char* str = "fr-FR-u-hc-h23"; + locale_result = ICU4XLocale_create_from_string(str, strlen(str)); + if (!locale_result.is_ok) { + printf("Could not create the locale."); + return 1; + } + locale = locale_result.ok; + str = "zh"; + result = ICU4XLocale_set_language(locale, str, strlen(str)); + if (!result.is_ok) { + printf("Could not set the language tag."); + return 1; + } + if (!test_locale(locale, "setting the language tag", "zh-FR-u-hc-h23")) { + return 1; + } + ICU4XLocale_destroy(locale); + + // Test setting the region + write = diplomat_simple_writeable(output, 40); + str = "es-ES-u-hc-h23"; + locale_result = ICU4XLocale_create_from_string(str, strlen(str)); + if (!locale_result.is_ok) { + printf("Could not create the locale."); + return 1; + } + locale = locale_result.ok; + if (!test_locale(locale, "The region starts as es-ES", "es-ES-u-hc-h23")) { + return 1; + } + str = "MX"; + result = ICU4XLocale_set_region(locale, str, strlen(str)); + if (!result.is_ok) { + printf("Could not set the region."); + return 1; + } + if (!test_locale(locale, "setting the region", "es-MX-u-hc-h23")) { + return 1; + } + ICU4XLocale_destroy(locale); + + // Test setting the script + write = diplomat_simple_writeable(output, 40); + str = "en-US"; + locale_result = ICU4XLocale_create_from_string(str, strlen(str)); + if (!locale_result.is_ok) { + printf("Could not create the locale."); + return 1; + } + locale = locale_result.ok; + if (!test_locale(locale, "initial script-less locale", "en-US")) { + return 1; + } + str = "Latn"; + result = ICU4XLocale_set_script(locale, str, strlen(str)); + if (!result.is_ok) { + printf("Could not set the script."); + return 1; + } + if (!test_locale(locale, "setting the script", "en-Latn-US")) { + return 1; + } + result = ICU4XLocale_set_script(locale, "", 0); + if (!result.is_ok) { + printf("Could not set the script."); + return 1; + } + if (!test_locale(locale, "removing the script", "en-US")) { + return 1; + } + + ICU4XLocale_destroy(locale); + + // Create a LocaleCanonicalizer and LocaleExpander. + ICU4XDataProvider* provider = ICU4XDataProvider_create_compiled(); + diplomat_result_box_ICU4XLocaleCanonicalizer_ICU4XError result2 = ICU4XLocaleCanonicalizer_create(provider); + if (!result2.is_ok) { + printf("Could not construct Locale Canonicalizer"); + return 1; + } + ICU4XLocaleCanonicalizer* lc = result2.ok; + diplomat_result_box_ICU4XLocaleExpander_ICU4XError result3 = ICU4XLocaleExpander_create(provider); + if (!result3.is_ok) { + printf("Could not construct Locale Canonicalizer"); + return 1; + } + ICU4XLocaleExpander* le = result3.ok; + + // Test maximize. + write = diplomat_simple_writeable(output, 40); + locale_result = ICU4XLocale_create_from_string("und", 3); + if (!locale_result.is_ok) { + printf("Could not create the locale."); + return 1; + } + locale = locale_result.ok; + ICU4XLocaleExpander_maximize(le, locale); + result = ICU4XLocale_to_string(locale, &write); + if (!result.is_ok) { + return 1; + } + printf("Output for maximizing is %s\n", output); + expected = u8"en-Latn-US"; + if (strcmp(output, expected) != 0) { + printf("Output does not match expected output!\n"); + return 1; + } + ICU4XLocale_destroy(locale); + + // Test minimize. + write = diplomat_simple_writeable(output, 40); + locale_result = ICU4XLocale_create_from_string("zh-Hant", 7); + if (!locale_result.is_ok) { + printf("Could not create the locale."); + return 1; + } + locale = locale_result.ok; + ICU4XLocaleExpander_minimize(le, locale); + result = ICU4XLocale_to_string(locale, &write); + if (!result.is_ok) { + return 1; + } + printf("Output for minimizing is %s\n", output); + expected = u8"zh-TW"; + if (strcmp(output, expected) != 0) { + printf("Output does not match expected output!\n"); + return 1; + } + ICU4XLocale_destroy(locale); + + // Test canonicalize. + write = diplomat_simple_writeable(output, 40); + locale_result = ICU4XLocale_create_from_string("no-nynorsk", 10); + if (!locale_result.is_ok) { + printf("Could not create the locale."); + return 1; + } + locale = locale_result.ok; + ICU4XLocaleCanonicalizer_canonicalize(lc, locale); + result = ICU4XLocale_to_string(locale, &write); + if (!result.is_ok) { + return 1; + } + printf("Output for canonicalizing is %s\n", output); + expected = u8"nn"; + if (strcmp(output, expected) != 0) { + printf("Output does not match expected output!\n"); + return 1; + } + ICU4XLocale_destroy(locale); + + ICU4XLocaleCanonicalizer_destroy(lc); + ICU4XLocaleExpander_destroy(le); + + return 0; +} |