+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
/********************************************************************
* COPYRIGHT:
- * Copyright (c) 1997-2012, International Business Machines Corporation and
+ * Copyright (c) 1997-2016, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************
*
#include "cintltst.h"
#include "cmsgtst.h"
#include "cformtst.h"
-
-#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
+#include "cmemory.h"
static const char* const txt_testCasePatterns[] = {
"Quotes '', '{', a {0,number,integer} '{'0}",
UMessageFormat formatter = umsg_open(testCasePatterns[0],patternLength,"en_US",NULL,&ec);
if(U_FAILURE(ec)){
- log_data_err("umsg_open() failed for testCasePattens[%d]. -> %s (Are you missing data?)\n",i, u_errorName(ec));
+ log_data_err("umsg_open() failed for testCasePattens[0]. -> %s (Are you missing data?)\n", u_errorName(ec));
return;
}
for(i = 0;i<cnt_testCases; i++){
UDate d2=0;
result=NULL;
- patternLength = u_strlen(testCasePatterns[i]);
-
+ // Alternate between specifying the length and using NUL-termination.
+ patternLength = ((i & 1) == 0) ? u_strlen(testCasePatterns[i]) : -1;
+
umsg_applyPattern(formatter,testCasePatterns[i],patternLength,&parseError,&ec);
if(U_FAILURE(ec)){
log_err("umsg_applyPattern() failed for testCasePattens[%d].\n",i);
static void TestNewFormatAndParseAPI(void)
{
- UChar *result, tzID[4], str[25];
+ UChar *result = NULL, tzID[4], str[25];
UChar pattern[100];
UChar expected[100];
int32_t resultLengthOut, resultlength;
UCalendar *cal;
UDate d1,d;
- UDateFormat *def1;
+ UDateFormat *def1 = NULL;
UErrorCode status = U_ZERO_ERROR;
int32_t value = 0;
UChar ret[30];
cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
if(U_FAILURE(status)){
log_data_err("error in ucal_open caldef : %s - (Are you missing data?)\n", myErrorName(status) );
- return;
+ goto cleanup;
}
ucal_setDateTime(cal, 1999, UCAL_MARCH, 18, 0, 0, 0, &status);
d1=ucal_getMillis(cal, &status);
if(U_FAILURE(status)){
- log_err("Error: failure in get millis: %s\n", myErrorName(status) );
- return;
+ log_err("Error: failure in get millis: %s\n", myErrorName(status) );
}
log_verbose("\nTesting with pattern test#4");
fmt = umsg_open(pattern,u_strlen(pattern),"en_US",&parseError,&status);
if(U_FAILURE(status)){
log_data_err("error in umsg_open : %s (Are you missing data?)\n", u_errorName(status) );
- return;
+ goto cleanup;
}
result=(UChar*)malloc(sizeof(UChar) * resultlength);
austrdup(myDateFormat(def1,d)), austrdup(myDateFormat(def1,d1)) );
}
}
+cleanup:
umsg_close(fmt);
udat_close(def1);
ucal_close(cal);
int32_t resultLengthOut, resultlength;
UCalendar *cal;
UDate d1,d;
- UDateFormat *def1;
+ UDateFormat *def1 = NULL;
UErrorCode status = U_ZERO_ERROR;
int32_t value = 0;
UChar ret[30];
}
if(U_FAILURE(status)){
log_data_err("ERROR: failure in message format test#4: %s (Are you missing data?)\n", myErrorName(status));
+ goto cleanup;
}
else if(u_strcmp(result, expected)==0)
log_verbose("PASS: MessagFormat successful on test#4\n");
austrdup(myDateFormat(def1,d)), austrdup(myDateFormat(def1,d1)) );
}
}
+cleanup:
udat_close(def1);
ucal_close(cal);
cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
if(U_FAILURE(status)){
log_data_err("error in ucal_open caldef : %s - (Are you missing data?)\n", myErrorName(status) );
+ return;
}
ucal_setDateTime(cal, 1999, UCAL_MARCH, 18, 0, 0, 0, &status);
d1=ucal_getMillis(cal, &status);
if(U_FAILURE(status)){
- log_data_err("Error: failure in get millis: %s - (Are you missing data?)\n", myErrorName(status) );
+ log_data_err("Error: failure in get millis: %s - (Are you missing data?)\n", myErrorName(status) );
}
log_verbose("\nTesting with pattern test#4");
}
if(U_FAILURE(status)){
log_data_err("ERROR: failure in message format test#4: %s (Are you missing data?)\n", myErrorName(status));
+ return;
}
else if(u_strcmp(result, expected)==0)
log_verbose("PASS: MessagFormat successful on test#4\n");
status=U_ZERO_ERROR;
resultlength=resultLengthOut+1;
result=(UChar*)malloc(sizeof(UChar) * resultlength);
- u_formatMessage( "fr", pattern, u_strlen(pattern), result, resultlength, &status, str , str1);
+ u_formatMessage( "fr", pattern, u_strlen(pattern), result, resultlength, &status, str , str1, 6);
if(u_strcmp(result, expected)==0)
log_verbose("PASS: MessagFormat successful on Select test#2\n");
else{
result, 256, &status,
string, 1/7.0,
789.0+1000*(56+60*(34+60*12)));
+ (void)length; /* Suppress set but not used warning. */
u_austrncpy(cresult, result, sizeof(cresult));
int32_t length=0;
UErrorCode status = U_ZERO_ERROR;
- u_uastrncpy(pattern, PAT, sizeof(pattern)/sizeof(pattern[0]));
+ u_uastrncpy(pattern, PAT, UPRV_LENGTHOF(pattern));
/* Test umsg_open */
f1 = umsg_open(pattern,length,NULL,NULL,&status);
UChar result[128] = {0};
UChar expected[sizeof(expectedChars)];
- u_uastrncpy(pattern, patChars, sizeof(pattern)/sizeof(pattern[0]));
- u_uastrncpy(expected, expectedChars, sizeof(expected)/sizeof(expected[0]));
+ u_uastrncpy(pattern, patChars, UPRV_LENGTHOF(pattern));
+ u_uastrncpy(expected, expectedChars, UPRV_LENGTHOF(expected));
- u_formatMessage("en_US", pattern, 6, result, sizeof(result)/sizeof(result[0]), &status, arg);
+ 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));
}
}
}
-static void TestMessageWithUnusedArgNumber() {
+static void TestMessageWithUnusedArgNumber(void) {
UErrorCode errorCode = U_ZERO_ERROR;
U_STRING_DECL(pattern, "abc {1} def", 11);
UChar x[2] = { 0x78, 0 }; // "x"
U_STRING_INIT(pattern, "abc {1} def", 11);
U_STRING_INIT(expected, "abc y def", 9);
- length = u_formatMessage("en", pattern, -1, result, LENGTHOF(result), &errorCode, x, y);
+ 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));