~ubuntu-branches/ubuntu/quantal/icu/quantal

« back to all changes in this revision

Viewing changes to source/test/cintltst/putiltst.c

  • Committer: Package Import Robot
  • Author(s): Yves Arrouye
  • Date: 2002-03-03 15:31:13 UTC
  • Revision ID: package-import@ubuntu.com-20020303153113-3ssceqlq45xbmbnc
Tags: upstream-2.0-2.1pre20020303
ImportĀ upstreamĀ versionĀ 2.0-2.1pre20020303

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/********************************************************************
 
2
 * COPYRIGHT: 
 
3
 * Copyright (c) 1998-2001, International Business Machines Corporation and
 
4
 * others. All Rights Reserved.
 
5
 ********************************************************************/
 
6
/*
 
7
* File putiltst.c (Tests the API in putil)
 
8
*
 
9
* Modification History:
 
10
*
 
11
*   Date          Name        Description
 
12
*   07/12/2000    Madhu       Creation 
 
13
*******************************************************************************
 
14
*/
 
15
 
 
16
#include "unicode/utypes.h"
 
17
#include "cintltst.h"
 
18
#include "cmemory.h"
 
19
#include "unicode/putil.h"
 
20
#include "unicode/ustring.h"
 
21
#include "cstring.h"
 
22
 
 
23
static UBool compareWithNAN(double x, double y);
 
24
static void doAssert(double expect, double got, const char *message);
 
25
 
 
26
static void TestPUtilAPI(){
 
27
 
 
28
    double  n1=0.0, y1=0.0, expn1, expy1;
 
29
    double  value1 = 0.021;
 
30
    UVersionInfo versionArray = {0x01, 0x00, 0x02, 0x02};
 
31
    char versionString[17]; /* xxx.xxx.xxx.xxx\0 */
 
32
    char *str=0;
 
33
    UBool isTrue=FALSE;
 
34
 
 
35
    log_verbose("Testing the API uprv_modf()\n");
 
36
    y1 = uprv_modf(value1, &n1);
 
37
    expn1=0;
 
38
    expy1=0.021;
 
39
    if(y1 != expy1   || n1 != expn1){
 
40
        log_err("Error in uprv_modf.  Expected IntegralValue=%f, Got=%f, \n Expected FractionalValue=%f, Got=%f\n",
 
41
             expn1, n1, expy1, y1);
 
42
    }
 
43
    if(VERBOSITY){
 
44
        log_verbose("[float]  x = %f  n = %f y = %f\n", value1, n1, y1);
 
45
    }
 
46
    log_verbose("Testing the API uprv_fmod()\n");
 
47
    expn1=uprv_fmod(30.50, 15.00);
 
48
    doAssert(expn1, 0.5, "uprv_fmod(30.50, 15.00) failed.");
 
49
 
 
50
    log_verbose("Testing the API uprv_ceil()\n");
 
51
    expn1=uprv_ceil(value1);
 
52
    doAssert(expn1, 1, "uprv_ceil(0.021) failed.");
 
53
 
 
54
    log_verbose("Testing the API uprv_floor()\n");
 
55
    expn1=uprv_floor(value1);
 
56
    doAssert(expn1, 0, "uprv_floor(0.021) failed.");
 
57
 
 
58
    log_verbose("Testing the API uprv_fabs()\n");
 
59
    expn1=uprv_fabs((2.02-1.345));
 
60
    doAssert(expn1, 0.675, "uprv_fabs(2.02-1.345) failed.");
 
61
    
 
62
    log_verbose("Testing the API uprv_fmax()\n");
 
63
    doAssert(uprv_fmax(2.4, 1.2), 2.4, "uprv_fmax(2.4, 1.2) failed.");
 
64
 
 
65
    log_verbose("Testing the API uprv_fmax() with x value= NaN\n");
 
66
    expn1=uprv_fmax(uprv_getNaN(), 1.2);
 
67
    doAssert(expn1, uprv_getNaN(), "uprv_fmax(uprv_getNaN(), 1.2) failed. when one parameter is NaN");
 
68
 
 
69
    log_verbose("Testing the API uprv_fmin()\n");
 
70
    doAssert(uprv_fmin(2.4, 1.2), 1.2, "uprv_fmin(2.4, 1.2) failed.");
 
71
 
 
72
    log_verbose("Testing the API uprv_fmin() with x value= NaN\n");
 
73
    expn1=uprv_fmin(uprv_getNaN(), 1.2);
 
74
    doAssert(expn1, uprv_getNaN(), "uprv_fmin(uprv_getNaN(), 1.2) failed. when one parameter is NaN");
 
75
 
 
76
    log_verbose("Testing the API uprv_max()\n");
 
77
    doAssert(uprv_max(4, 2), 4, "uprv_max(4, 2) failed.");
 
78
 
 
79
    log_verbose("Testing the API uprv_min()\n");
 
80
    doAssert(uprv_min(-4, 2), -4, "uprv_min(-4, 2) failed.");
 
81
 
 
82
    log_verbose("Testing the API uprv_trunc()\n");
 
83
    doAssert(uprv_trunc(12.3456), 12, "uprv_trunc(12.3456) failed.");
 
84
    doAssert(uprv_trunc(12.234E2), 1223, "uprv_trunc(12.234E2) failed.");
 
85
    doAssert(uprv_trunc(uprv_getNaN()), uprv_getNaN(), "uprv_trunc(uprv_getNaN()) failed. with parameter=NaN");
 
86
    doAssert(uprv_trunc(uprv_getInfinity()), uprv_getInfinity(), "uprv_trunc(uprv_getInfinity()) failed. with parameter=Infinity");
 
87
 
 
88
 
 
89
    log_verbose("Testing the API uprv_pow10()\n");
 
90
    doAssert(uprv_pow10(4), 10000, "uprv_pow10(4) failed.");
 
91
 
 
92
    log_verbose("Testing the API uprv_log10()\n");
 
93
    doAssert(uprv_log10(3456), 3, "uprv_log10(3456) failed.");
 
94
#ifdef OS390
 
95
    doAssert(uprv_log10(1.0e55), 55, "uprv_log10(1.0e55) failed.");
 
96
#else
 
97
    doAssert(uprv_log10(1.0e300), 300, "uprv_log10(1.0e300) failed.");
 
98
#endif          
 
99
    log_verbose("Testing the API uprv_isNegativeInfinity()\n");
 
100
    isTrue=uprv_isNegativeInfinity(uprv_getInfinity() * -1);
 
101
    if(isTrue != TRUE){
 
102
        log_err("ERROR: uprv_isNegativeInfinity failed.\n");
 
103
    }
 
104
    log_verbose("Testing the API uprv_isPositiveInfinity()\n");
 
105
    isTrue=uprv_isPositiveInfinity(uprv_getInfinity());
 
106
    if(isTrue != TRUE){
 
107
        log_err("ERROR: uprv_isPositiveInfinity failed.\n");
 
108
    }
 
109
    log_verbose("Testing the API uprv_isInfinite()\n");
 
110
    isTrue=uprv_isInfinite(uprv_getInfinity());
 
111
    if(isTrue != TRUE){
 
112
        log_err("ERROR: uprv_isInfinite failed.\n");
 
113
    }
 
114
 
 
115
    log_verbose("Testing the API uprv_digitsAfterDecimal()....\n");
 
116
    doAssert(uprv_digitsAfterDecimal(value1), 3, "uprv_digitsAfterDecimal() failed.");
 
117
    doAssert(uprv_digitsAfterDecimal(1.2345E2), 2, "uprv_digitsAfterDecimal(1.2345E2) failed.");
 
118
    doAssert(uprv_digitsAfterDecimal(1.2345E-2), 6, "uprv_digitsAfterDecimal(1.2345E-2) failed.");
 
119
    doAssert(uprv_digitsAfterDecimal(1.2345E2), 2, "uprv_digitsAfterDecimal(1.2345E2) failed.");
 
120
    doAssert(uprv_digitsAfterDecimal(-1.2345E-20), 24, "uprv_digitsAfterDecimal(1.2345E-20) failed.");
 
121
    doAssert(uprv_digitsAfterDecimal(1.2345E20), 0, "uprv_digitsAfterDecimal(1.2345E20) failed.");
 
122
    doAssert(uprv_digitsAfterDecimal(-0.021), 3, "uprv_digitsAfterDecimal(-0.021) failed.");
 
123
    doAssert(uprv_digitsAfterDecimal(23.0), 0, "uprv_digitsAfterDecimal(23.0) failed.");
 
124
    doAssert(uprv_digitsAfterDecimal(0.022223333321), 9, "uprv_digitsAfterDecimal(0.022223333321) failed.");
 
125
 
 
126
 
 
127
    log_verbose("Testing the API u_versionToString().....\n");
 
128
    u_versionToString(versionArray, versionString);
 
129
    if(strcmp(versionString, "1.0.2.2") != 0){
 
130
        log_err("ERROR: u_versionToString() failed. Expected: 1.0.2.2, Got=%s\n", versionString);
 
131
    }
 
132
    log_verbose("Testing the API u_versionToString().....with versionArray=NULL\n");
 
133
    u_versionToString(NULL, versionString);
 
134
    if(strcmp(versionString, "") != 0){
 
135
        log_err("ERROR: u_versionToString() failed. with versionArray=NULL. It should just return\n");
 
136
    }
 
137
    log_verbose("Testing the API u_versionToString().....with versionArray=NULL\n");
 
138
    u_versionToString(NULL, versionString);
 
139
    if(strcmp(versionString, "") != 0){
 
140
        log_err("ERROR: u_versionToString() failed . It should just return\n");
 
141
    }
 
142
    log_verbose("Testing the API u_versionToString().....with versionString=NULL\n");
 
143
    u_versionToString(versionArray, NULL);
 
144
    if(strcmp(versionString, "") != 0){
 
145
        log_err("ERROR: u_versionToString() failed. with versionArray=NULL  It should just return\n");
 
146
    }
 
147
    versionArray[0] = 0x0a;
 
148
    log_verbose("Testing the API u_versionToString().....\n");
 
149
    u_versionToString(versionArray, versionString);
 
150
    if(strcmp(versionString, "10.0.2.2") != 0){
 
151
        log_err("ERROR: u_versionToString() failed. Expected: 10.0.2.2, Got=%s\n", versionString);
 
152
    }
 
153
    versionArray[0] = 0xa0;
 
154
    u_versionToString(versionArray, versionString);
 
155
    if(strcmp(versionString, "160.0.2.2") != 0){
 
156
        log_err("ERROR: u_versionToString() failed. Expected: 160.0.2.2, Got=%s\n", versionString);
 
157
    }
 
158
    versionArray[0] = 0xa0;
 
159
    versionArray[1] = 0xa0;
 
160
    u_versionToString(versionArray, versionString);
 
161
    if(strcmp(versionString, "160.160.2.2") != 0){
 
162
        log_err("ERROR: u_versionToString() failed. Expected: 160.160.2.2, Got=%s\n", versionString);
 
163
    }
 
164
    versionArray[0] = 0x01;
 
165
    versionArray[1] = 0x0a;
 
166
    u_versionToString(versionArray, versionString);
 
167
    if(strcmp(versionString, "1.10.2.2") != 0){
 
168
        log_err("ERROR: u_versionToString() failed. Expected: 160.160.2.2, Got=%s\n", versionString);
 
169
    }
 
170
 
 
171
    log_verbose("Testing the API u_versionFromString() ....\n");
 
172
    u_versionFromString(versionArray, "1.3.5.6");
 
173
    u_versionToString(versionArray, versionString);
 
174
    if(strcmp(versionString, "1.3.5.6") != 0){
 
175
        log_err("ERROR: u_getVersion() failed. Expected: 1.3.5.6, Got=%s\n",  versionString);
 
176
    }
 
177
    log_verbose("Testing the API u_versionFromString() where versionArray=NULL....\n");
 
178
    u_versionFromString(NULL, "1.3.5.6");
 
179
    u_versionToString(versionArray, versionString);
 
180
    if(strcmp(versionString, "1.3.5.6") != 0){
 
181
        log_err("ERROR: u_getVersion() failed. Expected: 1.3.5.6, Got=%s\n",  versionString);
 
182
    }
 
183
 
 
184
    log_verbose("Testing the API u_getVersion().....\n");
 
185
    u_getVersion(versionArray);
 
186
    u_versionToString(versionArray, versionString);
 
187
    if(strcmp(versionString, U_ICU_VERSION) != 0){
 
188
        log_err("ERROR: u_getVersion() failed. Got=%s, expected %s\n",  versionString, U_ICU_VERSION);
 
189
    }
 
190
    log_verbose("Testing the API u_errorName()...\n");
 
191
    str=(char*)u_errorName((UErrorCode)0);
 
192
    if(strcmp(str, "U_ZERO_ERROR") != 0){
 
193
        log_err("ERROR: u_getVersion() failed. Expected: U_ZERO_ERROR Got=%s\n",  str);
 
194
    }
 
195
    log_verbose("Testing the API u_errorName()...\n");
 
196
    str=(char*)u_errorName((UErrorCode)-127);
 
197
    if(strcmp(str, "U_USING_DEFAULT_WARNING") != 0){
 
198
        log_err("ERROR: u_getVersion() failed. Expected: U_USING_DEFAULT_WARNING Got=%s\n",  str);
 
199
    }
 
200
    log_verbose("Testing the API u_errorName().. with BOGUS ERRORCODE...\n");
 
201
    str=(char*)u_errorName((UErrorCode)200);
 
202
    if(strcmp(str, "[BOGUS UErrorCode]") != 0){
 
203
        log_err("ERROR: u_getVersion() failed. Expected: [BOGUS UErrorCode] Got=%s\n",  str);
 
204
    }
 
205
 
 
206
    {
 
207
        const char* dataDirectory;
 
208
        UChar *udataDir=0;
 
209
        UChar temp[100];
 
210
        char *charvalue=0;
 
211
        log_verbose("Testing chars to UChars\n");
 
212
        
 
213
         /* This cannot really work on a japanese system. u_uastrcpy will have different results than */
 
214
        /* u_charsToUChars when there is a backslash in the string! */
 
215
        /*dataDirectory=u_getDataDirectory();*/
 
216
 
 
217
        dataDirectory="directory1";  /*no backslashes*/
 
218
        udataDir=(UChar*)uprv_malloc(sizeof(UChar) * (strlen(dataDirectory) + 1));
 
219
        u_charsToUChars(dataDirectory, udataDir, (strlen(dataDirectory)+1));
 
220
        u_uastrcpy(temp, dataDirectory);
 
221
       
 
222
        if(u_strcmp(temp, udataDir) != 0){
 
223
            log_err("ERROR: u_charsToUChars failed. Expected %s, Got %s\n", austrdup(temp), austrdup(udataDir));
 
224
        }
 
225
        log_verbose("Testing UChars to chars\n");
 
226
        charvalue=(char*)uprv_malloc(sizeof(char) * (u_strlen(udataDir) + 1));
 
227
 
 
228
        u_UCharsToChars(udataDir, charvalue, (u_strlen(udataDir)+1));
 
229
        if(strcmp(charvalue, dataDirectory) != 0){
 
230
            log_err("ERROR: u_UCharsToChars failed. Expected %s, Got %s\n", charvalue, dataDirectory);
 
231
        }
 
232
        uprv_free(charvalue);
 
233
        uprv_free(udataDir);
 
234
    }
 
235
   
 
236
    log_verbose("Testing uprv_timezone()....\n");
 
237
    {
 
238
        int32_t tzoffset = uprv_timezone();
 
239
        log_verbose("Value returned from uprv_timezone = %d\n",  tzoffset);
 
240
        if (tzoffset != 28800) {
 
241
            log_verbose("***** WARNING: If testing in the PST timezone, t_timezone should return 28800! *****");
 
242
        }
 
243
        if ((tzoffset % 1800 != 0)) {
 
244
            log_err("FAIL: t_timezone may be incorrect. It is not a multiple of 30min.");
 
245
        }
 
246
        tzoffset=uprv_getUTCtime();
 
247
 
 
248
    }
 
249
 
 
250
/* We can deprecate the test code right away; the API itself goes 2002-Jun-30 */
 
251
#ifdef ICU_ENABLE_DEPRECATED_NEXTDOUBLE
 
252
    log_verbose("Testing uprv_nextDouble() where the value is NaN ...\n");
 
253
    expn1=uprv_nextDouble(uprv_getNaN(), TRUE);
 
254
    doAssert(expn1, uprv_getNaN(), "uprv_nextDouble(uprv_getNaN(), TRUE) failed.");
 
255
 
 
256
#if defined(OS400)
 
257
    /* This is oddly enough the same value as DBL_MIN on Windows. */
 
258
    /* This number is used because really small IEEE 754 numbers get an underflow exception on this platform */
 
259
    y1=2.2250738585072014e-308;
 
260
#elif IEEE_754
 
261
    /* OS390 may define IEEE_754, so check it first */
 
262
    y1=4.9406564584125e-324;
 
263
#elif defined(OS390) || defined(XP_MAC)
 
264
    y1=4.9406564584125e-78;
 
265
#else
 
266
    /* This is the default IEEE754 value. The test should fail if it's not correct. */
 
267
    y1=4.9406564584125e-324;
 
268
#endif
 
269
    doAssert(uprv_nextDouble(0, TRUE),   y1, "uprv_nextDouble(0, TRUE) failed.");
 
270
    doAssert(uprv_nextDouble(0, FALSE), -y1, "uprv_nextDouble(0, FALSE) failed.");
 
271
       
 
272
    expn1=uprv_nextDouble(1.0, TRUE);
 
273
    if(expn1 < 1.0 || expn1 > 1.0001){
 
274
        log_err("ERROR: uprv_nextDouble failed. Expected : %lf, Got: %f\n", y1, expn1);
 
275
    }
 
276
    expn1=uprv_nextDouble(1.0, FALSE);
 
277
    if(expn1 > 1.0 || expn1 < 0.9999){
 
278
        log_err("ERROR: uprv_nextDouble failed. Expected : %lf, Got: %f\n", y1, expn1);
 
279
    }
 
280
#endif
 
281
 
 
282
}
 
