⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 devtest1.c

📁 嵌入式RMON,RMON为Remote monitor的缩写,基于SNMP为网络提供主动监控及错误告警,智能交换路由必备协议
💻 C
字号:
/************************************************************************
** MODULE INFORMATION*
**********************
**     FILE     NAME:       devtest1.c
**     SYSTEM   NAME:       
**     ORIGINAL AUTHOR(S):  Paul Lemmers
**     VERSION  NUMBER:     
**     CREATION DATE:       1990/8/14
**
** DESCRIPTION: 
**              Test file for devlib files (string and memory part).
**
**              There are two types of tests:
**              Type 1
**                 Predefined results (Check by testing result, direct
**                 or with "correct and tested" function).
**              Type 2
**                 Compare result of tested function with result of MSC
**                 function
**              
*************************************************************************
** CHANGES INFORMATION **
*************************
** REVISION:    $Revision:   1.2  $
** WORKFILE:    $Workfile:   devtest1.c  $
** LOGINFO:     $Log:   D:/CPROG/MYDEV/DEVLIB/VCS/DEVTEST1.C_V  $
**              
**                 Rev 1.2   17 Dec 1990 14:32:36   PAUL
**              os2def.h changes
**              
**                 Rev 1.1   09 Dec 1990 19:06:20   PAUL
**              Adaptions for MSC 6.00
**              
**                 Rev 1.0   14 Aug 1990 14:24:24   PAUL
**              Initial revision.
*************************************************************************/
#if ! defined(PRD)
static char _pvcs_hdr[] =
"$Header:   D:/CPROG/MYDEV/DEVLIB/VCS/DEVTEST1.C_V   1.2   17 Dec 1990 14:32:36   PAUL  $";
#endif

#define BUFSIZE	0x100

#include <stdio.h>
#include <string.h>

#include <os2def.h>
#include "devlib.h"

int   main(int argc, char *argv[]);
void  setbufs(int pattern);

int   memset_test(void);
int   memcpy_test(void);
int   strlen_test(void);
int   strcmp_test(void);
int   stricmp_test(void);
int   strcpy_test(void);
int   strncpy_test(void);

char  mscbuf[BUFSIZE];
char  mybuf[BUFSIZE];

struct {
	int    (*pfunc)(void);
	char   *fname;
	} testlist[] = {
		{ memset_test, "memset" },
		{ memcpy_test, "memcpy" },
		{ strlen_test, "strlen" },
		{ strcmp_test, "strcmp" },
		{ stricmp_test, "stricmp" },
		{ strcpy_test, "strcpy" },
		{ strncpy_test, "strncpy" }
		};


int	main(int argc, char *argv[])
{
	int	retc;
	int	n, i;

	argc; argv;
	fprintf(stdout, "Devlib tester.\n");

	n = sizeof(testlist)/sizeof(*testlist);
	i = 0;
	while ( i<n )
		{
		if ( retc=(*testlist[i].pfunc)() )
			{
			fprintf(stdout, "%s failed", testlist[i].fname);
			break; /* stop the other tests */
			}
		i++;
		}

	if ( retc )
		fprintf(stdout, " FATAL\n");

	return(retc);
}


int   memset_test(void)
{
/* Type 2 test */
	static struct _memsettest {
	int	start;
	int	count;
	int	val;
	} t1_ar[] = {
		{ 2, 4, ' '},
		{ 2, 5, ' '},
		{ 1, 4, ' '},
		{ 1, 5, ' '},
		{ 2, 0, ' '}
		};
	int	i;

	for ( i=0 ; i<(sizeof(t1_ar)/sizeof(*t1_ar)) ; i++)
		{
		setbufs(0);

		memset(mscbuf+t1_ar[i].start, t1_ar[i].val, t1_ar[i].count);
		dev_memset(mybuf+t1_ar[i].start, t1_ar[i].val, t1_ar[i].count);

		if ( memcmp(mscbuf, mybuf, BUFSIZE) )
			{
			fprintf(stdout, "  Memset failed on test %d\n", i);
			return(-1);
			}
		}

	return(0);
}


int   memcpy_test(void)
{
/* Type 2 test */
	static struct _memcpytest {
	int	start;
	int	count;
	int	val;
	int	dest;
	} ar[] = {
		{ 2, 6, ' ', 10}, /* No overlap */
		{ 2, 7, ' ', 15}, /* No overlap */
		{ 1, 4, ' ', 10}, /* No overlap */
		{ 1, 5, ' ', 15}, /* No overlap */
		};
	int	i;
	int	retc = 0;

	for ( i=0 ; i<(sizeof(ar)/sizeof(*ar)) ; i++)
		{
		setbufs(0);
		memset(mscbuf+ar[i].start, ar[i].val, ar[i].count);
		memset(mybuf+ar[i].start, ar[i].val, ar[i].count);

		dev_memcpy(mybuf+ar[i].dest, mybuf, ar[i].start+ar[i].count);
		memcpy(mscbuf+ar[i].dest, mscbuf, ar[i].start+ar[i].count);

		if ( memcmp(mscbuf, mybuf, BUFSIZE) )
			{
			fprintf(stdout, "  Memcpy failed on test %d\n", i);
			retc = -1;
			}
		}

	return(retc);
}



int   strlen_test(void)
{
/* Type 1 and type 2 test */
	static struct _strlentest {
	char	*s;
	unsigned short	count;
	} ar[] = {
		{ "", 0 },
		{ "abcde", 5},
		{ "\n", 1}
		};
	int	i;
	int	retc = 0;

	for ( i=0 ; i<(sizeof(ar)/sizeof(*ar)) ; i++)
		{
		if ( dev_strlen(ar[i].s) != ar[i].count )
			{
			fprintf(stdout, "  Strlen type 1 fail on test %d\n", i);
			retc = -1;
			}
		else if ( dev_strlen(ar[i].s) != strlen(ar[i].s) )
			{
			fprintf(stdout, "  Strlen type 2 fail on test %d\n", i);
			retc = -1;
			}
		}

	return(retc);
}



int   strcmp_test(void)
{
/* Type 1 and type 2 test */
	static struct _strlentest {
	char	*s1;
	char	*s2;
	int	result;
	} ar[] = {
		{ "", "", 0 },
		{ "a", "", 1 },
		{ "", "a", -1 },
		{ "ab", "ac", -1},
		{ "ab", "aa", 1},
		{ "ab", "ab", 0},
		{ "aab", "aac", -1},
		{ "aab", "aaa", 1},
		{ "aab", "aab", 0}
		};
	int	i;
	int	retc = 0;
	int	cmp_result;

	for ( i=0 ; i<(sizeof(ar)/sizeof(*ar)) ; i++)
		{
		cmp_result = dev_strcmp(ar[i].s1, ar[i].s2);
		if ( cmp_result != ar[i].result )
			{
			fprintf(stdout, "  Strcmp type 1 fail on test %d\n", i);
			retc = -1;
			}
		else if ( cmp_result != strcmp(ar[i].s1, ar[i].s2) )
			{
			fprintf(stdout, "  Strcmp type 2 fail on test %d\n", i);
			retc = -1;
			}
		}

	return(retc);
}



int   stricmp_test(void)
{
/* Type 1 and type 2 test */
	static struct _stricmptest {
	char	*s1;
	char	*s2;
	int	result;
	} ar[] = {
		{ "", "", 0 },
		{ "a", "", 1 },
		{ "", "a", -1 },
		{ "ab", "ac", -1},
		{ "ab", "aa", 1},
		{ "ab", "ab", 0},
		{ "aab", "aac", -1},
		{ "aab", "aaa", 1},
		{ "aab", "aab", 0},
		{ "Aab", "aab", 0},
		{ "aab", "Aab", 0},
		{ " ab", " ab", 0},
		{ "aab", "Bab", -1},
		{ "Bab", "aab", 1},
		{ "A]", "AA", -1}   /* nice cookie */
		};
	int	i;
	int	retc = 0;
	int	cmp_result;

	for ( i=0 ; i<(sizeof(ar)/sizeof(*ar)) ; i++)
		{
		cmp_result = dev_stricmp(ar[i].s1, ar[i].s2);
		if ( cmp_result != ar[i].result )
			{
			fprintf(stdout, "  Stricmp type 1 fail on test %d\n", i);
			retc = -1;
			}
		else if ( cmp_result != stricmp(ar[i].s1, ar[i].s2) )
			{
			fprintf(stdout, "  Stricmp type 2 fail on test %d\n", i);
			retc = -1;
			}
		}

	return(retc);
}




int   strcpy_test(void)
{
/* Type 2 test */
	static struct _strcpytest {
	char	*s;
	} ar[] = {
		{ "" },
		{ "a" },
		{ "bb" },
		{ "ccc" }
		};
	int	i;
	int	retc = 0;

	for ( i=0 ; i<(sizeof(ar)/sizeof(*ar)) ; i++)
		{
		setbufs(' '); /* non zero character! */

		strcpy(mscbuf+1, ar[i].s);
		dev_strcpy(mybuf+1, ar[i].s);

		if ( memcmp(mscbuf, mybuf, BUFSIZE) )
			{
			fprintf(stdout, "  strcpy failed on test %d\n", i);
			retc = -1;
			}
		}

	return(retc);
}



int   strncpy_test(void)
{
/* Type 2 test */
	static struct _strcpytest {
	char	*s;
	int	count;
	} ar[] = {
		{ "" , 0},
		{ "" , 1},
		{ "a", 0},
		{ "a", 1},
		{ "a", 2},
		{ "a", 3},
		{ "bb", 1},
		{ "bb", 2},
		{ "bb", 3},
		{ "bb", 4},
		{ "ccc", 10 },
		{ "ccc", 11 }
		};
	int	i;
	int	retc = 0;

	for ( i=0 ; i<(sizeof(ar)/sizeof(*ar)) ; i++)
		{
		setbufs(' '); /* non zero character! */

		strncpy(mscbuf+1, ar[i].s, ar[i].count);
		dev_strncpy(mybuf+1, ar[i].s, ar[i].count);

		if ( memcmp(mscbuf, mybuf, BUFSIZE) )
			{
			fprintf(stdout, "  strncpy failed on test %d\n", i);
			retc = -1;
			}
		}

	return(retc);
}



void  setbufs(int pattern)
{
		memset(mscbuf, pattern, BUFSIZE);
		memset(mybuf, pattern, BUFSIZE);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -