home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / intltest / ucdtest.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-11  |  14.9 KB  |  379 lines

  1. /*
  2. *****************************************************************************************
  3. *                                                                                       *
  4. * COPYRIGHT:                                                                            *
  5. *   (C) Copyright Taligent, Inc.,  1997                                                 *
  6. *   (C) Copyright International Business Machines Corporation,  1997-1998               *
  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. #include "ucdtest.h"
  15. #include "unicode.h"
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <assert.h>
  19. #include <iostream.h>
  20.  
  21. UnicodeTest::UnicodeTest()
  22. {
  23. }
  24.  
  25. UnicodeTest::~UnicodeTest()
  26. {
  27. }
  28.  
  29. void UnicodeTest::runIndexedTest( int32_t index, bool_t exec, char* &name, char* par )
  30. {
  31.     if (exec) logln("TestSuite UnicodeTest: ");
  32.     switch (index) {
  33.         case 0: name = "TestUpperLower"; if (exec) TestUpperLower(); break;
  34.         case 1: name = "TestLetterNumber"; if (exec) TestLetterNumber(); break;
  35.         case 2: name = "TestMisc"; if (exec) TestMisc(); break;
  36.         case 3: name = "TestUnicodeData"; if (exec) TestUnicodeData(); break;
  37.  
  38.         default: name = ""; break; //needed to end loop
  39.     }
  40. }
  41.  
  42. //====================================================
  43. // private data used by the tests
  44. //====================================================
  45.  
  46. const UChar  LAST_CHAR_CODE_IN_FILE = 0xFFFD;
  47. const char tagStrings[] = "MnMcMeNdNlNoZsZlZpCcCfCsCoCnLuLlLtLmLoPcPdPsPePoSmScSkSoPiPf";
  48. const int32_t tagValues[] =
  49.     {
  50.     /* Mn */ Unicode::NON_SPACING_MARK,
  51.     /* Mc */ Unicode::COMBINING_SPACING_MARK,
  52.     /* Me */ Unicode::ENCLOSING_MARK,
  53.     /* Nd */ Unicode::DECIMAL_DIGIT_NUMBER,
  54.     /* Nl */ Unicode::LETTER_NUMBER,
  55.     /* No */ Unicode::OTHER_NUMBER,
  56.     /* Zs */ Unicode::SPACE_SEPARATOR,
  57.     /* Zl */ Unicode::LINE_SEPARATOR,
  58.     /* Zp */ Unicode::PARAGRAPH_SEPARATOR,
  59.     /* Cc */ Unicode::CONTROL,
  60.     /* Cf */ Unicode::FORMAT,
  61.     /* Cs */ Unicode::SURROGATE,
  62.     /* Co */ Unicode::PRIVATE_USE,
  63.     /* Cn */ Unicode::UNASSIGNED,
  64.     /* Lu */ Unicode::UPPERCASE_LETTER,
  65.     /* Ll */ Unicode::LOWERCASE_LETTER,
  66.     /* Lt */ Unicode::TITLECASE_LETTER,
  67.     /* Lm */ Unicode::MODIFIER_LETTER,
  68.     /* Lo */ Unicode::OTHER_LETTER,
  69.     /* Pc */ Unicode::CONNECTOR_PUNCTUATION,
  70.     /* Pd */ Unicode::DASH_PUNCTUATION,
  71.     /* Ps */ Unicode::START_PUNCTUATION,
  72.     /* Pe */ Unicode::END_PUNCTUATION,
  73.     /* Po */ Unicode::OTHER_PUNCTUATION,
  74.     /* Sm */ Unicode::MATH_SYMBOL,
  75.     /* Sc */ Unicode::CURRENCY_SYMBOL,
  76.     /* Sk */ Unicode::MODIFIER_SYMBOL,
  77.     /* So */ Unicode::OTHER_SYMBOL,
  78.     /* Pi */ Unicode::INITIAL_PUNCTUATION,
  79.     /* Pf */ Unicode::FINAL_PUNCTUATION
  80.     };
  81. const char dirStrings[][5] = {
  82.     "L",
  83.     "R",
  84.     "EN",
  85.     "ES",
  86.     "ET",   
  87.     "AN",
  88.     "CS",
  89.     "B",
  90.     "S",
  91.     "WS",
  92.     "ON",
  93.     "LRE",
  94.     "LRO",
  95.     "AL",
  96.     "RLE",
  97.     "RLO",
  98.     "PDF",
  99.     "NSM",
  100.     "BN"
  101. };
  102.  
  103. //====================================================
  104. // test toUpperCase() and toLowerCase()
  105. //====================================================
  106. void UnicodeTest::TestUpperLower()
  107. {
  108.     static char* upperTest = "abcdefg123hij.?:klmno";
  109.     static char* lowerTest = "ABCDEFG123HIJ.?:KLMNO";
  110.     uint16_t i;
  111.  
  112. //Checks LetterLike Symbols which were previously a source of confusion
  113. //[Bertrand A. D. 02/04/98]
  114.     for (i=0x2100;i<0x2138;i++)
  115.     {
  116.         if(i!=0x2126 && i!=0x212a && i!=0x212b)
  117.         {
  118.         if (i != Unicode::toLowerCase(i)) // itself
  119.             errln("Failed case conversion with itself: " + UCharToUnicodeString(i));
  120.         if (i != Unicode::toUpperCase(i))
  121.             errln("Failed case conversion with itself: " + UCharToUnicodeString(i));
  122.         }
  123.     }
  124.  
  125.     for (i = 0; i < 21; i++) {
  126. //      logln((UnicodeString)"testing " + (int32_t)i + "...");
  127.         if (Unicode::isLetter(upperTest[i]) && !Unicode::isLowerCase(upperTest[i]))
  128.             errln("Failed isLowerCase test at " + UCharToUnicodeString(upperTest[i]));
  129.         else if (Unicode::isLetter(lowerTest[i]) && !Unicode::isUpperCase(lowerTest[i]))
  130.             errln("Failed isUpperCase test at " + UCharToUnicodeString(lowerTest[i]));
  131.         else if (upperTest[i] != Unicode::toLowerCase(lowerTest[i]))
  132.             errln("Failed case conversion : " + UCharToUnicodeString(upperTest[i]) + 
  133.             " to " + UCharToUnicodeString(lowerTest[i]));
  134.         else if (lowerTest[i] != Unicode::toUpperCase(upperTest[i]))
  135.             errln("Failed case conversion : " + UCharToUnicodeString(upperTest[i]) +
  136.             " to " + UCharToUnicodeString(lowerTest[i]));
  137.         else if (upperTest[i] != Unicode::toLowerCase(upperTest[i])) // itself
  138.             errln("Failed case conversion with itself: " + UCharToUnicodeString(upperTest[i]));
  139.         else if (lowerTest[i] != Unicode::toUpperCase(lowerTest[i]))
  140.             errln("Failed case conversion with itself: " + UCharToUnicodeString(lowerTest[i]));
  141.     }
  142. }
  143.  
  144. /* test isLetter() and isDigit() */
  145. void UnicodeTest::TestLetterNumber()
  146. {
  147.     UChar i;
  148.  
  149.     for (i = 0x0041; i < 0x005B; i++) {
  150. //      logln((UnicodeString)"testing " + (int32_t)i + "...");
  151.         if (!Unicode::isLetter(i))
  152.             errln("Failed isLetter test at " + UCharToUnicodeString(i));
  153.     }
  154.     for (i = 0x0660; i < 0x066A; i++) {
  155. //      logln((UnicodeString)"testing " + (int32_t)i + "...");
  156.         if (Unicode::isLetter(i))
  157.             errln("Failed isLetter test with numbers at " + i);
  158.     }
  159.     for (i = 0x0660; i < 0x066A; i++) {
  160. //      logln((UnicodeString)"testing " + (int32_t)i + "...");
  161.         if (!Unicode::isDigit(i))
  162.             errln("Failed isNumber test at " + i);
  163.     }
  164. }
  165.  
  166. /* Tests for isDefined(), isBaseForm(), isSpaceChar() and getCellWidth() */
  167. void UnicodeTest::TestMisc()
  168. {
  169.     const UChar sampleSpaces[] = {0x0020, 0x00a0, 0x2000, 0x2001, 0x2005};
  170.     const UChar sampleNonSpaces[] = {'a', 'b', 'c', 'd', 't'};
  171.     const UChar sampleUndefined[] = {0xfff1, 0xfff7, 0xfa30};
  172.     const UChar sampleDefined[] = {0x523E, 0x4f88, 0xfffd};
  173.     const UChar sampleBase[] = {0x0061, 0x0031, 0x03d2};
  174.     const UChar sampleNonBase[] = {0x002B, 0x0020, 0x203B};
  175.     const UChar sampleChars[] = {0x000a, 0x0045, 0x4e00, 0xDC00};
  176.     const UChar sampleDigits[]= {0x0030, 0x0662, 0x0F23, 0x0ED5};
  177.     const UChar sampleNonDigits[] = {0x0010, 0x0041, 0x0122, 0x68FE};
  178.     const int32_t sampleDigitValues[] = {0, 2, 3, 5};
  179.     const uint16_t sampleCellWidth[] = {Unicode::ZERO_WIDTH, 
  180.                                         Unicode::HALF_WIDTH, 
  181.                                         Unicode::FULL_WIDTH, 
  182.                                         Unicode::NEUTRAL};
  183.     int32_t i;
  184.     for (i = 0; i < 5; i++) {
  185. //      logln((UnicodeString)"testing " + (int32_t)i + "...");
  186.         if (!(Unicode::isSpaceChar(sampleSpaces[i])) ||
  187.                 (Unicode::isSpaceChar(sampleNonSpaces[i])))
  188.             errln((UnicodeString)"Space char test error : " + (int32_t)sampleSpaces[i] +
  189.             " or " + (int32_t)sampleNonSpaces[i]);
  190.     }
  191.     for (i = 0; i < 3; i++) {
  192. //      logln((UnicodeString)"testing " + (int32_t)i + "...");
  193.         if ((Unicode::isDefined(sampleUndefined[i])) ||
  194.                 !(Unicode::isDefined(sampleDefined[i])))
  195.             errln((UnicodeString)"Undefined char test error : " +
  196.             (int32_t)sampleUndefined[i] + " or " + (int32_t)sampleDefined[i]);
  197.     }
  198.     for (i = 0; i < 3; i++) {
  199. //      logln((UnicodeString)"testing " + (int32_t)i + "...");
  200.         if ((Unicode::isBaseForm(sampleNonBase[i])) ||
  201.                 !(Unicode::isBaseForm(sampleBase[i])))
  202.             errln((UnicodeString)"Non-baseform char test error : " +
  203.             (int32_t)sampleNonBase[i] + " or " + (int32_t)sampleBase[i]);
  204.     }
  205.     for (i = 0; i < 4; i++) {
  206. //      logln((UnicodeString)"testing " + (int32_t)i + "...");
  207.         if (Unicode::getCellWidth(sampleChars[i]) != sampleCellWidth[i])
  208.             errln((UnicodeString)"Cell width char test error : " +
  209.             (int32_t)sampleChars[i]);
  210.     }
  211.     for (i = 0; i < 4; i++) {
  212.         if ((Unicode::isDigit(sampleDigits[i]) && 
  213.             (Unicode::digitValue(sampleDigits[i])!= sampleDigitValues[i])) ||
  214.             (Unicode::isDigit(sampleNonDigits[i]))) {
  215.             errln((UnicodeString)"Digit char test error : " +
  216.             (int32_t)sampleDigits[i] + " or " + (int32_t)sampleNonDigits[i]);
  217.         }
  218.     }
  219. }
  220.  
  221. /* Tests for isControl() and isPrintable() */
  222. void UnicodeTest::TestControlPrint()
  223. {
  224.     const UChar sampleControl[] = {0x001b, 0x0097, 0x0082};
  225.     const UChar sampleNonControl[] = {'a', 0x0031, 0x00e2};
  226.     const UChar samplePrintable[] = {0x0042, 0x005f, 0x2014};
  227.     const UChar sampleNonPrintable[] = {0x200c, 0x009f, 0x001c};
  228.     int32_t i;
  229.     for (i = 0; i < 3; i++) {
  230. //      logln((UnicodeString)"testing " + (int32_t)i + "...");
  231.         if (!(Unicode::isControl(sampleControl[i])) ||
  232.                 (Unicode::isControl(sampleNonControl[i])))
  233.             errln((UnicodeString)"Control char test error : " + (int32_t)sampleControl[i] +
  234.             " or " + (int32_t)sampleNonControl[i]);
  235.     }
  236.     for (i = 0; i < 3; i++) {
  237. //      logln((UnicodeString)"testing " + (int32_t)i + "...");
  238.         if ((Unicode::isPrintable(samplePrintable[i])) ||
  239.                 !(Unicode::isPrintable(samplePrintable[i])))
  240.             errln((UnicodeString)"Printable char test error : " +
  241.             (int32_t)samplePrintable[i] + " or " + (int32_t)sampleNonPrintable[i]);
  242.     }
  243. }
  244.  
  245. /* Tests for isJavaIdentifierStart(), isJavaIdentifierPart(),
  246.  * isUnicodeIdentifierStart() and isUnicodeIdentifierPart() */
  247. void UnicodeTest::TestIdentifier()
  248. {
  249.     const UChar sampleJavaIDStart[] = {0x0071, 0x00e4, 0x005f};
  250.     const UChar sampleNonJavaIDStart[] = {0x0020, 0x2030, 0x0082};
  251.     const UChar sampleUnicodeIDStart[] = {0x0250, 0x00e2, 0x0061};
  252.     const UChar sampleNonUnicodeIDStart[] = {0x2000, 0x000a, 0x2019};
  253.     const UChar sampleJavaIDPart[] = {0x005f, 0x0032, 0x0045};
  254.     const UChar sampleNonJavaIDPart[] = {0x007f, 0x2020, 0x0020};
  255.     const UChar sampleUnicodeIDPart[] = {0x005f, 0x0032, 0x0045};
  256.     const UChar sampleNonUnicodeIDPart[] = {0x007f, 0x00a3, 0x0020};
  257.     const UChar sampleIDIgnore[] = {0x0006, 0x0010, 0x206b};
  258.     const UChar sampleNonIDIgnore[] = {0x0075, 0x00a3, 0x0061};
  259.  
  260.     int32_t i;
  261.     for (i = 0; i < 3; i++) {
  262. //      logln((UnicodeString)"testing " + (int32_t)i + "...");
  263.         if (!(Unicode::isJavaIdentifierStart(sampleJavaIDStart[i])) ||
  264.                 (Unicode::isJavaIdentifierStart(sampleNonJavaIDStart[i])))
  265.             errln((UnicodeString)"Java ID Start char test error : " + (int32_t)sampleJavaIDStart[i] +
  266.             " or " + (int32_t)sampleNonJavaIDStart[i]);
  267.     }
  268.     for (i = 0; i < 3; i++) {
  269. //      logln((UnicodeString)"testing " + (int32_t)i + "...");
  270.         if (!(Unicode::isJavaIdentifierPart(sampleJavaIDPart[i])) ||
  271.                 (Unicode::isJavaIdentifierPart(sampleNonJavaIDPart[i])))
  272.             errln((UnicodeString)"Java ID Part char test error : " + (int32_t)sampleJavaIDPart[i] +
  273.             " or " + (int32_t)sampleNonJavaIDPart[i]);
  274.     }
  275.     for (i = 0; i < 3; i++) {
  276. //      logln((UnicodeString)"testing " + (int32_t)i + "...");
  277.         if (!(Unicode::isUnicodeIdentifierStart(sampleUnicodeIDStart[i])) ||
  278.                 (Unicode::isUnicodeIdentifierStart(sampleNonUnicodeIDStart[i])))
  279.             errln((UnicodeString)"Unicode ID Start char test error : " + (int32_t)sampleUnicodeIDStart[i] +
  280.             " or " + (int32_t)sampleNonUnicodeIDStart[i]);
  281.     }
  282.     for (i = 0; i < 3; i++) {
  283. //      logln((UnicodeString)"testing " + (int32_t)i + "...");
  284.         if (!(Unicode::isUnicodeIdentifierPart(sampleUnicodeIDPart[i])) ||
  285.                 (Unicode::isUnicodeIdentifierPart(sampleNonUnicodeIDPart[i])))
  286.             errln((UnicodeString)"Unicode ID Part char test error : " + (int32_t)sampleUnicodeIDPart[i] +
  287.             " or " + (int32_t)sampleNonUnicodeIDPart[i]);
  288.     }
  289.     for (i = 0; i < 3; i++) {
  290. //      logln((UnicodeString)"testing " + (int32_t)i + "...");
  291.         if (!(Unicode::isIdentifierIgnorable(sampleIDIgnore[i])) ||
  292.                 (Unicode::isIdentifierIgnorable(sampleNonIDIgnore[i])))
  293.             errln((UnicodeString)"ID ignorable char test error : " + (int32_t)sampleIDIgnore[i] +
  294.             " or " + (int32_t)sampleNonIDIgnore[i]);
  295.     }
  296. }
  297.  
  298. /* tests for getType(), isTitleCase(), and toTitleCase() as well as characterDirection()*/
  299. void UnicodeTest::TestUnicodeData()
  300. {
  301.     FILE*   input;
  302.     char    buffer[1000];
  303.     char*   bufferPtr, *dirPtr;
  304.     char path[256];
  305.     const char* datafile = "UnicodeData-3.0.0.txt";
  306.   
  307.     strcpy(path ,  icu_getDefaultDataDirectory());
  308.     strcat(path, datafile);
  309.       
  310.     input = fopen( path, "r");
  311.     if (input == 0)
  312.     {
  313.         errln("Failed to open: " + UnicodeString(datafile));
  314.         return;
  315.     }
  316.  
  317.         int32_t unicode;
  318.         for(;;) {
  319.             bufferPtr = fgets(buffer, 999, input);
  320.             if (bufferPtr == NULL) break;
  321.             if (bufferPtr[0] == '#' || bufferPtr[0] == '\n' || bufferPtr[0] == 0) continue;
  322.             sscanf(bufferPtr, "%X", &unicode);
  323.             assert(0 <= unicode && unicode < 65536);
  324.             if (unicode == LAST_CHAR_CODE_IN_FILE)
  325.                 break;
  326.             bufferPtr = strchr(bufferPtr, ';');
  327.             assert(bufferPtr != NULL);
  328.             bufferPtr = strchr(bufferPtr + 1, ';'); // go to start of third field
  329.             assert(bufferPtr != NULL);
  330.             dirPtr = bufferPtr;
  331.             dirPtr = strchr(dirPtr + 1, ';');
  332.             assert(dirPtr != NULL);
  333.             dirPtr = strchr(dirPtr + 1, ';');
  334.             assert(dirPtr != NULL);
  335.             bufferPtr++;
  336.             bufferPtr[2] = 0;
  337. //          logln((UnicodeString)"testing " + (int32_t)unicode + "...");
  338.             if (Unicode::getType((UChar)unicode) != tagValues[MakeProp(bufferPtr)])
  339.                 errln("Unicode character type failed at " + unicode);
  340.             // test title case
  341.             if ((Unicode::toTitleCase((UChar)unicode) != Unicode::toUpperCase((UChar)unicode)) &&
  342.                 !(Unicode::isTitleCase(Unicode::toTitleCase((UChar)unicode))))
  343.                 errln("Title case test failed at " + unicode);
  344.             bufferPtr = strchr(dirPtr + 1, ';');
  345.             dirPtr++;
  346.             bufferPtr[0] = 0;
  347.             if (Unicode::characterDirection((UChar)unicode) != MakeDir(dirPtr))
  348.                 errln("Unicode character directionality failed at\n " + unicode);
  349.  
  350.  
  351.         }
  352.  
  353.         if (input) fclose(input);
  354.  
  355. }
  356.  
  357. int32_t UnicodeTest::MakeProp(char* str) 
  358. {
  359.     int32_t result = 0;
  360.     const char* matchPosition;
  361.     
  362.     matchPosition = strstr(tagStrings, str);
  363.     if (matchPosition == 0) errln((UnicodeString)"unrecognized type letter " + str);
  364.     else result = ((matchPosition - tagStrings) / 2);
  365.     return result;
  366. }
  367.  
  368. int32_t UnicodeTest::MakeDir(char* str) 
  369. {
  370.     int32_t pos = 0;
  371.     for (pos = 0; pos < 19; pos++) {
  372.         if (strcmp(str, dirStrings[pos]) == 0) {
  373.             return pos;
  374.         }
  375.     }
  376.     return -1;
  377. }
  378.  
  379.