1 /*-------------------------------------------------------------------------
2  * drawElements Memory Pool Library
3  * --------------------------------
4  *
5  * Copyright 2014 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief Memory pool hash class.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "dePoolHash.h"
25 
26 #include <string.h>
27 
28 DE_DECLARE_POOL_HASH(deTestHash, deInt16, int);
29 DE_IMPLEMENT_POOL_HASH(deTestHash, deInt16, int, deInt16Hash, deInt16Equal);
30 
31 DE_DECLARE_POOL_ARRAY(deTestIntArray, int);
32 DE_DECLARE_POOL_ARRAY(deTestInt16Array, deInt16);
33 
34 DE_DECLARE_POOL_HASH_TO_ARRAY(deTestHash, deTestInt16Array, deTestIntArray);
35 DE_IMPLEMENT_POOL_HASH_TO_ARRAY(deTestHash, deTestInt16Array, deTestIntArray);
36 
dePoolHash_selfTest(void)37 void dePoolHash_selfTest (void)
38 {
39 	deMemPool*	pool	= deMemPool_createRoot(DE_NULL, 0);
40 	deTestHash*	hash	= deTestHash_create(pool);
41 	int			iter;
42 
43 	for (iter = 0; iter < 3; iter++)
44 	{
45 		int i;
46 
47 		/* Test find() on empty hash. */
48 		DE_TEST_ASSERT(deTestHash_getNumElements(hash) == 0);
49 		for (i = 0; i < 15000; i++)
50 		{
51 			const int* val = deTestHash_find(hash, (deInt16)i);
52 			DE_TEST_ASSERT(!val);
53 		}
54 
55 		/* Test insert(). */
56 		for (i = 0; i < 5000; i++)
57 		{
58 			deTestHash_insert(hash, (deInt16)i, -i);
59 		}
60 
61 		DE_TEST_ASSERT(deTestHash_getNumElements(hash) == 5000);
62 		for (i = 0; i < 5000; i++)
63 		{
64 			const int* val = deTestHash_find(hash, (deInt16)i);
65 			DE_TEST_ASSERT(val && (*val == -i));
66 		}
67 
68 		/* Test delete(). */
69 		for (i = 0; i < 1000; i++)
70 			deTestHash_delete(hash, (deInt16)i);
71 
72 		DE_TEST_ASSERT(deTestHash_getNumElements(hash) == 4000);
73 		for (i = 0; i < 25000; i++)
74 		{
75 			const int* val = deTestHash_find(hash, (deInt16)i);
76 			if (deInBounds32(i, 1000, 5000))
77 				DE_TEST_ASSERT(val && (*val == -i));
78 			else
79 				DE_TEST_ASSERT(!val);
80 		}
81 
82 		/* Test insert() after delete(). */
83 		for (i = 10000; i < 12000; i++)
84 			deTestHash_insert(hash, (deInt16)i, -i);
85 
86 		for (i = 0; i < 25000; i++)
87 		{
88 			const int* val = deTestHash_find(hash, (deInt16)i);
89 			if (deInBounds32(i, 1000, 5000) || deInBounds32(i, 10000, 12000))
90 				DE_TEST_ASSERT(val && (*val == -i));
91 			else
92 				DE_TEST_ASSERT(!val);
93 		}
94 
95 		/* Test iterator. */
96 		{
97 			deTestHashIter	testIter;
98 			int				numFound = 0;
99 
100 			for (deTestHashIter_init(hash, &testIter); deTestHashIter_hasItem(&testIter); deTestHashIter_next(&testIter))
101 			{
102 				deInt16	key	= deTestHashIter_getKey(&testIter);
103 				int		val	= deTestHashIter_getValue(&testIter);
104 				DE_TEST_ASSERT(deInBounds32(key, 1000, 5000) || deInBounds32(key, 10000, 12000));
105 				DE_TEST_ASSERT(*deTestHash_find(hash, key) == -key);
106 				DE_TEST_ASSERT(val == -key);
107 				numFound++;
108 			}
109 
110 			DE_TEST_ASSERT(numFound == deTestHash_getNumElements(hash));
111 		}
112 
113 		/* Test copy-to-array. */
114 		{
115 			deTestInt16Array*	keyArray	= deTestInt16Array_create(pool);
116 			deTestIntArray*		valueArray	= deTestIntArray_create(pool);
117 			int					numElements	= deTestHash_getNumElements(hash);
118 			int					ndx;
119 
120 			deTestHash_copyToArray(hash, keyArray, DE_NULL);
121 			DE_TEST_ASSERT(deTestInt16Array_getNumElements(keyArray) == numElements);
122 
123 			deTestHash_copyToArray(hash, DE_NULL, valueArray);
124 			DE_TEST_ASSERT(deTestIntArray_getNumElements(valueArray) == numElements);
125 
126 			deTestInt16Array_setSize(keyArray, 0);
127 			deTestIntArray_setSize(valueArray, 0);
128 			deTestHash_copyToArray(hash, keyArray, valueArray);
129 			DE_TEST_ASSERT(deTestInt16Array_getNumElements(keyArray) == numElements);
130 			DE_TEST_ASSERT(deTestIntArray_getNumElements(valueArray) == numElements);
131 
132 			for (ndx = 0; ndx < numElements; ndx++)
133 			{
134 				deInt16 key = deTestInt16Array_get(keyArray, ndx);
135 				int		val = deTestIntArray_get(valueArray, ndx);
136 
137 				DE_TEST_ASSERT(val == -key);
138 				DE_TEST_ASSERT(*deTestHash_find(hash, key) == val);
139 			}
140 		}
141 
142 		/* Test reset(). */
143 		deTestHash_reset(hash);
144 		DE_TEST_ASSERT(deTestHash_getNumElements(hash) == 0);
145 	}
146 
147 	deMemPool_destroy(pool);
148 }
149