home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / cintltst / ccurrtst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  4.3 KB  |  130 lines

  1. /*
  2. *****************************************************************************************
  3. *                                                                                       *
  4. * COPYRIGHT:                                                                            *
  5. *   (C) Copyright Taligent, Inc.,  1996                                                 *
  6. *   (C) Copyright International Business Machines Corporation,  1999                    *
  7. *   Licensed Material - Program-Property of IBM - All Rights Reserved.                  *
  8. *   US Government Users Restricted Rights - Use, duplication, or disclosure             *
  9. *   restricted by GSA ADP Schedule Contract with IBM Corp.                              *
  10. *                                                                                       *
  11. *****************************************************************************************
  12. ********************************************************************************
  13. *
  14. * File CCURRTST.C
  15. *
  16. * Modification History:
  17. *        Name                     Description            
  18. *     Madhu Katragadda             Ported for C API
  19. *********************************************************************************
  20. */
  21.  
  22. #include "utypes.h"
  23. #include "ucol.h"
  24. #include "uloc.h"
  25. #include "cintltst.h"
  26. #include "ccurrtst.h"
  27. #include "ccolltst.h"
  28. #include "ustring.h"
  29. #include <memory.h>
  30.  
  31. #define ARRAY_LENGTH(array) (sizeof array / sizeof array[0]) 
  32.  
  33. void addCurrencyTest(TestNode** root)
  34. {
  35.     
  36.     addTest(root, &currTest, "tscoll/ccurrtst/currTest");
  37.     
  38. }
  39.  
  40.  
  41. void currTest()
  42. {
  43.     /* All the currency symbols, in collation order*/
  44.     static const UChar currency[][2] =
  45.     {
  46.         { 0x00a4, 0x0000}, /* generic currency*/
  47.         { 0x0e3f, 0x0000}, /* baht*/
  48.         { 0x00a2, 0x0000}, /* cent*/
  49.         { 0x20a1, 0x0000}, /* colon*/
  50.         { 0x20a2, 0x0000}, /* cruzeiro*/
  51.         { 0x0024, 0x0000}, /* dollar */
  52.         { 0x20ab, 0x0000}, /* dong */
  53.         { 0x20ac, 0x0000}, /* euro */
  54.         { 0x20a3, 0x0000}, /* franc */
  55.         { 0x20a4, 0x0000}, /* lira */
  56.         { 0x20a5, 0x0000}, /* mill */
  57.         { 0x20a6, 0x0000}, /* naira */
  58.         { 0x20a7, 0x0000}, /* peseta */
  59.         { 0x00a3, 0x0000}, /* pound */
  60.         { 0x20a8, 0x0000}, /* rupee */
  61.         { 0x20aa, 0x0000}, /* shekel*/
  62.         { 0x20a9, 0x0000}, /* won*/
  63.         { 0x00a5, 0x0000}  /* yen*/
  64.     };
  65.     UChar source[2], target[2];
  66.     int32_t i, j, sortklen;
  67.     int res;
  68.     UCollator *c;
  69.     uint8_t *sortKey1, *sortKey2;
  70.     UErrorCode status = U_ZERO_ERROR;
  71.     UCollationResult compareResult, keyResult;
  72.     UCollationResult expectedResult = UCOL_EQUAL;
  73.     log_verbose("Testing currency of all locales\n");
  74.     c = ucol_open(NULL, &status);
  75.     if (U_FAILURE(status))
  76.     {
  77.         log_err("collator open failed! :%s\n", myErrorName(status));
  78.         return;
  79.     }
  80.  
  81.     /*Compare each currency symbol against all the
  82.      currency symbols, including itself*/
  83.     for (i = 0; i < ARRAY_LENGTH(currency); i += 1)
  84.     {
  85.         for (j = 0; j < ARRAY_LENGTH(currency); j += 1)
  86.         {
  87.              u_strcpy(source, currency[i]);
  88.              u_strcpy(target, currency[j]);
  89.             
  90.             if (i < j)
  91.             {
  92.                 expectedResult = UCOL_LESS;
  93.             }
  94.             else if ( i == j)
  95.             {
  96.                 expectedResult = UCOL_EQUAL;
  97.             }
  98.             else
  99.             {
  100.                 expectedResult = UCOL_GREATER;
  101.             }
  102.  
  103.             compareResult = ucol_strcoll(c, source, u_strlen(source), target, u_strlen(target));
  104.  
  105.             
  106.             status = U_ZERO_ERROR;
  107.  
  108.             sortklen=ucol_getSortKey(c, source, u_strlen(source),  NULL, 0);
  109.             sortKey1=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
  110.             ucol_getSortKey(c, source, u_strlen(source), sortKey1, sortklen+1);
  111.     
  112.             sortklen=ucol_getSortKey(c, target, u_strlen(target),  NULL, 0);
  113.             sortKey2=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
  114.             ucol_getSortKey(c, target, u_strlen(target), sortKey2, sortklen+1);
  115.     
  116.             res = memcmp(sortKey1, sortKey2, sortklen);
  117.             if (res < 0) keyResult = -1;
  118.             else if (res > 0) keyResult = 1;
  119.             else keyResult = 0;
  120.             
  121.             reportCResult(source, target, sortKey1, sortKey2,
  122.                           compareResult, keyResult, expectedResult);
  123.  
  124.         }
  125.     }
  126. }
  127.  
  128.  
  129.  
  130.