283
 
 
284
#if 0
 
285
static void testIEEEremainder()
 
286
{
 
287
    double    pinf        = uprv_getInfinity();
 
288
    double    ninf        = -uprv_getInfinity();
 
289
    double    nan         = uprv_getNaN();
 
290
/*    double    pzero       = 0.0;*/
 
291
/*    double    nzero       = 0.0;
 
292
    nzero *= -1;*/
 
293
 
 
294
     /* simple remainder checks*/
 
295
    remainderTest(7.0, 2.5, -0.5);
 
296
    remainderTest(7.0, -2.5, -0.5);
 
297
     /* this should work
 
298
     remainderTest(43.7, 2.5, 1.2);
 
299
     */
 
300
 
 
301
    /* infinity and real*/
 
302
    remainderTest(1.0, pinf, 1.0);
 
303
    remainderTest(1.0, ninf, 1.0);
 
304
 
 
305
    /*test infinity and real*/
 
306
    remainderTest(nan, 1.0, nan);
 
307
    remainderTest(1.0, nan, nan);
 
308
    /*test infinity and nan*/
 
309
    remainderTest(ninf, nan, nan);
 
310
    remainderTest(pinf, nan, nan);
 
311
 
 
312
    /* test infinity and zero */
 
313
/*    remainderTest(pinf, pzero, 1.25);
 
314
    remainderTest(pinf, nzero, 1.25);
 
315
    remainderTest(ninf, pzero, 1.25);
 
316
    remainderTest(ninf, nzero, 1.25); */
 
317
}
 
