1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/hashes/hashes.cpp
3 // Purpose: wxHashTable, wxHashMap, wxHashSet unit test
4 // Author: Vadim Zeitlin, Mattia Barbon
5 // Modified: Mike Wetherell
7 // Copyright: (c) 2004 Vadim Zeitlin, Mattia Barbon, 2005 M. Wetherell
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
25 #include "wx/hashmap.h"
26 #include "wx/hashset.h"
28 #if defined wxLongLong_t && !defined wxLongLongIsLong && \
29 (!defined __VISUALC__ || __VISUALC__ > 1100) // doesn't work on VC5
33 // --------------------------------------------------------------------------
34 // helper class for typed/untyped wxHashTable
35 // --------------------------------------------------------------------------
39 Foo(int n_
) { n
= n_
; count
++; }
47 size_t Foo::count
= 0;
49 struct FooObject
: public wxObject
51 FooObject(int n_
) { n
= n_
; count
++; }
52 ~FooObject() { count
--; }
59 size_t FooObject::count
= 0;
61 // --------------------------------------------------------------------------
63 // --------------------------------------------------------------------------
65 class HashesTestCase
: public CppUnit::TestCase
71 CPPUNIT_TEST_SUITE( HashesTestCase
);
72 CPPUNIT_TEST( wxHashTableTest
);
73 CPPUNIT_TEST( wxUntypedHashTableDeleteContents
);
74 CPPUNIT_TEST( wxTypedHashTableTest
);
75 CPPUNIT_TEST( StringHashMapTest
);
76 CPPUNIT_TEST( PtrHashMapTest
);
77 CPPUNIT_TEST( LongHashMapTest
);
78 CPPUNIT_TEST( ULongHashMapTest
);
79 CPPUNIT_TEST( UIntHashMapTest
);
80 CPPUNIT_TEST( IntHashMapTest
);
81 CPPUNIT_TEST( ShortHashMapTest
);
82 CPPUNIT_TEST( UShortHashMapTest
);
84 CPPUNIT_TEST( LLongHashMapTest
);
85 CPPUNIT_TEST( ULLongHashMapTest
);
87 CPPUNIT_TEST( wxHashSetTest
);
88 CPPUNIT_TEST_SUITE_END();
90 void wxHashTableTest();
91 void wxUntypedHashTableDeleteContents();
92 void wxTypedHashTableTest();
93 void StringHashMapTest();
94 void PtrHashMapTest();
95 void LongHashMapTest();
96 void ULongHashMapTest();
97 void UIntHashMapTest();
98 void IntHashMapTest();
99 void ShortHashMapTest();
100 void UShortHashMapTest();
102 void LLongHashMapTest();
103 void ULLongHashMapTest();
105 void wxHashSetTest();
107 DECLARE_NO_COPY_CLASS(HashesTestCase
)
110 // register in the unnamed registry so that these tests are run by default
111 CPPUNIT_TEST_SUITE_REGISTRATION( HashesTestCase
);
113 // also include in its own registry so that these tests can be run alone
114 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( HashesTestCase
, "HashesTestCase" );
116 void HashesTestCase::wxHashTableTest()
118 const int COUNT
= 100;
121 wxHashTable
hash(wxKEY_INTEGER
, 10), hash2(wxKEY_STRING
);
125 for ( i
= 0; i
< COUNT
; ++i
)
129 wxHashTable::compatibility_iterator it
= hash
.Next();
138 CPPUNIT_ASSERT( i
== COUNT
);
140 for ( i
= 99; i
>= 0; --i
)
141 CPPUNIT_ASSERT( hash
.Get(i
) == &o
+ i
);
143 for ( i
= 0; i
< COUNT
; ++i
)
144 hash
.Put(i
, &o
+ i
+ 20);
146 for ( i
= 99; i
>= 0; --i
)
147 CPPUNIT_ASSERT( hash
.Get(i
) == &o
+ i
);
149 for ( i
= 0; i
< COUNT
/2; ++i
)
150 CPPUNIT_ASSERT( hash
.Delete(i
) == &o
+ i
);
152 for ( i
= COUNT
/2; i
< COUNT
; ++i
)
153 CPPUNIT_ASSERT( hash
.Get(i
) == &o
+ i
);
155 for ( i
= 0; i
< COUNT
/2; ++i
)
156 CPPUNIT_ASSERT( hash
.Get(i
) == &o
+ i
+ 20);
158 for ( i
= 0; i
< COUNT
/2; ++i
)
159 CPPUNIT_ASSERT( hash
.Delete(i
) == &o
+ i
+ 20);
161 for ( i
= 0; i
< COUNT
/2; ++i
)
162 CPPUNIT_ASSERT( hash
.Get(i
) == NULL
);
164 hash2
.Put(wxT("foo"), &o
+ 1);
165 hash2
.Put(wxT("bar"), &o
+ 2);
166 hash2
.Put(wxT("baz"), &o
+ 3);
168 CPPUNIT_ASSERT(hash2
.Get(wxT("moo")) == NULL
);
169 CPPUNIT_ASSERT(hash2
.Get(wxT("bar")) == &o
+ 2);
171 hash2
.Put(wxT("bar"), &o
+ 0);
173 CPPUNIT_ASSERT(hash2
.Get(wxT("bar")) == &o
+ 2);
176 // and now some corner-case testing; 3 and 13 hash to the same bucket
178 wxHashTable
hash(wxKEY_INTEGER
, 10);
184 CPPUNIT_ASSERT(hash
.Get(3) == NULL
);
187 hash
.Put(13, &dummy
);
190 CPPUNIT_ASSERT(hash
.Get(3) == NULL
);
194 CPPUNIT_ASSERT(hash
.Get(13) == NULL
);
197 hash
.Put(13, &dummy
);
200 CPPUNIT_ASSERT(hash
.Get(13) == NULL
);
204 CPPUNIT_ASSERT(hash
.Get(3) == NULL
);
207 // test for key + value access (specifically that supplying either
208 // wrong key or wrong value returns NULL)
210 wxHashTable
hash(wxKEY_INTEGER
, 10);
213 hash
.Put(3, 7, &dummy
+ 7);
214 hash
.Put(4, 8, &dummy
+ 8);
216 CPPUNIT_ASSERT(hash
.Get(7) == NULL
);
217 CPPUNIT_ASSERT(hash
.Get(3, 7) == &dummy
+ 7);
218 CPPUNIT_ASSERT(hash
.Get(4) == NULL
);
219 CPPUNIT_ASSERT(hash
.Get(3) == NULL
);
220 CPPUNIT_ASSERT(hash
.Get(8) == NULL
);
221 CPPUNIT_ASSERT(hash
.Get(8, 4) == NULL
);
223 CPPUNIT_ASSERT(hash
.Delete(7) == NULL
);
224 CPPUNIT_ASSERT(hash
.Delete(3) == NULL
);
225 CPPUNIT_ASSERT(hash
.Delete(3, 7) == &dummy
+ 7);
230 void HashesTestCase::wxUntypedHashTableDeleteContents()
232 // need a nested scope for destruction
235 hash
.DeleteContents(true);
237 CPPUNIT_ASSERT( hash
.GetCount() == 0 );
238 CPPUNIT_ASSERT( FooObject::count
== 0 );
240 static const int hashTestData
[] =
242 0, 1, 17, -2, 2, 4, -4, 345, 3, 3, 2, 1,
246 for ( n
= 0; n
< WXSIZEOF(hashTestData
); n
++ )
248 hash
.Put(hashTestData
[n
], n
, new FooObject(n
));
251 CPPUNIT_ASSERT( hash
.GetCount() == WXSIZEOF(hashTestData
) );
252 CPPUNIT_ASSERT( FooObject::count
== WXSIZEOF(hashTestData
) );
254 // delete from hash without deleting object
255 FooObject
* foo
= (FooObject
*)hash
.Delete(0l);
257 CPPUNIT_ASSERT( FooObject::count
== WXSIZEOF(hashTestData
) );
259 CPPUNIT_ASSERT( FooObject::count
== WXSIZEOF(hashTestData
) - 1 );
263 CPPUNIT_ASSERT( FooObject::count
== 0 );
266 WX_DECLARE_HASH(Foo
, wxListFoos
, wxHashFoos
);
268 void HashesTestCase::wxTypedHashTableTest()
270 // need a nested scope for destruction
273 hash
.DeleteContents(true);
275 CPPUNIT_ASSERT( hash
.GetCount() == 0 );
276 CPPUNIT_ASSERT( Foo::count
== 0 );
278 static const int hashTestData
[] =
280 0, 1, 17, -2, 2, 4, -4, 345, 3, 3, 2, 1,
284 for ( n
= 0; n
< WXSIZEOF(hashTestData
); n
++ )
286 hash
.Put(hashTestData
[n
], n
, new Foo(n
));
289 CPPUNIT_ASSERT( hash
.GetCount() == WXSIZEOF(hashTestData
) );
290 CPPUNIT_ASSERT( Foo::count
== WXSIZEOF(hashTestData
) );
292 for ( n
= 0; n
< WXSIZEOF(hashTestData
); n
++ )
294 Foo
*foo
= hash
.Get(hashTestData
[n
], n
);
296 CPPUNIT_ASSERT( foo
!= NULL
);
297 CPPUNIT_ASSERT( foo
->n
== (int)n
);
300 // element not in hash
301 CPPUNIT_ASSERT( hash
.Get(1234) == NULL
);
302 CPPUNIT_ASSERT( hash
.Get(1, 0) == NULL
);
304 // delete from hash without deleting object
305 Foo
* foo
= hash
.Delete(0);
307 CPPUNIT_ASSERT( Foo::count
== WXSIZEOF(hashTestData
) );
309 CPPUNIT_ASSERT( Foo::count
== WXSIZEOF(hashTestData
) - 1 );
313 CPPUNIT_ASSERT( Foo::count
== 0 );
316 // test compilation of basic map types
317 WX_DECLARE_HASH_MAP( int*, int*, wxPointerHash
, wxPointerEqual
, myPtrHashMap
);
318 WX_DECLARE_HASH_MAP( long, long, wxIntegerHash
, wxIntegerEqual
, myLongHashMap
);
319 WX_DECLARE_HASH_MAP( unsigned long, unsigned, wxIntegerHash
, wxIntegerEqual
,
321 WX_DECLARE_HASH_MAP( unsigned int, unsigned, wxIntegerHash
, wxIntegerEqual
,
323 WX_DECLARE_HASH_MAP( int, unsigned, wxIntegerHash
, wxIntegerEqual
,
325 WX_DECLARE_HASH_MAP( short, unsigned, wxIntegerHash
, wxIntegerEqual
,
327 WX_DECLARE_HASH_MAP( unsigned short, unsigned, wxIntegerHash
, wxIntegerEqual
,
331 // WX_DECLARE_HASH_MAP( wxString, wxString, wxStringHash, wxStringEqual,
332 // myStringHashMap );
333 WX_DECLARE_STRING_HASH_MAP(wxString
, myStringHashMap
);
336 WX_DECLARE_HASH_MAP( wxLongLong_t
, wxLongLong_t
,
337 wxIntegerHash
, wxIntegerEqual
, myLLongHashMap
);
338 WX_DECLARE_HASH_MAP( wxULongLong_t
, wxULongLong_t
,
339 wxIntegerHash
, wxIntegerEqual
, myULLongHashMap
);
342 // Helpers to generate a key value pair for item 'i', out of a total of 'count'
343 void MakeKeyValuePair(size_t i
, size_t /*count*/, wxString
& key
, wxString
& val
)
347 val
= wxT("A") + key
+ wxT("C");
350 // for integral keys generate a range of keys that will use all the bits of
352 template <class IntT
, class KeyT
>
353 IntT
MakeKey(size_t i
, size_t count
)
356 max
<<= sizeof(KeyT
) * 8 - 2;
357 max
-= count
/ 4 + 1;
359 return max
/ count
* 4 * i
+ i
/ 3;
362 // make key/value pairs for integer types
363 template <class KeyT
, class ValueT
>
364 void MakeKeyValuePair(size_t i
, size_t count
, KeyT
& key
, ValueT
& value
)
366 key
= MakeKey
<KeyT
, KeyT
>(i
, count
);
367 value
= wx_truncate_cast(ValueT
, key
);
370 // make key/values paris for pointer types
371 template <class T
, class ValueT
>
372 void MakeKeyValuePair(size_t i
, size_t count
, T
*& key
, ValueT
& value
)
374 key
= (T
*)wxUIntToPtr(MakeKey
<wxUIntPtr
, T
*>(i
, count
));
375 value
= wx_truncate_cast(ValueT
, key
);
379 template <class HashMapT
>
382 typedef typename
HashMapT::value_type::second_type value_type
;
383 typedef typename
HashMapT::key_type key_type
;
384 typedef typename
HashMapT::iterator Itor
;
386 HashMapT
sh(0); // as small as possible
390 const size_t count
= 10000;
392 // init with some data
393 for( i
= 0; i
< count
; ++i
)
395 MakeKeyValuePair(i
, count
, buf
, value
);
399 // test that insertion worked
400 CPPUNIT_ASSERT( sh
.size() == count
);
402 for( i
= 0; i
< count
; ++i
)
404 MakeKeyValuePair(i
, count
, buf
, value
);
405 CPPUNIT_ASSERT( sh
[buf
] == value
);
408 // check that iterators work
410 for( i
= 0, it
= sh
.begin(); it
!= sh
.end(); ++it
, ++i
)
412 CPPUNIT_ASSERT( i
!= count
);
413 CPPUNIT_ASSERT( it
->second
== sh
[it
->first
] );
416 CPPUNIT_ASSERT( sh
.size() == i
);
418 // test copy ctor, assignment operator
419 HashMapT
h1( sh
), h2( 0 );
422 for( i
= 0, it
= sh
.begin(); it
!= sh
.end(); ++it
, ++i
)
424 CPPUNIT_ASSERT( h1
[it
->first
] == it
->second
);
425 CPPUNIT_ASSERT( h2
[it
->first
] == it
->second
);
429 for( i
= 0; i
< count
; ++i
)
431 MakeKeyValuePair(i
, count
, buf
, value
);
432 size_t sz
= sh
.size();
434 // test find() and erase(it)
438 CPPUNIT_ASSERT( it
!= sh
.end() );
442 CPPUNIT_ASSERT( sh
.find( buf
) == sh
.end() );
447 size_t c
= sh
.erase( buf
);
448 CPPUNIT_ASSERT( c
== 1 );
449 CPPUNIT_ASSERT( sh
.find( buf
) == sh
.end() );
452 // count should decrease
453 CPPUNIT_ASSERT( sh
.size() == sz
- 1 );
457 void HashesTestCase::StringHashMapTest() { HashMapTest
<myStringHashMap
>(); }
458 void HashesTestCase::PtrHashMapTest() { HashMapTest
<myPtrHashMap
>(); }
459 void HashesTestCase::LongHashMapTest() { HashMapTest
<myLongHashMap
>(); }
460 void HashesTestCase::ULongHashMapTest() { HashMapTest
<myUnsignedHashMap
>(); }
461 void HashesTestCase::UIntHashMapTest() { HashMapTest
<myTestHashMap1
>(); }
462 void HashesTestCase::IntHashMapTest() { HashMapTest
<myTestHashMap2
>(); }
463 void HashesTestCase::ShortHashMapTest() { HashMapTest
<myTestHashMap3
>(); }
464 void HashesTestCase::UShortHashMapTest() { HashMapTest
<myTestHashMap4
>(); }
467 void HashesTestCase::LLongHashMapTest() { HashMapTest
<myLLongHashMap
>(); }
468 void HashesTestCase::ULLongHashMapTest() { HashMapTest
<myULLongHashMap
>(); }
472 #if __VISUALC__ <= 1200
473 #pragma warning(disable:4284) // operator->() returns a non-UDT
475 #endif // __VISUALC__
477 // test compilation of basic set types
478 WX_DECLARE_HASH_SET( int*, wxPointerHash
, wxPointerEqual
, myPtrHashSet
);
479 WX_DECLARE_HASH_SET( long, wxIntegerHash
, wxIntegerEqual
, myLongHashSet
);
480 WX_DECLARE_HASH_SET( unsigned long, wxIntegerHash
, wxIntegerEqual
,
482 WX_DECLARE_HASH_SET( unsigned int, wxIntegerHash
, wxIntegerEqual
,
484 WX_DECLARE_HASH_SET( int, wxIntegerHash
, wxIntegerEqual
,
486 WX_DECLARE_HASH_SET( short, wxIntegerHash
, wxIntegerEqual
,
488 WX_DECLARE_HASH_SET( unsigned short, wxIntegerHash
, wxIntegerEqual
,
490 WX_DECLARE_HASH_SET( wxString
, wxStringHash
, wxStringEqual
,
502 unsigned long operator()(const MyStruct
& s
) const
503 { return m_dummy(s
.ptr
); }
504 MyHash
& operator=(const MyHash
&) { return *this; }
506 wxPointerHash m_dummy
;
512 bool operator()(const MyStruct
& s1
, const MyStruct
& s2
) const
513 { return s1
.ptr
== s2
.ptr
; }
514 MyEqual
& operator=(const MyEqual
&) { return *this; }
517 WX_DECLARE_HASH_SET( MyStruct
, MyHash
, MyEqual
, mySet
);
519 typedef myTestHashSet5 wxStringHashSet
;
521 void HashesTestCase::wxHashSetTest()
523 wxStringHashSet set1
;
525 set1
.insert( wxT("abc") );
527 CPPUNIT_ASSERT( set1
.size() == 1 );
529 set1
.insert( wxT("bbc") );
530 set1
.insert( wxT("cbc") );
532 CPPUNIT_ASSERT( set1
.size() == 3 );
534 set1
.insert( wxT("abc") );
536 CPPUNIT_ASSERT( set1
.size() == 3 );
542 tmp
.ptr
= &dummy
; tmp
.str
= wxT("ABC");
544 tmp
.ptr
= &dummy
+ 1;
546 tmp
.ptr
= &dummy
; tmp
.str
= wxT("CDE");
549 CPPUNIT_ASSERT( set2
.size() == 2 );
551 mySet::iterator it
= set2
.find( tmp
);
553 CPPUNIT_ASSERT( it
!= set2
.end() );
554 CPPUNIT_ASSERT( it
->ptr
== &dummy
);
555 CPPUNIT_ASSERT( it
->str
== wxT("ABC") );