home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / cintltst / cnormtst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  10.7 KB  |  237 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 CNORMTST.C
  15. *
  16. * Modification History:
  17. *        Name                     Description            
  18. *     Madhu Katragadda            Ported for C API
  19. *********************************************************************************
  20. /*tests for u_normalization*/
  21. #include "utypes.h"
  22. #include "ucol.h"
  23. #include "uloc.h"
  24. #include "cintltst.h"
  25. #include "cnormtst.h"
  26. #include "ccolltst.h"
  27. #include "ustring.h"
  28. #include <memory.h>
  29. #define ARRAY_LENGTH(array) (sizeof (array) / sizeof (*array))
  30.  
  31.  
  32.  
  33. static UCollator *myCollation;
  34. const static char* canonTests[][3] = {
  35.     /* Input*/                    /*Decomposed*/                /*Composed*/
  36.     { "cat",                    "cat",                        "cat"                    },
  37.     { "\\u00e0ardvark",            "a\\u0300ardvark",            "\\u00e0ardvark",        },
  38.  
  39.     { "\\u1e0a",                "D\\u0307",                    "\\u1e0a"                }, /* D-dot_above*/
  40.     { "D\\u0307",                "D\\u0307",                    "\\u1e0a"                }, /* D dot_above*/
  41.     
  42.     { "\\u1e0c\\u0307",            "D\\u0323\\u0307",            "\\u1e0c\\u0307"        }, /* D-dot_below dot_above*/
  43.     { "\\u1e0a\\u0323",            "D\\u0323\\u0307",            "\\u1e0c\\u0307"        }, /* D-dot_above dot_below */
  44.     { "D\\u0307\\u0323",        "D\\u0323\\u0307",            "\\u1e0c\\u0307"        }, /* D dot_below dot_above */
  45.     
  46.     { "\\u1e10\\u0307\\u0323",    "D\\u0327\\u0323\\u0307",    "\\u1e10\\u0323\\u0307"    }, /*D dot_below cedilla dot_above*/
  47.     { "D\\u0307\\u0328\\u0323",    "D\\u0328\\u0323\\u0307",    "\\u1e0c\\u0328\\u0307"    }, /* D dot_above ogonek dot_below*/
  48.  
  49.     { "\\u1E14",                "E\\u0304\\u0300",            "\\u1E14"                }, /* E-macron-grave*/
  50.     { "\\u0112\\u0300",            "E\\u0304\\u0300",            "\\u1E14"                }, /* E-macron + grave*/
  51.     { "\\u00c8\\u0304",            "E\\u0300\\u0304",            "\\u00c8\\u0304"        }, /* E-grave + macron*/
  52.     
  53.     { "\\u212b",                "A\\u030a",                    "\\u00c5"                }, /* angstrom_sign*/
  54.     { "\\u00c5",                "A\\u030a",                    "\\u00c5"                }, /* A-ring*/
  55.     
  56.     { "\\u00C4ffin",            "A\\u0308ffin",                "\\u00C4ffin"                    },
  57.     { "\\u00C4\\uFB03n",        "A\\u0308\\uFB03n",            "\\u00C4\\uFB03n"                },
  58.  
  59.     { "Henry IV",                "Henry IV",                    "Henry IV"                },
  60.     { "Henry \\u2163",            "Henry \\u2163",            "Henry \\u2163"            },
  61.  
  62.     { "\\u30AC",                "\\u30AB\\u3099",            "\\u30AC"                }, /* ga (Katakana)*/
  63.     { "\\u30AB\\u3099",            "\\u30AB\\u3099",            "\\u30AC"                }, /*ka + ten*/
  64.     { "\\uFF76\\uFF9E",            "\\uFF76\\uFF9E",            "\\uFF76\\uFF9E"        }, /* hw_ka + hw_ten*/
  65.     { "\\u30AB\\uFF9E",            "\\u30AB\\uFF9E",            "\\u30AB\\uFF9E"        }, /* ka + hw_ten*/
  66.     { "\\uFF76\\u3099",            "\\uFF76\\u3099",            "\\uFF76\\u3099"        }  /* hw_ka + ten*/
  67. };
  68.  
  69. const static char* compatTests[][3] = {
  70.     /* Input*/                        /*Decomposed    */                /*Composed*/
  71.     { "cat",                        "cat",                            "cat"                },
  72.  
  73.     { "\\uFB4f",                    "\\u05D0\\u05DC",                "\\u05D0\\u05DC"    }, /* Alef-Lamed vs. Alef, Lamed*/
  74.  
  75.     { "\\u00C4ffin",                "A\\u0308ffin",                    "\\u00C4ffin"             },
  76.     { "\\u00C4\\uFB03n",            "A\\u0308ffin",                    "\\u00C4ffin"                }, /* ffi ligature -> f + f + i*/
  77.  
  78.     { "Henry IV",                    "Henry IV",                        "Henry IV"            },
  79.     { "Henry \\u2163",                "Henry IV",                        "Henry IV"            },
  80.  
  81.     { "\\u30AC",                    "\\u30AB\\u3099",                "\\u30AC"            }, /* ga (Katakana)*/
  82.     { "\\u30AB\\u3099",                "\\u30AB\\u3099",                "\\u30AC"            }, /*ka + ten*/
  83.     
  84.     { "\\uFF76\\u3099",                "\\u30AB\\u3099",                "\\u30AC"            }, /* hw_ka + ten*/
  85.  
  86.     /*These two are broken in Unicode 2.1.2 but fixed in 2.1.5 and later*/
  87.     { "\\uFF76\\uFF9E",                "\\u30AB\\u3099",                "\\u30AC"            }, /* hw_ka + hw_ten*/
  88.     { "\\u30AB\\uFF9E",                "\\u30AB\\u3099",                "\\u30AC"            } /* ka + hw_ten*/
  89.     
  90. };
  91.  
  92.  
  93. void addNormTest(TestNode** root)
  94. {
  95.     
  96.     addTest(root, &TestDecomp, "tscoll/cnormtst/TestDecomp");
  97.     addTest(root, &TestCompatDecomp, "tscoll/cnormtst/TestCompatDecomp");
  98.     addTest(root, &TestCanonDecompCompose, "tscoll/cnormtst/TestCanonDecompCompose");
  99.     addTest(root, &TestCompatDecompCompose, "tscoll/cnormtst/CompatDecompCompose");
  100.  
  101. }
  102.  
  103. void TestDecomp() 
  104. {
  105.     UErrorCode status = U_ZERO_ERROR;
  106.     int32_t x, neededLen, resLen;
  107.     UChar *source, *result; 
  108.     status = U_ZERO_ERROR;
  109.     myCollation = ucol_open("en_US", &status);
  110.     if(U_FAILURE(status)){
  111.         log_err("ERROR: in creation of rule based collator: %s\n", myErrorName(status));
  112.     }
  113.     resLen=0;
  114.     log_verbose("Testing u_normalize with  Decomp canonical\n");
  115.     for(x=0; x < ARRAY_LENGTH(canonTests); x++)
  116.     {
  117.         source=CharsToUChars(canonTests[x][0]);
  118.         neededLen= u_normalize(source, u_strlen(source), UCOL_DECOMP_CAN, UCOL_IGNORE_HANGUL, NULL, 0, &status); 
  119.         if(status==U_BUFFER_OVERFLOW_ERROR)
  120.         {
  121.         status=U_ZERO_ERROR;
  122.         resLen=neededLen+1;
  123.         result=(UChar*)malloc(sizeof(UChar*) * resLen);
  124.         u_normalize(source, u_strlen(source), UCOL_DECOMP_CAN, UCOL_IGNORE_HANGUL, result, resLen, &status); 
  125.         }
  126.         if(U_FAILURE(status)){
  127.             log_err("ERROR in u_normalize at %s:  %s\n", austrdup(source), myErrorName(status) );
  128.         }
  129.         assertEqual(result, CharsToUChars(canonTests[x][1]), x);
  130.         free(result);
  131.     }
  132.     ucol_close(myCollation);            
  133. }
  134. void TestCompatDecomp() 
  135. {
  136.     UErrorCode status = U_ZERO_ERROR;
  137.     int32_t x, neededLen, resLen;
  138.     UChar *source, *result; 
  139.     status = U_ZERO_ERROR;
  140.     myCollation = ucol_open("en_US", &status);
  141.     if(U_FAILURE(status)){
  142.         log_err("ERROR: in creation of rule based collator: %s\n", myErrorName(status));
  143.     }
  144.     resLen=0;
  145.     log_verbose("Testing u_normalize with  Decomp compat\n");
  146.     for(x=0; x < ARRAY_LENGTH(compatTests); x++)
  147.     {
  148.         source=CharsToUChars(compatTests[x][0]);
  149.         neededLen= u_normalize(source, u_strlen(source), UCOL_DECOMP_COMPAT, UCOL_IGNORE_HANGUL, NULL, 0, &status); 
  150.         if(status==U_BUFFER_OVERFLOW_ERROR)
  151.         {
  152.         status=U_ZERO_ERROR;
  153.         resLen=neededLen+1;
  154.         result=(UChar*)malloc(sizeof(UChar*) * resLen);
  155.         u_normalize(source, u_strlen(source), UCOL_DECOMP_COMPAT,UCOL_IGNORE_HANGUL, result, resLen, &status); 
  156.         }
  157.         if(U_FAILURE(status)){
  158.             log_err("ERROR in u_normalize at %s:  %s\n", austrdup(source), myErrorName(status) );
  159.         }
  160.         assertEqual(result, CharsToUChars(compatTests[x][1]), x);
  161.         free(result);
  162.     }
  163.     ucol_close(myCollation);            
  164. }
  165. void TestCanonDecompCompose() 
  166. {
  167.     UErrorCode status = U_ZERO_ERROR;
  168.     int32_t x, neededLen, resLen;
  169.     UChar *source, *result; 
  170.     status = U_ZERO_ERROR;
  171.     myCollation = ucol_open("en_US", &status);
  172.     if(U_FAILURE(status)){
  173.         log_err("ERROR: in creation of rule based collator: %s\n", myErrorName(status));
  174.     }
  175.     resLen=0;
  176.     log_verbose("Testing u_normalize with Decomp can compose compat\n");
  177.     for(x=0; x < ARRAY_LENGTH(canonTests); x++)
  178.     {
  179.         source=CharsToUChars(canonTests[x][0]);
  180.         neededLen= u_normalize(source, u_strlen(source), UCOL_DECOMP_CAN_COMP_COMPAT, UCOL_IGNORE_HANGUL, NULL, 0, &status); 
  181.         if(status==U_BUFFER_OVERFLOW_ERROR)
  182.         {
  183.         status=U_ZERO_ERROR;
  184.         resLen=neededLen+1;
  185.         result=(UChar*)malloc(sizeof(UChar*) * resLen);
  186.         u_normalize(source, u_strlen(source), UCOL_DECOMP_CAN_COMP_COMPAT, UCOL_IGNORE_HANGUL, result, resLen, &status); 
  187.         }
  188.         if(U_FAILURE(status)){
  189.             log_err("ERROR in u_normalize at %s:  %s\n", austrdup(source),myErrorName(status) );
  190.         }
  191.         assertEqual(result, CharsToUChars(canonTests[x][2]), x);
  192.         free(result);
  193.     }
  194.     ucol_close(myCollation);            
  195. }
  196. void TestCompatDecompCompose() 
  197. {
  198.     UErrorCode status = U_ZERO_ERROR;
  199.     int32_t x, neededLen, resLen;
  200.     UChar *source, *result; 
  201.     status = U_ZERO_ERROR;
  202.     myCollation = ucol_open("en_US", &status);
  203.     if(U_FAILURE(status)){
  204.         log_err("ERROR: in creation of rule based collator: %s\n", myErrorName(status));
  205.     }
  206.     resLen=0;
  207.     log_verbose("Testing u_normalize with compat decomp compose can\n");
  208.     for(x=0; x < ARRAY_LENGTH(compatTests); x++)
  209.     {
  210.         source=CharsToUChars(compatTests[x][0]);
  211.         neededLen= u_normalize(source, u_strlen(source), UCOL_DECOMP_COMPAT_COMP_CAN, UCOL_IGNORE_HANGUL, NULL, 0, &status); 
  212.         if(status==U_BUFFER_OVERFLOW_ERROR)
  213.         {
  214.         status=U_ZERO_ERROR;
  215.         resLen=neededLen+1;
  216.         result=(UChar*)malloc(sizeof(UChar*) * resLen);
  217.         u_normalize(source, u_strlen(source), UCOL_DECOMP_COMPAT_COMP_CAN, UCOL_IGNORE_HANGUL, result, resLen, &status); 
  218.         }
  219.         if(U_FAILURE(status)){
  220.             log_err("ERROR in u_normalize at %s:  %s\n", austrdup(source), myErrorName(status) );
  221.         }
  222.         assertEqual(result, CharsToUChars(compatTests[x][2]), x);
  223.         free(result);
  224.     }
  225.     ucol_close(myCollation);            
  226. }
  227.  
  228.  
  229.  
  230. void assertEqual(const UChar* result, const UChar* expected, int32_t index)
  231. {
  232.     if(u_strcmp(result, expected)!=0){
  233.         log_err("ERROR in decomposition at index = %d. EXPECTED: %s , GOT: %s\n", index, austrdup(expected),
  234.             austrdup(result) );
  235.     }
  236. }
  237.