318
 
 
319
static void remainderTest(double x, double y, double exp)
 
320
{
 
321
    double result = uprv_IEEEremainder(x,y);
 
322
 
 
323
    if(        uprv_isNaN(result) && 
 
324
        ! ( uprv_isNaN(x) || uprv_isNaN(y))) {
 
325
        log_err("FAIL: got NaN as result without NaN as argument");
 
326
        log_err("      IEEEremainder(%f, %f) is %f, expected %f\n", x, y, result, exp);
 
327
    }
 
328
    else if(!compareWithNAN(result, exp)) {
 
329
        log_err("FAIL:  IEEEremainder(%f, %f) is %f, expected %f\n", x, y, result, exp);
 
330
    } else{
 
331
        log_verbose("OK: IEEEremainder(%f, %f) is %f\n", x, y, result);
 
332
    }
 
333
 
 
334
}
 
335
#endif
 
336
 
 
337
static UBool compareWithNAN(double x, double y)
 
338
{
 
339
  if( uprv_isNaN(x) || uprv_isNaN(y) ) {
 
340
    if(!uprv_isNaN(x) || !uprv_isNaN(y) ) {
 
341
      return FALSE;
 
342
    }
 
343
  }
 
344
  else if (y != x) { /* no NaN's involved */
 
345
    return FALSE;
 
346
  }
 
347
 
 
348
  return TRUE;
 
349
}
 
