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 set class.
22 *//*--------------------------------------------------------------------*/
23
24 #include "dePoolSet.h"
25
26 #include <string.h>
27
28 DE_DECLARE_POOL_SET(deTestSet, deInt16);
29 DE_IMPLEMENT_POOL_SET(deTestSet, deInt16, deInt16Hash, deInt16Equal);
30
dePoolSet_selfTest(void)31 void dePoolSet_selfTest (void)
32 {
33 deMemPool* pool = deMemPool_createRoot(DE_NULL, 0);
34 deTestSet* set = deTestSet_create(pool);
35 int i;
36
37 /* Test exists() on empty set. */
38 DE_TEST_ASSERT(deTestSet_getNumElements(set) == 0);
39 for (i = 0; i < 15000; i++)
40 DE_TEST_ASSERT(!deTestSet_exists(set, (deInt16)i));
41
42 /* Test insert(). */
43 for (i = 0; i < 5000; i++)
44 deTestSet_insert(set, (deInt16)i);
45
46 DE_TEST_ASSERT(deTestSet_getNumElements(set) == 5000);
47 for (i = 0; i < 25000; i++)
48 {
49 deBool inserted = deInBounds32(i, 0, 5000);
50 deBool found = deTestSet_exists(set, (deInt16)i);
51 DE_TEST_ASSERT(found == inserted);
52 }
53
54 /* Test delete(). */
55 for (i = 0; i < 1000; i++)
56 deTestSet_delete(set, (deInt16)i);
57
58 DE_TEST_ASSERT(deTestSet_getNumElements(set) == 4000);
59 for (i = 0; i < 25000; i++)
60 {
61 deBool inserted = deInBounds32(i, 1000, 5000);
62 deBool found = deTestSet_exists(set, (deInt16)i);
63 DE_TEST_ASSERT(found == inserted);
64 }
65
66 /* Test insert() after delete(). */
67 for (i = 10000; i < 12000; i++)
68 deTestSet_insert(set, (deInt16)i);
69
70 DE_TEST_ASSERT(deTestSet_getNumElements(set) == 6000);
71
72 for (i = 0; i < 25000; i++)
73 {
74 deBool inserted = (deInBounds32(i, 1000, 5000) || deInBounds32(i, 10000, 12000));
75 deBool found = deTestSet_exists(set, (deInt16)i);
76 DE_TEST_ASSERT(found == inserted);
77 }
78
79 /* Test iterator. */
80 {
81 deTestSetIter iter;
82 int numFound = 0;
83
84 for (deTestSetIter_init(set, &iter); deTestSetIter_hasItem(&iter); deTestSetIter_next(&iter))
85 {
86 deInt16 key = deTestSetIter_getKey(&iter);
87 DE_TEST_ASSERT(deInBounds32(key, 1000, 5000) || deInBounds32(key, 10000, 12000));
88 DE_TEST_ASSERT(deTestSet_exists(set, key));
89 numFound++;
90 }
91
92 DE_TEST_ASSERT(numFound == deTestSet_getNumElements(set));
93 }
94
95 deMemPool_destroy(pool);
96 }
97