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 multiset class.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "dePoolMultiSet.h"
25 
26 DE_DECLARE_POOL_MULTISET(deTestMultiSet, deInt16);
27 DE_IMPLEMENT_POOL_MULTISET(deTestMultiSet, deInt16, deInt16Hash, deInt16Equal);
28 
dePoolMultiSet_selfTest(void)29 void dePoolMultiSet_selfTest (void)
30 {
31 	deMemPool*		pool	= deMemPool_createRoot(DE_NULL, 0);
32 	deTestMultiSet*	set		= deTestMultiSet_create(pool);
33 	int				i;
34 
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));
39 
40 	/* Test insert(). */
41 	for (i = 0; i < 5000; i++)
42 		deTestMultiSet_insert(set, (deInt16)i);
43 
44 	DE_TEST_ASSERT(deTestMultiSet_getNumElements(set) == 5000);
45 	for (i = 0; i < 25000; i++)
46 	{
47 		deBool inserted	= deInBounds32(i, 0, 5000);
48 		deBool found	= deTestMultiSet_exists(set, (deInt16)i);
49 		DE_TEST_ASSERT(found == inserted);
50 	}
51 
52 	/* Test delete(). */
53 	for (i = 0; i < 1000; i++)
54 		deTestMultiSet_delete(set, (deInt16)i);
55 
56 	DE_TEST_ASSERT(deTestMultiSet_getNumElements(set) == 4000);
57 	for (i = 0; i < 25000; i++)
58 	{
59 		deBool inserted	= deInBounds32(i, 1000, 5000);
60 		deBool found	= deTestMultiSet_exists(set, (deInt16)i);
61 		DE_TEST_ASSERT(found == inserted);
62 	}
63 
64 	/* Test insert() after delete(). */
65 	for (i = 10000; i < 12000; i++)
66 		deTestMultiSet_insert(set, (deInt16)i);
67 
68 	DE_TEST_ASSERT(deTestMultiSet_getNumElements(set) == 6000);
69 
70 	for (i = 0; i < 25000; i++)
71 	{
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);
75 	}
76 
77 	/* Test reset. */
78 	deTestMultiSet_reset(set);
79 	DE_TEST_ASSERT(deTestMultiSet_getNumElements(set) == 0);
80 
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);
88 
89 	DE_TEST_ASSERT(deTestMultiSet_getNumElements(set) == 1000 + 500 + 250);
90 
91 	for (i = 0; i < 2000; i++)
92 	{
93 		int		count		= 0;
94 		deBool	found		= deTestMultiSet_exists(set, (deInt16)i);
95 		int		gotCount	= deTestMultiSet_getKeyCount(set, (deInt16)i);
96 
97 		count += deInBounds32(i, 0, 1000) ? 1 : 0;
98 		count += deInBounds32(i, 0, 500) ? 1 : 0;
99 		count += deInBounds32(i, 0, 250) ? 1 : 0;
100 
101 		DE_TEST_ASSERT(found == (count > 0));
102 		DE_TEST_ASSERT(count == gotCount);
103 	}
104 
105 	/* Test multiset deletion rules. */
106 	for (i = 0; i < 1000; i++)
107 		deTestMultiSet_delete(set, (deInt16)i);
108 
109 	DE_TEST_ASSERT(deTestMultiSet_getNumElements(set) == 500 + 250);
110 
111 	for (i = 0; i < 2000; i++)
112 	{
113 		int		count		= 0;
114 		deBool	found		= deTestMultiSet_exists(set, (deInt16)i);
115 		int		gotCount	= deTestMultiSet_getKeyCount(set, (deInt16)i);
116 
117 		count += deInBounds32(i, 0, 500) ? 1 : 0;
118 		count += deInBounds32(i, 0, 250) ? 1 : 0;
119 
120 		DE_TEST_ASSERT(found == (count > 0));
121 		DE_TEST_ASSERT(count == gotCount);
122 	}
123 
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);
129 
130 	DE_TEST_ASSERT(deTestMultiSet_getNumElements(set) == 250 * 4);
131 
132 	for (i = 0; i < 2000; i++)
133 	{
134 		int		count		= 0;
135 		deBool	found		= deTestMultiSet_exists(set, (deInt16)i);
136 		int		gotCount	= deTestMultiSet_getKeyCount(set, (deInt16)i);
137 
138 		count += deInBounds32(i, 250, 500) ? 1 : 0;
139 		count += deInBounds32(i, 750, 1000) ? 3 : 0;
140 
141 		DE_TEST_ASSERT(found == (count > 0));
142 		DE_TEST_ASSERT(gotCount == count);
143 	}
144 
145 	deMemPool_destroy(pool);
146 }
147