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:  WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE
  28. *               DESCRIBE IT HERE!
  29. *
  30. ****************************************************************************/
  31.  
  32.  
  33. /*
  34.  *  MEMTEST.C
  35.  *  Non-exhaustive test of the C library memory manipulation functions.
  36.  */
  37.  
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #if defined(__386__) || defined(M_I86)
  42.     #include <i86.h>
  43. #endif
  44. #ifdef __SW_BW
  45.   #include <wdefwin.h>
  46. #endif
  47.  
  48.  
  49. #define VERIFY( exp )   if( !(exp) ) {                                      \
  50.                             printf( "%s: ***FAILURE*** at line %d of %s.\n",\
  51.                                     ProgramName, __LINE__,                  \
  52.                                     strlwr(__FILE__) );                     \
  53.                             NumErrors++;                                    \
  54.                             exit(-1);                                       \
  55.                         }
  56.  
  57. void TestCompare( void );
  58. void TestCompareF( void );
  59. void TestCopy( void );
  60. void TestCopyF( void );
  61. void TestOverlap( void );
  62. void TestOverlapF( void );
  63. void TestMisc( void );
  64.  
  65.  
  66. char ProgramName[128];                          /* executable filename */
  67. int NumErrors = 0;                              /* number of errors */
  68.  
  69.  
  70.  
  71. /****
  72. ***** Program entry point.
  73. ****/
  74.  
  75. int main( int argc, char *argv[] )
  76. {
  77.     #ifdef __SW_BW
  78.         FILE *my_stdout;
  79.         my_stdout = freopen( "tmp.log", "a", stdout );
  80.         if( my_stdout == NULL ) {
  81.             fprintf( stderr, "Unable to redirect stdout\n" );
  82.             exit( -1 );
  83.         }
  84.     #endif
  85.  
  86.     /*** Initialize ***/
  87.     strcpy( ProgramName, strlwr(argv[0]) );     /* store filename */
  88.  
  89.     /*** Test various functions ***/
  90.     TestCompare();                              /* comparing stuff */
  91.     TestCopy();                                 /* copying stuff */
  92.     TestOverlap();                              /* test overlapping copy */
  93.     TestMisc();                                 /* other stuff */
  94.  
  95.     #if defined(__386__) || defined(M_I86)
  96.         TestCompareF();
  97.         TestCopyF();
  98.         TestOverlapF();
  99.     #endif
  100.  
  101.     /*** Print a pass/fail message and quit ***/
  102.     if( NumErrors!=0 ) {
  103.         printf( "%s: SUCCESS.\n", ProgramName );
  104.         return( EXIT_SUCCESS );
  105.     }
  106.     printf( "Tests completed (%s).\n", strlwr( argv[0] ) );
  107.     #ifdef __SW_BW
  108.     {
  109.         fprintf( stderr, "Tests completed (%s).\n", strlwr( argv[0] ) );
  110.         fclose( my_stdout );
  111.         _dwShutDown();
  112.     }
  113.     #endif
  114.     return( 0 );
  115. }
  116.  
  117.  
  118.  
  119. /****
  120. ***** Test memcmp(), memicmp(), and memchr().
  121. ****/
  122.  
  123. void TestCompare( void )
  124. {
  125.     char                buf[80];
  126.     char *              ptr;
  127.     int                 status;
  128.  
  129.     strcpy( buf, "Foo !" );                     /* initialize string */
  130.  
  131.     status = memcmp( buf, "Foo", 3 );           /* compare */
  132.     VERIFY( status == 0 );
  133.  
  134.     status = memcmp( buf, "Foo!", 4 );          /* compare */
  135.     VERIFY( status != 0 );
  136.  
  137.     status = memicmp( buf, "FOO", 3 );          /* compare */
  138.     VERIFY( status == 0 );
  139.  
  140.     status = memicmp( buf, "fOo!", 4 );         /* compare */
  141.     VERIFY( status != 0 );
  142.  
  143.     ptr = memchr( buf, '~', 6 );                /* try to find a tilde */
  144.     VERIFY( ptr == NULL );
  145.  
  146.     ptr = memchr( buf, '!', 6 );                /* find the '!' */
  147.     VERIFY( ptr == buf+4 );
  148. }
  149.  
  150.  
  151. #if defined(__386__) || defined(M_I86)
  152. void TestCompareF( void )
  153. {
  154.     char                buf[80];
  155.     char __far *        ptr;
  156.     int                 status;
  157.  
  158.     strcpy( buf, "Foo !" );                     /* initialize string */
  159.  
  160.     status = _fmemcmp( buf, "Foo", 3 );         /* compare */
  161.     VERIFY( status == 0 );
  162.  
  163.     status = _fmemcmp( buf, "Foo!", 4 );        /* compare */
  164.     VERIFY( status != 0 );
  165.  
  166.     ptr = _fmemchr( buf, '~', 6 );              /* try to find a tilde */
  167.     VERIFY( ptr == NULL );
  168.  
  169.     ptr = _fmemchr( buf, '!', 6 );              /* find the '!' */
  170.     VERIFY( ptr == buf+4 );
  171. }
  172. #endif
  173.  
  174.  
  175.  
  176. /****
  177. ***** Test memcpy(), memccpy(), and movedata().
  178. ****/
  179.  
  180. void TestCopy( void )
  181. {
  182.     char                bufA[80], bufB[80];
  183.  
  184.     strcpy( bufB, "Hello, world!" );            /* initialize bufB */
  185.     memccpy( bufA, bufB, 'o', strlen(bufB)+1 ); /* copy "Hello" to bufA */
  186.     VERIFY( !memcmp(bufA, "Hello", 5) );        /* ensure copied ok */
  187.  
  188.     memcpy( bufA, bufB, strlen(bufB)+1 );       /* copy to bufA */
  189.     VERIFY( !strcmp(bufA, bufB) );              /* ensure copied ok */
  190. }
  191.  
  192.  
  193. #if defined(__386__) || defined(M_I86)
  194. void TestCopyF( void )
  195. {
  196.     char __far          bufA[80], bufB[80];
  197.     char __far          testStr[] = "Foo Bar Goober Blah";
  198.  
  199.     strcpy( bufB, "Hello, world!" );            /* initialize bufB */
  200.     _fmemccpy( bufA, bufB, 'o', strlen(bufB)+1 );   /* copy "Hello" to bufA */
  201.     VERIFY( !_fmemcmp(bufA, "Hello", 5) );      /* ensure copied ok */
  202.  
  203.     _fmemcpy( bufA, bufB, strlen(bufB)+1 );     /* copy to bufA */
  204.     VERIFY( !_fstrcmp(bufA, bufB) );            /* ensure copied ok */
  205.  
  206.     movedata( FP_SEG(bufA), FP_OFF(bufA),       /* copy data */
  207.               FP_SEG(testStr), FP_OFF(testStr),
  208.               _fstrlen(testStr) );
  209.     VERIFY( !_fmemcmp(bufA, testStr, _fstrlen(testStr)) );
  210. }
  211. #endif
  212.  
  213.  
  214.  
  215. /****
  216. ***** Test memmove().
  217. ****/
  218.  
  219. void TestOverlap( void )
  220. {
  221.     char                bufA[80], bufB[80];
  222.  
  223.     strcpy( bufA, " Hello, world!" );           /* initialize string */
  224.  
  225.     memmove( bufB, bufA, strlen(bufA)+1 );      /* copy string */
  226.     VERIFY( !strcmp(bufB, bufA) );
  227.  
  228.     memmove( bufA, bufA+1, strlen(bufA+1) );    /* shift one character over */
  229.     VERIFY( !strcmp(bufA, "Hello, world!!") );
  230.  
  231.     memmove( bufA+1, bufA, strlen(bufA+1) );
  232.     VERIFY( !strcmp(bufA, "HHello, world!") );
  233. }
  234.  
  235.  
  236. #if defined(__386__) || defined(M_I86)
  237. void TestOverlapF( void )
  238. {
  239.     char                bufA[80], bufB[80];
  240.  
  241.     strcpy( bufA, " Hello, world!" );           /* initialize string */
  242.  
  243.     _fmemmove( bufB, bufA, strlen(bufA)+1 );    /* copy string */
  244.     VERIFY( !strcmp(bufB, bufA) );
  245.  
  246.     _fmemmove( bufA, bufA+1, strlen(bufA+1) );  /* shift one character over */
  247.     VERIFY( !strcmp(bufA, "Hello, world!!") );
  248.  
  249.     _fmemmove( bufA+1, bufA, strlen(bufA+1) );
  250.     VERIFY( !strcmp(bufA, "HHello, world!") );
  251. }
  252. #endif
  253.  
  254.  
  255.  
  256. /****
  257. ***** Test memset(), _fmemset(), and swab().
  258. ****/
  259.  
  260. void TestMisc( void )
  261. {
  262.     char                bufA[80], bufB[80];
  263.     void *              addr;
  264.     int                 count;
  265.  
  266.     addr = memset( bufA, 0x00, 80 );            /* zero out memory */
  267.     VERIFY( addr == bufA );
  268.  
  269.     for( count=0; count<80; count++ )           /* ensure all zero bytes */
  270.         VERIFY( bufA[count] == 0x00 );
  271.  
  272.     #if defined(__386__) || defined(M_I86)
  273.     {
  274.         void __far *    addrFar;
  275.         addrFar = _fmemset( bufB, 0x00, 80 );   /* zero out memory */
  276.         VERIFY( addrFar == bufB );
  277.  
  278.         for( count=0; count<80; count++ ) {     /* ensure all zero bytes */
  279.             VERIFY( bufB[count] == 0x00 );
  280.         }
  281.     }
  282.     #else
  283.         memset( bufB, 0x00, 80 );               /* zero out memory */
  284.     #endif
  285.  
  286.     strcpy( bufA, "eHll,ow rodl" );             /* initialize string */
  287.     swab( bufA, bufB, strlen(bufA) );           /* swap pairs of characters */
  288.     VERIFY( !strcmp(bufB, "Hello, world") );    /* ensure swapped ok */
  289. }
  290.