350
 
 
351
static void doAssert(double got, double expect, const char *message)
 
352
{
 
353
  if(! compareWithNAN(expect, got) ) {
 
354
    log_err("ERROR :  %s. Expected : %lf, Got: %lf\n", message, expect, got);
 
355
  }
 
356
}
 
357
 
 
358
 
 
359
#define _CODE_ARR_LEN 8
 
360
static const UErrorCode errorCode[_CODE_ARR_LEN] = {
 
361
    U_USING_FALLBACK_WARNING,
 
362
    U_STRING_NOT_TERMINATED_WARNING,
 
363
    U_ILLEGAL_ARGUMENT_ERROR,
 
364
    U_STATE_TOO_OLD_ERROR,
 
365
    U_BAD_VARIABLE_DEFINITION,
 
366
    U_RULE_MASK_ERROR,
 
367
    U_UNEXPECTED_TOKEN,
 
368
    U_UNSUPPORTED_ATTRIBUTE
 
369
};
 
370
 
 
371
static const char* str[] = {
 
372
    "U_USING_FALLBACK_WARNING",
 
373
    "U_STRING_NOT_TERMINATED_WARNING",
 
374
    "U_ILLEGAL_ARGUMENT_ERROR",
 
375
    "U_STATE_TOO_OLD_ERROR",
 
376
    "U_BAD_VARIABLE_DEFINITION",
 
377
    "U_RULE_MASK_ERROR",
 
378
    "U_UNEXPECTED_TOKEN",
 
379
    "U_UNSUPPORTED_ATTRIBUTE"
 
380
};
 
381
 
 
382
static void TestErrorName(){
 
383
    int32_t code=0;
 
384
    const char* errorName ;
 
385
    for(;code<U_ERROR_LIMIT;code++){
 
386
        errorName = u_errorName((UErrorCode)code);
 
387
    }
 
388
 
 
389
    for(code=0;code<_CODE_ARR_LEN; code++){
 
390
        errorName = u_errorName(errorCode[code]);
 
391
        if(uprv_strcmp(str[code],errorName )!=0){
 
392
            log_err("Error : u_errorName failed. Expected: %s Got: %s \n",str[code],errorName);
 
393
        }
 
394
    }
 
395
}
 
396
 
 
397
void addPUtilTest(TestNode** root);
 
398
 
 
399
void
 
400
addPUtilTest(TestNode** root)
 
401
{
 
402
    addTest(root, &TestPUtilAPI,       "putiltst/TestPUtilAPI");
 
403
/*    addTest(root, &testIEEEremainder,  "putiltst/testIEEEremainder"); */
 
404
    addTest(root, &TestErrorName, "putiltst/TestErrorName");
 
405
}
 
406