2010-01-16 03:07:18 +00:00
/********************************************************************
* COPYRIGHT :
2016-05-26 22:32:17 +00:00
* Copyright ( C ) 2016 and later : Unicode , Inc . and others .
* License & terms of use : http : //www.unicode.org/copyright.html
2010-01-16 03:07:18 +00:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include "unicode/utypes.h"
# if !UCONFIG_NO_FORMATTING
# include "selfmts.h"
# include "cmemory.h"
# include "unicode/selfmt.h"
2011-04-25 20:47:32 +00:00
2010-07-28 16:08:12 +00:00
# define SIMPLE_PATTERN_STRING "feminine {feminineVerbValue} other{otherVerbValue}"
2010-01-16 03:07:18 +00:00
# define SELECT_PATTERN_DATA 4
# define SELECT_SYNTAX_DATA 10
# define EXP_FORMAT_RESULT_DATA 12
# define NUM_OF_FORMAT_ARGS 3
2010-07-28 16:08:12 +00:00
# define VERBOSE_INT(x) {logln("%s:%d: int %s=%d\n", __FILE__, __LINE__, #x, (x));}
# define VERBOSE_USTRING(text) {logln("%s:%d: UnicodeString %s(%d) = ", __FILE__, __LINE__, #text, text.length()); logln(UnicodeString(" \"")+text+UnicodeString("\";"));}
2010-01-16 03:07:18 +00:00
void SelectFormatTest : : runIndexedTest ( int32_t index , UBool exec , const char * & name , char * /*par*/ )
{
if ( exec ) logln ( " TestSuite SelectFormat " ) ;
switch ( index ) {
TESTCASE ( 0 , selectFormatAPITest ) ;
TESTCASE ( 1 , selectFormatUnitTest ) ;
default : name = " " ;
break ;
}
}
/**
* Unit tests of SelectFormat class .
*/
void SelectFormatTest : : selectFormatUnitTest ( /*char *par*/ )
{
2010-07-28 16:08:12 +00:00
const UnicodeString SIMPLE_PATTERN ( SIMPLE_PATTERN_STRING ) ; /* Don't static init this! */
2010-01-16 03:07:18 +00:00
UnicodeString patternTestData [ SELECT_PATTERN_DATA ] = {
UNICODE_STRING_SIMPLE ( " fem {femValue} other{even} " ) ,
UNICODE_STRING_SIMPLE ( " other{odd or even} " ) ,
UNICODE_STRING_SIMPLE ( " odd{The number {0, number, integer} is odd.}other{The number {0, number, integer} is even.} " ) ,
UNICODE_STRING_SIMPLE ( " odd{The number {1} is odd}other{The number {1} is even} " ) ,
} ;
UnicodeString formatArgs [ NUM_OF_FORMAT_ARGS ] = {
UNICODE_STRING_SIMPLE ( " fem " ) ,
UNICODE_STRING_SIMPLE ( " other " ) ,
UNICODE_STRING_SIMPLE ( " odd " )
} ;
2010-01-17 09:44:17 +00:00
UnicodeString expFormatResult [ ] [ NUM_OF_FORMAT_ARGS ] = {
2010-01-16 03:07:18 +00:00
{
UNICODE_STRING_SIMPLE ( " femValue " ) ,
UNICODE_STRING_SIMPLE ( " even " ) ,
UNICODE_STRING_SIMPLE ( " even " )
} ,
{
UNICODE_STRING_SIMPLE ( " odd or even " ) ,
UNICODE_STRING_SIMPLE ( " odd or even " ) ,
UNICODE_STRING_SIMPLE ( " odd or even " ) ,
} ,
{
UNICODE_STRING_SIMPLE ( " The number {0, number, integer} is even. " ) ,
UNICODE_STRING_SIMPLE ( " The number {0, number, integer} is even. " ) ,
UNICODE_STRING_SIMPLE ( " The number {0, number, integer} is odd. " ) ,
} ,
{
UNICODE_STRING_SIMPLE ( " The number {1} is even " ) ,
UNICODE_STRING_SIMPLE ( " The number {1} is even " ) ,
UNICODE_STRING_SIMPLE ( " The number {1} is odd " ) ,
}
} ;
UnicodeString checkSyntaxData [ SELECT_SYNTAX_DATA ] = {
UNICODE_STRING_SIMPLE ( " odd{foo} " ) ,
2011-04-25 20:47:32 +00:00
UNICODE_STRING_SIMPLE ( " *odd{foo} other{bar} " ) ,
2010-01-16 03:07:18 +00:00
UNICODE_STRING_SIMPLE ( " odd{foo},other{bar} " ) ,
UNICODE_STRING_SIMPLE ( " od d{foo} other{bar} " ) ,
UNICODE_STRING_SIMPLE ( " odd{foo}{foobar}other{foo} " ) ,
UNICODE_STRING_SIMPLE ( " odd{foo1}other{foo2}} " ) ,
UNICODE_STRING_SIMPLE ( " odd{foo1}other{{foo2} " ) ,
UNICODE_STRING_SIMPLE ( " odd{fo{o1}other{foo2}} " )
} ;
UErrorCode status = U_ZERO_ERROR ;
2010-07-28 16:08:12 +00:00
VERBOSE_USTRING ( SIMPLE_PATTERN ) ;
2010-01-16 03:07:18 +00:00
SelectFormat * selFmt = new SelectFormat ( SIMPLE_PATTERN , status ) ;
if ( U_FAILURE ( status ) ) {
dataerrln ( " ERROR: SelectFormat Unit Test constructor failed in unit tests.- exitting " ) ;
return ;
}
2011-04-25 20:47:32 +00:00
2010-01-16 03:07:18 +00:00
// ======= Test SelectFormat pattern syntax.
logln ( " SelectFormat Unit Test : Testing SelectFormat pattern syntax. " ) ;
for ( int32_t i = 0 ; i < SELECT_SYNTAX_DATA ; + + i ) {
status = U_ZERO_ERROR ;
2010-07-28 16:08:12 +00:00
VERBOSE_INT ( i ) ;
VERBOSE_USTRING ( checkSyntaxData [ i ] ) ;
2010-01-16 03:07:18 +00:00
selFmt - > applyPattern ( checkSyntaxData [ i ] , status ) ;
2011-04-25 20:47:32 +00:00
if ( U_SUCCESS ( status ) ) {
errln ( " \n ERROR: Unexpected result - SelectFormat Unit Test failed to detect syntax error with pattern: " + checkSyntaxData [ i ] ) ;
2010-01-16 03:07:18 +00:00
}
}
2011-04-25 20:47:32 +00:00
// ICU 4.8 does not check for duplicate keywords any more.
status = U_ZERO_ERROR ;
selFmt - > applyPattern ( " odd{foo} odd{bar} other{foobar} " , status ) ;
FieldPosition format_ignore ( FieldPosition : : DONT_CARE ) ;
UnicodeString format_result ;
selFmt - > format ( UnicodeString ( " odd " ) , format_result , format_ignore , status ) ;
assertEquals ( " should use first occurrence of the 'odd' keyword " , " foo " , format_result ) ;
format_result . remove ( ) ;
selFmt - > applyPattern ( " odd{foo} other{bar} other{foobar} " , status ) ;
selFmt - > format ( UnicodeString ( " other " ) , format_result , format_ignore , status ) ;
assertEquals ( " should use first occurrence of the 'other' keyword " , " bar " , format_result ) ;
2010-03-12 05:44:13 +00:00
delete selFmt ;
selFmt = NULL ;
2010-01-30 00:46:40 +00:00
logln ( " SelectFormat Unit Test : Creating format object for Testing applying various patterns " ) ;
2010-01-16 03:07:18 +00:00
status = U_ZERO_ERROR ;
selFmt = new SelectFormat ( SIMPLE_PATTERN , status ) ;
//SelectFormat* selFmt1 = new SelectFormat( SIMPLE_PATTERN , status);
if ( U_FAILURE ( status ) ) {
errln ( " ERROR: SelectFormat Unit Test constructor failed in unit tests.- exitting " ) ;
return ;
}
// ======= Test applying and formatting with various pattern
logln ( " SelectFormat Unit test: Testing applyPattern() and format() ... " ) ;
UnicodeString result ;
FieldPosition ignore ( FieldPosition : : DONT_CARE ) ;
for ( int32_t i = 0 ; i < SELECT_PATTERN_DATA ; + + i ) {
status = U_ZERO_ERROR ;
selFmt - > applyPattern ( patternTestData [ i ] , status ) ;
if ( U_FAILURE ( status ) ) {
errln ( " ERROR: SelectFormat Unit Test failed to apply pattern- " + patternTestData [ i ] ) ;
continue ;
}
//Format with the keyword array
for ( int32_t j = 0 ; j < 3 ; j + + ) {
result . remove ( ) ;
selFmt - > format ( formatArgs [ j ] , result , ignore , status ) ;
if ( U_FAILURE ( status ) ) {
errln ( " ERROR: SelectFormat Unit test failed in format() with argument: " + formatArgs [ j ] + " and error is " + u_errorName ( status ) ) ;
} else {
if ( result ! = expFormatResult [ i ] [ j ] ) {
errln ( " ERROR: SelectFormat Unit test failed in format() with unexpected result \n with argument: " + formatArgs [ j ] + " \n result obtained: " + result + " \n and expected is: " + expFormatResult [ i ] [ j ] ) ;
}
}
}
}
//Test with an invalid keyword
2011-04-25 20:47:32 +00:00
// one which contains Pattern_Syntax or Pattern_White_Space.
2010-01-16 03:07:18 +00:00
logln ( " SelectFormat Unit test: Testing format() with keyword method and with invalid keywords... " ) ;
status = U_ZERO_ERROR ;
result . remove ( ) ;
UnicodeString keywords [ ] = {
2011-04-25 20:47:32 +00:00
" 9Keyword-_ " ,
" -Keyword-_ " ,
" _Keyword-_ " ,
" \\ u00E9Keyword-_ " ,
" Key word-_ " ,
" Keyword-_ " ,
" Key*word-_ " ,
" *Keyword-_ "
2010-01-16 03:07:18 +00:00
} ;
2010-03-12 05:44:13 +00:00
delete selFmt ;
selFmt = NULL ;
2010-01-16 03:07:18 +00:00
selFmt = new SelectFormat ( SIMPLE_PATTERN , status ) ;
2014-08-28 22:13:45 +00:00
for ( int32_t i = 0 ; i < UPRV_LENGTHOF ( keywords ) ; i + + ) {
2010-01-30 00:46:40 +00:00
status = U_ZERO_ERROR ;
2010-01-16 03:07:18 +00:00
selFmt - > format ( keywords [ i ] , result , ignore , status ) ;
if ( ! U_FAILURE ( status ) ) {
2011-04-25 20:47:32 +00:00
errln ( " ERROR: SelectFormat Unit test failed in format() with keyWord and with an invalid keyword as : " +
keywords [ i ] + " ( " + u_errorName ( status ) + " ) " ) ;
2010-01-16 03:07:18 +00:00
}
}
delete selFmt ;
}
/**
* Test various generic API methods of SelectFormat for Basic API usage .
* This is to make sure the API test coverage is 100 % .
*/
void SelectFormatTest : : selectFormatAPITest ( /*char *par*/ )
{
2010-07-28 16:08:12 +00:00
const UnicodeString SIMPLE_PATTERN ( SIMPLE_PATTERN_STRING ) ; /* Don't static init this! */
2010-01-16 03:07:18 +00:00
int numOfConstructors = 3 ;
UErrorCode status [ 3 ] ;
2010-05-25 22:17:12 +00:00
SelectFormat * selFmt [ 3 ] = { NULL , NULL , NULL } ;
2010-01-16 03:07:18 +00:00
// ========= Test constructors
logln ( " SelectFormat API test: Testing SelectFormat constructors ... " ) ;
for ( int32_t i = 0 ; i < numOfConstructors ; + + i ) {
status [ i ] = U_ZERO_ERROR ;
}
2010-01-30 03:21:38 +00:00
selFmt [ 0 ] = new SelectFormat ( SIMPLE_PATTERN , status [ 0 ] ) ;
2010-01-16 03:07:18 +00:00
if ( U_FAILURE ( status [ 0 ] ) ) {
2010-07-28 16:08:12 +00:00
errln ( " ERROR: SelectFormat API test constructor with pattern and status failed! with %s \n " , u_errorName ( status [ 0 ] ) ) ;
2010-01-16 03:07:18 +00:00
return ;
}
// =========== Test copy constructor
logln ( " SelectFormat API test: Testing copy constructor and == operator ... " ) ;
2010-01-30 03:21:38 +00:00
SelectFormat fmt = * selFmt [ 0 ] ;
2010-01-16 03:07:18 +00:00
SelectFormat * dupPFmt = new SelectFormat ( fmt ) ;
2010-01-30 03:21:38 +00:00
if ( ( * selFmt [ 0 ] ) ! = ( * dupPFmt ) ) {
2010-01-16 03:07:18 +00:00
errln ( " ERROR: SelectFormat API test Failed in copy constructor or == operator! " ) ;
}
delete dupPFmt ;
// ======= Test clone && == operator.
logln ( " SelectFormat API test: Testing clone and == operator ... " ) ;
2010-01-30 03:21:38 +00:00
if ( U_SUCCESS ( status [ 0 ] ) ) {
2010-01-16 03:07:18 +00:00
selFmt [ 1 ] = ( SelectFormat * ) selFmt [ 0 ] - > clone ( ) ;
if ( selFmt [ 1 ] ! = NULL ) {
if ( * selFmt [ 1 ] ! = * selFmt [ 0 ] ) {
errln ( " ERROR: SelectFormat API test clone test failed! " ) ;
}
2010-05-25 22:17:12 +00:00
} else {
errln ( " ERROR: SelectFormat API test clone test failed with NULL! " ) ;
return ;
2010-01-16 03:07:18 +00:00
}
2010-05-25 22:17:12 +00:00
} else {
errln ( " ERROR: could not create [0]: %s \n " , u_errorName ( status [ 0 ] ) ) ;
return ;
2010-01-16 03:07:18 +00:00
}
// ======= Test assignment operator && == operator.
logln ( " SelectFormat API test: Testing assignment operator and == operator ... " ) ;
selFmt [ 2 ] = new SelectFormat ( SIMPLE_PATTERN , status [ 2 ] ) ;
if ( U_SUCCESS ( status [ 2 ] ) ) {
* selFmt [ 1 ] = * selFmt [ 2 ] ;
if ( selFmt [ 1 ] ! = NULL ) {
if ( ( * selFmt [ 1 ] ! = * selFmt [ 2 ] ) ) {
errln ( " ERROR: SelectFormat API test assignment operator test failed! " ) ;
}
}
delete selFmt [ 1 ] ;
}
else {
errln ( " ERROR: SelectFormat constructor failed in assignment operator! " ) ;
}
delete selFmt [ 0 ] ;
delete selFmt [ 2 ] ;
// ======= Test getStaticClassID() and getStaticClassID()
logln ( " SelectFormat API test: Testing getStaticClassID() and getStaticClassID() ... " ) ;
UErrorCode status1 = U_ZERO_ERROR ;
SelectFormat * selFmt1 = new SelectFormat ( SIMPLE_PATTERN , status1 ) ;
if ( U_FAILURE ( status1 ) ) {
errln ( " ERROR: SelectFormat constructor failed in staticClassID test! Exitting " ) ;
return ;
}
logln ( " Testing getStaticClassID() " ) ;
if ( selFmt1 - > getDynamicClassID ( ) ! = SelectFormat : : getStaticClassID ( ) ) {
errln ( " ERROR: SelectFormat API test getDynamicClassID() didn't return the expected value " ) ;
}
// ======= Test applyPattern() and toPattern()
logln ( " SelectFormat API test: Testing applyPattern() and toPattern() ... " ) ;
UnicodeString pattern = UnicodeString ( " masculine{masculineVerbValue} other{otherVerbValue} " ) ;
status1 = U_ZERO_ERROR ;
selFmt1 - > applyPattern ( pattern , status1 ) ;
if ( U_FAILURE ( status1 ) ) {
errln ( " ERROR: SelectFormat API test failed in applyPattern() with pattern: " + pattern ) ;
} else {
UnicodeString checkPattern ;
selFmt1 - > toPattern ( checkPattern ) ;
if ( checkPattern ! = pattern ) {
errln ( " ERROR: SelectFormat API test failed in toPattern() with unexpected result with pattern: " + pattern ) ;
}
}
// ======= Test different format() methods
logln ( " SelectFormat API test: Testing format() with keyword method ... " ) ;
status1 = U_ZERO_ERROR ;
UnicodeString result ;
FieldPosition ignore ( FieldPosition : : DONT_CARE ) ;
UnicodeString keyWord = UnicodeString ( " masculine " ) ;
selFmt1 - > format ( keyWord , result , ignore , status1 ) ;
if ( U_FAILURE ( status1 ) ) {
errln ( " ERROR: SelectFormat API test failed in format() with keyWord: " + keyWord ) ;
} else {
UnicodeString expected = UnicodeString ( " masculineVerbValue " ) ;
if ( result ! = expected ) {
errln ( " ERROR: SelectFormat API test failed in format() with unexpected result with keyWord: " + keyWord ) ;
}
}
logln ( " SelectFormat API test: Testing format() with Formattable obj method ... " ) ;
status1 = U_ZERO_ERROR ;
result . remove ( ) ;
UnicodeString result1 ;
Formattable testArgs = Formattable ( " other " ) ;
selFmt1 - > format ( testArgs , result1 , ignore , status1 ) ;
if ( U_FAILURE ( status1 ) ) {
errln ( " ERROR: SelectFormat API test failed in format() with Formattable " ) ;
} else {
UnicodeString expected = UnicodeString ( " otherVerbValue " ) ;
if ( result1 ! = expected ) {
errln ( " ERROR: SelectFormat API test failed in format() with unexpected result with Formattable " ) ;
}
}
delete selFmt1 ;
}
# endif /* #if !UCONFIG_NO_FORMATTING */