strtest.c
来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 759 行 · 第 1/2 页
C
759 行
/****************************************************************************
*
* Open Watcom Project
*
* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
* ========================================================================
*
* This file contains Original Code and/or Modifications of Original
* Code as defined in and that are subject to the Sybase Open Watcom
* Public License version 1.0 (the 'License'). You may not use this file
* except in compliance with the License. BY USING THIS FILE YOU AGREE TO
* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
* provided with the Original Code and Modifications, and is also
* available at www.sybase.com/developer/opensource.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
* NON-INFRINGEMENT. Please see the License for the specific language
* governing rights and limitations under the License.
*
* ========================================================================
*
* Description: Non-exhaustive test of the C library string functions.
*
****************************************************************************/
#include <errno.h>
#include <locale.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <malloc.h> // for malloc RSIZE_MAX buffer
#ifdef __SW_BW
#include <wdefwin.h>
#endif
#define VERIFY( exp ) if( !(exp) ) { \
printf( "%s: ***FAILURE*** at line %d of %s.\n",\
ProgramName, __LINE__, \
strlwr(__FILE__) ); \
NumErrors++; \
exit( -1 ); \
}
void TestCompare( void );
void TestCase( void );
void TestMove( void );
void TestSearch( void );
void TestSubstring( void );
void TestToken( void );
void TestLocale( void );
void TestError( void );
void TestFormatted( void );
void TestBounded( void );
void Test__vbprintf( char *buf, char *format, ... );
int Test_vsscanf( char *buf, char *format, ... );
void Test_vsprintf( char *buf, char *format, ... );
int Test_vsnprintf( char *buf, char *format, ... );
#ifdef __X86__
void TestCompareF( void );
void TestCaseF( void );
void TestMoveF( void );
void TestSearchF( void );
void TestSubstringF( void );
void TestTokenF( void );
#endif
char ProgramName[128]; /* executable filename */
int NumErrors = 0; /* number of errors */
/****
***** Test strcmp(), stricmp(), strcmpi(), strncmp(), and strnicmp().
****/
void TestCompare( void )
{
char bufA[80] = "FoO baR gOoBeR bLaH";
char bufLower[80] = "foo bar goober blah";
char bufUpper[80] = "FOO BAR GOOBER BLAH";
int status;
status = strcmp( bufA, bufA ); /* ensure same */
VERIFY( status == 0 );
status = strcmp( bufA, bufLower ); /* ensure not same */
VERIFY( status != 0 );
status = stricmp( bufA, bufUpper ); /* ensure same */
VERIFY( status == 0 );
status = stricmp( bufA, "foo" ); /* ensure not same */
VERIFY( status != 0 );
status = strcmpi( bufA, bufUpper ); /* ensure same */
VERIFY( status == 0 );
status = strcmpi( bufA, "foo" ); /* ensure not same */
VERIFY( status != 0 );
status = strncmp( bufA, bufA, 100 ); /* ensure same */
VERIFY( status == 0 );
status = strncmp( bufA, bufLower, 1 ); /* ensure not same */
VERIFY( status != 0 );
status = strnicmp( bufA, bufUpper, 100 ); /* ensure same */
VERIFY( status == 0 );
status = strnicmp( bufA, "fOo B!!!", 5 ); /* ensure same */
VERIFY( status == 0 );
status = strnicmp( bufA, "fOo B!!!", 6 ); /* ensure not same */
VERIFY( status != 0 );
}
#ifdef __X86__
void TestCompareF( void )
{
char bufA[80] = "FoO baR gOoBeR bLaH";
char bufLower[80] = "foo bar goober blah";
char bufUpper[80] = "FOO BAR GOOBER BLAH";
int status;
status = _fstrcmp( bufA, bufA ); /* ensure same */
VERIFY( status == 0 );
status = _fstrcmp( bufA, bufLower ); /* ensure not same */
VERIFY( status != 0 );
status = _fstricmp( bufA, bufUpper ); /* ensure same */
VERIFY( status == 0 );
status = _fstricmp( bufA, "foo" ); /* ensure not same */
VERIFY( status != 0 );
status = _fstrncmp( bufA, bufA, 100 ); /* ensure same */
VERIFY( status == 0 );
status = _fstrncmp( bufA, bufLower, 1 ); /* ensure not same */
VERIFY( status != 0 );
status = _fstrnicmp( bufA, bufUpper, 100 ); /* ensure same */
VERIFY( status == 0 );
status = _fstrnicmp( bufA, "fOo B!!!", 5 ); /* ensure same */
VERIFY( status == 0 );
status = _fstrnicmp( bufA, "fOo B!!!", 6 ); /* ensure not same */
VERIFY( status != 0 );
}
#endif
/****
***** Test strcpy(), strcat(), strset(), strncpy(), strncat(), strnset(),
***** and strrev().
****/
void TestMove( void )
{
char bufA[80] = "FoO baR gOoBeR bLaH";
char bufB[80];
char *bufPtr;
char *newBuf;
int status;
bufPtr = strcpy( bufB, "FoO baR" ); /* copy string */
VERIFY( bufPtr == bufB );
bufPtr = strcat( bufB, " gOoBeR bLaH" ); /* append the rest */
VERIFY( bufPtr == bufB );
status = strcmp( bufA, bufB ); /* check result */
VERIFY( status == 0 );
bufPtr = strset( bufB, 0x00 ); /* zero out buffer */
VERIFY( bufPtr == bufB );
bufPtr = strncpy( bufB, "ABCDEFGHIJ", 2 ); /* copy two bytes */
VERIFY( bufPtr == bufB );
bufPtr = strncat( bufB, "CDEFGHIJ", 3 ); /* copy three more */
VERIFY( bufPtr == bufB );
status = strcmp( bufB, "ABCDE" ); /* ensure only five bytes */
VERIFY( status == 0 );
bufPtr = strnset( bufB, 0x00, 10 ); /* blank string */
VERIFY( bufPtr == bufB );
status = strcmp( bufB, "" ); /* verify empty */
VERIFY( status == 0 );
bufPtr = strcpy( bufB, "abcdefghij" ); /* copy string */
VERIFY( bufPtr == bufB );
bufPtr = strrev( bufB ); /* reverse it */
VERIFY( bufPtr == bufB );
status = strcmp( bufB, "jihgfedcba" ); /* ensure reversed ok */
VERIFY( status == 0 );
newBuf = strdup( bufA ); /* duplicate string */
status = strcmp( bufA, newBuf );
VERIFY( status == 0 );
}
/****
***** Test strlcpy() and strlcat().
****/
void TestBounded( void )
{
char bufA[80] = "FoO baR gOoBeR bLaH";
char bufB[80];
char *bufPtr;
int status;
size_t len;
len = strlcpy( bufB, "FoO baR", sizeof( bufB ) ); /* copy string */
VERIFY( len == strlen( "FoO baR" ) );
len = strlcat( bufB, " gOoBeR bLaH", sizeof( bufB ) ); /* append rest */
VERIFY( len == strlen( bufA ) );
status = strcmp( bufA, bufB ); /* check result */
VERIFY( status == 0 );
bufPtr = strset( bufB, 0x00 ); /* zero out buffer */
VERIFY( bufPtr == bufB );
len = strlcpy( bufB, "ABCDEFGHIJ", 3 ); /* copy two chars */
VERIFY( len == strlen( "ABCDEFGHIJ" ) );
len = strlcat( bufB, "CDEFGHIJ", 6 ); /* copy three more */
VERIFY( len == strlen( "ABCDEFGHIJ" ) );
status = strcmp( bufB, "ABCDE" ); /* ensure only five chars */
VERIFY( status == 0 );
len = strlcat( bufB, "junk", 3 ); /* ensure no running off */
VERIFY( len == 3 );
bufPtr = strnset( bufB, 0x00, 10 ); /* blank string */
VERIFY( bufPtr == bufB );
}
#ifdef __X86__
void TestMoveF( void )
{
char bufA[80] = "FoO baR gOoBeR bLaH";
char bufB[80];
char __far *bufPtr;
char __far *newBuf;
int status;
bufPtr = _fstrcpy( bufB, "FoO baR" ); /* copy string */
VERIFY( bufPtr == bufB );
bufPtr = _fstrcat( bufB, " gOoBeR bLaH" ); /* append the rest */
VERIFY( bufPtr == bufB );
status = _fstrcmp( bufA, bufB ); /* check result */
VERIFY( status == 0 );
bufPtr = _fstrset( bufB, 0x00 ); /* zero out buffer */
VERIFY( bufPtr == bufB );
bufPtr = _fstrncpy( bufB, "ABCDEFGHIJ", 2 );/* copy two bytes */
VERIFY( bufPtr == bufB );
bufPtr = _fstrncat( bufB, "CDEFGHIJ", 3 ); /* copy three more */
VERIFY( bufPtr == bufB );
status = _fstrcmp( bufB, "ABCDE" ); /* ensure only five bytes */
VERIFY( status == 0 );
bufPtr = _fstrnset( bufB, 0x00, 10 ); /* blank string */
VERIFY( bufPtr == bufB );
status = _fstrcmp( bufB, "" ); /* verify empty */
VERIFY( status == 0 );
bufPtr = _fstrcpy( bufB, "abcdefghij" ); /* copy string */
VERIFY( bufPtr == bufB );
bufPtr = _fstrrev( bufB ); /* reverse it */
VERIFY( bufPtr == bufB );
status = _fstrcmp( bufB, "jihgfedcba" ); /* ensure reversed ok */
VERIFY( status == 0 );
newBuf = _fstrdup( bufA ); /* duplicate string */
status = _fstrcmp( bufA, newBuf );
VERIFY( status == 0 );
}
#endif
/****
***** Test strlwr() and strupr().
****/
void TestCase( void )
{
char bufA[80] = "FoO baR gOoBeR bLaH";
char bufB[80];
char bufLower[80] = "foo bar goober blah";
char bufUpper[80] = "FOO BAR GOOBER BLAH";
char *bufPtr;
int status;
bufPtr = strcpy( bufB, bufA ); /* copy string */
VERIFY( bufPtr == bufB );
bufPtr = strlwr( bufB ); /* lowercase */
VERIFY( bufPtr == bufB );
status = strcmp( bufB, bufLower ); /* ensure ok */
VERIFY( status == 0 );
bufPtr = strcpy( bufB, bufA ); /* copy string */
VERIFY( bufPtr == bufB );
bufPtr = strupr( bufB ); /* uppercase */
VERIFY( bufPtr == bufB );
status = strcmp( bufB, bufUpper ); /* ensure ok */
VERIFY( status == 0 );
}
#ifdef __X86__
void TestCaseF( void )
{
char bufA[80] = "FoO baR gOoBeR bLaH";
char bufB[80];
char bufLower[80] = "foo bar goober blah";
char bufUpper[80] = "FOO BAR GOOBER BLAH";
char __far *bufPtr;
int status;
bufPtr = _fstrcpy( bufB, bufA ); /* copy string */
VERIFY( bufPtr == bufB );
bufPtr = _fstrlwr( bufB ); /* lowercase */
VERIFY( bufPtr == bufB );
status = _fstrcmp( bufB, bufLower ); /* ensure ok */
VERIFY( status == 0 );
bufPtr = _fstrcpy( bufB, bufA ); /* copy string */
VERIFY( bufPtr == bufB );
bufPtr = _fstrupr( bufB ); /* uppercase */
VERIFY( bufPtr == bufB );
status = _fstrcmp( bufB, bufUpper ); /* ensure ok */
VERIFY( status == 0 );
}
#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?