2000-07-12 23:33:20 +00:00
/********************************************************************
* COPYRIGHT :
2010-01-30 06:53:52 +00:00
* Copyright ( c ) 1998 - 2010 , International Business Machines Corporation and
2000-07-12 23:33:20 +00:00
* others . All Rights Reserved .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* File putiltst . c ( Tests the API in putil )
*
* Modification History :
*
* Date Name Description
2000-08-11 02:08:59 +00:00
* 07 / 12 / 2000 Madhu Creation
2000-07-12 23:33:20 +00:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
# include "unicode/utypes.h"
# include "cintltst.h"
2001-05-31 23:07:40 +00:00
# include "cmemory.h"
2000-07-12 23:33:20 +00:00
# include "unicode/putil.h"
# include "unicode/ustring.h"
2009-09-17 21:55:11 +00:00
# include "unicode/icudataver.h"
2001-09-11 18:33:53 +00:00
# include "cstring.h"
2004-10-18 02:43:33 +00:00
# include "putilimp.h"
2010-02-24 22:49:36 +00:00
# include "toolutil.h"
2000-07-12 23:33:20 +00:00
2000-08-02 22:47:18 +00:00
static UBool compareWithNAN ( double x , double y ) ;
2001-10-05 21:55:15 +00:00
static void doAssert ( double expect , double got , const char * message ) ;
2000-07-12 23:33:20 +00:00
2002-03-28 18:26:25 +00:00
static void TestPUtilAPI ( void ) {
2000-08-11 02:08:59 +00:00
2000-07-12 23:33:20 +00:00
double n1 = 0.0 , y1 = 0.0 , expn1 , expy1 ;
double value1 = 0.021 ;
char * str = 0 ;
UBool isTrue = FALSE ;
2000-08-11 02:08:59 +00:00
2000-07-12 23:33:20 +00:00
log_verbose ( " Testing the API uprv_modf() \n " ) ;
y1 = uprv_modf ( value1 , & n1 ) ;
expn1 = 0 ;
expy1 = 0.021 ;
if ( y1 ! = expy1 | | n1 ! = expn1 ) {
log_err ( " Error in uprv_modf. Expected IntegralValue=%f, Got=%f, \n Expected FractionalValue=%f, Got=%f \n " ,
expn1 , n1 , expy1 , y1 ) ;
}
if ( VERBOSITY ) {
2001-05-31 23:07:40 +00:00
log_verbose ( " [float] x = %f n = %f y = %f \n " , value1 , n1 , y1 ) ;
2000-07-12 23:33:20 +00:00
}
log_verbose ( " Testing the API uprv_fmod() \n " ) ;
expn1 = uprv_fmod ( 30.50 , 15.00 ) ;
doAssert ( expn1 , 0.5 , " uprv_fmod(30.50, 15.00) failed. " ) ;
2000-08-11 02:08:59 +00:00
2000-07-12 23:33:20 +00:00
log_verbose ( " Testing the API uprv_ceil() \n " ) ;
expn1 = uprv_ceil ( value1 ) ;
doAssert ( expn1 , 1 , " uprv_ceil(0.021) failed. " ) ;
2000-08-11 02:08:59 +00:00
2000-07-12 23:33:20 +00:00
log_verbose ( " Testing the API uprv_floor() \n " ) ;
expn1 = uprv_floor ( value1 ) ;
doAssert ( expn1 , 0 , " uprv_floor(0.021) failed. " ) ;
2000-08-11 02:08:59 +00:00
2000-07-12 23:33:20 +00:00
log_verbose ( " Testing the API uprv_fabs() \n " ) ;
expn1 = uprv_fabs ( ( 2.02 - 1.345 ) ) ;
doAssert ( expn1 , 0.675 , " uprv_fabs(2.02-1.345) failed. " ) ;
log_verbose ( " Testing the API uprv_fmax() \n " ) ;
doAssert ( uprv_fmax ( 2.4 , 1.2 ) , 2.4 , " uprv_fmax(2.4, 1.2) failed. " ) ;
log_verbose ( " Testing the API uprv_fmax() with x value= NaN \n " ) ;
expn1 = uprv_fmax ( uprv_getNaN ( ) , 1.2 ) ;
doAssert ( expn1 , uprv_getNaN ( ) , " uprv_fmax(uprv_getNaN(), 1.2) failed. when one parameter is NaN " ) ;
2000-08-11 02:08:59 +00:00
2000-07-12 23:33:20 +00:00
log_verbose ( " Testing the API uprv_fmin() \n " ) ;
doAssert ( uprv_fmin ( 2.4 , 1.2 ) , 1.2 , " uprv_fmin(2.4, 1.2) failed. " ) ;
2000-08-11 02:08:59 +00:00
2000-07-12 23:33:20 +00:00
log_verbose ( " Testing the API uprv_fmin() with x value= NaN \n " ) ;
expn1 = uprv_fmin ( uprv_getNaN ( ) , 1.2 ) ;
doAssert ( expn1 , uprv_getNaN ( ) , " uprv_fmin(uprv_getNaN(), 1.2) failed. when one parameter is NaN " ) ;
2000-08-11 02:08:59 +00:00
2000-07-12 23:33:20 +00:00
log_verbose ( " Testing the API uprv_max() \n " ) ;
doAssert ( uprv_max ( 4 , 2 ) , 4 , " uprv_max(4, 2) failed. " ) ;
2000-08-11 02:08:59 +00:00
2000-07-12 23:33:20 +00:00
log_verbose ( " Testing the API uprv_min() \n " ) ;
doAssert ( uprv_min ( - 4 , 2 ) , - 4 , " uprv_min(-4, 2) failed. " ) ;
2000-08-11 02:08:59 +00:00
2000-07-12 23:33:20 +00:00
log_verbose ( " Testing the API uprv_trunc() \n " ) ;
doAssert ( uprv_trunc ( 12.3456 ) , 12 , " uprv_trunc(12.3456) failed. " ) ;
doAssert ( uprv_trunc ( 12.234E2 ) , 1223 , " uprv_trunc(12.234E2) failed. " ) ;
doAssert ( uprv_trunc ( uprv_getNaN ( ) ) , uprv_getNaN ( ) , " uprv_trunc(uprv_getNaN()) failed. with parameter=NaN " ) ;
doAssert ( uprv_trunc ( uprv_getInfinity ( ) ) , uprv_getInfinity ( ) , " uprv_trunc(uprv_getInfinity()) failed. with parameter=Infinity " ) ;
2000-08-11 02:08:59 +00:00
2000-07-12 23:33:20 +00:00
log_verbose ( " Testing the API uprv_pow10() \n " ) ;
doAssert ( uprv_pow10 ( 4 ) , 10000 , " uprv_pow10(4) failed. " ) ;
log_verbose ( " Testing the API uprv_isNegativeInfinity() \n " ) ;
isTrue = uprv_isNegativeInfinity ( uprv_getInfinity ( ) * - 1 ) ;
if ( isTrue ! = TRUE ) {
log_err ( " ERROR: uprv_isNegativeInfinity failed. \n " ) ;
}
log_verbose ( " Testing the API uprv_isPositiveInfinity() \n " ) ;
isTrue = uprv_isPositiveInfinity ( uprv_getInfinity ( ) ) ;
if ( isTrue ! = TRUE ) {
log_err ( " ERROR: uprv_isPositiveInfinity failed. \n " ) ;
}
log_verbose ( " Testing the API uprv_isInfinite() \n " ) ;
isTrue = uprv_isInfinite ( uprv_getInfinity ( ) ) ;
if ( isTrue ! = TRUE ) {
log_err ( " ERROR: uprv_isInfinite failed. \n " ) ;
}
2004-05-10 19:27:09 +00:00
#if 0
2000-07-12 23:33:20 +00:00
log_verbose ( " Testing the API uprv_digitsAfterDecimal().... \n " ) ;
2001-06-14 00:29:38 +00:00
doAssert ( uprv_digitsAfterDecimal ( value1 ) , 3 , " uprv_digitsAfterDecimal() failed. " ) ;
2000-07-12 23:33:20 +00:00
doAssert ( uprv_digitsAfterDecimal ( 1.2345E2 ) , 2 , " uprv_digitsAfterDecimal(1.2345E2) failed. " ) ;
doAssert ( uprv_digitsAfterDecimal ( 1.2345E-2 ) , 6 , " uprv_digitsAfterDecimal(1.2345E-2) failed. " ) ;
doAssert ( uprv_digitsAfterDecimal ( 1.2345E2 ) , 2 , " uprv_digitsAfterDecimal(1.2345E2) failed. " ) ;
2001-06-14 00:29:38 +00:00
doAssert ( uprv_digitsAfterDecimal ( - 1.2345E-20 ) , 24 , " uprv_digitsAfterDecimal(1.2345E-20) failed. " ) ;
doAssert ( uprv_digitsAfterDecimal ( 1.2345E20 ) , 0 , " uprv_digitsAfterDecimal(1.2345E20) failed. " ) ;
2000-07-12 23:33:20 +00:00
doAssert ( uprv_digitsAfterDecimal ( - 0.021 ) , 3 , " uprv_digitsAfterDecimal(-0.021) failed. " ) ;
doAssert ( uprv_digitsAfterDecimal ( 23.0 ) , 0 , " uprv_digitsAfterDecimal(23.0) failed. " ) ;
doAssert ( uprv_digitsAfterDecimal ( 0.022223333321 ) , 9 , " uprv_digitsAfterDecimal(0.022223333321) failed. " ) ;
2004-05-10 19:27:09 +00:00
# endif
2001-06-14 00:29:38 +00:00
2009-01-29 00:17:44 +00:00
log_verbose ( " Testing the API u_errorName()... \n " ) ;
str = ( char * ) u_errorName ( ( UErrorCode ) 0 ) ;
if ( strcmp ( str , " U_ZERO_ERROR " ) ! = 0 ) {
log_err ( " ERROR: u_getVersion() failed. Expected: U_ZERO_ERROR Got=%s \n " , str ) ;
}
log_verbose ( " Testing the API u_errorName()... \n " ) ;
str = ( char * ) u_errorName ( ( UErrorCode ) - 127 ) ;
if ( strcmp ( str , " U_USING_DEFAULT_WARNING " ) ! = 0 ) {
log_err ( " ERROR: u_getVersion() failed. Expected: U_USING_DEFAULT_WARNING Got=%s \n " , str ) ;
}
log_verbose ( " Testing the API u_errorName().. with BOGUS ERRORCODE... \n " ) ;
str = ( char * ) u_errorName ( ( UErrorCode ) 200 ) ;
if ( strcmp ( str , " [BOGUS UErrorCode] " ) ! = 0 ) {
log_err ( " ERROR: u_getVersion() failed. Expected: [BOGUS UErrorCode] Got=%s \n " , str ) ;
}
{
const char * dataDirectory ;
int32_t dataDirectoryLen ;
UChar * udataDir = 0 ;
UChar temp [ 100 ] ;
char * charvalue = 0 ;
log_verbose ( " Testing chars to UChars \n " ) ;
/* This cannot really work on a japanese system. u_uastrcpy will have different results than */
/* u_charsToUChars when there is a backslash in the string! */
/*dataDirectory=u_getDataDirectory();*/
dataDirectory = " directory1 " ; /*no backslashes*/
dataDirectoryLen = ( int32_t ) strlen ( dataDirectory ) ;
udataDir = ( UChar * ) malloc ( sizeof ( UChar ) * ( dataDirectoryLen + 1 ) ) ;
u_charsToUChars ( dataDirectory , udataDir , ( dataDirectoryLen + 1 ) ) ;
u_uastrcpy ( temp , dataDirectory ) ;
if ( u_strcmp ( temp , udataDir ) ! = 0 ) {
log_err ( " ERROR: u_charsToUChars failed. Expected %s, Got %s \n " , austrdup ( temp ) , austrdup ( udataDir ) ) ;
}
log_verbose ( " Testing UChars to chars \n " ) ;
charvalue = ( char * ) malloc ( sizeof ( char ) * ( u_strlen ( udataDir ) + 1 ) ) ;
u_UCharsToChars ( udataDir , charvalue , ( u_strlen ( udataDir ) + 1 ) ) ;
if ( strcmp ( charvalue , dataDirectory ) ! = 0 ) {
log_err ( " ERROR: u_UCharsToChars failed. Expected %s, Got %s \n " , charvalue , dataDirectory ) ;
}
free ( charvalue ) ;
free ( udataDir ) ;
}
log_verbose ( " Testing uprv_timezone().... \n " ) ;
{
int32_t tzoffset = uprv_timezone ( ) ;
log_verbose ( " Value returned from uprv_timezone = %d \n " , tzoffset ) ;
if ( tzoffset ! = 28800 ) {
log_verbose ( " ***** WARNING: If testing in the PST timezone, t_timezone should return 28800! ***** " ) ;
}
if ( ( tzoffset % 1800 ! = 0 ) ) {
log_err ( " FAIL: t_timezone may be incorrect. It is not a multiple of 30min. " ) ;
}
/*tzoffset=uprv_getUTCtime();*/
}
}
static void TestVersion ( )
{
UVersionInfo versionArray = { 0x01 , 0x00 , 0x02 , 0x02 } ;
UVersionInfo versionArray2 = { 0x01 , 0x00 , 0x02 , 0x02 } ;
char versionString [ 17 ] ; /* xxx.xxx.xxx.xxx\0 */
UChar versionUString [ ] = { 0x0031 , 0x002E , 0x0030 , 0x002E ,
0x0032 , 0x002E , 0x0038 , 0x0000 } ; /* 1.0.2.8 */
2009-09-17 21:55:11 +00:00
UBool isModified = FALSE ;
UVersionInfo version ;
UErrorCode status = U_ZERO_ERROR ;
2001-06-14 00:29:38 +00:00
2000-07-12 23:33:20 +00:00
log_verbose ( " Testing the API u_versionToString()..... \n " ) ;
u_versionToString ( versionArray , versionString ) ;
if ( strcmp ( versionString , " 1.0.2.2 " ) ! = 0 ) {
log_err ( " ERROR: u_versionToString() failed. Expected: 1.0.2.2, Got=%s \n " , versionString ) ;
}
log_verbose ( " Testing the API u_versionToString().....with versionArray=NULL \n " ) ;
u_versionToString ( NULL , versionString ) ;
if ( strcmp ( versionString , " " ) ! = 0 ) {
log_err ( " ERROR: u_versionToString() failed. with versionArray=NULL. It should just return \n " ) ;
}
log_verbose ( " Testing the API u_versionToString().....with versionArray=NULL \n " ) ;
u_versionToString ( NULL , versionString ) ;
if ( strcmp ( versionString , " " ) ! = 0 ) {
log_err ( " ERROR: u_versionToString() failed . It should just return \n " ) ;
}
log_verbose ( " Testing the API u_versionToString().....with versionString=NULL \n " ) ;
u_versionToString ( versionArray , NULL ) ;
if ( strcmp ( versionString , " " ) ! = 0 ) {
log_err ( " ERROR: u_versionToString() failed. with versionArray=NULL It should just return \n " ) ;
}
versionArray [ 0 ] = 0x0a ;
log_verbose ( " Testing the API u_versionToString()..... \n " ) ;
u_versionToString ( versionArray , versionString ) ;
if ( strcmp ( versionString , " 10.0.2.2 " ) ! = 0 ) {
log_err ( " ERROR: u_versionToString() failed. Expected: 10.0.2.2, Got=%s \n " , versionString ) ;
}
versionArray [ 0 ] = 0xa0 ;
u_versionToString ( versionArray , versionString ) ;
if ( strcmp ( versionString , " 160.0.2.2 " ) ! = 0 ) {
log_err ( " ERROR: u_versionToString() failed. Expected: 160.0.2.2, Got=%s \n " , versionString ) ;
}
versionArray [ 0 ] = 0xa0 ;
versionArray [ 1 ] = 0xa0 ;
u_versionToString ( versionArray , versionString ) ;
if ( strcmp ( versionString , " 160.160.2.2 " ) ! = 0 ) {
log_err ( " ERROR: u_versionToString() failed. Expected: 160.160.2.2, Got=%s \n " , versionString ) ;
}
versionArray [ 0 ] = 0x01 ;
versionArray [ 1 ] = 0x0a ;
u_versionToString ( versionArray , versionString ) ;
if ( strcmp ( versionString , " 1.10.2.2 " ) ! = 0 ) {
log_err ( " ERROR: u_versionToString() failed. Expected: 160.160.2.2, Got=%s \n " , versionString ) ;
}
log_verbose ( " Testing the API u_versionFromString() .... \n " ) ;
u_versionFromString ( versionArray , " 1.3.5.6 " ) ;
u_versionToString ( versionArray , versionString ) ;
if ( strcmp ( versionString , " 1.3.5.6 " ) ! = 0 ) {
log_err ( " ERROR: u_getVersion() failed. Expected: 1.3.5.6, Got=%s \n " , versionString ) ;
}
log_verbose ( " Testing the API u_versionFromString() where versionArray=NULL.... \n " ) ;
u_versionFromString ( NULL , " 1.3.5.6 " ) ;
u_versionToString ( versionArray , versionString ) ;
if ( strcmp ( versionString , " 1.3.5.6 " ) ! = 0 ) {
log_err ( " ERROR: u_getVersion() failed. Expected: 1.3.5.6, Got=%s \n " , versionString ) ;
}
log_verbose ( " Testing the API u_getVersion()..... \n " ) ;
u_getVersion ( versionArray ) ;
u_versionToString ( versionArray , versionString ) ;
if ( strcmp ( versionString , U_ICU_VERSION ) ! = 0 ) {
2000-11-01 19:21:35 +00:00
log_err ( " ERROR: u_getVersion() failed. Got=%s, expected %s \n " , versionString , U_ICU_VERSION ) ;
2000-07-12 23:33:20 +00:00
}
2009-01-29 00:17:44 +00:00
/* test unicode */
log_verbose ( " Testing u_versionFromUString... \n " ) ;
u_versionFromString ( versionArray , " 1.0.2.8 " ) ;
u_versionFromUString ( versionArray2 , versionUString ) ;
u_versionToString ( versionArray2 , versionString ) ;
if ( memcmp ( versionArray , versionArray2 , sizeof ( UVersionInfo ) ) ) {
log_err ( " FAIL: u_versionFromUString produced a different result - not 1.0.2.8 but %s [%x.%x.%x.%x] \n " ,
versionString ,
( int ) versionArray2 [ 0 ] ,
( int ) versionArray2 [ 1 ] ,
( int ) versionArray2 [ 2 ] ,
( int ) versionArray2 [ 3 ] ) ;
}
else {
log_verbose ( " from UString: %s \n " , versionString ) ;
2000-07-12 23:33:20 +00:00
}
2009-09-17 21:55:11 +00:00
/* Test the data version API for better code coverage */
u_getDataVersion ( version , & status ) ;
if ( U_FAILURE ( status ) ) {
2009-11-16 05:16:59 +00:00
log_data_err ( " ERROR: Unable to get data version. %s \n " , u_errorName ( status ) ) ;
2009-09-17 21:55:11 +00:00
} else {
u_isDataOlder ( version , & isModified , & status ) ;
if ( U_FAILURE ( status ) ) {
2009-11-16 05:16:59 +00:00
log_err ( " ERROR: Unable to compare data version. %s \n " , u_errorName ( status ) ) ;
2009-09-17 21:55:11 +00:00
}
}
2009-01-29 00:17:44 +00:00
}
2000-07-12 23:33:20 +00:00
2009-01-29 00:17:44 +00:00
static void TestCompareVersions ( )
{
/* use a 1d array to be palatable to java */
const char * testCases [ ] = {
/* v1 <|=|> v2 */
" 0.0.0.0 " , " = " , " 0.0.0.0 " ,
" 3.1.2.0 " , " > " , " 3.0.9.0 " ,
" 3.2.8.6 " , " < " , " 3.4 " ,
" 4.0 " , " > " , " 3.2 " ,
NULL , NULL , NULL
} ;
const char * v1str ;
const char * opstr ;
const char * v2str ;
int32_t op , invop , got , invgot ;
UVersionInfo v1 , v2 ;
int32_t j ;
2009-06-23 18:04:40 +00:00
log_verbose ( " Testing memcmp() \n " ) ;
2009-01-29 00:17:44 +00:00
for ( j = 0 ; testCases [ j ] ! = NULL ; j + = 3 ) {
v1str = testCases [ j + 0 ] ;
opstr = testCases [ j + 1 ] ;
v2str = testCases [ j + 2 ] ;
switch ( opstr [ 0 ] ) {
case ' - ' :
case ' < ' : op = - 1 ; break ;
case ' 0 ' :
case ' = ' : op = 0 ; break ;
case ' + ' :
case ' > ' : op = 1 ; break ;
default : log_err ( " Bad operator at j/3=%d \n " , ( j / 3 ) ) ; return ;
2000-07-12 23:33:20 +00:00
}
2009-01-29 00:17:44 +00:00
invop = 0 - op ; /* inverse operation: with v1 and v2 switched */
u_versionFromString ( v1 , v1str ) ;
u_versionFromString ( v2 , v2str ) ;
2010-01-30 06:53:52 +00:00
got = memcmp ( v1 , v2 , sizeof ( UVersionInfo ) ) ;
invgot = memcmp ( v2 , v1 , sizeof ( UVersionInfo ) ) ; /* Opposite */
2009-06-23 21:06:29 +00:00
if ( ( got < = 0 & & op < = 0 ) | | ( got > = 0 & & op > = 0 ) ) {
2009-01-29 00:17:44 +00:00
log_verbose ( " %d: %s %s %s, OK \n " , ( j / 3 ) , v1str , opstr , v2str ) ;
} else {
2009-06-23 19:47:15 +00:00
log_err ( " %d: %s %s %s: wanted values of the same sign, %d got %d \n " , ( j / 3 ) , v1str , opstr , v2str , op , got ) ;
2009-01-29 00:17:44 +00:00
}
2009-06-23 21:06:29 +00:00
if ( ( invgot > = 0 & & invop > = 0 ) | | ( invgot < = 0 & & invop < = 0 ) ) {
2009-01-29 00:17:44 +00:00
log_verbose ( " %d: %s (%d) %s, OK (inverse) \n " , ( j / 3 ) , v2str , invop , v1str ) ;
} else {
2009-06-23 19:47:15 +00:00
log_err ( " %d: %s (%d) %s: wanted values of the same sign, %d got %d \n " , ( j / 3 ) , v2str , invop , v1str , invop , invgot ) ;
2000-07-12 23:33:20 +00:00
}
2009-01-29 00:17:44 +00:00
}
2000-07-12 23:33:20 +00:00
}
2000-11-21 04:05:39 +00:00
2009-01-29 00:17:44 +00:00
2001-10-05 21:55:15 +00:00
#if 0
2000-11-21 04:05:39 +00:00
static void testIEEEremainder ( )
2000-07-12 23:33:20 +00:00
{
double pinf = uprv_getInfinity ( ) ;
double ninf = - uprv_getInfinity ( ) ;
double nan = uprv_getNaN ( ) ;
2000-08-11 02:08:59 +00:00
/* double pzero = 0.0;*/
2001-02-21 01:58:15 +00:00
/* double nzero = 0.0;
nzero * = - 1 ; */
2000-07-12 23:33:20 +00:00
/* simple remainder checks*/
remainderTest ( 7.0 , 2.5 , - 0.5 ) ;
remainderTest ( 7.0 , - 2.5 , - 0.5 ) ;
/* this should work
remainderTest ( 43.7 , 2.5 , 1.2 ) ;
*/
/* infinity and real*/
remainderTest ( 1.0 , pinf , 1.0 ) ;
remainderTest ( 1.0 , ninf , 1.0 ) ;
/*test infinity and real*/
2000-07-18 15:59:55 +00:00
remainderTest ( nan , 1.0 , nan ) ;
remainderTest ( 1.0 , nan , nan ) ;
2000-07-12 23:33:20 +00:00
/*test infinity and nan*/
2000-07-18 15:59:55 +00:00
remainderTest ( ninf , nan , nan ) ;
remainderTest ( pinf , nan , nan ) ;
2000-07-12 23:33:20 +00:00
2000-08-02 22:47:18 +00:00
/* test infinity and zero */
/* remainderTest(pinf, pzero, 1.25);
2000-07-12 23:33:20 +00:00
remainderTest ( pinf , nzero , 1.25 ) ;
remainderTest ( ninf , pzero , 1.25 ) ;
2000-08-02 22:47:18 +00:00
remainderTest ( ninf , nzero , 1.25 ) ; */
2000-07-12 23:33:20 +00:00
}
2000-08-02 22:47:18 +00:00
2000-11-21 04:05:39 +00:00
static void remainderTest ( double x , double y , double exp )
2000-07-12 23:33:20 +00:00
{
double result = uprv_IEEEremainder ( x , y ) ;
if ( uprv_isNaN ( result ) & &
! ( uprv_isNaN ( x ) | | uprv_isNaN ( y ) ) ) {
log_err ( " FAIL: got NaN as result without NaN as argument " ) ;
log_err ( " IEEEremainder(%f, %f) is %f, expected %f \n " , x , y , result , exp ) ;
}
2000-08-02 22:47:18 +00:00
else if ( ! compareWithNAN ( result , exp ) ) {
2000-07-12 23:33:20 +00:00
log_err ( " FAIL: IEEEremainder(%f, %f) is %f, expected %f \n " , x , y , result , exp ) ;
} else {
log_verbose ( " OK: IEEEremainder(%f, %f) is %f \n " , x , y , result ) ;
}
}
2001-10-05 21:55:15 +00:00
# endif
static UBool compareWithNAN ( double x , double y )
{
if ( uprv_isNaN ( x ) | | uprv_isNaN ( y ) ) {
if ( ! uprv_isNaN ( x ) | | ! uprv_isNaN ( y ) ) {
return FALSE ;
}
}
else if ( y ! = x ) { /* no NaN's involved */
return FALSE ;
}
return TRUE ;
}
2000-11-21 04:05:39 +00:00
static void doAssert ( double got , double expect , const char * message )
2000-07-12 23:33:20 +00:00
{
2000-08-02 22:47:18 +00:00
if ( ! compareWithNAN ( expect , got ) ) {
log_err ( " ERROR : %s. Expected : %lf, Got: %lf \n " , message , expect , got ) ;
}
2000-07-12 23:33:20 +00:00
}
2001-09-11 18:33:53 +00:00
# define _CODE_ARR_LEN 8
static const UErrorCode errorCode [ _CODE_ARR_LEN ] = {
U_USING_FALLBACK_WARNING ,
U_STRING_NOT_TERMINATED_WARNING ,
U_ILLEGAL_ARGUMENT_ERROR ,
U_STATE_TOO_OLD_ERROR ,
U_BAD_VARIABLE_DEFINITION ,
U_RULE_MASK_ERROR ,
U_UNEXPECTED_TOKEN ,
U_UNSUPPORTED_ATTRIBUTE
} ;
static const char * str [ ] = {
" U_USING_FALLBACK_WARNING " ,
" U_STRING_NOT_TERMINATED_WARNING " ,
" U_ILLEGAL_ARGUMENT_ERROR " ,
" U_STATE_TOO_OLD_ERROR " ,
" U_BAD_VARIABLE_DEFINITION " ,
" U_RULE_MASK_ERROR " ,
" U_UNEXPECTED_TOKEN " ,
" U_UNSUPPORTED_ATTRIBUTE "
} ;
2002-03-28 18:26:25 +00:00
static void TestErrorName ( void ) {
2001-09-11 18:33:53 +00:00
int32_t code = 0 ;
const char * errorName ;
for ( ; code < U_ERROR_LIMIT ; code + + ) {
errorName = u_errorName ( ( UErrorCode ) code ) ;
2010-01-30 06:53:52 +00:00
if ( ! errorName | | errorName [ 0 ] = = 0 ) {
log_err ( " Error: u_errorName(0x%X) failed. \n " , code ) ;
}
2001-09-11 18:33:53 +00:00
}
for ( code = 0 ; code < _CODE_ARR_LEN ; code + + ) {
errorName = u_errorName ( errorCode [ code ] ) ;
if ( uprv_strcmp ( str [ code ] , errorName ) ! = 0 ) {
log_err ( " Error : u_errorName failed. Expected: %s Got: %s \n " , str [ code ] , errorName ) ;
}
}
2001-09-18 21:02:14 +00:00
}
2001-10-05 21:55:15 +00:00
void addPUtilTest ( TestNode * * root ) ;
2010-02-24 22:49:36 +00:00
static void addToolUtilTests ( TestNode * * root ) ;
2001-10-05 21:55:15 +00:00
void
addPUtilTest ( TestNode * * root )
{
2009-01-29 00:17:44 +00:00
addTest ( root , & TestVersion , " putiltst/TestVersion " ) ;
addTest ( root , & TestCompareVersions , " putiltst/TestCompareVersions " ) ;
2001-10-05 21:55:15 +00:00
/* addTest(root, &testIEEEremainder, "putiltst/testIEEEremainder"); */
2001-11-13 21:15:13 +00:00
addTest ( root , & TestErrorName , " putiltst/TestErrorName " ) ;
2009-09-18 14:31:42 +00:00
addTest ( root , & TestPUtilAPI , " putiltst/TestPUtilAPI " ) ;
2010-02-24 22:49:36 +00:00
addToolUtilTests ( root ) ;
}
/* Tool Util Tests ================ */
# define TOOLUTIL_TESTBUF_SIZE 2048
static char toolutil_testBuf [ TOOLUTIL_TESTBUF_SIZE ] ;
static const char * NULLSTR = " NULL " ;
/**
* Normalize NULL to ' NULL ' for testing
*/
# define STRNULL(x) ((x)?(x):NULLSTR)
static void toolutil_findBasename ( void )
{
struct {
const char * inBuf ;
const char * expectResult ;
} testCases [ ] = {
{
U_FILE_SEP_STRING " usr " U_FILE_SEP_STRING " bin " U_FILE_SEP_STRING " pkgdata " ,
" pkgdata "
} ,
{
U_FILE_SEP_STRING " usr " U_FILE_SEP_STRING " bin " U_FILE_SEP_STRING ,
" "
} ,
{
U_FILE_ALT_SEP_STRING " usr " U_FILE_ALT_SEP_STRING " bin " U_FILE_ALT_SEP_STRING " pkgdata " ,
" pkgdata "
} ,
{
U_FILE_ALT_SEP_STRING " usr " U_FILE_ALT_SEP_STRING " bin " U_FILE_ALT_SEP_STRING ,
" "
} ,
} ;
int32_t count = ( sizeof ( testCases ) / sizeof ( testCases [ 0 ] ) ) ;
int32_t i ;
log_verbose ( " Testing findBaseName() \n " ) ;
for ( i = 0 ; i < count ; i + + ) {
const char * result ;
const char * input = STRNULL ( testCases [ i ] . inBuf ) ;
const char * expect = STRNULL ( testCases [ i ] . expectResult ) ;
log_verbose ( " Test case [%d/%d]: %s \n " , i , count - 1 , input ) ;
result = STRNULL ( findBasename ( testCases [ i ] . inBuf ) ) ;
if ( result = = expect | | ! strcmp ( result , expect ) ) {
log_verbose ( " -> %s PASS \n " , result ) ;
} else {
log_err ( " FAIL: Test case [%d/%d]: %s -> %s but expected %s \n " , i , count - 1 , input , result , expect ) ;
}
}
}
static void toolutil_findDirname ( void )
{
int i ;
struct {
const char * inBuf ;
int32_t outBufLen ;
UErrorCode expectStatus ;
const char * expectResult ;
} testCases [ ] = {
{
U_FILE_SEP_STRING " usr " U_FILE_SEP_STRING " bin " U_FILE_SEP_STRING " pkgdata " ,
200 ,
U_ZERO_ERROR ,
U_FILE_SEP_STRING " usr " U_FILE_SEP_STRING " bin " ,
} ,
{
U_FILE_SEP_STRING " usr " U_FILE_SEP_STRING " bin " U_FILE_SEP_STRING " pkgdata " ,
2 ,
U_BUFFER_OVERFLOW_ERROR ,
NULL
} ,
{
U_FILE_ALT_SEP_STRING " usr " U_FILE_ALT_SEP_STRING " bin " U_FILE_ALT_SEP_STRING " pkgdata " ,
200 ,
U_ZERO_ERROR ,
U_FILE_ALT_SEP_STRING " usr " U_FILE_ALT_SEP_STRING " bin "
} ,
{
U_FILE_ALT_SEP_STRING " usr " U_FILE_ALT_SEP_STRING " bin " U_FILE_ALT_SEP_STRING " pkgdata " ,
2 ,
U_BUFFER_OVERFLOW_ERROR ,
NULL
} ,
{
U_FILE_ALT_SEP_STRING " usr " U_FILE_ALT_SEP_STRING " bin " U_FILE_SEP_STRING " pkgdata " ,
200 ,
U_ZERO_ERROR ,
U_FILE_ALT_SEP_STRING " usr " U_FILE_ALT_SEP_STRING " bin "
} ,
{
U_FILE_ALT_SEP_STRING " usr " U_FILE_SEP_STRING " bin " U_FILE_ALT_SEP_STRING " pkgdata " ,
200 ,
U_ZERO_ERROR ,
U_FILE_ALT_SEP_STRING " usr " U_FILE_SEP_STRING " bin "
} ,
{
U_FILE_ALT_SEP_STRING " usr " U_FILE_ALT_SEP_STRING " bin " U_FILE_ALT_SEP_STRING " pkgdata " ,
2 ,
U_BUFFER_OVERFLOW_ERROR ,
NULL
} ,
{
U_FILE_ALT_SEP_STRING " vmlinuz " ,
200 ,
U_ZERO_ERROR ,
U_FILE_ALT_SEP_STRING
} ,
{
2010-02-25 15:10:12 +00:00
U_FILE_SEP_STRING " vmlinux " ,
2010-02-24 22:49:36 +00:00
200 ,
U_ZERO_ERROR ,
U_FILE_SEP_STRING
} ,
{
" pkgdata " ,
0 ,
U_BUFFER_OVERFLOW_ERROR ,
NULL
} ,
{
" pkgdata " ,
1 ,
U_BUFFER_OVERFLOW_ERROR ,
NULL
} ,
{
" pkgdata " ,
2 ,
U_ZERO_ERROR ,
" . "
} ,
{
" pkgdata " ,
20 ,
U_ZERO_ERROR ,
" . "
}
} ;
int32_t count = ( sizeof ( testCases ) / sizeof ( testCases [ 0 ] ) ) ;
log_verbose ( " Testing findDirname() \n " ) ;
for ( i = 0 ; i < count ; i + + ) {
const char * result ;
const char * input = STRNULL ( testCases [ i ] . inBuf ) ;
const char * expect = STRNULL ( testCases [ i ] . expectResult ) ;
UErrorCode status = U_ZERO_ERROR ;
uprv_memset ( toolutil_testBuf , 0x55 , TOOLUTIL_TESTBUF_SIZE ) ;
log_verbose ( " Test case [%d/%d]: %s \n " , i , count - 1 , input ) ;
result = STRNULL ( findDirname ( testCases [ i ] . inBuf , toolutil_testBuf , testCases [ i ] . outBufLen , & status ) ) ;
log_verbose ( " -> %s, \n " , u_errorName ( status ) ) ;
if ( status ! = testCases [ i ] . expectStatus ) {
log_verbose ( " FAIL: Test case [%d/%d]: %s got error code %s but expected %s \n " , i , count - 1 , input , u_errorName ( status ) , u_errorName ( testCases [ i ] . expectStatus ) ) ;
}
if ( result = = expect | | ! strcmp ( result , expect ) ) {
log_verbose ( " = -> %s \n " , result ) ;
} else {
log_err ( " FAIL: Test case [%d/%d]: %s -> %s but expected %s \n " , i , count - 1 , input , result , expect ) ;
}
}
2001-10-05 21:55:15 +00:00
}
2010-02-24 22:49:36 +00:00
static void addToolUtilTests ( TestNode * * root ) {
addTest ( root , & toolutil_findBasename , " putiltst/toolutil/findBasename " ) ;
addTest ( root , & toolutil_findDirname , " putiltst/toolutil/findDirname " ) ;
/*
Not yet tested :
addTest ( root , & toolutil_getLongPathname , " putiltst/toolutil/getLongPathname " ) ;
addTest ( root , & toolutil_getCurrentYear , " putiltst/toolutil/getCurrentYear " ) ;
addTest ( root , & toolutil_UToolMemory , " putiltst/toolutil/UToolMemory " ) ;
*/
}