]>
git.saurik.com Git - apple/libc.git/blob - tests/collections_edgecases.c
2 * Copyright (c) 2020 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 #include <os/collections.h>
29 #include <darwintest.h>
31 T_DECL(map_edge_64
, "Make sure 64 bit map edge cases work",
32 T_META("owner", "Core Darwin Daemons & Tools"))
34 os_map_64_t edge_64_map
;
35 __block
bool got_cafebabe
= false;
36 __block
bool got_deadbeaf
= false;
37 __block
bool got_beebait
= false;
38 __block
bool got_badfood
= false;
43 // *** BASIC 64 bit key testing ***
45 os_map_init(&edge_64_map
, NULL
);
47 T_ASSERT_EQ(os_map_count(&edge_64_map
), 0, "Expect map to be empty");
49 os_map_insert(&edge_64_map
, 0x0, (void *)0xCAFEBABE);
50 os_map_insert(&edge_64_map
, 0x1, (void *)0xDEADBEEF);
51 os_map_insert(&edge_64_map
, 0x2, (void *)0xBEEB8);
52 os_map_insert(&edge_64_map
, 0x3, (void *)0xBADF00D);
54 T_ASSERT_EQ(os_map_count(&edge_64_map
), 4,
55 "Expect map to have 4 entries");
57 os_map_foreach(&edge_64_map
, ^bool (uint64_t key
, void *value
){
58 T_LOG("Foreach called for 0x%llx, 0x%llx",
59 (unsigned long long)key
, (unsigned long long)value
);
61 T_ASSERT_EQ(value
, (void *)0xCAFEBABE,
62 "Callback expect 0xCAFEBABE");
64 } else if (key
== 0x1) {
65 T_ASSERT_EQ(value
, (void *)0xDEADBEEF,
66 "Callback expect 0xDEADBEEF");
68 } else if (key
== 0x2) {
69 T_ASSERT_EQ(value
, (void *)0xBEEB8,
70 "Callback expect 0xBEEB8");
72 } else if (key
== 0x3) {
73 T_ASSERT_EQ(value
, (void *)0xBADF00D,
74 "Callback expect 0xBADF00D");
77 T_FAIL("Got unexpected callback 0x%llx, 0x%llx",
78 (unsigned long long)key
,
79 (unsigned long long)value
);
84 if (!got_cafebabe
|| !got_deadbeaf
|| !got_beebait
|| !got_badfood
) {
85 T_FAIL("Failed to get callback");
88 value
= (uint64_t)os_map_find(&edge_64_map
, 0x0);
89 T_ASSERT_EQ(value
, (uint64_t)0xCAFEBABE, "Find 1");
91 value
= (uint64_t)os_map_find(&edge_64_map
, 0x1);
92 T_ASSERT_EQ(value
, (uint64_t)0xDEADBEEF, "Find 2");
94 value
= (uint64_t)os_map_find(&edge_64_map
, 0x2);
95 T_ASSERT_EQ(value
, (uint64_t)0xBEEB8, "Find 3");
97 value
= (uint64_t)os_map_find(&edge_64_map
, 0x3);
98 T_ASSERT_EQ(value
, (uint64_t)0xBADF00D, "Find 4");
100 os_map_delete(&edge_64_map
, 0x0);
101 os_map_delete(&edge_64_map
, 0x2);
103 T_ASSERT_EQ(os_map_count(&edge_64_map
), 2,
104 "Expect map to have 2 entries");
106 value
= (uint64_t)os_map_find(&edge_64_map
, 0x0);
107 T_ASSERT_EQ(value
, (uint64_t)0x0, "After-delete Find 1");
109 value
= (uint64_t)os_map_find(&edge_64_map
, 0x2);
110 T_ASSERT_EQ(value
, (uint64_t)0x0, "After-delete Find 1");
112 value
= (uint64_t)os_map_find(&edge_64_map
, 0x1);
113 T_ASSERT_EQ(value
, (uint64_t)0xDEADBEEF, "After-delete find 3");
115 value
= (uint64_t)os_map_find(&edge_64_map
, 0x3);
116 T_ASSERT_EQ(value
, (uint64_t)0xBADF00D, "After-delete find 4");
118 os_map_delete(&edge_64_map
, 0x1);
119 os_map_delete(&edge_64_map
, 0x3);
121 T_ASSERT_EQ(os_map_count(&edge_64_map
), 0, "Expect map to be empty");
123 value
= (uint64_t)os_map_find(&edge_64_map
, 0x1);
124 T_ASSERT_EQ(value
, (uint64_t)0x0, "After-delete Find 5");
126 value
= (uint64_t)os_map_find(&edge_64_map
, 0x3);
127 T_ASSERT_EQ(value
, (uint64_t)0x0, "After-delete find 6");
129 os_map_destroy(&edge_64_map
);
132 T_DECL(map_edge_32
, "Make sure 32 bit map edge cases work",
133 T_META("owner", "Core Darwin Daemons & Tools"))
135 os_map_32_t edge_32_map
;
136 __block
bool got_cafebabe
= false;
137 __block
bool got_deadbeaf
= false;
138 __block
bool got_beebait
= false;
139 __block
bool got_badfood
= false;
144 // *** BASIC 64 bit key testing ***
146 os_map_init(&edge_32_map
, NULL
);
148 T_ASSERT_EQ(os_map_count(&edge_32_map
), 0, "Expect map to be empty");
150 os_map_insert(&edge_32_map
, 0x0, (void *)0xCAFEBABE);
151 os_map_insert(&edge_32_map
, 0x1, (void *)0xDEADBEEF);
152 os_map_insert(&edge_32_map
, 0x2, (void *)0xBEEB8);
153 os_map_insert(&edge_32_map
, 0x3, (void *)0xBADF00D);
155 T_ASSERT_EQ(os_map_count(&edge_32_map
), 4,
156 "Expect map to have 4 entries");
158 os_map_foreach(&edge_32_map
, ^bool (uint32_t key
, void *value
){
159 T_LOG("Foreach called for 0x%llx, 0x%llx",
160 (unsigned long long)key
, (unsigned long long)value
);
162 T_ASSERT_EQ(value
, (void *)0xCAFEBABE,
163 "Callback expect 0xCAFEBABE");
165 } else if (key
== 0x1) {
166 T_ASSERT_EQ(value
, (void *)0xDEADBEEF,
167 "Callback expect 0xDEADBEEF");
169 } else if (key
== 0x2) {
170 T_ASSERT_EQ(value
, (void *)0xBEEB8,
171 "Callback expect 0xBEEB8");
173 } else if (key
== 0x3) {
174 T_ASSERT_EQ(value
, (void *)0xBADF00D,
175 "Callback expect 0xBADF00D");
178 T_FAIL("Got unexpected callback 0x%llx, 0x%llx",
179 (unsigned long long)key
,
180 (unsigned long long)value
);
185 if (!got_cafebabe
|| !got_deadbeaf
|| !got_beebait
|| !got_badfood
) {
186 T_FAIL("Failed to get callback");
189 value
= (uint64_t)os_map_find(&edge_32_map
, 0x0);
190 T_ASSERT_EQ(value
, (uint64_t)0xCAFEBABE, "Find 1");
192 value
= (uint64_t)os_map_find(&edge_32_map
, 0x1);
193 T_ASSERT_EQ(value
, (uint64_t)0xDEADBEEF, "Find 2");
195 value
= (uint64_t)os_map_find(&edge_32_map
, 0x2);
196 T_ASSERT_EQ(value
, (uint64_t)0xBEEB8, "Find 3");
198 value
= (uint64_t)os_map_find(&edge_32_map
, 0x3);
199 T_ASSERT_EQ(value
, (uint64_t)0xBADF00D, "Find 4");
201 os_map_delete(&edge_32_map
, 0x0);
202 os_map_delete(&edge_32_map
, 0x2);
204 T_ASSERT_EQ(os_map_count(&edge_32_map
), 2,
205 "Expect map to have 2 entries");
207 value
= (uint64_t)os_map_find(&edge_32_map
, 0x0);
208 T_ASSERT_EQ(value
, (uint64_t)0x0, "After-delete Find 1");
210 value
= (uint64_t)os_map_find(&edge_32_map
, 0x2);
211 T_ASSERT_EQ(value
, (uint64_t)0x0, "After-delete Find 1");
213 value
= (uint64_t)os_map_find(&edge_32_map
, 0x1);
214 T_ASSERT_EQ(value
, (uint64_t)0xDEADBEEF, "After-delete find 3");
216 value
= (uint64_t)os_map_find(&edge_32_map
, 0x3);
217 T_ASSERT_EQ(value
, (uint64_t)0xBADF00D, "After-delete find 4");
219 os_map_delete(&edge_32_map
, 0x1);
220 os_map_delete(&edge_32_map
, 0x3);
222 T_ASSERT_EQ(os_map_count(&edge_32_map
), 0, "Expect map to be empty");
224 value
= (uint64_t)os_map_find(&edge_32_map
, 0x1);
225 T_ASSERT_EQ(value
, (uint64_t)0x0, "After-delete Find 5");
227 value
= (uint64_t)os_map_find(&edge_32_map
, 0x3);
228 T_ASSERT_EQ(value
, (uint64_t)0x0, "After-delete find 6");
230 os_map_destroy(&edge_32_map
);