diff options
author | Mike Buland <eichlan@xagasoft.com> | 2006-05-26 14:36:57 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2006-05-26 14:36:57 +0000 |
commit | a820665eea71a64b40e74ed24afeaf07a7a99db4 (patch) | |
tree | 515af31ac2ed78aa92ce7e90e478bdb452e6e438 /src/unit/hashtable.cpp | |
parent | bd5bb1ca60a6a97b110cbf221b3625e6e6200141 (diff) | |
download | libbu++-a820665eea71a64b40e74ed24afeaf07a7a99db4.tar.gz libbu++-a820665eea71a64b40e74ed24afeaf07a7a99db4.tar.bz2 libbu++-a820665eea71a64b40e74ed24afeaf07a7a99db4.tar.xz libbu++-a820665eea71a64b40e74ed24afeaf07a7a99db4.zip |
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.
Diffstat (limited to 'src/unit/hashtable.cpp')
-rw-r--r-- | src/unit/hashtable.cpp | 107 |
1 files changed, 107 insertions, 0 deletions
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 @@ | |||
1 | #include <cstdlib> | ||
2 | #include <cstring> | ||
3 | #include <iostream> | ||
4 | #include <cpptest.h> | ||
5 | #include <string.h> | ||
6 | #include <set> | ||
7 | #include <map> | ||
8 | |||
9 | #include "hashfunctionstring.h" | ||
10 | #include "hashfunctioncasestring.h" | ||
11 | #include "hashfunctionint.h" | ||
12 | |||
13 | class HashFunctionSuite : public Test::Suite | ||
14 | { | ||
15 | public: | ||
16 | HashFunctionSuite() | ||
17 | { | ||
18 | TEST_ADD( HashFunctionSuite::functionString ) | ||
19 | TEST_ADD( HashFunctionSuite::functionCaseString ) | ||
20 | TEST_ADD( HashFunctionSuite::functionInt ) | ||
21 | } | ||
22 | |||
23 | private: | ||
24 | void functionStringWorker( HashFunction &hf, std::set<unsigned long> &sCodes, char *str, int nLevel, int nMax ) | ||
25 | { | ||
26 | for( char let = 'A'; let <= 'z'; let += 3 ) | ||
27 | { | ||
28 | str[nLevel+1] = '\0'; | ||
29 | str[nLevel] = let; | ||
30 | unsigned long x = hf.hash( str ); | ||
31 | TEST_ASSERT( sCodes.find( x ) == sCodes.end() ); | ||
32 | TEST_ASSERT( hf.cmpIDs( str, str ) ); | ||
33 | sCodes.insert( x ); | ||
34 | |||
35 | if( nLevel < nMax ) | ||
36 | functionStringWorker( hf, sCodes, str, nLevel+1, nMax ); | ||
37 | } | ||
38 | } | ||
39 | |||
40 | void functionString() | ||
41 | { | ||
42 | HashFunctionString hf; | ||
43 | char str[10]; | ||
44 | |||
45 | std::set<unsigned long> sCodes; | ||
46 | |||
47 | functionStringWorker( hf, sCodes, str, 0, 3 ); | ||
48 | } | ||
49 | |||
50 | void functionCaseStringWorker( HashFunction &hf, std::map<unsigned long, char *> &sCodes, char *str, int nLevel, int nMax ) | ||
51 | { | ||
52 | for( char let = 'A'; let <= 'z'; let += 3 ) | ||
53 | { | ||
54 | str[nLevel+1] = '\0'; | ||
55 | str[nLevel] = let; | ||
56 | unsigned long x = hf.hash( str ); | ||
57 | std::map<unsigned long, char *>::iterator i = sCodes.find( x ); | ||
58 | if( i == sCodes.end() ) | ||
59 | { | ||
60 | sCodes[x] = strdup( str ); | ||
61 | } | ||
62 | else | ||
63 | { | ||
64 | TEST_ASSERT( strcasecmp( (*i).second, str ) == 0 ); | ||
65 | TEST_ASSERT( hf.cmpIDs( (*i).second, str ) == true ); | ||
66 | } | ||
67 | |||
68 | if( nLevel < nMax ) | ||
69 | functionCaseStringWorker( hf, sCodes, str, nLevel+1, nMax ); | ||
70 | } | ||
71 | } | ||
72 | |||
73 | void functionCaseString() | ||
74 | { | ||
75 | HashFunctionCaseString hf; | ||
76 | char str[10]; | ||
77 | |||
78 | std::map<unsigned long, char *> sCodes; | ||
79 | |||
80 | functionCaseStringWorker( hf, sCodes, str, 0, 3 ); | ||
81 | |||
82 | std::map<unsigned long, char *>::iterator i; | ||
83 | for( i = sCodes.begin(); i != sCodes.end(); i++ ) | ||
84 | { | ||
85 | free( (*i).second ); | ||
86 | } | ||
87 | } | ||
88 | |||
89 | void functionInt() | ||
90 | { | ||
91 | HashFunctionInt hf; | ||
92 | |||
93 | for( long i = -100000; i <= 100000; i += 100 ) | ||
94 | { | ||
95 | TEST_ASSERT( ((long)hf.hash( (void *)i )) == i ); | ||
96 | TEST_ASSERT( ((long)hf.cmpIDs( (void *)i, (void *)i )) ); | ||
97 | } | ||
98 | } | ||
99 | }; | ||
100 | |||
101 | int main( int argc, char *argv[] ) | ||
102 | { | ||
103 | Test::TextOutput output( Test::TextOutput::Verbose ); | ||
104 | HashFunctionSuite ts; | ||
105 | return ts.run( output ) ? EXIT_SUCCESS : EXIT_FAILURE; | ||
106 | } | ||
107 | |||