From d8b6309bdea56b9632b6c6414cf87a14da8450ee Mon Sep 17 00:00:00 2001 From: Michael Wetherell Date: Tue, 27 Sep 2005 19:11:38 +0000 Subject: [PATCH 1/1] Extend the hashmap tests to cover integer and pointer keys git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@35744 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- tests/hashes/hashes.cpp | 120 +++++++++++++++++++++++++++++++++++----- 1 file changed, 107 insertions(+), 13 deletions(-) diff --git a/tests/hashes/hashes.cpp b/tests/hashes/hashes.cpp index e33d6ff3b0..229703f9c5 100644 --- a/tests/hashes/hashes.cpp +++ b/tests/hashes/hashes.cpp @@ -2,9 +2,10 @@ // Name: tests/hashes/hashes.cpp // Purpose: wxHashTable, wxHashMap, wxHashSet unit test // Author: Vadim Zeitlin, Mattia Barbon +// Modified: Mike Wetherell // Created: 2004-05-16 // RCS-ID: $Id$ -// Copyright: (c) 2004 Vadim Zeitlin, Mattia Barbon +// Copyright: (c) 2004 Vadim Zeitlin, Mattia Barbon, 2005 M. Wetherell /////////////////////////////////////////////////////////////////////////////// // ---------------------------------------------------------------------------- @@ -25,6 +26,11 @@ #include "wx/hashmap.h" #include "wx/hashset.h" +#if defined wxLongLong_t && !defined wxLongLongIsLong && \ + (!defined __VISUALC__ || __VISUALC__ > 1100) // doesn't work on VC5 + #define TEST_LONGLONG +#endif + // -------------------------------------------------------------------------- // helper class for typed/untyped wxHashTable // -------------------------------------------------------------------------- @@ -67,14 +73,36 @@ private: CPPUNIT_TEST( wxHashTableTest ); CPPUNIT_TEST( wxUntypedHashTableDeleteContents ); CPPUNIT_TEST( wxTypedHashTableTest ); - CPPUNIT_TEST( wxHashMapTest ); + CPPUNIT_TEST( StringHashMapTest ); + CPPUNIT_TEST( PtrHashMapTest ); + CPPUNIT_TEST( LongHashMapTest ); + CPPUNIT_TEST( ULongHashMapTest ); + CPPUNIT_TEST( UIntHashMapTest ); + CPPUNIT_TEST( IntHashMapTest ); + CPPUNIT_TEST( ShortHashMapTest ); + CPPUNIT_TEST( UShortHashMapTest ); +#ifdef TEST_LONGLONG + CPPUNIT_TEST( LLongHashMapTest ); + CPPUNIT_TEST( ULLongHashMapTest ); +#endif CPPUNIT_TEST( wxHashSetTest ); CPPUNIT_TEST_SUITE_END(); void wxHashTableTest(); void wxUntypedHashTableDeleteContents(); void wxTypedHashTableTest(); - void wxHashMapTest(); + void StringHashMapTest(); + void PtrHashMapTest(); + void LongHashMapTest(); + void ULongHashMapTest(); + void UIntHashMapTest(); + void IntHashMapTest(); + void ShortHashMapTest(); + void UShortHashMapTest(); +#ifdef TEST_LONGLONG + void LLongHashMapTest(); + void ULLongHashMapTest(); +#endif void wxHashSetTest(); DECLARE_NO_COPY_CLASS(HashesTestCase) @@ -314,20 +342,72 @@ WX_DECLARE_HASH_MAP( unsigned short, unsigned, wxIntegerHash, wxIntegerEqual, // myStringHashMap ); WX_DECLARE_STRING_HASH_MAP(wxString, myStringHashMap); -typedef myStringHashMap::iterator Itor; +#ifdef TEST_LONGLONG + WX_DECLARE_HASH_MAP( wxLongLong_t, wxLongLong_t, + wxIntegerHash, wxIntegerEqual, myLLongHashMap ); + WX_DECLARE_HASH_MAP( wxULongLong_t, wxULongLong_t, + wxIntegerHash, wxIntegerEqual, myULLongHashMap ); +#endif + +// Helpers to generate a key value pair for item 'i', out of a total of 'count' +void MakeKeyValuePair(size_t i, size_t /*count*/, wxString& key, wxString& val) +{ + key.clear(); + key << i; + val = wxT("A") + key + wxT("C"); +} + +// for integral keys generate a range of keys that will use all the bits of +// the key type +template +IntT MakeKey(size_t i, size_t count) +{ + IntT max = 1; + max <<= sizeof(KeyT) * 8 - 2; + max -= count / 4 + 1; + + return max / count * 4 * i + i / 3; +} -void HashesTestCase::wxHashMapTest() +// make key/value pairs for integer types +template +void MakeKeyValuePair(size_t i, size_t count, KeyT& key, ValueT& value) { - myStringHashMap sh(0); // as small as possible - wxString buf; + key = MakeKey(i, count); + value = wx_truncate_cast(ValueT, key); +} + +// make key/values paris for pointer types +template +void MakeKeyValuePair(size_t i, size_t count, T*& key, ValueT& value) +{ + key = (T*)wxUIntToPtr(MakeKey(i, count)); + value = wx_truncate_cast(ValueT, key); +} + +// the test +template +void HashMapTest() +{ +#if wxUSE_STL && defined HAVE_STL_HASH_MAP + typedef typename HashMapT::value_type::second_type value_type; +#else + typedef typename HashMapT::value_type::t2 value_type; +#endif + typedef typename HashMapT::key_type key_type; + typedef typename HashMapT::iterator Itor; + + HashMapT sh(0); // as small as possible + key_type buf; + value_type value; size_t i; const size_t count = 10000; // init with some data for( i = 0; i < count; ++i ) { - buf.Printf(wxT("%d"), i ); - sh[buf] = wxT("A") + buf + wxT("C"); + MakeKeyValuePair(i, count, buf, value); + sh[buf] = value; } // test that insertion worked @@ -335,8 +415,8 @@ void HashesTestCase::wxHashMapTest() for( i = 0; i < count; ++i ) { - buf.Printf(wxT("%d"), i ); - CPPUNIT_ASSERT( sh[buf] == wxT("A") + buf + wxT("C") ); + MakeKeyValuePair(i, count, buf, value); + CPPUNIT_ASSERT( sh[buf] == value ); } // check that iterators work @@ -350,7 +430,7 @@ void HashesTestCase::wxHashMapTest() CPPUNIT_ASSERT( sh.size() == i ); // test copy ctor, assignment operator - myStringHashMap h1( sh ), h2( 0 ); + HashMapT h1( sh ), h2( 0 ); h2 = sh; for( i = 0, it = sh.begin(); it != sh.end(); ++it, ++i ) @@ -362,7 +442,7 @@ void HashesTestCase::wxHashMapTest() // other tests for( i = 0; i < count; ++i ) { - buf.Printf(wxT("%d"), i ); + MakeKeyValuePair(i, count, buf, value); size_t sz = sh.size(); // test find() and erase(it) @@ -388,6 +468,20 @@ void HashesTestCase::wxHashMapTest() } } +void HashesTestCase::StringHashMapTest() { HashMapTest(); } +void HashesTestCase::PtrHashMapTest() { HashMapTest(); } +void HashesTestCase::LongHashMapTest() { HashMapTest(); } +void HashesTestCase::ULongHashMapTest() { HashMapTest(); } +void HashesTestCase::UIntHashMapTest() { HashMapTest(); } +void HashesTestCase::IntHashMapTest() { HashMapTest(); } +void HashesTestCase::ShortHashMapTest() { HashMapTest(); } +void HashesTestCase::UShortHashMapTest() { HashMapTest(); } + +#ifdef TEST_LONGLONG +void HashesTestCase::LLongHashMapTest() { HashMapTest(); } +void HashesTestCase::ULLongHashMapTest() { HashMapTest(); } +#endif + // test compilation of basic set types WX_DECLARE_HASH_SET( int*, wxPointerHash, wxPointerEqual, myPtrHashSet ); WX_DECLARE_HASH_SET( long, wxIntegerHash, wxIntegerEqual, myLongHashSet ); -- 2.45.2