Update contrib.
1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test/buffer/t_hashtab.cpp
19 #include <e32hashtab.h>
22 #if defined(__VC32__) || defined(__CW32__)
23 typedef unsigned short wch;
29 RTest test(_L("T_HASHTAB"));
32 typedef TBuf8<80> TTestName;
33 typedef RHashSet<TInt> TIntSet;
34 typedef RHashSet<TTestName> TNameSet;
35 typedef RPtrHashSet<TDesC8> TStringSet8;
36 typedef RPtrHashSet<TDesC16> TStringSet16;
37 typedef RPtrHashMap<TDesC8,TDesC8> TStringMap8;
38 typedef RPtrHashMap<TDesC16,TDesC16> TStringMap16;
40 #define INTSET(x) TIntSet x
41 #define NAMESET(x) TNameSet x(&HashTestName, &TestNameIdentity)
42 #define STRSET8(x) TStringSet8 x
43 #define STRSET16(x) TStringSet16 x
44 #define STRMAP8(x) TStringMap8 x
45 #define STRMAP16(x) TStringMap16 x
47 RPointerArray<TDesC8> DesC8Array;
48 RPointerArray<TDesC16> DesC16Array;
50 class HashTest : public RHashTableBase
53 HashTest() : RHashTableBase(0,0,0,0) {}
54 using RHashTableBase::ConsistencyCheck;
55 using RHashTableBase::Count;
58 void CCheck(RHashTableBase& aHT)
63 HashTest* h = (HashTest*)&aHT;
64 h->ConsistencyCheck(&dc, &cmp, 1024, ci);
65 test.Printf(_L("Count = %d, Deleted = %d, Cmp = %d\n"), h->Count(), dc, cmp);
67 for (i=0; i<1024; ++i)
70 test.Printf(_L("%d chains of length %d\n"), ci[i], i);
74 const char* Numbers[] =
108 TTestName NumberInWords(const TInt& aNum)
114 n.Copy((const TUint8*)Numbers[a]);
121 n.Copy((const TUint8*)Numbers[tens-2+20]);
125 n.Append(TPtrC8((const TUint8*)Numbers[units]));
132 n.Copy((const TUint8*)Numbers[h]);
134 n.Append(TPtrC8((const TUint8*)Numbers[28]));
138 TTestName n2(NumberInWords(a));
139 n.Append(_L8(" and "));
145 n = NumberInWords(t);
147 n.Append(TPtrC8((const TUint8*)Numbers[29]));
151 TTestName n2(NumberInWords(a));
153 n.Append(_L8(" and "));
161 void PrintNumberInWords(TInt a)
164 TTestName n(NumberInWords(a));
166 test.Printf(_L("%d: %S\n"), a, &buf);
169 TUint32 HashTestName(const TTestName& aN)
171 return DefaultHash::Des8(aN);
174 TBool TestNameIdentity(const TTestName& aA, const TTestName& aB)
179 /******************************************************************************
180 * Utility functions for RHashSet<T>
181 ******************************************************************************/
183 void Union(RHashSet<T>& aD, const RHashSet<T>& aS)
187 TInt d0 = aD.Count();
189 typename RHashSet<T>::TIter iS(aS);
192 const T* p = iS.Next();
196 TInt r = aD.Insert(*p);
200 test(d1 == aD.Count());
205 void Subtract(RHashSet<T>& aD, const RHashSet<T>& aS)
209 TInt d0 = aD.Count();
212 typename RHashSet<T>::TIter iS(aS);
215 const T* p = iS.Next();
219 TInt r = aD.Remove(*p);
220 test(r==KErrNone || r==KErrNotFound);
226 test(d1 == aD.Count());
228 test( (d0-d1) + nfd == c);
232 void Intersect(RHashSet<T>& aD, const RHashSet<T>& aS)
234 typename RHashSet<T>::TIter iD(aD);
237 const T* p = iD.Next();
246 void CheckIdentical(const RHashSet<T>& aA, const RHashSet<T>& aB)
248 test(aA.Count()==aB.Count());
250 typename RHashSet<T>::TIter iA(aA);
253 const T* p = iA.Next();
257 test(aB.Find(*p)!=0);
261 typename RHashSet<T>::TIter iB(aB);
264 const T* p = iB.Next();
268 test(aA.Find(*p)!=0);
274 /******************************************************************************
275 * Utility functions for RPtrHashSet<T>
276 ******************************************************************************/
278 void Union(RPtrHashSet<T>& aD, const RPtrHashSet<T>& aS)
282 TInt d0 = aD.Count();
284 typename RPtrHashSet<T>::TIter iS(aS);
287 const T* p = iS.Next();
291 TInt r = aD.Insert(p);
295 test(d1 == aD.Count());
300 void Subtract(RPtrHashSet<T>& aD, const RPtrHashSet<T>& aS)
304 TInt d0 = aD.Count();
307 typename RPtrHashSet<T>::TIter iS(aS);
310 const T* p = iS.Next();
314 TInt r = aD.Remove(p);
315 test(r==KErrNone || r==KErrNotFound);
321 test(d1 == aD.Count());
323 test( (d0-d1) + nfd == c);
327 void Intersect(RPtrHashSet<T>& aD, const RPtrHashSet<T>& aS)
329 typename RPtrHashSet<T>::TIter iD(aD);
332 const T* p = iD.Next();
341 void CheckIdentical(const RPtrHashSet<T>& aA, const RPtrHashSet<T>& aB)
343 test(aA.Count()==aB.Count());
345 typename RPtrHashSet<T>::TIter iA(aA);
348 const T* p = iA.Next();
352 test(aB.Find(*p)!=0);
356 typename RPtrHashSet<T>::TIter iB(aB);
359 const T* p = iB.Next();
363 test(aA.Find(*p)!=0);
369 /******************************************************************************
370 * Utility functions for RHashMap<K,V>
371 ******************************************************************************/
372 template <class K, class V>
373 void UnionTransform(RHashMap<K,V>& aD, const RHashSet<K>& aS, V (*aTransform)(const K&) )
377 TInt d0 = aD.Count();
379 typename RHashSet<K>::TIter iS(aS);
382 const K* p = iS.Next();
386 TInt r = aD.Insert(*p, (*aTransform)(*p) );
390 test(d1 == aD.Count());
395 template <class K, class V>
396 void UnionMap(RHashSet<V>& aD, const RHashSet<K>& aS, const RHashMap<K,V>& aM)
400 TInt d0 = aD.Count();
402 typename RHashSet<K>::TIter iS(aS);
405 const K* p = iS.Next();
409 const V* q = aM.Find(*p);
412 TInt r = aD.Insert(*q);
416 test(d1 == aD.Count());
421 template <class K, class V>
422 void UnionKeys(RHashSet<K>& aD, const RHashMap<K,V>& aM)
424 typename RHashMap<K,V>::TIter iM(aM);
427 const K* p = iM.NextKey();
435 template <class K, class V>
436 void UnionValues(RHashSet<V>& aD, const RHashMap<K,V>& aM)
438 typename RHashMap<K,V>::TIter iM(aM);
441 const K* p = iM.NextKey();
444 const V* q = iM.CurrentValue();
450 template <class K, class V>
451 void UnionTransform(RHashMap<K,V>& aD, const RHashMap<K,V>& aS, V (*aTransform)(const V&) )
455 TInt d0 = aD.Count();
457 typename RHashMap<K,V>::TIter iS(aS);
460 const K* p = iS.NextKey();
464 TInt r = aD.Insert(*p, (*aTransform)(*iS.CurrentValue()) );
468 test(d1 == aD.Count());
473 template <class K, class V>
474 void UnionInverse(RHashMap<V,K>& aD, const RHashMap<K,V>& aS)
478 TInt d0 = aD.Count();
480 typename RHashMap<K,V>::TIter iS(aS);
483 const K* p = iS.NextKey();
487 const V* q = iS.CurrentValue();
488 TInt r = aD.Insert(*q, *p);
492 test(d1 == aD.Count());
496 template <class K, class V>
497 void SetMap(RHashMap<V,K>& aM, const V& aV)
499 typename RHashMap<K,V>::TIter iM(aM);
502 const K* p = iM.NextKey();
505 V* q = iM.CurrentValue();
512 /******************************************************************************
513 * Utility functions for RPtrHashMap<K,V>
514 ******************************************************************************/
515 template <class K, class V>
516 void UnionTransform(RPtrHashMap<K,V>& aD, const RPtrHashMap<K,V>& aS, V* (*aTransform)(const V*) )
520 TInt d0 = aD.Count();
522 typename RPtrHashMap<K,V>::TIter iS(aS);
525 const K* p = iS.NextKey();
529 TInt r = aD.Insert(p, (*aTransform)(iS.CurrentValue()) );
533 test(d1 == aD.Count());
538 template <class A, class B>
539 void UnionInverse(RPtrHashMap<B,A>& aD, const RPtrHashMap<A,B>& a1)
541 typename RPtrHashMap<A,B>::TIter iter(a1);
543 while ( (pA=iter.NextKey()) != 0 )
545 const B* pB = iter.CurrentValue();
546 TInt r = aD.Insert(pB, pA);
552 template <class A, class B, class C>
553 void UnionCompose(RPtrHashMap<A,C>& aD, const RPtrHashMap<A,B>& a1, const RPtrHashMap<B,C>& a2)
555 typename RPtrHashMap<A,B>::TIter iter(a1);
557 while ( (pA=iter.NextKey()) != 0 )
559 const B* pB = iter.CurrentValue();
560 const C* pC = a2.Find(*pB);
563 TInt r = aD.Insert(pA, pC);
570 template <class K, class V>
571 void CheckIdenticalMaps(const RPtrHashMap<K,V>& aA, const RPtrHashMap<K,V>& aB)
573 test(aA.Count()==aB.Count());
577 typename RPtrHashMap<K,V>::TIter iA(aA);
578 while( (p=iA.NextKey()) != 0)
582 test(q && q==iA.CurrentValue());
586 typename RPtrHashMap<K,V>::TIter iB(aB);
587 while( (p=iB.NextKey()) != 0)
591 test(q && q==iB.CurrentValue());
600 /******************************************************************************
602 ******************************************************************************/
603 void Populate(TIntSet& aH, TInt aS, TInt aE, TInt aM)
610 void PrimeSieve(TIntSet& aH, TInt aStart, TInt aEnd)
613 TInt e = (aEnd&1) ? aEnd : aEnd-1;
614 TInt s = (aStart<2) ? 2 : aStart|1;
617 // test.Printf(_L("add %d\n"),x);
623 for (; m*m<=e && !aH.Find(m); ++m)
627 for (x=m*2; x<=e; x+=m)
629 // test.Printf(_L("remove %d\n"),x);
636 TBool IsPrime(TInt x)
643 for (d=3; d*d<=x; d+=2)
651 void CheckSieveOutput(TIntSet& aH)
653 RHashSet<TInt>::TIter iter(aH);
657 test.Printf(_L("%d elements:\n"),aH.Count());
660 const TInt* p = iter.Next();
666 // test.Printf(_L("%d\n"),*p);
676 for (x=min; x<=max; x+=2)
681 for (x=min; x<=max; x+=2)
684 test(aH.Find(x)==&aH.FindL(x));
687 TRAPD(rr,aH.FindL(x));
688 test(rr==KErrNotFound);
693 TUint32 CheckSmallHashSet(const TIntSet& aA)
696 RHashSet<TInt>::TIter iter(aA);
699 const TInt* p = iter.Next();
707 void AddToSmallHashSet(TIntSet& aA, TUint32 aMask)
709 CheckSmallHashSet(aA);
722 void RemoveFromSmallHashSet(TIntSet& aA, TUint32 aMask)
724 TUint32 m = CheckSmallHashSet(aA);
735 test(r==KErrNotFound);
742 test.Next(_L("Test RHashSet"));
745 CCheck(hs); // check consistency for empty table
748 PrimeSieve(hs, 1, 100);
749 CheckSieveOutput(hs);
750 test(hs.Reserve(1000)==KErrNone);
752 CheckSieveOutput(hs); // check that Reserve() preserves existing entries
759 Populate(m1,2,100,1);
760 Populate(m2,4,100,2);
761 Populate(m3,6,100,3);
762 Populate(m5,10,100,5);
763 Populate(m7,14,100,7);
769 CheckSieveOutput(hs3);
770 CheckIdentical(hs,hs3);
789 CheckSieveOutput(hs3);
790 CheckIdentical(hs,hs3);
804 PrimeSieve(hs2, 1, 1000);
805 CheckSieveOutput(hs2);
806 test(hs2.Reserve(1000)==KErrNone);
808 CheckSieveOutput(hs2); // check that Reserve() preserves existing entries
810 PrimeSieve(hs, 100, 997);
811 CheckSieveOutput(hs);
813 CheckIdentical(hs,hs2);
817 CheckIdentical(hs,hs2);
818 CheckSieveOutput(hs2);
823 test(CheckSmallHashSet(hs)==0);
824 AddToSmallHashSet(hs, 1);
825 test(CheckSmallHashSet(hs)==1);
826 AddToSmallHashSet(hs, 4);
827 test(CheckSmallHashSet(hs)==5);
828 AddToSmallHashSet(hs, 0x80000001);
829 test(CheckSmallHashSet(hs)==0x80000005);
830 AddToSmallHashSet(hs, 0x317217f8);
831 test(CheckSmallHashSet(hs)==0xb17217fd);
832 RemoveFromSmallHashSet(hs, 0x00000007);
833 test(CheckSmallHashSet(hs)==0xb17217f8);
834 RemoveFromSmallHashSet(hs, 0xffffffff);
835 test(CheckSmallHashSet(hs)==0);
841 test.Next(_L("Test iterators"));
843 RHashSet<TInt> hs; // empty
844 RHashSet<TInt>::TIter iter(hs);
845 test(iter.Next() == 0);
846 test(iter.Current() == 0);
847 iter.RemoveCurrent();
848 test(iter.Next() == 0);
849 test(iter.Current() == 0);
851 test(hs.Insert(1) == KErrNone);
852 test(hs.Insert(2) == KErrNone);
853 test(hs.Insert(3) == KErrNone);
859 const TInt* p = iter.Next();
862 test (x>=1 && x<=3 && (mask&(1<<x))!=0);
865 test(iter.Next() == 0);
867 test(CheckSmallHashSet(hs)==0x0e);
868 test(hs.Insert(4) == KErrNone);
869 test(hs.Insert(5) == KErrNone);
870 test(hs.Insert(6) == KErrNone);
871 test(hs.Insert(7) == KErrNone);
872 test(CheckSmallHashSet(hs)==0xfe);
876 if ((*iter.Current() & 1) == 0)
877 iter.RemoveCurrent();
879 test(CheckSmallHashSet(hs)==0xaa);
883 iter.RemoveCurrent();
885 test(CheckSmallHashSet(hs)==0);
887 test(iter.Next() == 0);
888 test(iter.Current() == 0);
889 RHashSet<TInt> empty;
890 CheckIdentical(hs, empty);
893 test(empty.Insert(1)==KErrNoMemory);
894 test(empty.Insert(1)==KErrNone);
897 test(hs.Insert(1)==KErrNone);
900 test(hs.Insert(1)==KErrNoMemory);
905 void Print(const char* aTitle, const RHashMap<TInt,TTestName>& aM)
908 buf.Copy(TPtrC8((const TUint8*)aTitle));
909 test.Printf(_L("%S\n"), &buf);
910 RHashMap<TInt,TTestName>::TIter iter(aM);
913 const TInt* p = iter.NextKey();
915 buf.Copy(*iter.CurrentValue());
916 test.Printf(_L("%d: %S\n"), *p, &buf);
920 void Print(const char* aTitle, const RHashSet<TTestName>& aS)
923 buf.Copy(TPtrC8((const TUint8*)aTitle));
924 test.Printf(_L("%S\n"), &buf);
925 RHashSet<TTestName>::TIter iter(aS);
930 buf.Copy(*iter.Current());
931 test.Printf(_L("%S\n"), &buf);
937 test.Next(_L("Test RHashMap"));
939 RHashMap<TInt,TInt> ht;
940 CCheck(ht); // check consistency for empty table
942 for (x=0; x<200; x++)
944 TInt r = ht.Insert(x*x, x);
947 test(ht.Count()==200);
951 for (x=0; x<40000; x++)
953 const TInt* e = ht.Find(x);
956 const TInt& ee = ht.FindL(x);
959 // test.Printf(_L("Find(%d) -> %d\n"), x, y);
966 TRAPD(rr, ht.FindL(x));
967 test(rr==KErrNotFound);
972 for (x=0; x<200; x++)
974 TInt r = ht.Insert(x*x*x, x);
977 test(ht.Count()==200*2-6);
982 for (x=0; x<8000000; x++)
984 const TInt* e = ht.Find(x);
987 const TInt& ee = ht.FindL(x);
990 // test.Printf(_L("Find(%d) -> %d\n"), x, y);
1006 for (x=0; x<200; x++)
1008 TInt r = ht.Remove(x*x);
1011 test(ht.Count()==200-6);
1015 for (x=2; x<8000000; x++)
1017 const TInt* e = ht.Find(x);
1020 const TInt& ee = ht.FindL(x);
1023 // test.Printf(_L("Find(%d) -> %d\n"), x, y);
1024 if (cb == 4 || cb == 9 || cb == 16 || cb == 25) ++cb;
1025 test(x == cb*cb*cb);
1031 SetMap<TInt,TInt>(ht, 17);
1035 PrintNumberInWords(2000);
1036 PrintNumberInWords(2001);
1037 PrintNumberInWords(131072);
1038 PrintNumberInWords(111111);
1039 PrintNumberInWords(524288);
1042 Populate(all,0,1000,1);
1043 RHashMap<TInt,TTestName> to_words(&DefaultHash::Integer, &DefaultIdentity::Integer);
1044 UnionTransform<TInt,TTestName>(to_words, all, &NumberInWords);
1045 RHashMap<TTestName,TInt> from_words(&HashTestName, &TestNameIdentity);
1046 UnionInverse<TInt,TTestName>(from_words, to_words);
1047 // Print("TO WORDS:", to_words);
1049 PrimeSieve(primes, 1, 100);
1050 // Print("Primes 1-100", primes);
1051 RHashMap<TInt,TTestName> prime_map(&DefaultHash::Integer, &DefaultIdentity::Integer);
1052 UnionTransform<TInt,TTestName>(prime_map, primes, &NumberInWords);
1053 // Print("Prime map 1-100", prime_map);
1055 UnionKeys<TInt,TTestName>(pmkeys, prime_map);
1058 UnionValues<TInt,TTestName>(pmval, prime_map);
1059 CheckIdentical(pmkeys, primes);
1061 UnionMap<TTestName,TInt>(pr2, pmval, from_words);
1062 CheckIdentical(pr2, primes);
1064 Union(pmval2, pmval);
1065 // Print("pmval",pmval);
1066 // Print("pmval2",pmval2);
1067 CheckIdentical(pmval2, pmval);
1068 UnionMap<TTestName,TInt>(pr2, pmval2, from_words);
1069 CheckIdentical(pr2, primes);
1080 Populate(all,2,1000,1);
1084 UnionMap<TInt,TTestName>(pr3, all, to_words);
1088 Populate(m,4,1000,2);
1089 UnionMap<TInt,TTestName>(nm, m, to_words);
1094 Populate(m,6,1000,3);
1095 UnionMap<TInt,TTestName>(nm, m, to_words);
1100 Populate(m,10,1000,5);
1101 UnionMap<TInt,TTestName>(nm, m, to_words);
1106 Populate(m,14,1000,7);
1107 UnionMap<TInt,TTestName>(nm, m, to_words);
1112 Populate(m,22,1000,11);
1113 UnionMap<TInt,TTestName>(nm, m, to_words);
1118 Populate(m,26,1000,13);
1119 UnionMap<TInt,TTestName>(nm, m, to_words);
1124 Populate(m,34,1000,17);
1125 UnionMap<TInt,TTestName>(nm, m, to_words);
1130 Populate(m,38,1000,19);
1131 UnionMap<TInt,TTestName>(nm, m, to_words);
1136 Populate(m,46,1000,23);
1137 UnionMap<TInt,TTestName>(nm, m, to_words);
1142 Populate(m,58,1000,29);
1143 UnionMap<TInt,TTestName>(nm, m, to_words);
1148 Populate(m,62,1000,31);
1149 UnionMap<TInt,TTestName>(nm, m, to_words);
1154 // Print("pr3",pr3);
1155 PrimeSieve(primes, 1, 1000);
1156 UnionMap<TInt,TTestName>(nm, primes, to_words);
1157 CheckIdentical(nm, pr3);
1159 UnionMap<TTestName,TInt>(m, pr3, from_words);
1160 CheckIdentical(m, primes);
1170 void PopulateArray8(TInt aMax)
1173 for (i=0; i<aMax; ++i)
1175 TTestName n(NumberInWords(i));
1176 HBufC8* p = n.Alloc();
1178 TInt r = DesC8Array.Append(p);
1183 void PopulateArray16(TInt aMax)
1186 for (i=0; i<aMax; ++i)
1188 TTestName n(NumberInWords(i));
1191 HBufC16* p = n16.Alloc();
1193 TInt r = DesC16Array.Append(p);
1198 TUint32 istrh8(const TDesC8* const & a)
1200 return DefaultHash::Des8(*a);
1203 TBool istrid8(const TDesC8* const & aA, const TDesC8* const & aB)
1208 TUint32 istrh16(const TDesC16* const & a)
1210 return DefaultHash::Des16(*a);
1213 TBool istrid16(const TDesC16* const & aA, const TDesC16* const & aB)
1218 void TestPtrHashSet()
1220 test.Next(_L("Test RPtrHashSet"));
1223 CCheck(s); // check consistency for empty table
1224 RHashMap<const TDesC8*, TInt> hm(&istrh8, &istrid8);
1225 RPtrHashSet<TDesC8>::TIter iter(s);
1228 for (i=0; i<4096; ++i)
1230 TInt r = s.Insert(DesC8Array[i]);
1232 r = hm.Insert(DesC8Array[i], i);
1236 for (i=0; i<4100; ++i)
1238 TTestName n(NumberInWords(i));
1239 const TDesC8* p = s.Find(n);
1242 const TDesC8& pp = s.FindL(n);
1243 test(p && *p==n && p==DesC8Array[i]);
1249 TRAPD(rr,s.FindL(n));
1250 test(rr==KErrNotFound);
1253 while((q=iter.Next())!=0)
1255 const TInt* qi = hm.Find(q);
1256 test(qi!=0 && *qi>=0 && *qi<4096);
1257 test(DesC8Array[*qi]==q);
1259 for (i=0; i<4100; i+=2)
1261 TTestName n(NumberInWords(i));
1262 TInt r = s.Remove(&n);
1266 test(r==KErrNotFound);
1268 test(s.Count()==2048);
1270 for (i=0; i<4100; ++i)
1272 TTestName n(NumberInWords(i));
1273 const TDesC8* p = s.Find(n);
1274 if (i<4096 && (i&1))
1275 test(p && *p==n && p==DesC8Array[i]);
1280 while((q=iter.Next())!=0)
1282 const TInt* qi = hm.Find(q);
1283 test(qi!=0 && *qi>=0 && *qi<4096 && (*qi&1));
1284 test(DesC8Array[*qi]==q);
1286 for (i=2; i<4096; i+=4)
1288 TInt r = s.Insert(DesC8Array[i]);
1291 test(s.Count()==3072);
1293 for (i=0; i<4100; ++i)
1295 TTestName n(NumberInWords(i));
1296 const TDesC8* p = s.Find(n);
1297 if (i<4096 && (i&3))
1298 test(p && *p==n && p==DesC8Array[i]);
1303 while((q=iter.Next())!=0)
1305 const TInt* qi = hm.Find(q);
1306 test(qi!=0 && *qi>=0 && *qi<4096 && (*qi&3));
1307 test(DesC8Array[*qi]==q);
1311 // test ResetAndDestroy
1312 for (i=0; i<16; ++i)
1314 TInt r = s.Insert(DesC8Array[i]->Alloc());
1318 while((q=iter.Next())!=0)
1320 const TInt* qi = hm.Find(q);
1321 test(qi!=0 && *qi>=0 && *qi<16);
1322 test(*DesC8Array[*qi]==*q);
1323 test(DesC8Array[*qi]!=q);
1325 s.ResetAndDestroy();
1330 RHashMap<const TDesC16*, TInt> HM(&istrh16, &istrid16);
1331 RPtrHashSet<TDesC16>::TIter ITER(S);
1333 for (i=0; i<4096; ++i)
1335 TInt r = S.Insert(DesC16Array[i]);
1337 r = HM.Insert(DesC16Array[i], i);
1341 for (i=0; i<4100; ++i)
1343 TTestName n(NumberInWords(i));
1346 const TDesC16* p = S.Find(buf);
1348 test(p && *p==buf && p==DesC16Array[i]);
1352 while((Q=ITER.Next())!=0)
1354 const TInt* qi = HM.Find(Q);
1355 test(qi!=0 && *qi>=0 && *qi<4096);
1356 test(DesC16Array[*qi]==Q);
1358 for (i=0; i<4100; i+=2)
1360 TTestName n(NumberInWords(i));
1363 TInt r = S.Remove(&buf);
1367 test(r==KErrNotFound);
1369 test(S.Count()==2048);
1371 for (i=0; i<4100; ++i)
1373 TTestName n(NumberInWords(i));
1376 const TDesC16* p = S.Find(buf);
1377 if (i<4096 && (i&1))
1378 test(p && *p==buf && p==DesC16Array[i]);
1383 while((Q=ITER.Next())!=0)
1385 const TInt* qi = HM.Find(Q);
1386 test(qi!=0 && *qi>=0 && *qi<4096 && (*qi&1));
1387 test(DesC16Array[*qi]==Q);
1389 for (i=2; i<4096; i+=4)
1391 TInt r = S.Insert(DesC16Array[i]);
1394 test(S.Count()==3072);
1396 for (i=0; i<4100; ++i)
1398 TTestName n(NumberInWords(i));
1401 const TDesC16* p = S.Find(buf);
1402 if (i<4096 && (i&3))
1403 test(p && *p==buf && p==DesC16Array[i]);
1408 while((Q=ITER.Next())!=0)
1410 const TInt* qi = HM.Find(Q);
1411 test(qi!=0 && *qi>=0 && *qi<4096 && (*qi&3));
1412 test(DesC16Array[*qi]==Q);
1416 // test ResetAndDestroy
1417 for (i=0; i<16; ++i)
1419 TInt r = S.Insert(DesC16Array[i]->Alloc());
1423 while((Q=ITER.Next())!=0)
1425 const TInt* qi = HM.Find(Q);
1426 test(qi!=0 && *qi>=0 && *qi<16);
1427 test(*DesC16Array[*qi]==*Q);
1428 test(DesC16Array[*qi]!=Q);
1430 S.ResetAndDestroy();
1434 void TestPtrHashMap()
1436 test.Next(_L("Test RPtrHashMap"));
1439 CCheck(map); // check consistency for empty table
1442 for (i=0; i<4096; ++i)
1444 TInt r = map.Insert(DesC8Array[i], DesC8Array[(i+1)&0xfff]);
1446 r = id.Insert(DesC8Array[i], DesC8Array[i]);
1449 for (i=0; i<4096; ++i)
1451 const TDesC8* p = map.Find(*DesC8Array[i]);
1452 test(p == DesC8Array[(i+1)&0xfff]);
1453 const TDesC8& pp = map.FindL(*DesC8Array[i]);
1456 TRAPD(rr,map.FindL(_L8("two")));
1458 TRAP(rr,map.FindL(_L8("twx")));
1459 test(rr==KErrNotFound);
1464 UnionInverse<TDesC8,TDesC8>(mapi, map);
1465 UnionCompose<TDesC8,TDesC8,TDesC8>(map2, map, map);
1466 UnionCompose<TDesC8,TDesC8,TDesC8>(map3, map, mapi);
1467 CheckIdenticalMaps<TDesC8,TDesC8>(map3, id);
1469 UnionCompose<TDesC8,TDesC8,TDesC8>(map3, map2, mapi);
1470 CheckIdenticalMaps<TDesC8,TDesC8>(map3, map);
1472 for (i=0; i<4096; ++i)
1474 TInt r = map3.Insert(DesC8Array[i], DesC8Array[(i+2)&0xfff]);
1477 CheckIdenticalMaps<TDesC8,TDesC8>(map3, map2);
1485 // test ResetAndDestroy()
1488 TInt r = map.Insert(DesC8Array[i]->Alloc(), DesC8Array[i*i]->Alloc());
1491 map.ResetAndDestroy();
1496 // Max out memory and check it still works
1497 test.Next(_L("Test OOM"));
1511 test(r==KErrNoMemory);
1512 TRAPD(rr,set.InsertL(x));
1513 test(rr==KErrNoMemory);
1514 test.Printf(_L("%d integers stored\n"), n);
1515 test(set.Count()==n);
1516 TRAP(rr,set.InsertL(0x58b90bfb)); // already there
1517 test(rr==KErrNone); // should succeed
1519 // final count should be a power of 2 minus 1
1520 test( (n&(n+1)) == 0 );
1524 for (i=0; i<=n; ++i) // check everything has been stored correctly
1527 const TInt* p = set.Find(x);
1535 for (nn=256; nn<=n+256; nn+=256)
1537 r = set.Reserve(nn);
1542 test.Printf(_L("Max reserve %d\n"),nn);
1543 TInt thresh = 3*((n+1)>>2);
1544 test(nn == thresh + 256);
1547 class RDummyAllocator : public RAllocator
1550 virtual TAny* Alloc(TInt) {test(0); return 0;}
1551 virtual void Free(TAny*) {test(0);}
1552 virtual TAny* ReAlloc(TAny*, TInt, TInt) {test(0); return 0;}
1553 virtual TInt AllocLen(const TAny*) const {test(0); return 0;}
1554 virtual TInt Compress() {test(0); return 0;}
1555 virtual void Reset() {test(0);}
1556 virtual TInt AllocSize(TInt&) const {test(0); return 0;}
1557 virtual TInt Available(TInt&) const {test(0); return 0;}
1558 virtual TInt DebugFunction(TInt, TAny*, TAny*) {test(0); return 0;}
1559 virtual TInt Extension_(TUint, TAny*&, TAny*) {test(0); return 0;}
1562 void IntegerBenchmark(TInt aCount, TBool aReserve)
1565 TUint32 before, after, diff;
1570 test.Printf(_L("**** INTEGER BENCHMARKS ***\n"));
1574 before = User::NTickCount();
1575 for (i=0; i<aCount; ++i)
1578 TInt r = array.InsertInOrder(x);
1581 after = User::NTickCount();
1582 diff = after - before;
1583 diff *= NanoTickPeriod;
1584 avg = (double)diff / (double)aCount;
1585 test.Printf(_L("ARRAY: %d insertions take %dus (%.2gus each)\n"), aCount, diff, avg);
1588 before = User::NTickCount();
1589 for (i=0; i<aCount; ++i)
1592 TInt r = array.FindInOrder(x);
1595 after = User::NTickCount();
1596 diff = after - before;
1597 diff *= NanoTickPeriod;
1598 avg = (double)diff / (double)aCount;
1599 test.Printf(_L("ARRAY: %d successful finds take %dus (%.2gus each)\n"), aCount, diff, avg);
1601 before = User::NTickCount();
1602 for (i=0; i<aCount; ++i)
1605 TInt r = array.FindInOrder(x);
1606 test(r==KErrNotFound);
1608 after = User::NTickCount();
1609 diff = after - before;
1610 diff *= NanoTickPeriod;
1611 avg = (double)diff / (double)aCount;
1612 test.Printf(_L("ARRAY: %d unsuccessful finds take %dus (%.2gus each)\n"), aCount, diff, avg);
1615 before = User::NTickCount();
1616 for (i=0; i<aCount; ++i)
1619 TInt r = array.FindInOrder(x);
1623 after = User::NTickCount();
1624 diff = after - before;
1625 diff *= NanoTickPeriod;
1626 avg = (double)diff / (double)aCount;
1627 test.Printf(_L("ARRAY: %d deletions take %dus (%.2gus each)\n"), aCount, diff, avg);
1637 test(set.Reserve(aCount)==KErrNone);
1638 pA = User::SwitchAllocator(&da); // check that no memory accesses occur
1639 test(set.Reserve(10)==KErrNone); // shouldn't need to do anything
1640 test(set.Reserve(aCount/2)==KErrNone); // shouldn't need to do anything
1641 test(set.Reserve(aCount)==KErrNone); // shouldn't need to do anything
1643 before = User::NTickCount();
1644 for (i=0; i<aCount; ++i)
1647 TInt r = set.Insert(x); // we have no heap here so this tests that Reserve() has preallocated sufficient memory
1650 after = User::NTickCount();
1651 diff = after - before;
1652 diff *= NanoTickPeriod;
1653 avg = (double)diff / (double)aCount;
1655 User::SwitchAllocator(pA);
1656 test.Printf(_L("HASH TABLE: %d insertions take %dus (%.2gus each)\n"), aCount, diff, avg);
1659 before = User::NTickCount();
1660 for (i=0; i<aCount; ++i)
1663 const TInt* p = set.Find(x);
1666 after = User::NTickCount();
1667 diff = after - before;
1668 diff *= NanoTickPeriod;
1669 avg = (double)diff / (double)aCount;
1670 test.Printf(_L("HASH TABLE: %d successful finds take %dus (%.2gus each)\n"), aCount, diff, avg);
1672 before = User::NTickCount();
1673 for (i=0; i<aCount; ++i)
1676 const TInt* p = set.Find(x);
1679 after = User::NTickCount();
1680 diff = after - before;
1681 diff *= NanoTickPeriod;
1682 avg = (double)diff / (double)aCount;
1683 test.Printf(_L("HASH TABLE: %d unsuccessful finds take %dus (%.2gus each)\n"), aCount, diff, avg);
1686 before = User::NTickCount();
1687 for (i=0; i<aCount; ++i)
1690 TInt r = set.Remove(x);
1693 after = User::NTickCount();
1694 diff = after - before;
1695 diff *= NanoTickPeriod;
1696 avg = (double)diff / (double)aCount;
1697 test.Printf(_L("HASH TABLE: %d deletions take %dus (%.2gus each)\n"), aCount, diff, avg);
1701 TInt des8order(const TDesC8& aA, const TDesC8& aB)
1703 return aA.Compare(aB);
1706 void StringBenchmark8(TInt aCount, TBool aReserve)
1708 RPointerArray<TDesC8> array;
1709 TUint32 before, after, diff;
1713 const TDesC8** base = (const TDesC8**)&DesC8Array[0];
1714 test(base[1331] == DesC8Array[1331]);
1716 test.Printf(_L("**** 8 BIT STRING BENCHMARKS ***\n"));
1720 before = User::NTickCount();
1721 for (i=0; i<aCount; ++i)
1723 x = (x+4339)&0x3fff;
1724 TInt r = array.InsertInOrder(base[x], &des8order);
1727 after = User::NTickCount();
1728 diff = after - before;
1729 diff *= NanoTickPeriod;
1730 avg = (double)diff / (double)aCount;
1731 test.Printf(_L("ARRAY: %d insertions take %dus (%.2gus each)\n"), aCount, diff, avg);
1734 before = User::NTickCount();
1735 for (i=0; i<aCount; ++i)
1737 x = (x+4339)&0x3fff;
1738 TInt r = array.FindInOrder(base[x], &des8order);
1741 after = User::NTickCount();
1742 diff = after - before;
1743 diff *= NanoTickPeriod;
1744 avg = (double)diff / (double)aCount;
1745 test.Printf(_L("ARRAY: %d successful finds take %dus (%.2gus each)\n"), aCount, diff, avg);
1747 before = User::NTickCount();
1748 for (i=0; i<aCount; ++i)
1750 x = (x+4339)&0x3fff;
1751 TInt r = array.FindInOrder(base[x], &des8order);
1752 test(r==KErrNotFound);
1754 after = User::NTickCount();
1755 diff = after - before;
1756 diff *= NanoTickPeriod;
1757 avg = (double)diff / (double)aCount;
1758 test.Printf(_L("ARRAY: %d unsuccessful finds take %dus (%.2gus each)\n"), aCount, diff, avg);
1761 before = User::NTickCount();
1762 for (i=0; i<aCount; ++i)
1764 x = (x+4339)&0x3fff;
1765 TInt r = array.FindInOrder(base[x], &des8order);
1769 after = User::NTickCount();
1770 diff = after - before;
1771 diff *= NanoTickPeriod;
1772 avg = (double)diff / (double)aCount;
1773 test.Printf(_L("ARRAY: %d deletions take %dus (%.2gus each)\n"), aCount, diff, avg);
1780 test(set.Reserve(aCount)==KErrNone);
1781 before = User::NTickCount();
1782 for (i=0; i<aCount; ++i)
1784 x = (x+4339)&0x3fff;
1785 TInt r = set.Insert(base[x]);
1788 after = User::NTickCount();
1789 diff = after - before;
1790 diff *= NanoTickPeriod;
1791 avg = (double)diff / (double)aCount;
1792 test.Printf(_L("HASH TABLE: %d insertions take %dus (%.2gus each)\n"), aCount, diff, avg);
1795 before = User::NTickCount();
1796 for (i=0; i<aCount; ++i)
1798 x = (x+4339)&0x3fff;
1799 const TDesC8* p = set.Find(*base[x]);
1802 after = User::NTickCount();
1803 diff = after - before;
1804 diff *= NanoTickPeriod;
1805 avg = (double)diff / (double)aCount;
1806 test.Printf(_L("HASH TABLE: %d successful finds take %dus (%.2gus each)\n"), aCount, diff, avg);
1808 before = User::NTickCount();
1809 for (i=0; i<aCount; ++i)
1811 x = (x+4339)&0x3fff;
1812 const TDesC8* p = set.Find(*base[x]);
1815 after = User::NTickCount();
1816 diff = after - before;
1817 diff *= NanoTickPeriod;
1818 avg = (double)diff / (double)aCount;
1819 test.Printf(_L("HASH TABLE: %d unsuccessful finds take %dus (%.2gus each)\n"), aCount, diff, avg);
1822 before = User::NTickCount();
1823 for (i=0; i<aCount; ++i)
1825 x = (x+4339)&0x3fff;
1826 TInt r = set.Remove(base[x]);
1829 after = User::NTickCount();
1830 diff = after - before;
1831 diff *= NanoTickPeriod;
1832 avg = (double)diff / (double)aCount;
1833 test.Printf(_L("HASH TABLE: %d deletions take %dus (%.2gus each)\n"), aCount, diff, avg);
1837 TInt des16order(const TDesC16& aA, const TDesC16& aB)
1839 return aA.Compare(aB);
1842 void StringBenchmark16(TInt aCount, TBool aReserve)
1844 RPointerArray<TDesC16> array;
1845 TUint32 before, after, diff;
1849 const TDesC16** base = (const TDesC16**)&DesC16Array[0];
1850 test(base[1331] == DesC16Array[1331]);
1852 test.Printf(_L("**** 16 BIT STRING BENCHMARKS ***\n"));
1856 before = User::NTickCount();
1857 for (i=0; i<aCount; ++i)
1859 x = (x+4339)&0x3fff;
1860 TInt r = array.InsertInOrder(base[x], &des16order);
1863 after = User::NTickCount();
1864 diff = after - before;
1865 diff *= NanoTickPeriod;
1866 avg = (double)diff / (double)aCount;
1867 test.Printf(_L("ARRAY: %d insertions take %dus (%.2gus each)\n"), aCount, diff, avg);
1870 before = User::NTickCount();
1871 for (i=0; i<aCount; ++i)
1873 x = (x+4339)&0x3fff;
1874 TInt r = array.FindInOrder(base[x], &des16order);
1877 after = User::NTickCount();
1878 diff = after - before;
1879 diff *= NanoTickPeriod;
1880 avg = (double)diff / (double)aCount;
1881 test.Printf(_L("ARRAY: %d successful finds take %dus (%.2gus each)\n"), aCount, diff, avg);
1883 before = User::NTickCount();
1884 for (i=0; i<aCount; ++i)
1886 x = (x+4339)&0x3fff;
1887 TInt r = array.FindInOrder(base[x], &des16order);
1888 test(r==KErrNotFound);
1890 after = User::NTickCount();
1891 diff = after - before;
1892 diff *= NanoTickPeriod;
1893 avg = (double)diff / (double)aCount;
1894 test.Printf(_L("ARRAY: %d unsuccessful finds take %dus (%.2gus each)\n"), aCount, diff, avg);
1897 before = User::NTickCount();
1898 for (i=0; i<aCount; ++i)
1900 x = (x+4339)&0x3fff;
1901 TInt r = array.FindInOrder(base[x], &des16order);
1905 after = User::NTickCount();
1906 diff = after - before;
1907 diff *= NanoTickPeriod;
1908 avg = (double)diff / (double)aCount;
1909 test.Printf(_L("ARRAY: %d deletions take %dus (%.2gus each)\n"), aCount, diff, avg);
1916 test(set.Reserve(aCount)==KErrNone);
1917 before = User::NTickCount();
1918 for (i=0; i<aCount; ++i)
1920 x = (x+4339)&0x3fff;
1921 TInt r = set.Insert(base[x]);
1924 after = User::NTickCount();
1925 diff = after - before;
1926 diff *= NanoTickPeriod;
1927 avg = (double)diff / (double)aCount;
1928 test.Printf(_L("HASH TABLE: %d insertions take %dus (%.2gus each)\n"), aCount, diff, avg);
1931 before = User::NTickCount();
1932 for (i=0; i<aCount; ++i)
1934 x = (x+4339)&0x3fff;
1935 const TDesC16* p = set.Find(*base[x]);
1938 after = User::NTickCount();
1939 diff = after - before;
1940 diff *= NanoTickPeriod;
1941 avg = (double)diff / (double)aCount;
1942 test.Printf(_L("HASH TABLE: %d successful finds take %dus (%.2gus each)\n"), aCount, diff, avg);
1944 before = User::NTickCount();
1945 for (i=0; i<aCount; ++i)
1947 x = (x+4339)&0x3fff;
1948 const TDesC16* p = set.Find(*base[x]);
1951 after = User::NTickCount();
1952 diff = after - before;
1953 diff *= NanoTickPeriod;
1954 avg = (double)diff / (double)aCount;
1955 test.Printf(_L("HASH TABLE: %d unsuccessful finds take %dus (%.2gus each)\n"), aCount, diff, avg);
1958 before = User::NTickCount();
1959 for (i=0; i<aCount; ++i)
1961 x = (x+4339)&0x3fff;
1962 TInt r = set.Remove(base[x]);
1965 after = User::NTickCount();
1966 diff = after - before;
1967 diff *= NanoTickPeriod;
1968 avg = (double)diff / (double)aCount;
1969 test.Printf(_L("HASH TABLE: %d deletions take %dus (%.2gus each)\n"), aCount, diff, avg);
1975 test.Next(_L("Benchmarks ..."));
1977 IntegerBenchmark(1000, EFalse);
1978 IntegerBenchmark(1000, ETrue);
1979 IntegerBenchmark(2000, EFalse);
1980 IntegerBenchmark(2000, ETrue);
1981 IntegerBenchmark(4000, EFalse);
1982 IntegerBenchmark(4000, ETrue);
1983 IntegerBenchmark(8000, EFalse);
1984 IntegerBenchmark(8000, ETrue);
1985 IntegerBenchmark(16000, EFalse);
1986 IntegerBenchmark(16000, ETrue);
1987 IntegerBenchmark(32000, EFalse);
1988 IntegerBenchmark(32000, ETrue);
1990 PopulateArray8(16384);
1991 StringBenchmark8(1000, EFalse);
1992 StringBenchmark8(2000, EFalse);
1993 StringBenchmark8(4000, EFalse);
1994 StringBenchmark8(8000, EFalse);
1995 DesC8Array.ResetAndDestroy();
1997 PopulateArray16(16384);
1998 StringBenchmark16(1000, EFalse);
1999 StringBenchmark16(2000, EFalse);
2000 StringBenchmark16(4000, EFalse);
2001 StringBenchmark16(8000, EFalse);
2002 DesC16Array.ResetAndDestroy();
2006 TUint32 mp(TUint32 x)
2008 TUint32 x3 = x+(x<<1);
2009 TUint32 x7 = x+(x3<<1);
2010 TUint32 x15 = x+(x7<<1);
2011 TUint32 y = x + (x7<<3) + (x3<<7) + (x15<<11) + (x7<<16) + (x3<<20) + (x15<<25) + (x<<31);
2015 TUint32 strh(const char* aIn)
2031 TUint32 strh(const wch* aIn)
2043 case 1: c<<=16; break;
2044 case 2: c<<=8; break;
2045 default: c=(c<<24)|(c>>8); break;
2053 void TestHash(TInt* aIn, TUint32 aExpected)
2055 THashFunction32<TInt*> hf(&DefaultHash::IntegerPtr);
2056 TUint32 out = hf.Hash(aIn);
2057 test(aExpected == mp((TUint32)aIn));
2058 if (out != aExpected)
2060 test.Printf(_L("Hashing %08x Expected %08x Got %08x\n"), aIn, aExpected, out);
2065 void TestHash(TDesC8* aIn, TUint32 aExpected)
2067 THashFunction32<TDesC8*> hf(&DefaultHash::Des8Ptr);
2068 TUint32 out = hf.Hash(aIn);
2069 test(aExpected == mp((TUint32)aIn));
2070 if (out != aExpected)
2072 test.Printf(_L("Hashing %08x Expected %08x Got %08x\n"), aIn, aExpected, out);
2077 void TestHash(TDesC16* aIn, TUint32 aExpected)
2079 THashFunction32<TDesC16*> hf(&DefaultHash::Des16Ptr);
2080 TUint32 out = hf.Hash(aIn);
2081 test(aExpected == mp((TUint32)aIn));
2082 if (out != aExpected)
2084 test.Printf(_L("Hashing %08x Expected %08x Got %08x\n"), aIn, aExpected, out);
2090 void TestHash(TInt aIn, TUint32 aExpected)
2092 THashFunction32<TInt> hf(&DefaultHash::Integer);
2093 TUint32 out = hf.Hash(aIn);
2094 test(aExpected == mp((TUint32)aIn));
2095 if (out != aExpected)
2097 test.Printf(_L("Hashing %08x Expected %08x Got %08x\n"), aIn, aExpected, out);
2102 void TestHash(const char* aIn, TUint32 aExpected)
2104 THashFunction32<TDesC8> hf(&DefaultHash::Des8);
2105 TPtrC8 p((const TUint8*)aIn);
2106 TUint32 out = hf.Hash(p);
2107 test(aExpected == strh(aIn));
2108 if (out != aExpected)
2112 test.Printf(_L("Hashing %S (len %d) Expected %08x Got %08x\n"), &buf, p.Length(), aExpected, out);
2117 void TestHash(const wch* aIn)
2119 THashFunction32<TDesC16> hf(&DefaultHash::Des16);
2120 TPtrC16 p((const TUint16*)aIn);
2121 TUint32 out = hf.Hash(p);
2122 TUint32 exp = strh(aIn);
2125 test.Printf(_L("Hashing %S (len %d) Expected %08x Got %08x\n"), &p, p.Size(), exp, out);
2132 test.Next(_L("Test integer hash"));
2133 TestHash(1,0x9e3779b9);
2134 TestHash(2,0x3c6ef372);
2135 TestHash(4,0x78dde6e4);
2136 TestHash(8,0xf1bbcdc8);
2137 TestHash(16,0xe3779b90);
2138 TestHash(0xc90fdaa2,0xbf999112);
2139 TestHash(0xb504f334,0x7fb35494);
2140 TestHash(0xddb3d743,0xd11a3a6b);
2141 TestHash(0xadf85458,0x873a8b98);
2142 TestHash(0x11730859,0xb4321951);
2143 TestHash(0x64636261,0x8628f119);
2146 void TestIntegerPtrHash()
2150 test.Next(_L("Test Integer pointer hash"));
2151 for (ptr=i; ptr<i+5; ptr++)
2153 TestHash(ptr, DefaultHash::IntegerPtr(ptr));
2157 void TestDes8PtrHash()
2161 test.Next(_L("Test Des8 pointer hash"));
2162 for (ptr=i; ptr<i+5; ptr++)
2164 TestHash(ptr, DefaultHash::Des8Ptr(ptr));
2168 void TestDes16PtrHash()
2172 test.Next(_L("Test Des16 pointer hash"));
2173 for (ptr=i; ptr<i+5; ptr++)
2175 TestHash(ptr, DefaultHash::Des16Ptr(ptr));
2179 const char teststr[] = "zyxwvutsrq";
2180 void TestStringHash()
2182 test.Next(_L("Test 8 bit string hash"));
2184 TestHash("a",0xf3051f19);
2185 TestHash("b",0x913c98d2);
2186 TestHash("ab",0x2f9df119);
2187 TestHash("ba",0x965bb1d2);
2188 TestHash("abc",0x4228f119);
2189 TestHash("abcd",0x8628f119);
2190 TestHash("abcde",0xb75e1e9c);
2191 TestHash("abcdef",0x3693149c);
2192 TestHash("abcdefg",0xc1c2149c);
2193 TestHash("abcdefgh",0xe9c2149c);
2194 TestHash("abcdefghi",0x5fcbf20d);
2195 TestHash("abcdefghj",0xfe036bc6);
2197 TestHash(teststr, 0x108ca51e);
2198 TestHash(teststr+1, 0x551002ad);
2199 TestHash(teststr+2, 0x37dc0d6c);
2200 TestHash(teststr+3, 0x2937f92c);
2201 TestHash(teststr+4, 0xf0818a94);
2202 TestHash(teststr+5, 0xb1b25f1c);
2203 TestHash(teststr+6, 0x7a3342d4);
2204 TestHash(teststr+7, 0x81c9101b);
2205 TestHash(teststr+8, 0xf16edd62);
2206 TestHash(teststr+9, 0xd67cbaa9);
2207 TestHash(teststr+10, 0);
2211 for (i=0; i<=4; ++i)
2213 for (j=0; j<=10; ++j)
2215 const char* s = teststr + j;
2216 int l = User::StringLength((const TUint8*)s);
2217 memset(t, 0xbb, 16);
2218 memcpy(t+i, s, l+1);
2222 case 0: h = 0x108ca51e; break;
2223 case 1: h = 0x551002ad; break;
2224 case 2: h = 0x37dc0d6c; break;
2225 case 3: h = 0x2937f92c; break;
2226 case 4: h = 0xf0818a94; break;
2227 case 5: h = 0xb1b25f1c; break;
2228 case 6: h = 0x7a3342d4; break;
2229 case 7: h = 0x81c9101b; break;
2230 case 8: h = 0xf16edd62; break;
2231 case 9: h = 0xd67cbaa9; break;
2232 default: h = 0; break;
2239 const wch wteststr[] = L"zyxwvutsrq";
2240 void TestWStringHash()
2242 test.Next(_L("Test 16 bit string hash"));
2251 TestHash(L"abcdef");
2252 TestHash(L"abcdefg");
2253 TestHash(L"abcdefgh");
2254 TestHash(L"abcdefghi");
2255 TestHash(L"abcdefghj");
2258 TestHash(wteststr+1);
2259 TestHash(wteststr+2);
2260 TestHash(wteststr+3);
2261 TestHash(wteststr+4);
2262 TestHash(wteststr+5);
2263 TestHash(wteststr+6);
2264 TestHash(wteststr+7);
2265 TestHash(wteststr+8);
2266 TestHash(wteststr+9);
2267 TestHash(wteststr+10);
2271 for (i=0; i<=4; ++i)
2273 for (j=0; j<=10; ++j)
2275 const wch* s = wteststr + j;
2276 int l = User::StringLength((const TUint16*)s);
2277 memset(t, 0xbb, 2*16);
2278 memcpy(t+i, s, 2*(l+1));
2283 template <class K,class V>
2284 void TestHashMapPtr(RHashMap<K,V> &map, K ptr, V* i)
2286 test(map.Reserve(5) == KErrNone);
2287 for (ptr=i;ptr<i+5;ptr++)
2289 test(map.Insert(ptr,*ptr) == KErrNone);
2291 for (ptr=i+4;ptr>=i;ptr--)
2293 test(*(map.Find(ptr)) == *ptr);
2295 test(map.Count() == 5);
2296 test(map.Remove(i) == KErrNone);
2297 test(map.Count()==4);
2298 test(map.Find(i)==NULL);
2302 void TestPtrHashMaps()
2305 test.Next(_L("Test RHashMap of default pointer types"));
2308 RHashMap<TInt*,TInt> mp;
2309 TestHashMapPtr(mp, ptr, i);
2313 RHashMap<TInt32*,TInt32> mp1;
2314 TestHashMapPtr(mp1,ptr1,i1);
2318 RHashMap<TUint*,TUint> mp2;
2319 TestHashMapPtr(mp2,ptr2,i2);
2323 RHashMap<TUint32*,TUint32> mp3;
2324 TestHashMapPtr(mp3,ptr3,i3);
2328 RHashMap<TDesC8*,TDesC8> mp4;
2329 for (ptr4=i4; ptr4 < i4+5; ptr4++)
2331 test(mp4.Insert(ptr4,*ptr4) == KErrNone);
2333 for (ptr4=i4+4; ptr4 >= i4; ptr4--)
2335 test(*(mp4.Find(ptr4)) == *ptr4);
2337 test(mp4.Count()==5);
2338 test(mp4.Remove(i4) == KErrNone);
2339 test(mp4.Find(i4) == NULL);
2340 test(mp4.Count()==4);
2346 RHashMap<TDesC16*,TDesC16> mp5;
2347 for (ptr5=i5; ptr5 < i5+5; ptr5++)
2349 test(mp5.Insert(ptr5,*ptr5) == KErrNone);
2351 for (ptr5=i5+4; ptr5 >= i5; ptr5--)
2353 test(*(mp5.Find(ptr5)) == *ptr5);
2355 test(mp5.Count()==5);
2356 test(mp5.Remove(i5) == KErrNone);
2357 test(mp5.Find(i5) == NULL);
2358 test(mp5.Count()==4);
2363 /** Tests that Reserve() will always allocate memory for new tables
2364 even for small reserve sizes
2367 void TestSmallReserve()
2369 test.Next(_L("Test RHashTableBase::Reserve preallocates memory, even for small no of elements"));
2373 // Reserve should allocate the memory required for the table of 1 element
2375 RHashMap<TInt,TInt> hashMap;
2377 test(set.Reserve(1) == KErrNone);
2378 test(hashMap.Reserve(1) == KErrNone);
2380 pA = User::SwitchAllocator(&da);
2382 // No more memory should be allocated for the table as it should
2383 // have been already allocated by Reserve()
2384 test(set.Insert(123) == KErrNone);
2385 test(hashMap.Insert(123,456) == KErrNone);
2387 // Switch back to allow set to be closed
2388 User::SwitchAllocator(pA);
2398 test(HAL::Get(HAL::ENanoTickPeriod, NanoTickPeriod)==KErrNone);
2399 test.Printf(_L("NanoTickPeriod %dus\n"), NanoTickPeriod);
2403 test.Start(_L("Testing hash tables"));
2408 TestIntegerPtrHash();
2417 PopulateArray8(4096);
2418 PopulateArray16(4096);
2421 DesC16Array.ResetAndDestroy();
2422 DesC8Array.ResetAndDestroy();