📄 testmain.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/Base/TestMain.cpp,v $
* $Date: 2003/05/13 18:41:56 $
*
Description:
\*____________________________________________________________________________*/
//$SourceTop:$
#include <iostream.h>
#include <stdio.h>
#include <errno.h>
#include "PIString.h"
#include "StrToken.h"
#include "SwapStr.h"
#include "DeQuote.h"
#include "BSearch.h"
#if defined(PI_LINUX) || defined(PI_SOLARIS) /* UNIX */
#include <unistd.h>
#include <sys/time.h>
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
struct timeval tvGlobal;
void timerinit();
void writetime()
{
struct timeval tv;
if ( gettimeofday( &tv, 0 ) )
{
cout << "Error getting time" << errno << endl;
}
else
{
tv.tv_sec -= tvGlobal.tv_sec;
tv.tv_usec -= tvGlobal.tv_usec;
if ( tv.tv_usec < 0 )
{
tv.tv_usec += 1000000;
tv.tv_sec --;
};
cout << "Elapsed time: " << tv.tv_sec << '.';
cout.width( 6 );
cout << tv.tv_usec << " seconds." << endl;
timerinit();
};
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
void timerinit()
{
if ( gettimeofday( &tvGlobal, 0 ) )
{
cout << "Error initialising timer" << endl;
};
}
#else
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
void writetime()
{
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
void timerinit()
{
}
#endif
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
int iDummyNumber;
void DummyFunction()
{
iDummyNumber=37;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
inline void DummyInlineFunction()
{
iDummyNumber=37;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
main()
{
cout << "0...Checking for infinite loops in lists" << endl;
{
DblList t;
cout << "\t.."; cout.flush();
DblListIterator i(t);
for(; !i.BadIndex(); i++);
cout << "1.."; cout.flush();
t.PushBack( 0 );
cout << "2.."; cout.flush();
for(i=t; !i.BadIndex(); i++);
cout << "3.."; cout.flush();
t.Append( 0 );
cout << "4.."; cout.flush();
for(i=t; !i.BadIndex(); i++);
cout << "5.."; cout.flush();
cout << "0...End" << endl;
cout << "0.5...Checking for infinite loops in lists" << endl;
cout << "Test out swapping sub strings, replace <is> with <ab>" << endl;
PIString sTmp = "This is another of this isisisi is and another is";
cout << "BEFORE: " << sTmp << endl;
SwapSubStrings tSwap( sTmp, PIString("is"), PIString("ab") );
cout << "AFTER : " << tSwap.GetString() << endl;
cout << "0.5...End" << endl;
};
int i;
long l;
PIString sString0( "123 4\"56 \"567 384" );
cout << sString0 << endl;
StringTokenizer tTokens( sString0, " ", 0 );
for( i=0; i<tTokens.NumTokens(); i++)
{
cout << "\t" << i << "..." << tTokens.GetToken(i) << endl;
};
PIString sDeQuote1( "Hello" );
PIString sDeQuote2( "\"Hello\"" );
PIString sDeQuote3( "\"Hello" );
PIString sDeQuote4( "Hello\"" );
PIString sDeQuote5( "He\"llo" );
PIString sDeQuote6( "He\"l\"lo" );
DeQuote dDeQuote1( sDeQuote1 );
DeQuote dDeQuote2( sDeQuote2 );
DeQuote dDeQuote3( sDeQuote3 );
DeQuote dDeQuote4( sDeQuote4 );
DeQuote dDeQuote5( sDeQuote5 );
DeQuote dDeQuote6( sDeQuote6 );
cout << "\tDQ.1='" << sDeQuote1 << "', \t'" << dDeQuote1 << "'" << endl;
cout << "\tDQ.2='" << sDeQuote2 << "', \t'" << dDeQuote2 << "'" << endl;
cout << "\tDQ.3='" << sDeQuote3 << "', \t'" << dDeQuote3 << "'" << endl;
cout << "\tDQ.4='" << sDeQuote4 << "', \t'" << dDeQuote4 << "'" << endl;
cout << "\tDQ.5='" << sDeQuote5 << "', \t'" << dDeQuote5 << "'" << endl;
cout << "\tDQ.6='" << sDeQuote6 << "', \t'" << dDeQuote6 << "'" << endl;
PIString sString1( "This is the first string" );
StringTokenizer tDiv( sString1, " " );
cout << "1..." << sString1 << endl;
cout << "2..." << tDiv.GetString() << endl;
for(i=0; i<tDiv.NumTokens(); i++)
{
cout << "\t" << i << "..." << tDiv.GetToken(i) << endl;
};
cout << "********** Starting performance test ***********" << endl;
/* --- set padding characters in cout etc. --- */
cout.fill( '0' );
timerinit();
int iTry=1000000;
cout << "1..Construct and destructing PIString " << iTry <<
" times." << endl;
writetime();
for(i=0; i<iTry; i++)
{
PIString tTest( "Hello" );
(void)tTest;
};
writetime();
cout << "1..Done" << endl;
iTry=25000;
DblList tList;
cout << "2..Inserting and appending an element " << iTry <<
" times each into a linked list." << endl;
writetime();
for(i=0; i<iTry; i++)
{
tList.Append( (DblList::type)0 );
tList.PushBack( (DblList::type)0 );
};
writetime();
cout << "2..Done (the list now has " << tList.Size() << " elements)."
<< endl;
iTry=50;
cout << "3..Full scan of the linked list from previous step " << iTry <<
" times." << endl;
writetime();
for(i=0; i<iTry; i++)
{
for(DblListIterator j(tList); !j.BadIndex(); j++)
{
j.Current();
};
};
writetime();
cout << "3..Done" << endl;
#if 0
iTry=50;
cout << "3.5..Full scan of the linked list again - in reverse " << iTry <<
" times." << endl;
writetime();
for(i=0; i<iTry; i++)
{
for(DblListIterator j(tList); !j.Befor; j--)
{
j.Current();
};
};
writetime();
cout << "3.5..Done" << endl;
#endif
cout << "4..Clearing the list from the previous steps " << endl;
writetime();
tList.Clear();
writetime();
cout << "4..Done" << endl;
long lTry=10000000;
cout << "5..Call a function and make an assignment " << lTry <<
" times." << endl;
/* --- stop DummyFunction from being inlined by evaluating its
address --- */
cout << "Address of DummyFunction() is " << (void *)DummyFunction << endl;
writetime();
for(l=0; l<lTry; l++)
{
DummyFunction();
};
writetime();
cout << "5..Done" << endl;
cout << "6..Call an inline function and make an assignment " << lTry <<
" times." << endl;
writetime();
for(l=0; l<lTry; l++)
{
DummyFunction();
};
writetime();
cout << "6..Done" << endl;
lTry = 5000;
cout << "7..inserting " << lTry << " unique names into BSearch " << endl;
BSearch<void *> tSearch;
enum { BUF_SIZE=63 };
char szBuf[BUF_SIZE+1];
for(l=lTry-1; l>=0; l--)
{
sprintf( szBuf, "%lX", l );
#if defined(NDEBUG)
tSearch.Add( szBuf, (void *)l );
#else
assert( tSearch.Add( szBuf, (void *)l ) );
#endif
};
cout << "7..Now compiling." << endl;
tSearch.Compile();
cout << "7..looking up " << lTry << " unique names in BSearch "
<< endl;
for(l=0; l<lTry; l++)
{
sprintf( szBuf, "%lX", l );
for( int k=0; k<1000; k++ )
{
#if defined(NDEBUG)
if ( tSearch.Lookup( szBuf )!=(void *)l )
{
cout << "Error" << endl;
return 0;
};
#else
assert( tSearch.Lookup( szBuf )==(void *)l );
#endif
};
};
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -