`

C 支持动态添加测试数据的测试代码

阅读更多
/下面的定义为了支持可扩增。
//当需要增加一个新的测试用列的时候需要这么做:
//1. 修改 TEST_ARR_SIZE ==> 增加数组的大小  ==> #define TEST_ARR_SIZE 		6//(5+1)
//2.添加一个信的数据组: g_arrTestVal5 ==> int g_arrTestVal5[] = {相关int数据};
//3.往数组g_pnArrTest中,增加一个元素g_arrTestVal5 ==>  int* g_pnArrTest[] = {g_arrTestVal0, g_arrTestVal1, g_arrTestVal2, g_arrTestVal3, g_arrTestVal4, g_arrTestVal5};
//4.在line57 增加: GET_ARR_LEN_CASE(5, nLen);

 

 

#include <assert.h>
#include <iostream>
using namespace std;

//下面的定义为了支持可扩增。
//当需要增加一个新的测试用列的时候需要这么做:
//1. 修改 TEST_ARR_SIZE ==> 增加数组的大小  ==> #define TEST_ARR_SIZE 		6//(5+1)
//2.添加一个信的数据组: g_arrTestVal5 ==> int g_arrTestVal5[] = {相关int数据};
//3.往数组g_pnArrTest中,增加一个元素g_arrTestVal5 ==>  int* g_pnArrTest[] = {g_arrTestVal0, g_arrTestVal1, g_arrTestVal2, g_arrTestVal3, g_arrTestVal4, g_arrTestVal5};
//4.在line57 增加: GET_ARR_LEN_CASE(5, nLen);

//#define TEST_ARR_SIZE  	(sizeof(g_pnArrTest) / sizeof(int*))
#define TEST_ARR_SIZE 		5
#define TEST_ARR_VAL_SIZE(INDEX) 	(sizeof(g_arrTestVal##INDEX) / sizeof(int))
#define GET_ARR_LEN_CASE(INDEX, ARR_LEN) \
	case INDEX:\
	{\
		if ( (INDEX < TEST_ARR_SIZE) && (INDEX >= 0) )\
		{\
			ARR_LEN = TEST_ARR_VAL_SIZE(INDEX);\
		}\
		else\
		{\
			ARR_LEN = 0;\
		}\
		break;\
	}

int g_arrTestVal0[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
int g_arrTestVal1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int g_arrTestVal2[] = {1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 10};
int g_arrTestVal3[] = {0, 2, 4, 6, 8, 10, 1, 3, 5, 7, 9};
int g_arrTestVal4[] = {5, 4, 3, 2, 1, 0, 6, 7, 8, 9, 10};

int* g_pnArrTest[] = {g_arrTestVal0, g_arrTestVal1, g_arrTestVal2, g_arrTestVal3, g_arrTestVal4};

int GetTestCaseLen()
{
	return TEST_ARR_SIZE;
}

int GetTestArrLen(const int nIndex)
{
	int nLen = 0;
	int nArrLen = TEST_ARR_SIZE;

	cout << "nArrLen = " << nArrLen << endl;

	switch (nIndex)
	{
	GET_ARR_LEN_CASE(0, nLen);
	GET_ARR_LEN_CASE(1, nLen);
	GET_ARR_LEN_CASE(2, nLen);
	GET_ARR_LEN_CASE(3, nLen);
	GET_ARR_LEN_CASE(4, nLen);

	default:
		nLen = 0;
		break;
	}

	return nLen;
}

int* GetTestCase(const int nIndex, int& nLen)
{
	nLen = 0;

	if ( (nIndex < 0) || (nIndex >= (int)TEST_ARR_SIZE) )
	{
		return NULL;
	}

	nLen = GetTestArrLen(nIndex);
	cout << "GetTestArrLen return val:" << nLen << endl;

	return g_pnArrTest[nIndex];
}

void SwapInt(int& a, int& b)
{
	int nTmp = a;
	a = b;
	b = nTmp;
}

bool CompareInt(const int a, const int b)
{
	return a > b;
}

void PrintArr(const int* pnArr, const int nLen)
{
	assert(pnArr && (nLen > 0));

	int i = 0;
	for (i = 0; i < nLen; i++)
	{
		cout << pnArr[i] << "\t";
	}

	cout << endl;
}

int main()
{
	int nArrLen = GetTestCaseLen();
	cout << nArrLen << endl;


	for (int i = 0; i < nArrLen; i++)
	{
		cout << "================= get test case " << i << " =================" << endl;
		int nLen = 0;
		int* pnArr = GetTestCase(i, nLen);

		if (NULL != pnArr)
		{
			PrintArr(pnArr, nLen);
		}

		cout << endl << endl;
	}
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics