1 /*-------------------------------------------------------------------------
2 * drawElements Memory Pool Library
3 * --------------------------------
5 * Copyright 2014 The Android Open Source Project
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * \brief Memory pool multiset class.
22 *//*--------------------------------------------------------------------*/
24 #include "dePoolMultiSet.h"
26 DE_DECLARE_POOL_MULTISET(deTestMultiSet, deInt16);
27 DE_IMPLEMENT_POOL_MULTISET(deTestMultiSet, deInt16, deInt16Hash, deInt16Equal);
29 void dePoolMultiSet_selfTest (void)
31 deMemPool* pool = deMemPool_createRoot(DE_NULL, 0);
32 deTestMultiSet* set = deTestMultiSet_create(pool);
35 /* Test exists() on empty set. */
36 DE_TEST_ASSERT(deTestMultiSet_getNumElements(set) == 0);
37 for (i = 0; i < 15000; i++)
38 DE_TEST_ASSERT(!deTestMultiSet_exists(set, (deInt16)i));
41 for (i = 0; i < 5000; i++)
42 deTestMultiSet_insert(set, (deInt16)i);
44 DE_TEST_ASSERT(deTestMultiSet_getNumElements(set) == 5000);
45 for (i = 0; i < 25000; i++)
47 deBool inserted = deInBounds32(i, 0, 5000);
48 deBool found = deTestMultiSet_exists(set, (deInt16)i);
49 DE_TEST_ASSERT(found == inserted);
53 for (i = 0; i < 1000; i++)
54 deTestMultiSet_delete(set, (deInt16)i);
56 DE_TEST_ASSERT(deTestMultiSet_getNumElements(set) == 4000);
57 for (i = 0; i < 25000; i++)
59 deBool inserted = deInBounds32(i, 1000, 5000);
60 deBool found = deTestMultiSet_exists(set, (deInt16)i);
61 DE_TEST_ASSERT(found == inserted);
64 /* Test insert() after delete(). */
65 for (i = 10000; i < 12000; i++)
66 deTestMultiSet_insert(set, (deInt16)i);
68 DE_TEST_ASSERT(deTestMultiSet_getNumElements(set) == 6000);
70 for (i = 0; i < 25000; i++)
72 deBool inserted = (deInBounds32(i, 1000, 5000) || deInBounds32(i, 10000, 12000));
73 deBool found = deTestMultiSet_exists(set, (deInt16)i);
74 DE_TEST_ASSERT(found == inserted);
78 deTestMultiSet_reset(set);
79 DE_TEST_ASSERT(deTestMultiSet_getNumElements(set) == 0);
81 /* Test insertion multiple times. */
82 for (i = 0; i < 1000; i++)
83 deTestMultiSet_insert(set, (deInt16)i);
84 for (i = 0; i < 500; i++)
85 deTestMultiSet_insert(set, (deInt16)i);
86 for (i = 0; i < 250; i++)
87 deTestMultiSet_insert(set, (deInt16)i);
89 DE_TEST_ASSERT(deTestMultiSet_getNumElements(set) == 1000 + 500 + 250);
91 for (i = 0; i < 2000; i++)
94 deBool found = deTestMultiSet_exists(set, (deInt16)i);
95 int gotCount = deTestMultiSet_getKeyCount(set, (deInt16)i);
97 count += deInBounds32(i, 0, 1000) ? 1 : 0;
98 count += deInBounds32(i, 0, 500) ? 1 : 0;
99 count += deInBounds32(i, 0, 250) ? 1 : 0;
101 DE_TEST_ASSERT(found == (count > 0));
102 DE_TEST_ASSERT(count == gotCount);
105 /* Test multiset deletion rules. */
106 for (i = 0; i < 1000; i++)
107 deTestMultiSet_delete(set, (deInt16)i);
109 DE_TEST_ASSERT(deTestMultiSet_getNumElements(set) == 500 + 250);
111 for (i = 0; i < 2000; i++)
114 deBool found = deTestMultiSet_exists(set, (deInt16)i);
115 int gotCount = deTestMultiSet_getKeyCount(set, (deInt16)i);
117 count += deInBounds32(i, 0, 500) ? 1 : 0;
118 count += deInBounds32(i, 0, 250) ? 1 : 0;
120 DE_TEST_ASSERT(found == (count > 0));
121 DE_TEST_ASSERT(count == gotCount);
124 /* Test setKeyCount(). */
125 for (i = 0; i < 250; i++)
126 deTestMultiSet_setKeyCount(set, (deInt16)i, 0);
127 for (i = 750; i < 1000; i++)
128 deTestMultiSet_setKeyCount(set, (deInt16)i, 3);
130 DE_TEST_ASSERT(deTestMultiSet_getNumElements(set) == 250 * 4);
132 for (i = 0; i < 2000; i++)
135 deBool found = deTestMultiSet_exists(set, (deInt16)i);
136 int gotCount = deTestMultiSet_getKeyCount(set, (deInt16)i);
138 count += deInBounds32(i, 250, 500) ? 1 : 0;
139 count += deInBounds32(i, 750, 1000) ? 3 : 0;
141 DE_TEST_ASSERT(found == (count > 0));
142 DE_TEST_ASSERT(gotCount == count);
145 deMemPool_destroy(pool);