]> git.saurik.com Git - apple/libc.git/blob - tests/collections_edgecases.c
Libc-1439.100.3.tar.gz
[apple/libc.git] / tests / collections_edgecases.c
1 /*
2 * Copyright (c) 2020 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <stdlib.h>
25 #include <os/collections.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <darwintest.h>
30
31 T_DECL(map_edge_64, "Make sure 64 bit map edge cases work",
32 T_META("owner", "Core Darwin Daemons & Tools"))
33 {
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;
39 uint64_t value;
40
41 T_LOG("Start");
42
43 // *** BASIC 64 bit key testing ***
44
45 os_map_init(&edge_64_map, NULL);
46
47 T_ASSERT_EQ(os_map_count(&edge_64_map), 0, "Expect map to be empty");
48
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);
53
54 T_ASSERT_EQ(os_map_count(&edge_64_map), 4,
55 "Expect map to have 4 entries");
56
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);
60 if (key == 0x0) {
61 T_ASSERT_EQ(value, (void *)0xCAFEBABE,
62 "Callback expect 0xCAFEBABE");
63 got_cafebabe = true;
64 } else if (key == 0x1) {
65 T_ASSERT_EQ(value, (void *)0xDEADBEEF,
66 "Callback expect 0xDEADBEEF");
67 got_deadbeaf = true;
68 } else if (key == 0x2) {
69 T_ASSERT_EQ(value, (void *)0xBEEB8,
70 "Callback expect 0xBEEB8");
71 got_beebait = true;
72 } else if (key == 0x3) {
73 T_ASSERT_EQ(value, (void *)0xBADF00D,
74 "Callback expect 0xBADF00D");
75 got_badfood = true;
76 } else {
77 T_FAIL("Got unexpected callback 0x%llx, 0x%llx",
78 (unsigned long long)key,
79 (unsigned long long)value);
80 }
81 return true;
82 });
83
84 if (!got_cafebabe || !got_deadbeaf || !got_beebait || !got_badfood) {
85 T_FAIL("Failed to get callback");
86 }
87
88 value = (uint64_t)os_map_find(&edge_64_map, 0x0);
89 T_ASSERT_EQ(value, (uint64_t)0xCAFEBABE, "Find 1");
90
91 value = (uint64_t)os_map_find(&edge_64_map, 0x1);
92 T_ASSERT_EQ(value, (uint64_t)0xDEADBEEF, "Find 2");
93
94 value = (uint64_t)os_map_find(&edge_64_map, 0x2);
95 T_ASSERT_EQ(value, (uint64_t)0xBEEB8, "Find 3");
96
97 value = (uint64_t)os_map_find(&edge_64_map, 0x3);
98 T_ASSERT_EQ(value, (uint64_t)0xBADF00D, "Find 4");
99
100 os_map_delete(&edge_64_map, 0x0);
101 os_map_delete(&edge_64_map, 0x2);
102
103 T_ASSERT_EQ(os_map_count(&edge_64_map), 2,
104 "Expect map to have 2 entries");
105
106 value = (uint64_t)os_map_find(&edge_64_map, 0x0);
107 T_ASSERT_EQ(value, (uint64_t)0x0, "After-delete Find 1");
108
109 value = (uint64_t)os_map_find(&edge_64_map, 0x2);
110 T_ASSERT_EQ(value, (uint64_t)0x0, "After-delete Find 1");
111
112 value = (uint64_t)os_map_find(&edge_64_map, 0x1);
113 T_ASSERT_EQ(value, (uint64_t)0xDEADBEEF, "After-delete find 3");
114
115 value = (uint64_t)os_map_find(&edge_64_map, 0x3);
116 T_ASSERT_EQ(value, (uint64_t)0xBADF00D, "After-delete find 4");
117
118 os_map_delete(&edge_64_map, 0x1);
119 os_map_delete(&edge_64_map, 0x3);
120
121 T_ASSERT_EQ(os_map_count(&edge_64_map), 0, "Expect map to be empty");
122
123 value = (uint64_t)os_map_find(&edge_64_map, 0x1);
124 T_ASSERT_EQ(value, (uint64_t)0x0, "After-delete Find 5");
125
126 value = (uint64_t)os_map_find(&edge_64_map, 0x3);
127 T_ASSERT_EQ(value, (uint64_t)0x0, "After-delete find 6");
128
129 os_map_destroy(&edge_64_map);
130 }
131
132 T_DECL(map_edge_32, "Make sure 32 bit map edge cases work",
133 T_META("owner", "Core Darwin Daemons & Tools"))
134 {
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;
140 uint64_t value;
141
142 T_LOG("Start");
143
144 // *** BASIC 64 bit key testing ***
145
146 os_map_init(&edge_32_map, NULL);
147
148 T_ASSERT_EQ(os_map_count(&edge_32_map), 0, "Expect map to be empty");
149
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);
154
155 T_ASSERT_EQ(os_map_count(&edge_32_map), 4,
156 "Expect map to have 4 entries");
157
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);
161 if (key == 0x0) {
162 T_ASSERT_EQ(value, (void *)0xCAFEBABE,
163 "Callback expect 0xCAFEBABE");
164 got_cafebabe = true;
165 } else if (key == 0x1) {
166 T_ASSERT_EQ(value, (void *)0xDEADBEEF,
167 "Callback expect 0xDEADBEEF");
168 got_deadbeaf = true;
169 } else if (key == 0x2) {
170 T_ASSERT_EQ(value, (void *)0xBEEB8,
171 "Callback expect 0xBEEB8");
172 got_beebait = true;
173 } else if (key == 0x3) {
174 T_ASSERT_EQ(value, (void *)0xBADF00D,
175 "Callback expect 0xBADF00D");
176 got_badfood = true;
177 } else {
178 T_FAIL("Got unexpected callback 0x%llx, 0x%llx",
179 (unsigned long long)key,
180 (unsigned long long)value);
181 }
182 return true;
183 });
184
185 if (!got_cafebabe || !got_deadbeaf || !got_beebait || !got_badfood) {
186 T_FAIL("Failed to get callback");
187 }
188
189 value = (uint64_t)os_map_find(&edge_32_map, 0x0);
190 T_ASSERT_EQ(value, (uint64_t)0xCAFEBABE, "Find 1");
191
192 value = (uint64_t)os_map_find(&edge_32_map, 0x1);
193 T_ASSERT_EQ(value, (uint64_t)0xDEADBEEF, "Find 2");
194
195 value = (uint64_t)os_map_find(&edge_32_map, 0x2);
196 T_ASSERT_EQ(value, (uint64_t)0xBEEB8, "Find 3");
197
198 value = (uint64_t)os_map_find(&edge_32_map, 0x3);
199 T_ASSERT_EQ(value, (uint64_t)0xBADF00D, "Find 4");
200
201 os_map_delete(&edge_32_map, 0x0);
202 os_map_delete(&edge_32_map, 0x2);
203
204 T_ASSERT_EQ(os_map_count(&edge_32_map), 2,
205 "Expect map to have 2 entries");
206
207 value = (uint64_t)os_map_find(&edge_32_map, 0x0);
208 T_ASSERT_EQ(value, (uint64_t)0x0, "After-delete Find 1");
209
210 value = (uint64_t)os_map_find(&edge_32_map, 0x2);
211 T_ASSERT_EQ(value, (uint64_t)0x0, "After-delete Find 1");
212
213 value = (uint64_t)os_map_find(&edge_32_map, 0x1);
214 T_ASSERT_EQ(value, (uint64_t)0xDEADBEEF, "After-delete find 3");
215
216 value = (uint64_t)os_map_find(&edge_32_map, 0x3);
217 T_ASSERT_EQ(value, (uint64_t)0xBADF00D, "After-delete find 4");
218
219 os_map_delete(&edge_32_map, 0x1);
220 os_map_delete(&edge_32_map, 0x3);
221
222 T_ASSERT_EQ(os_map_count(&edge_32_map), 0, "Expect map to be empty");
223
224 value = (uint64_t)os_map_find(&edge_32_map, 0x1);
225 T_ASSERT_EQ(value, (uint64_t)0x0, "After-delete Find 5");
226
227 value = (uint64_t)os_map_find(&edge_32_map, 0x3);
228 T_ASSERT_EQ(value, (uint64_t)0x0, "After-delete find 6");
229
230 os_map_destroy(&edge_32_map);
231 }
232