Added test for bugs 2792371 and 2694060. Cleaned up code.
git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@1136 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
parent
5fdebe2bf7
commit
cda3ef96a2
1 changed files with 370 additions and 138 deletions
|
@ -16,6 +16,8 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include <cctype>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
#include "../SmallObj/timer.h"
|
#include "../SmallObj/timer.h"
|
||||||
|
|
||||||
|
@ -104,17 +106,33 @@ void TestCase2(const string& fmt, T value, U value2)
|
||||||
assert(s == buf);
|
assert(s == buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
// ----------------------------------------------------------------------------
|
||||||
{
|
|
||||||
|
|
||||||
if (argc == 2)
|
void test_dword()
|
||||||
{
|
{
|
||||||
|
typedef signed int Int;
|
||||||
|
typedef unsigned int UInt;
|
||||||
|
typedef signed long Long;
|
||||||
|
typedef unsigned long ULong;
|
||||||
|
Int i(0);
|
||||||
|
UInt ui(0);
|
||||||
|
Long l(0);
|
||||||
|
ULong ul(0);
|
||||||
|
Printf("%d")(i);
|
||||||
|
Printf("%d")(ui);
|
||||||
|
Printf("%d")(l);
|
||||||
|
Printf("%d")(ul);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void SpeedTest( unsigned int loop )
|
||||||
|
{
|
||||||
|
cout << "Starting SpeedTest" << endl;
|
||||||
|
|
||||||
// test speed
|
// test speed
|
||||||
|
|
||||||
Timer t;
|
Timer t;
|
||||||
|
|
||||||
int loop = atoi(argv[1]);
|
|
||||||
|
|
||||||
if(loop < 100)
|
if(loop < 100)
|
||||||
loop = 100;
|
loop = 100;
|
||||||
|
|
||||||
|
@ -144,13 +162,110 @@ int main(int argc, char** argv)
|
||||||
t.print(t_printf,"printf : ");
|
t.print(t_printf,"printf : ");
|
||||||
t.print(t_Printf,"Printf : ");
|
t.print(t_Printf,"Printf : ");
|
||||||
t.print(t_cout, "std::cout: ");
|
t.print(t_cout, "std::cout: ");
|
||||||
|
|
||||||
|
cout << "Finished SpeedTest" << endl;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void FormatTest( void )
|
||||||
{
|
{
|
||||||
|
cout << "Starting FormatTest" << endl;
|
||||||
|
|
||||||
|
string result;
|
||||||
|
unsigned int i = 1;
|
||||||
|
signed short ss = 2;
|
||||||
|
float f = 3.4;
|
||||||
|
const char * message = " over here! ";
|
||||||
|
|
||||||
|
const char * format = "";
|
||||||
|
::Loki::SPrintf( result, format );
|
||||||
|
assert( result == format );
|
||||||
|
result.clear();
|
||||||
|
|
||||||
|
format = "abc";
|
||||||
|
::Loki::SPrintf( result, format );
|
||||||
|
assert( result == format );
|
||||||
|
result.clear();
|
||||||
|
|
||||||
|
format = "%%";
|
||||||
|
::Loki::SPrintf( result, format );
|
||||||
|
assert( result == "%" );
|
||||||
|
result.clear();
|
||||||
|
|
||||||
|
format = "%d";
|
||||||
|
::Loki::SPrintf( result, format )( i );
|
||||||
|
assert( result == "1" );
|
||||||
|
result.clear();
|
||||||
|
|
||||||
|
format = "%s";
|
||||||
|
::Loki::SPrintf( result, format )( message );
|
||||||
|
assert( result == message );
|
||||||
|
result.clear();
|
||||||
|
|
||||||
|
format = "%d";
|
||||||
|
try
|
||||||
|
{
|
||||||
|
::Loki::SPrintf( result, format )( i )( message );
|
||||||
|
assert( false );
|
||||||
|
}
|
||||||
|
catch ( const ::std::logic_error & ex )
|
||||||
|
{
|
||||||
|
assert( true );
|
||||||
|
}
|
||||||
|
result.clear();
|
||||||
|
|
||||||
|
format = "%d";
|
||||||
|
try
|
||||||
|
{
|
||||||
|
::Loki::SPrintf( result, format )( i )( message )( ss );
|
||||||
|
assert( false );
|
||||||
|
}
|
||||||
|
catch ( const ::std::logic_error & ex )
|
||||||
|
{
|
||||||
|
assert( true );
|
||||||
|
}
|
||||||
|
result.clear();
|
||||||
|
|
||||||
|
format = "%d";
|
||||||
|
try
|
||||||
|
{
|
||||||
|
::Loki::SPrintf( result, format )( i )( ss );
|
||||||
|
assert( false );
|
||||||
|
}
|
||||||
|
catch ( const ::std::logic_error & ex )
|
||||||
|
{
|
||||||
|
assert( true );
|
||||||
|
}
|
||||||
|
result.clear();
|
||||||
|
|
||||||
|
format = "%d";
|
||||||
|
try
|
||||||
|
{
|
||||||
|
::Loki::SPrintf( result, format )( i )( f );
|
||||||
|
assert( false );
|
||||||
|
}
|
||||||
|
catch ( const ::std::logic_error & ex )
|
||||||
|
{
|
||||||
|
assert( true );
|
||||||
|
}
|
||||||
|
result.clear();
|
||||||
|
|
||||||
|
cout << "Finished FormatTest" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void RandomTest( unsigned int loopCount )
|
||||||
|
{
|
||||||
|
cout << "Starting RandomTest" << endl;
|
||||||
|
if ( loopCount < 100 )
|
||||||
|
loopCount = 100;
|
||||||
|
|
||||||
//srand(time(0));
|
//srand(time(0));
|
||||||
srand(0);
|
srand(0);
|
||||||
printf("\nNumber of tests:\n");
|
printf("\nNumber of tests:\n");
|
||||||
for (unsigned i = 0; ; ++i)
|
for ( unsigned int i = 0; i < loopCount; ++i )
|
||||||
{
|
{
|
||||||
printf("%u\r", i);
|
printf("%u\r", i);
|
||||||
|
|
||||||
|
@ -238,23 +353,140 @@ int main(int argc, char** argv)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
cout << endl << "Finished RandomTest" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_dword()
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
class CommandLineArgs
|
||||||
{
|
{
|
||||||
typedef signed int Int;
|
public:
|
||||||
typedef unsigned int UInt;
|
|
||||||
typedef signed long Long;
|
CommandLineArgs( unsigned int argc, const char * argv[] );
|
||||||
typedef unsigned long ULong;
|
|
||||||
Int i(0);
|
inline unsigned int GetSpeedLoopCount( void ) const { return m_speedLoopCount; }
|
||||||
UInt ui(0);
|
inline unsigned int GetRandomLoopCount( void ) const { return m_randomLoopCount; }
|
||||||
Long l(0);
|
|
||||||
ULong ul(0);
|
inline bool DoSpeedTest( void ) const { return ( 0 < m_speedLoopCount ); }
|
||||||
Printf("%d")(i);
|
inline bool DoRandomTest( void ) const { return ( 0 < m_randomLoopCount ); }
|
||||||
Printf("%d")(ui);
|
inline bool DoFormatTest( void ) const { return m_doFormatTest; }
|
||||||
Printf("%d")(l);
|
inline bool DoShowHelp( void ) const { return m_showHelp; }
|
||||||
Printf("%d")(ul);
|
|
||||||
|
void ShowHelp( void ) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
unsigned int m_speedLoopCount;
|
||||||
|
unsigned int m_randomLoopCount;
|
||||||
|
bool m_doFormatTest;
|
||||||
|
bool m_showHelp;
|
||||||
|
const char * m_exeName;
|
||||||
|
};
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
CommandLineArgs::CommandLineArgs( unsigned int argc, const char * argv[] ) :
|
||||||
|
m_speedLoopCount( 0 ),
|
||||||
|
m_randomLoopCount( 0 ),
|
||||||
|
m_doFormatTest( false ),
|
||||||
|
m_showHelp( false ),
|
||||||
|
m_exeName( argv[0] )
|
||||||
|
{
|
||||||
|
|
||||||
|
if ( argc < 2 )
|
||||||
|
{
|
||||||
|
m_showHelp = true;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isValid = true;
|
||||||
|
for ( unsigned int ii = 1; ( isValid ) && ( ii < argc ); ++ii )
|
||||||
|
{
|
||||||
|
const char * arg = argv[ii];
|
||||||
|
const unsigned int length = ::strlen( arg );
|
||||||
|
if ( ( '-' != *arg ) || ( length < 2 ) )
|
||||||
|
{
|
||||||
|
isValid = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ( arg[1] )
|
||||||
|
{
|
||||||
|
default: isValid = false; break;
|
||||||
|
case 'h': m_showHelp = true; break;
|
||||||
|
case 'f': m_doFormatTest = true; break;
|
||||||
|
|
||||||
|
case 'r':
|
||||||
|
{
|
||||||
|
if ( ( length < 3 ) || ( ':' != arg[2] ) || ( 0 == isdigit( arg[3] ) ) )
|
||||||
|
{
|
||||||
|
isValid = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
m_randomLoopCount = ::atoi( arg + 3 );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case 's':
|
||||||
|
{
|
||||||
|
if ( ( length < 3 ) || ( ':' != arg[2] ) || ( 0 == isdigit( arg[3] ) ) )
|
||||||
|
{
|
||||||
|
isValid = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
m_speedLoopCount = ::atoi( arg + 3 );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( !isValid )
|
||||||
|
{
|
||||||
|
m_showHelp = true;
|
||||||
|
m_speedLoopCount = 0;
|
||||||
|
m_randomLoopCount = 0;
|
||||||
|
m_doFormatTest = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void CommandLineArgs::ShowHelp( void ) const
|
||||||
|
{
|
||||||
|
cout << "Usage: " << m_exeName << " [-h] [-f] [-r:#] [-s:#]" << endl;
|
||||||
|
cout << " -h Show this help info and exit. Overrides all other options." << endl;
|
||||||
|
cout << " -f Run formatting tests." << endl;
|
||||||
|
cout << " -r:# Run random tests for # of loops. # is a positive decimal value greater than 100." << endl;
|
||||||
|
cout << " -s:# Run speed tests for # of loops. # is a positive decimal value greater than 100." << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
int main( int argc, const char * argv[] )
|
||||||
|
{
|
||||||
|
|
||||||
|
const CommandLineArgs args( argc, argv );
|
||||||
|
|
||||||
|
if ( args.DoShowHelp() )
|
||||||
|
{
|
||||||
|
args.ShowHelp();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( args.DoSpeedTest() )
|
||||||
|
{
|
||||||
|
SpeedTest( args.GetSpeedLoopCount() );
|
||||||
|
}
|
||||||
|
if ( args.DoRandomTest() )
|
||||||
|
{
|
||||||
|
RandomTest( args.GetRandomLoopCount() );
|
||||||
|
}
|
||||||
|
if ( args.DoFormatTest() )
|
||||||
|
{
|
||||||
|
FormatTest();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
Loading…
Add table
Reference in a new issue