summaryrefslogtreecommitdiffstats
path: root/intl/icu_capi/c/examples/fixeddecimal
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /intl/icu_capi/c/examples/fixeddecimal
parentInitial commit. (diff)
downloadfirefox-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/fixeddecimal')
-rw-r--r--intl/icu_capi/c/examples/fixeddecimal/.gitignore2
-rw-r--r--intl/icu_capi/c/examples/fixeddecimal/Makefile26
-rw-r--r--intl/icu_capi/c/examples/fixeddecimal/test.c98
3 files changed, 126 insertions, 0 deletions
diff --git a/intl/icu_capi/c/examples/fixeddecimal/.gitignore b/intl/icu_capi/c/examples/fixeddecimal/.gitignore
new file mode 100644
index 0000000000..cb34c546b3
--- /dev/null
+++ b/intl/icu_capi/c/examples/fixeddecimal/.gitignore
@@ -0,0 +1,2 @@
+a.out
+a.out.dSYM
diff --git a/intl/icu_capi/c/examples/fixeddecimal/Makefile b/intl/icu_capi/c/examples/fixeddecimal/Makefile
new file mode 100644
index 0000000000..b068183f41
--- /dev/null
+++ b/intl/icu_capi/c/examples/fixeddecimal/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/fixeddecimal/test.c b/intl/icu_capi/c/examples/fixeddecimal/test.c
new file mode 100644
index 0000000000..edf4523431
--- /dev/null
+++ b/intl/icu_capi/c/examples/fixeddecimal/test.c
@@ -0,0 +1,98 @@
+// 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/ICU4XFixedDecimalFormatter.h"
+#include "../../include/ICU4XLogger.h"
+#include <string.h>
+#include <stdio.h>
+
+int main() {
+ ICU4XLogger_init_simple_logger();
+ diplomat_result_box_ICU4XLocale_ICU4XError locale_result = ICU4XLocale_create_from_string("bn", 2);
+ if (!locale_result.is_ok) {
+ return 1;
+ }
+ ICU4XLocale* locale = locale_result.ok;
+ ICU4XDataProvider* provider = ICU4XDataProvider_create_compiled();
+
+ ICU4XFixedDecimal* decimal = ICU4XFixedDecimal_create_from_u64(1000007);
+
+ diplomat_result_box_ICU4XFixedDecimalFormatter_ICU4XError fdf_result =
+ ICU4XFixedDecimalFormatter_create_with_grouping_strategy(provider, locale, ICU4XFixedDecimalGroupingStrategy_Auto);
+ if (!fdf_result.is_ok) {
+ printf("Failed to create FixedDecimalFormatter\n");
+ return 1;
+ }
+ ICU4XFixedDecimalFormatter* fdf = fdf_result.ok;
+ char output[40];
+
+ DiplomatWriteable write = diplomat_simple_writeable(output, 40);
+
+ bool success = ICU4XFixedDecimalFormatter_format(fdf, decimal, &write).is_ok;
+ if (!success) {
+ printf("Failed to write result of FixedDecimalFormatter::format to string.\n");
+ return 1;
+ }
+ printf("Output is %s\n", output);
+
+ const char* expected = u8"১০,০০,০০৭";
+ if (strcmp(output, expected) != 0) {
+ printf("Output does not match expected output!\n");
+ return 1;
+ }
+
+ ICU4XFixedDecimal_multiply_pow10(decimal, 2);
+ if (!success) {
+ printf("Failed to multiply FixedDecimal\n");
+ return 1;
+ }
+
+ ICU4XFixedDecimal_set_sign(decimal, ICU4XFixedDecimalSign_Negative);
+
+ write = diplomat_simple_writeable(output, 40);
+
+ success = ICU4XFixedDecimalFormatter_format(fdf, decimal, &write).is_ok;
+ if (!success) {
+ printf("Failed to write result of FixedDecimalFormatter::format to string.\n");
+ return 1;
+ }
+ printf("Output x100 and negated is %s\n", output);
+
+ expected = u8"-১০,০০,০০,৭০০";
+ if (strcmp(output, expected) != 0) {
+ printf("Output does not match expected output!\n");
+ return 1;
+ }
+
+ ICU4XFixedDecimal_destroy(decimal);
+
+ diplomat_result_box_ICU4XFixedDecimal_ICU4XError fd_result = ICU4XFixedDecimal_create_from_string("1000007.070", 11);
+ if (!fd_result.is_ok) {
+ printf("Failed to create FixedDecimal from string.\n");
+ return 1;
+ }
+ decimal = fd_result.ok;
+
+ write = diplomat_simple_writeable(output, 40);
+
+ success = ICU4XFixedDecimalFormatter_format(fdf, decimal, &write).is_ok;
+ if (!success) {
+ printf("Failed to write result of FixedDecimalFormatter::format to string.\n");
+ return 1;
+ }
+ printf("Output is %s\n", output);
+
+ expected = u8"১০,০০,০০৭.০৭০";
+ if (strcmp(output, expected) != 0) {
+ printf("Output does not match expected output!\n");
+ return 1;
+ }
+
+ ICU4XFixedDecimal_destroy(decimal);
+ ICU4XFixedDecimalFormatter_destroy(fdf);
+ ICU4XLocale_destroy(locale);
+ ICU4XDataProvider_destroy(provider);
+
+ return 0;
+}