From a820665eea71a64b40e74ed24afeaf07a7a99db4 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 26 May 2006 14:36:57 +0000 Subject: Added the first of many unit tests. For now the unit tests are just built with everything else in the all target of the makefile, which is fine, but relies on CppTest, which can be found at http://cpptest.sf.net Also fixed some things I've been meaning to get to for a while in the xml system, including a few bugs that will make coping with malformed data not hang other programs, and do the error reporting in a nice way. --- src/unit/hashtable.cpp | 107 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 src/unit/hashtable.cpp (limited to 'src/unit/hashtable.cpp') diff --git a/src/unit/hashtable.cpp b/src/unit/hashtable.cpp new file mode 100644 index 0000000..b2e1cf5 --- /dev/null +++ b/src/unit/hashtable.cpp @@ -0,0 +1,107 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "hashfunctionstring.h" +#include "hashfunctioncasestring.h" +#include "hashfunctionint.h" + +class HashFunctionSuite : public Test::Suite +{ +public: + HashFunctionSuite() + { + TEST_ADD( HashFunctionSuite::functionString ) + TEST_ADD( HashFunctionSuite::functionCaseString ) + TEST_ADD( HashFunctionSuite::functionInt ) + } + +private: + void functionStringWorker( HashFunction &hf, std::set &sCodes, char *str, int nLevel, int nMax ) + { + for( char let = 'A'; let <= 'z'; let += 3 ) + { + str[nLevel+1] = '\0'; + str[nLevel] = let; + unsigned long x = hf.hash( str ); + TEST_ASSERT( sCodes.find( x ) == sCodes.end() ); + TEST_ASSERT( hf.cmpIDs( str, str ) ); + sCodes.insert( x ); + + if( nLevel < nMax ) + functionStringWorker( hf, sCodes, str, nLevel+1, nMax ); + } + } + + void functionString() + { + HashFunctionString hf; + char str[10]; + + std::set sCodes; + + functionStringWorker( hf, sCodes, str, 0, 3 ); + } + + void functionCaseStringWorker( HashFunction &hf, std::map &sCodes, char *str, int nLevel, int nMax ) + { + for( char let = 'A'; let <= 'z'; let += 3 ) + { + str[nLevel+1] = '\0'; + str[nLevel] = let; + unsigned long x = hf.hash( str ); + std::map::iterator i = sCodes.find( x ); + if( i == sCodes.end() ) + { + sCodes[x] = strdup( str ); + } + else + { + TEST_ASSERT( strcasecmp( (*i).second, str ) == 0 ); + TEST_ASSERT( hf.cmpIDs( (*i).second, str ) == true ); + } + + if( nLevel < nMax ) + functionCaseStringWorker( hf, sCodes, str, nLevel+1, nMax ); + } + } + + void functionCaseString() + { + HashFunctionCaseString hf; + char str[10]; + + std::map sCodes; + + functionCaseStringWorker( hf, sCodes, str, 0, 3 ); + + std::map::iterator i; + for( i = sCodes.begin(); i != sCodes.end(); i++ ) + { + free( (*i).second ); + } + } + + void functionInt() + { + HashFunctionInt hf; + + for( long i = -100000; i <= 100000; i += 100 ) + { + TEST_ASSERT( ((long)hf.hash( (void *)i )) == i ); + TEST_ASSERT( ((long)hf.cmpIDs( (void *)i, (void *)i )) ); + } + } +}; + +int main( int argc, char *argv[] ) +{ + Test::TextOutput output( Test::TextOutput::Verbose ); + HashFunctionSuite ts; + return ts.run( output ) ? EXIT_SUCCESS : EXIT_FAILURE; +} + -- cgit v1.2.3