xref: /aosp_15_r20/external/deqp/framework/delibs/depool/dePoolHash.c (revision 35238bce31c2a825756842865a792f8cf7f89930)
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, int16_t, int);
29 DE_IMPLEMENT_POOL_HASH(deTestHash, int16_t, int, deInt16Hash, deInt16Equal);
30 
31 DE_DECLARE_POOL_ARRAY(deTestIntArray, int);
32 DE_DECLARE_POOL_ARRAY(deTestInt16Array, int16_t);
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, (int16_t)i);
52             DE_TEST_ASSERT(!val);
53         }
54 
55         /* Test insert(). */
56         for (i = 0; i < 5000; i++)
57         {
58             deTestHash_insert(hash, (int16_t)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, (int16_t)i);
65             DE_TEST_ASSERT(val && (*val == -i));
66         }
67 
68         /* Test delete(). */
69         for (i = 0; i < 1000; i++)
70             deTestHash_delete(hash, (int16_t)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, (int16_t)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, (int16_t)i, -i);
85 
86         for (i = 0; i < 25000; i++)
87         {
88             const int *val = deTestHash_find(hash, (int16_t)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);
101                  deTestHashIter_next(&testIter))
102             {
103                 int16_t key = deTestHashIter_getKey(&testIter);
104                 int val     = deTestHashIter_getValue(&testIter);
105                 DE_TEST_ASSERT(deInBounds32(key, 1000, 5000) || deInBounds32(key, 10000, 12000));
106                 DE_TEST_ASSERT(*deTestHash_find(hash, key) == -key);
107                 DE_TEST_ASSERT(val == -key);
108                 numFound++;
109             }
110 
111             DE_TEST_ASSERT(numFound == deTestHash_getNumElements(hash));
112         }
113 
114         /* Test copy-to-array. */
115         {
116             deTestInt16Array *keyArray = deTestInt16Array_create(pool);
117             deTestIntArray *valueArray = deTestIntArray_create(pool);
118             int numElements            = deTestHash_getNumElements(hash);
119             int ndx;
120 
121             deTestHash_copyToArray(hash, keyArray, DE_NULL);
122             DE_TEST_ASSERT(deTestInt16Array_getNumElements(keyArray) == numElements);
123 
124             deTestHash_copyToArray(hash, DE_NULL, valueArray);
125             DE_TEST_ASSERT(deTestIntArray_getNumElements(valueArray) == numElements);
126 
127             deTestInt16Array_setSize(keyArray, 0);
128             deTestIntArray_setSize(valueArray, 0);
129             deTestHash_copyToArray(hash, keyArray, valueArray);
130             DE_TEST_ASSERT(deTestInt16Array_getNumElements(keyArray) == numElements);
131             DE_TEST_ASSERT(deTestIntArray_getNumElements(valueArray) == numElements);
132 
133             for (ndx = 0; ndx < numElements; ndx++)
134             {
135                 int16_t key = deTestInt16Array_get(keyArray, ndx);
136                 int val     = deTestIntArray_get(valueArray, ndx);
137 
138                 DE_TEST_ASSERT(val == -key);
139                 DE_TEST_ASSERT(*deTestHash_find(hash, key) == val);
140             }
141         }
142 
143         /* Test reset(). */
144         deTestHash_reset(hash);
145         DE_TEST_ASSERT(deTestHash_getNumElements(hash) == 0);
146     }
147 
148     deMemPool_destroy(pool);
149 }
150