summaryrefslogtreecommitdiff
path: root/yapf/unittest/test_hashtable.h
blob: 7b794b84845677e642cbf9ff4d2bb422a3fd0d03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* $Id$ */

struct CHashItem1 {
	struct CKey {
		int k;
		FORCEINLINE int CalcHash() const {return k;};
		FORCEINLINE bool operator == (const CKey& other) const {return (k == other.k);}
	};
	typedef CKey Key;
	CKey  key;
	int   val;
	CHashItem1* m_next;

	CHashItem1() : m_next(NULL) {}
	FORCEINLINE const Key& GetKey() const {return key;}
	CHashItem1* GetHashNext() {return m_next;}
	void SetHashNext(CHashItem1* next) {m_next = next;}
};

static int TestHashTable1(bool silent)
{
	typedef CHashItem1 Item;
	typedef CHashTableT<Item, 12> HashTable1_t;
	typedef CArrayT<Item, 1024, 16384> Array_t;
	typedef CHashTableT<Item, 16> HashTable2_t;

	int res = 0;
	{
		HashTable1_t ht1;
		HashTable2_t ht2;
		Array_t      ar1;
		Array_t      ar2;

#ifdef _DEBUG
		static const int nItems = 10000;
#else
		static const int nItems = 1000000;
#endif
		{
			srand(0);
			for (int i = 0; i < nItems; i++) {
				int r1 = i;
				int r2 = rand() & 0x0000FFFF | (rand() << 16);
				Item& I1 = ar1.Add();
				Item& I2 = ar2.Add();
				I1.key.k = r1;
				I2.key.k = r1;
				I1.val = r2;
				I2.val = r2;
				ht1.Push(I1);
				ht2.Push(I2);
			}
		}
		{
			srand(0);
			for (int i = 0; i < nItems; i++) {
				int r1 = i;
				int r2 = rand() & 0x0000FFFF | (rand() << 16);
				HashTable1_t::Tkey k; k.k = r1;
				Item& i1 = ht1.Find(k);
				Item& i2 = ht2.Find(k);

				CHECK_INT(0, &i1 != NULL, 1);
				CHECK_INT(1, &i2 != NULL, 1);

				if (&i1 != NULL) CHECK_INT(2, i1.val, r2);
				if (&i2 != NULL) CHECK_INT(3, i2.val, r2);
			}
		}
	}
	return res;
}