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

📄 testmain.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
字号:
/*____________________________________________________________________________*\
 *

 Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.

 These sources, libraries and applications are
 FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
 as long as the following conditions are adhered to.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer. 

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.

 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.

 *____________________________________________________________________________*|
 *
 * $Source: /cvsroot/pi3web/Pi3Web_200/Source/DB/TestMain.cpp,v $
 * $Date: 2003/05/13 18:41:56 $
 *
 Description:
	Test module for _PIDB module
\*____________________________________________________________________________*/
//$SourceTop:$

#include <iostream.h>
#include <errno.h>
#include "PIString.h"
#include "PISymTab.h"
#include "PIDBTree.h"

/*____________________________________________________________________________*\
 *
 Global declarations:
	Definitions for TestTree
\*____________________________________________________________________________*/
#define PIDBTYPE_TESTTREE_PISTRING PIDBTYPE_BUILTINSIZE
#define PIDBTYPE_TESTTREE_SIZE 1
class TestTree : public PIDBTree
{
private:
	static void *AddS( void *pV )
		{
		return PI_NEW( PIString( (const char *)pV ) );
		};
	static void RenderS( ostream &os, const void *pV, int /*iLevel*/ )
		{
		os << *((PIString *)pV);
		};
	static void DestructorS( void *pV )
		{
		PI_DELETE( (PIString *)pV );
		};
	static TypeFrame aTypeFrames[PIDBTYPE_TESTTREE_SIZE];

public:
	TestTree( _PIDB *pTree )
	:	PIDBTree( pTree, "TestTree", PIDBTYPE_TESTTREE_SIZE, aTypeFrames )	{};
};
/* --- declare the stack frame --- */
TypeFrame TestTree::aTypeFrames[PIDBTYPE_TESTTREE_SIZE] =
{
{ "Test String", 0, TestTree::AddS, TestTree::RenderS, TestTree::DestructorS },
};

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void TestSymbolTable( const char *pS )
{
	const char *pSym = PISymbolTable_LookupOrInsert( pS, 1 );
	cout << "Insert <" << pS << ">, addr is <"
		<< (void *)pS << ">, symbol address <" 
		<< (void *)pSym << ">, string is <" << pSym << ">" << endl;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void WriteElapsed( PITimer *pTimer )
{
	assert( pTimer );
	long lSec, lUSec;
	PITimer_elapsed( pTimer, &lSec, &lUSec );
	cout << "ELAPSED " << lSec << ".";
	cout.fill( '0' ); cout.width( 6 );
	cout << lUSec << endl;
	PITimer_reset( pTimer );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int iRet = 0;
void DoTest( _PIDB * )
{
	PITimer *pTimer = PITimer_new();

	cout << "1..Testing the symbol table" << endl;

	PIString sTmp0( "John" );
	PIString sTmp1( sTmp0 );
	PIString sTmp2( sTmp1 );
	PIString sTmp3( sTmp1 );
	PIString sTmp4( sTmp1 );
	PIString sTmp5( "Other" );
	PIString sTmp6( sTmp5 );
	PIString sTmp7( sTmp5 );
	PIString sTmp8( sTmp5 );
	PIString sTmp9( sTmp5 );

	PITimer_reset( pTimer );

	TestSymbolTable( sTmp0 );
	TestSymbolTable( sTmp1 );
	TestSymbolTable( sTmp2 );
	TestSymbolTable( sTmp3 );
	TestSymbolTable( sTmp4 );
	TestSymbolTable( sTmp5 );
	TestSymbolTable( sTmp6 );
	TestSymbolTable( sTmp7 );
	TestSymbolTable( sTmp8 );
	TestSymbolTable( sTmp9 );
	
	WriteElapsed( pTimer );
	cout << "1..Done" << endl;

	/* --- start on the TestTree --- */
	cout << "2..Create and delete _PIDB" << endl;
	_PIDB *pDB = PI_NEW( TestTree( 0 ) );
	PI_DELETE( pDB );
	cout << "2..Done" << endl;

	/* --- --- */
	cout << "3..Create TestTree, add top level elements and iterate over \
all of them "
			<< endl;
	_PIDB *pDB1 = PI_NEW( TestTree( 0 ) );
	pDB1->Add( PIDBTYPE_TESTTREE_PISTRING, (const char *)"Server1", (void *)"Level1,1", 0 );
	pDB1->Add( PIDBTYPE_TESTTREE_PISTRING, (const char *)"Server1", (void *)"Level1,2", 0 );
	pDB1->Add( PIDBTYPE_TESTTREE_PISTRING, (const char *)"Server1", (void *)"Level1,3", 0 );
	pDB1->Add( PIDBTYPE_TESTTREE_PISTRING, (const char *)"Server2", (void *)"Level1,4", 0 );
	pDB1->Add( PIDBTYPE_TESTTREE_PISTRING, (const char *)"Server2", (void *)"Level1,5", 0 );
	pDB1->Add( PIDBTYPE_TESTTREE_PISTRING, (const char *)"Server2", (void *)"Level1,6", 0 );
	PIDBIterator *pIter=pDB1->GetIterator(PIDBTYPE_TESTTREE_PISTRING, 0, 0 );
	int i;
	for(i=0; pIter->AtValidElement(); pIter->Next(), i++ )
		{
		const char *pKey;
		void *pVal = pIter->Current( &pKey );
		PIString *pStr = ( PIString * )pVal;
		cout << '\t' << i << "...|"
			<< pKey << "|" << (*pStr) << endl; 
		};
	PI_DELETE( pIter );
	cout << "3..Done" << endl;

	/* --- --- */
	cout << "4..Create another TestTree, add elements and iterate \
over all elements which match 'Server2'. Search up into parent Tree"
			<< endl;
	_PIDB *pDB2 = PI_NEW( TestTree( pDB1 ) );
	pDB2->Add( PIDBTYPE_TESTTREE_PISTRING, (const char *)"Server1", (void *)"Level2,1", 0 );
	pDB2->Add( PIDBTYPE_TESTTREE_PISTRING, (const char *)"Server1", (void *)"Level2,2", 0 );
	pDB2->Add( PIDBTYPE_TESTTREE_PISTRING, (const char *)"Server1", (void *)"Level2,3", 0 );
	pDB2->Add( PIDBTYPE_TESTTREE_PISTRING, (const char *)"Server2", (void *)"Level2,4", 0 );
	pDB2->Add( PIDBTYPE_TESTTREE_PISTRING, (const char *)"Server2", (void *)"Level2,5", 0 );
	pDB2->Add( PIDBTYPE_TESTTREE_PISTRING, (const char *)"Server2", (void *)"Level2,6", 0 );
	pIter=pDB2->GetIterator(PIDBTYPE_TESTTREE_PISTRING, "Server2", 0 );
	for(i=0; pIter->AtValidElement(); pIter->Next(), i++ )
		{
		const char *pKey;
		void *pVal = pIter->Current( &pKey );
		PIString *pStr = ( PIString * )pVal;
		cout << '\t' << i << "...|"
			<< pKey << "|" << (*pStr) << endl; 
		};
	PI_DELETE( pIter );
	cout << "4..Done" << endl;

	/* --- --- */
	cout << "5..Create another TestTree, under the list, with four elements \
Server1, Server2, Server1, Server2. Delete the first Server2. Display tree"
			<< endl;
	_PIDB *pDB3 = PI_NEW( TestTree( pDB2 ) );
	pDB3->Add( PIDBTYPE_TESTTREE_PISTRING, (const char *)"Server1", (void *)"Level3,1", 0 );
	pDB3->Add( PIDBTYPE_TESTTREE_PISTRING, (const char *)"Server2", (void *)"Level3,2", 0 );
	pDB3->Add( PIDBTYPE_TESTTREE_PISTRING, (const char *)"Server1", (void *)"Level3,3", 0 );
	pDB3->Add( PIDBTYPE_TESTTREE_PISTRING, (const char *)"Server2", (void *)"Level3,4", 0 );
	pDB3->Add( PIDBTYPE_TESTTREE_PISTRING, (const char *)"Server2", (void *)"Level3,4", 0 );
	cout << "Full tree before delete" << endl;
	pIter=pDB3->GetIterator(PIDBTYPE_TESTTREE_PISTRING, 0, 0 );
	for(i=0; pIter->AtValidElement(); pIter->Next(), i++ )
		{
		const char *pKey;
		void *pVal = pIter->Current( &pKey );
		PIString *pStr = ( PIString * )pVal;
		cout << '\t' << i << "...|"
			<< pKey << "|" << (*pStr) << endl; 
		};
	PI_DELETE( pIter );
	cout << "Deleting tree" << endl;
	pIter=pDB3->GetIterator(PIDBTYPE_TESTTREE_PISTRING, "Server2", 0 );
	if ( pIter->AtValidElement() )
		{
		const char *pKey;
		void *pVal = pIter->Current( &pKey );
		PIString *pStr = ( PIString * )pVal;
		cout << "Deleting...|"
			<< pKey << "|" << (*pStr) << endl; 
		pIter->RemoveCurrent();
		}
	else
		{
		cout << "Element not found" << endl;
		};
	PI_DELETE( pIter );
	cout << "Full tree after delete" << endl;
	pIter=pDB3->GetIterator(PIDBTYPE_TESTTREE_PISTRING, 0, 0 );
	for(i=0; pIter->AtValidElement(); pIter->Next(), i++ )
		{
		const char *pKey;
		void *pVal = pIter->Current( &pKey );
		PIString *pStr = ( PIString * )pVal;
		cout << '\t' << i << "...|"
			<< pKey << "|" << (*pStr) << endl; 
		};
	PI_DELETE( pIter );
	cout << "5..Done" << endl;

	/* --- --- */
	cout << "6..Delete every 'Server1' in the entire tree" << endl;
	pIter=pDB3->GetIterator(PIDBTYPE_TESTTREE_PISTRING, "Server1", 0 );
	for(i=0; pIter->AtValidElement(); pIter->Next(), i++ )
		{
		pIter->RemoveCurrent();
		};
	PI_DELETE( pIter );
	cout << "Full tree after delete" << endl;
	pIter=pDB3->GetIterator(PIDBTYPE_TESTTREE_PISTRING, 0, 0 );
	for(i=0; pIter->AtValidElement(); pIter->Next(), i++ )
		{
		const char *pKey;
		void *pVal = pIter->Current( &pKey );
		PIString *pStr = ( PIString * )pVal;
		cout << '\t' << i << "...|"
			<< pKey << "|" << (*pStr) << endl; 
		};
	PI_DELETE( pIter );
	cout << "6..Done" << endl;

	/* --- --- */
#define NTIMES 100 
	int iNum=NTIMES;
	cout << "7..Add distinct names to each tree, " << iNum << " times." << endl;
	PITimer_reset( pTimer );
	for(i=0; i<iNum; i++ )
		{
		PIString sResult;
		pi_ltoa( i, 16, sResult );
		pDB1->Add( PIDBTYPE_TESTTREE_PISTRING, sResult, (void *)"Level1", 0 );
		pDB2->Add( PIDBTYPE_TESTTREE_PISTRING, sResult, (void *)"Level2", 0 );
		pDB3->Add( PIDBTYPE_TESTTREE_PISTRING, sResult, (void *)"Level3", 0 );
		};
	WriteElapsed( pTimer );
	cout << "7..Done" << endl;

	/* --- --- */
	iNum=NTIMES;
	cout << "8..Iterate over entire tree, " << iNum << " times." << endl;
	PITimer_reset( pTimer );
	for(i=0; i<iNum; i++ )
		{
		PIString sResult;
		pi_ltoa( i, 16, sResult );
		pIter=pDB3->GetIterator(PIDBTYPE_TESTTREE_PISTRING, sResult, 0 );
		for(; pIter->AtValidElement(); pIter->Next())
			{
#if 0
			const char *pKey;
			void *pVal = pIter->Current( &pKey );
			PIString *pStr = ( PIString * )pVal;
			cout << '\t' << i << "...|"
				<< pKey << "|" << (*pStr) << endl; 
#endif
			};
		PI_DELETE( pIter );
		};
	WriteElapsed( pTimer );
	cout << "8..Done" << endl;

	/* --- write out the entire tree --- */
	cout << "9..Render tree" << endl;
/*	pDB1->Render( cout ); */
	cout << "9..Done" << endl;

	/* --- delete the TestTree --- */
	cout << "END..Delete the TestTree" << endl;
	PI_DELETE( pDB1 );
	cout << "END..Done" << endl;

	PITimer_delete( pTimer );

	iRet = 0;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int main( int iArgc, char *ppArgv[] )
{
	if ( iArgc<1 )
		{ cout << "bad arguments" << endl; return -1; };
	PIProgram_enter( ppArgv[0], PIPROGRAM_VERSION_1_0, DoTest );
	return iRet;
}

⌨️ 快捷键说明

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