yapf/unittest/test_hashtable.h
changeset 4562 48ab394a84e8
parent 4561 98779da22b99
child 4563 241260bd5505
equal deleted inserted replaced
4561:98779da22b99 4562:48ab394a84e8
     1 /* $Id$ */
       
     2 
       
     3 struct CHashItem1 {
       
     4 	struct CKey {
       
     5 		int k;
       
     6 		FORCEINLINE int CalcHash() const {return k;};
       
     7 		FORCEINLINE bool operator == (const CKey& other) const {return (k == other.k);}
       
     8 	};
       
     9 	typedef CKey Key;
       
    10 	CKey  key;
       
    11 	int   val;
       
    12 	CHashItem1* m_next;
       
    13 
       
    14 	CHashItem1() : m_next(NULL) {}
       
    15 	FORCEINLINE const Key& GetKey() const {return key;}
       
    16 	CHashItem1* GetHashNext() {return m_next;}
       
    17 	void SetHashNext(CHashItem1* next) {m_next = next;}
       
    18 };
       
    19 
       
    20 static int TestHashTable1(bool silent)
       
    21 {
       
    22 	typedef CHashItem1 Item;
       
    23 	typedef CHashTableT<Item, 12> HashTable1_t;
       
    24 	typedef CArrayT<Item, 1024, 16384> Array_t;
       
    25 	typedef CHashTableT<Item, 16> HashTable2_t;
       
    26 
       
    27 	int res = 0;
       
    28 	{
       
    29 		HashTable1_t ht1;
       
    30 		HashTable2_t ht2;
       
    31 		Array_t      ar1;
       
    32 		Array_t      ar2;
       
    33 
       
    34 #ifdef _DEBUG
       
    35 		static const int nItems = 10000;
       
    36 #else
       
    37 		static const int nItems = 1000000;
       
    38 #endif
       
    39 		{
       
    40 			srand(0);
       
    41 			for (int i = 0; i < nItems; i++) {
       
    42 				int r1 = i;
       
    43 				int r2 = rand() & 0x0000FFFF | (rand() << 16);
       
    44 				Item& I1 = ar1.Add();
       
    45 				Item& I2 = ar2.Add();
       
    46 				I1.key.k = r1;
       
    47 				I2.key.k = r1;
       
    48 				I1.val = r2;
       
    49 				I2.val = r2;
       
    50 				ht1.Push(I1);
       
    51 				ht2.Push(I2);
       
    52 			}
       
    53 		}
       
    54 		{
       
    55 			srand(0);
       
    56 			for (int i = 0; i < nItems; i++) {
       
    57 				int r1 = i;
       
    58 				int r2 = rand() & 0x0000FFFF | (rand() << 16);
       
    59 				HashTable1_t::Tkey k; k.k = r1;
       
    60 				Item& i1 = ht1.Find(k);
       
    61 				Item& i2 = ht2.Find(k);
       
    62 
       
    63 				CHECK_INT(0, &i1 != NULL, 1);
       
    64 				CHECK_INT(1, &i2 != NULL, 1);
       
    65 
       
    66 				if (&i1 != NULL) CHECK_INT(2, i1.val, r2);
       
    67 				if (&i2 != NULL) CHECK_INT(3, i2.val, r2);
       
    68 			}
       
    69 		}
       
    70 	}
       
    71 	return res;
       
    72 }