+static void MessageLength(void)
+{
+ UErrorCode status = U_ZERO_ERROR;
+ const char patChars[] = {"123{0}456{0}"};
+ const char expectedChars[] = {"123abc"};
+ UChar pattern[sizeof(patChars)];
+ UChar arg[] = {0x61,0x62,0x63,0};
+ UChar result[128] = {0};
+ UChar expected[sizeof(expectedChars)];
+
+ u_uastrncpy(pattern, patChars, UPRV_LENGTHOF(pattern));
+ u_uastrncpy(expected, expectedChars, UPRV_LENGTHOF(expected));
+
+ u_formatMessage("en_US", pattern, 6, result, UPRV_LENGTHOF(result), &status, arg);
+ if (U_FAILURE(status)) {
+ log_err("u_formatMessage method failed. Error: %s \n",u_errorName(status));
+ }
+ if (u_strcmp(result, expected) != 0) {
+ log_err("u_formatMessage didn't return expected result\n");
+ }
+}
+
+static void TestMessageWithUnusedArgNumber(void) {
+ UErrorCode errorCode = U_ZERO_ERROR;
+ U_STRING_DECL(pattern, "abc {1} def", 11);
+ UChar x[2] = { 0x78, 0 }; // "x"
+ UChar y[2] = { 0x79, 0 }; // "y"
+ U_STRING_DECL(expected, "abc y def", 9);
+ UChar result[20];
+ int32_t length;
+
+ U_STRING_INIT(pattern, "abc {1} def", 11);
+ U_STRING_INIT(expected, "abc y def", 9);
+ length = u_formatMessage("en", pattern, -1, result, UPRV_LENGTHOF(result), &errorCode, x, y);
+ if (U_FAILURE(errorCode) || length != u_strlen(expected) || u_strcmp(result, expected) != 0) {
+ log_err("u_formatMessage(pattern with only {1}, 2 args) failed: result length %d, UErrorCode %s \n",
+ (int)length, u_errorName(errorCode));
+ }
+}
+
+static void TestErrorChaining(void) {
+ UErrorCode status = U_USELESS_COLLATOR_ERROR;
+
+ umsg_open(NULL, 0, NULL, NULL, &status);
+ umsg_applyPattern(NULL, NULL, 0, NULL, &status);
+ umsg_toPattern(NULL, NULL, 0, &status);
+ umsg_clone(NULL, &status);
+ umsg_format(NULL, NULL, 0, &status);
+ umsg_parse(NULL, NULL, 0, NULL, &status);
+ umsg_close(NULL);
+
+ /* All of this code should have done nothing. */
+ if (status != U_USELESS_COLLATOR_ERROR) {
+ log_err("Status got changed to %s\n", u_errorName(status));
+ }
+
+ status = U_ZERO_ERROR;
+ umsg_open(NULL, 0, NULL, NULL, &status);
+ if (status != U_ILLEGAL_ARGUMENT_ERROR) {
+ log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status));
+ }
+ status = U_ZERO_ERROR;
+ umsg_applyPattern(NULL, NULL, 0, NULL, &status);
+ if (status != U_ILLEGAL_ARGUMENT_ERROR) {
+ log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status));
+ }
+ status = U_ZERO_ERROR;
+ umsg_toPattern(NULL, NULL, 0, &status);
+ if (status != U_ILLEGAL_ARGUMENT_ERROR) {
+ log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status));
+ }
+ status = U_ZERO_ERROR;
+ umsg_clone(NULL, &status);
+ if (status != U_ILLEGAL_ARGUMENT_ERROR) {
+ log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status));
+ }
+}