Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /****************************************************************************
  2. *
  3. *                            Open Watcom Project
  4. *
  5. *    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
  6. *
  7. *  ========================================================================
  8. *
  9. *    This file contains Original Code and/or Modifications of Original
  10. *    Code as defined in and that are subject to the Sybase Open Watcom
  11. *    Public License version 1.0 (the 'License'). You may not use this file
  12. *    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
  13. *    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
  14. *    provided with the Original Code and Modifications, and is also
  15. *    available at www.sybase.com/developer/opensource.
  16. *
  17. *    The Original Code and all software distributed under the License are
  18. *    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  19. *    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
  20. *    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
  21. *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
  22. *    NON-INFRINGEMENT. Please see the License for the specific language
  23. *    governing rights and limitations under the License.
  24. *
  25. *  ========================================================================
  26. *
  27. * Description:  Non-exhaustive test of ctype.h functions and macros.
  28. *               Note: Tests assume the C locale.
  29. *
  30. ****************************************************************************/
  31.  
  32.  
  33. #include <ctype.h>
  34. #include <wctype.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38.  
  39.  
  40. #define VERIFY( exp )   if( !(exp) ) {                                      \
  41.                             printf( "%s: ***FAILURE*** at line %d of %s.\n",\
  42.                                     ProgramName, __LINE__,                  \
  43.                                     strlwr(__FILE__) );                     \
  44.                             NumErrors++;                                    \
  45.                         }
  46.  
  47. #define TEST_ARRAY_SIZE         256
  48. #define TEST_ARRAY_SIZE_WIDE    512
  49.  
  50.  
  51. struct CtypeBits {
  52.     unsigned alnum  : 1;
  53.     unsigned alpha  : 1;
  54.     unsigned blank  : 1;
  55.     unsigned cntrl  : 1;
  56.     unsigned digit  : 1;
  57.     unsigned graph  : 1;
  58.     unsigned lower  : 1;
  59.     unsigned print  : 1;
  60.     unsigned punct  : 1;
  61.     unsigned space  : 1;
  62.     unsigned upper  : 1;
  63.     unsigned xdigit : 1;
  64.     unsigned ascii  : 1;
  65.     unsigned csym   : 1;
  66.     unsigned csymf  : 1;
  67. };
  68.  
  69. struct CtypeBits MacroResults[TEST_ARRAY_SIZE];
  70. struct CtypeBits FunctResults[TEST_ARRAY_SIZE];
  71. struct CtypeBits WideMacroResults[TEST_ARRAY_SIZE_WIDE];
  72. struct CtypeBits WideFunctResults[TEST_ARRAY_SIZE_WIDE];
  73.  
  74. char    ProgramName[_MAX_PATH];                 /* executable filename */
  75. int     NumErrors = 0;                          /* number of errors */
  76.  
  77. int far far_data = 0;
  78.  
  79. void TestClassifyMacro( void )
  80. /****************************/
  81. {
  82.     int     i;
  83.  
  84.     far_data++; // set ds outside DGROUP
  85.  
  86.     MacroResults[0].alnum  = isalnum( EOF );
  87.     MacroResults[0].alpha  = isalpha( EOF );
  88.     MacroResults[0].blank  = isblank( EOF );
  89.     MacroResults[0].cntrl  = iscntrl( EOF );
  90.     MacroResults[0].digit  = isdigit( EOF );
  91.     MacroResults[0].graph  = isgraph( EOF );
  92.     MacroResults[0].lower  = islower( EOF );
  93.     MacroResults[0].print  = isprint( EOF );
  94.     MacroResults[0].punct  = ispunct( EOF );
  95.     MacroResults[0].space  = isspace( EOF );
  96.     MacroResults[0].upper  = isupper( EOF );
  97.     MacroResults[0].xdigit = isxdigit( EOF );
  98.     MacroResults[0].ascii  = isascii( EOF );
  99.     MacroResults[0].csym   = __iscsym( EOF );
  100.     MacroResults[0].csymf  = __iscsymf( EOF );
  101.  
  102.     for( i = 1; i < TEST_ARRAY_SIZE; i++ ) {
  103.         MacroResults[i].alnum  = isalnum( i );
  104.         MacroResults[i].alpha  = isalpha( i );
  105.         MacroResults[i].blank  = isblank( i );
  106.         MacroResults[i].cntrl  = iscntrl( i );
  107.         MacroResults[i].digit  = isdigit( i );
  108.         MacroResults[i].graph  = isgraph( i );
  109.         MacroResults[i].lower  = islower( i );
  110.         MacroResults[i].print  = isprint( i );
  111.         MacroResults[i].punct  = ispunct( i );
  112.         MacroResults[i].space  = isspace( i );
  113.         MacroResults[i].upper  = isupper( i );
  114.         MacroResults[i].xdigit = isxdigit( i );
  115.         MacroResults[i].ascii  = isascii( i );
  116.         MacroResults[i].csym   = __iscsym( i );
  117.         MacroResults[i].csymf  = __iscsymf( i );
  118.     }
  119. }
  120.  
  121. void TestClassifyFunct( void )
  122. /****************************/
  123. {
  124.     int     i;
  125.  
  126.     far_data++; // set ds outside DGROUP
  127.  
  128.     FunctResults[0].alnum  = (isalnum)( EOF );
  129.     FunctResults[0].alpha  = (isalpha)( EOF );
  130.     FunctResults[0].blank  = (isblank)( EOF );
  131.     FunctResults[0].cntrl  = (iscntrl)( EOF );
  132.     FunctResults[0].digit  = (isdigit)( EOF );
  133.     FunctResults[0].graph  = (isgraph)( EOF );
  134.     FunctResults[0].lower  = (islower)( EOF );
  135.     FunctResults[0].print  = (isprint)( EOF );
  136.     FunctResults[0].punct  = (ispunct)( EOF );
  137.     FunctResults[0].space  = (isspace)( EOF );
  138.     FunctResults[0].upper  = (isupper)( EOF );
  139.     FunctResults[0].xdigit = (isxdigit)( EOF );
  140.     FunctResults[0].ascii  = (isascii)( EOF );
  141.     FunctResults[0].csym   = (__iscsym)( EOF );
  142.     FunctResults[0].csymf  = (__iscsymf)( EOF );
  143.  
  144.     for( i = 1; i < TEST_ARRAY_SIZE; i++ ) {
  145.         FunctResults[i].alnum  = (isalnum)( i );
  146.         FunctResults[i].alpha  = (isalpha)( i );
  147.         FunctResults[i].blank  = (isblank)( i );
  148.         FunctResults[i].cntrl  = (iscntrl)( i );
  149.         FunctResults[i].digit  = (isdigit)( i );
  150.         FunctResults[i].graph  = (isgraph)( i );
  151.         FunctResults[i].lower  = (islower)( i );
  152.         FunctResults[i].print  = (isprint)( i );
  153.         FunctResults[i].punct  = (ispunct)( i );
  154.         FunctResults[i].space  = (isspace)( i );
  155.         FunctResults[i].upper  = (isupper)( i );
  156.         FunctResults[i].xdigit = (isxdigit)( i );
  157.         FunctResults[i].ascii  = (isascii)( i );
  158.         FunctResults[i].csym   = (__iscsym)( i );
  159.         FunctResults[i].csymf  = (__iscsymf)( i );
  160.     }
  161. }
  162.  
  163. void TestClassifyWideMacro( void )
  164. /********************************/
  165. {
  166.     int     i;
  167.  
  168.     far_data++; // set ds outside DGROUP
  169.  
  170.     WideMacroResults[0].alnum  = iswalnum( WEOF );
  171.     WideMacroResults[0].alpha  = iswalpha( WEOF );
  172.     WideMacroResults[0].blank  = iswblank( WEOF );
  173.     WideMacroResults[0].cntrl  = iswcntrl( WEOF );
  174.     WideMacroResults[0].digit  = iswdigit( WEOF );
  175.     WideMacroResults[0].graph  = iswgraph( WEOF );
  176.     WideMacroResults[0].lower  = iswlower( WEOF );
  177.     WideMacroResults[0].print  = iswprint( WEOF );
  178.     WideMacroResults[0].punct  = iswpunct( WEOF );
  179.     WideMacroResults[0].space  = iswspace( WEOF );
  180.     WideMacroResults[0].upper  = iswupper( WEOF );
  181.     WideMacroResults[0].xdigit = iswxdigit( WEOF );
  182.     WideMacroResults[0].ascii  = isascii( WEOF );
  183.     WideMacroResults[0].csym   = __iscsym( WEOF );
  184.     WideMacroResults[0].csymf  = __iscsymf( WEOF );
  185.  
  186.     for( i = 1; i < TEST_ARRAY_SIZE_WIDE; i++ ) {
  187.         WideMacroResults[i].alnum  = iswalnum( i );
  188.         WideMacroResults[i].alpha  = iswalpha( i );
  189.         WideMacroResults[i].blank  = iswblank( i );
  190.         WideMacroResults[i].cntrl  = iswcntrl( i );
  191.         WideMacroResults[i].digit  = iswdigit( i );
  192.         WideMacroResults[i].graph  = iswgraph( i );
  193.         WideMacroResults[i].lower  = iswlower( i );
  194.         WideMacroResults[i].print  = iswprint( i );
  195.         WideMacroResults[i].punct  = iswpunct( i );
  196.         WideMacroResults[i].space  = iswspace( i );
  197.         WideMacroResults[i].upper  = iswupper( i );
  198.         WideMacroResults[i].xdigit = iswxdigit( i );
  199.         WideMacroResults[i].ascii  = isascii( i );
  200.         WideMacroResults[i].csym   = __iscsym( i );
  201.         WideMacroResults[i].csymf  = __iscsymf( i );
  202.     }
  203. }
  204.  
  205. void TestClassifyWideFunct( void )
  206. /********************************/
  207. {
  208.     int i;
  209.  
  210.     far_data++; // set ds outside DGROUP
  211.  
  212.     WideFunctResults[0].alnum  = (iswalnum)( WEOF );
  213.     WideFunctResults[0].alpha  = (iswalpha)( WEOF );
  214.     WideFunctResults[0].blank  = (iswblank)( WEOF );
  215.     WideFunctResults[0].cntrl  = (iswcntrl)( WEOF );
  216.     WideFunctResults[0].digit  = (iswdigit)( WEOF );
  217.     WideFunctResults[0].graph  = (iswgraph)( WEOF );
  218.     WideFunctResults[0].lower  = (iswlower)( WEOF );
  219.     WideFunctResults[0].print  = (iswprint)( WEOF );
  220.     WideFunctResults[0].punct  = (iswpunct)( WEOF );
  221.     WideFunctResults[0].space  = (iswspace)( WEOF );
  222.     WideFunctResults[0].upper  = (iswupper)( WEOF );
  223.     WideFunctResults[0].xdigit = (iswxdigit)( WEOF );
  224.     WideFunctResults[0].ascii  = (isascii)( WEOF );
  225.     WideFunctResults[0].csym   = (__iscsym)( WEOF );
  226.     WideFunctResults[0].csymf  = (__iscsymf)( WEOF );
  227.  
  228.     for( i = 1; i < TEST_ARRAY_SIZE_WIDE; i++ ) {
  229.         WideFunctResults[i].alnum  = (iswalnum)( i );
  230.         WideFunctResults[i].alpha  = (iswalpha)( i );
  231.         WideFunctResults[i].blank  = (iswblank)( i );
  232.         WideFunctResults[i].cntrl  = (iswcntrl)( i );
  233.         WideFunctResults[i].digit  = (iswdigit)( i );
  234.         WideFunctResults[i].graph  = (iswgraph)( i );
  235.         WideFunctResults[i].lower  = (iswlower)( i );
  236.         WideFunctResults[i].print  = (iswprint)( i );
  237.         WideFunctResults[i].punct  = (iswpunct)( i );
  238.         WideFunctResults[i].space  = (iswspace)( i );
  239.         WideFunctResults[i].upper  = (iswupper)( i );
  240.         WideFunctResults[i].xdigit = (iswxdigit)( i );
  241.         WideFunctResults[i].ascii  = (isascii)( i );
  242.         WideFunctResults[i].csym   = (__iscsym)( i );
  243.         WideFunctResults[i].csymf  = (__iscsymf)( i );
  244.     }
  245. }
  246.  
  247. /* Helper function to print mismatches in human readable form */
  248. void CheckResults( struct CtypeBits *s1, struct CtypeBits *s2, int count )
  249. /************************************************************************/
  250. {
  251.     int i;
  252.  
  253.     far_data++; // set ds outside DGROUP
  254.  
  255.     for( i = 0; i < TEST_ARRAY_SIZE; i++ ) {
  256.         if( s1[i].alnum != WideMacroResults[i].alnum )
  257.             printf( "Mismatch at %d (alnum)\n", i );
  258.         if( s1[i].alpha != s2[i].alpha )
  259.             printf( "Mismatch at %d (alpha)\n", i );
  260.         if( s1[i].blank != s2[i].blank )
  261.             printf( "Mismatch at %d (blank)\n", i );
  262.         if( s1[i].cntrl != s2[i].cntrl )
  263.             printf( "Mismatch at %d (cntrl)\n", i );
  264.         if( s1[i].digit != s2[i].digit )
  265.             printf( "Mismatch at %d (digit)\n", i );
  266.         if( s1[i].graph != s2[i].graph )
  267.             printf( "Mismatch at %d (graph)\n", i );
  268.         if( s1[i].lower != s2[i].lower )
  269.             printf( "Mismatch at %d (lower)\n", i );
  270.         if( s1[i].print != s2[i].print )
  271.             printf( "Mismatch at %d (print)\n", i );
  272.         if( s1[i].punct != s2[i].punct )
  273.             printf( "Mismatch at %d (punct)\n", i );
  274.         if( s1[i].space != s2[i].space )
  275.             printf( "Mismatch at %d (space)\n", i );
  276.         if( s1[i].upper != s2[i].upper )
  277.             printf( "Mismatch at %d (upper)\n", i );
  278.         if( s1[i].xdigit != s2[i].xdigit )
  279.             printf( "Mismatch at %d (xdigit)\n", i );
  280.         if( s1[i].ascii != s2[i].ascii )
  281.             printf( "Mismatch at %d (ascii)\n", i );
  282.         if( s1[i].csym != s2[i].csym )
  283.             printf( "Mismatch at %d (csym)\n", i );
  284.         if( s1[i].csymf != s2[i].csymf )
  285.             printf( "Mismatch at %d (csymf)\n", i );
  286.     }
  287. }
  288.  
  289. void TestResults( void )
  290. /**********************/
  291. {
  292.     size_t  len;
  293.     size_t  wide_len;
  294.  
  295.     far_data++; // set ds outside DGROUP
  296.  
  297.     len = sizeof( MacroResults );
  298.     wide_len = sizeof( WideMacroResults );
  299.  
  300.     CheckResults( MacroResults, FunctResults, TEST_ARRAY_SIZE );
  301.     VERIFY( !memcmp( MacroResults, FunctResults, len ) );
  302.     VERIFY( !memcmp( WideMacroResults, WideFunctResults, wide_len ) );
  303.     VERIFY( !memcmp( MacroResults, WideMacroResults, len ) );
  304.     VERIFY( !memcmp( MacroResults, WideFunctResults, len ) );
  305. }
  306.  
  307. void TestConversion( void )
  308. /*************************/
  309. {
  310.     int c, c1, c2;
  311.  
  312.     far_data++; // set ds outside DGROUP
  313.  
  314.     for( c = 0; c < 256; c++ ) {
  315.         c1 = tolower( c );
  316.         c2 = toupper( c );
  317.         if( isalpha( c ) ) {
  318.             if( islower( c ) )
  319.                 VERIFY( (c1 == c) && (c2 != c) );
  320.             if( isupper( c ) )
  321.                 VERIFY( (c1 != c) && (c2 == c) );
  322.         } else {
  323.             VERIFY( !isalpha( c1 ) && !isalpha( c2 ) );
  324.         }
  325.     }
  326. }
  327.  
  328. void TestWideConversion( void )
  329. /*****************************/
  330. {
  331.     wchar_t c, c1, c2;
  332.  
  333.     far_data++; // set ds outside DGROUP
  334.  
  335.     for( c = 0; c < 1024; c++ ) {
  336.         c1 = towlower( c );
  337.         c2 = towupper( c );
  338.         if( iswalpha( c ) ) {
  339.             if( iswlower( c ) )
  340.                 VERIFY( (c1 == c) && (c2 != c) );
  341.             if( iswupper( c ) )
  342.                 VERIFY( (c1 != c) && (c2 == c) );
  343.         } else {
  344.             VERIFY( !iswalpha( c1 ) && !iswalpha( c2 ) );
  345.         }
  346.     }
  347. }
  348.  
  349. int main( int argc, char *argv[] )
  350. /********************************/
  351. {
  352.     far_data++; // set ds outside DGROUP
  353.  
  354.     /*** Initialize ***/
  355.     strcpy( ProgramName, strlwr(argv[0]) );
  356.  
  357.     /*** Test stuff ***/
  358.     TestClassifyMacro();
  359.     TestClassifyFunct();
  360.     TestClassifyWideMacro();
  361.     TestClassifyWideFunct();
  362.     TestResults();
  363.     TestConversion();
  364.     TestWideConversion();
  365.  
  366.     /*** Print a pass/fail message and quit ***/
  367.     if( NumErrors == 0 ) {
  368.         printf( "%s: SUCCESS.\n", ProgramName );
  369.         return( EXIT_SUCCESS );
  370.     } else {
  371.         printf( "%s: FAILURE (%d errors).\n", ProgramName, NumErrors );
  372.         return( EXIT_FAILURE );
  373.     }
  374. }
  375.