Debian Codegraph @

c/tests/object_test.cpp

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. 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,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 #include "./pn_test.hpp"
23
24 #include <proton/object.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 using Catch::Matchers::Equals;
31
32 static char mem;
33 static void *END = &mem;
34
35 static pn_list_t *build_list(size_t capacity, ...) {
36 pn_list_t *result = pn_list(PN_OBJECT, capacity);
37 va_list ap;
38
39 va_start(ap, capacity);
40 while (true) {
41 void *arg = va_arg(ap, void *);
42 if (arg == END) {
43 break;
44 }
45
46 pn_list_add(result, arg);
47 pn_class_decref(PN_OBJECT, arg);
48 }
49 va_end(ap);
50
51 return result;
52 }
53
54 static pn_map_t *build_map(float load_factor, size_t capacity, ...) {
55 pn_map_t *result = pn_map(PN_OBJECT, PN_OBJECT, capacity, load_factor);
56 va_list ap;
57
58 void *prev = NULL;
59
60 va_start(ap, capacity);
61 int count = 0;
62 while (true) {
63 void *arg = va_arg(ap, void *);
64 bool last = arg == END;
65 if (arg == END) {
66 arg = NULL;
67 }
68
69 if (count % 2) {
70 pn_map_put(result, prev, arg);
71 pn_class_decref(PN_OBJECT, prev);
72 pn_class_decref(PN_OBJECT, arg);
73 } else {
74 prev = arg;
75 }
76
77 if (last) {
78 break;
79 }
80
81 count++;
82 }
83 va_end(ap);
84
85 return result;
86 }
87
88 static void noop(void *o) {}
89 static uintptr_t zero(void *o) { return 0; }
90 static intptr_t delta(void *a, void *b) { return (uintptr_t)b - (uintptr_t)a; }
91
92 #define CID_noop CID_pn_object
93 #define noop_initialize noop
94 #define noop_finalize noop
95 #define noop_hashcode zero
96 #define noop_compare delta
97 #define noop_inspect NULL
98
99 static const pn_class_t noop_class = PN_CLASS(noop);
100
101 static void test_class(const pn_class_t *clazz, size_t size) {
102 INFO("class=" << pn_class_name(clazz) << " size=" << size);
103 void *a = pn_class_new(clazz, size);
104 void *b = pn_class_new(clazz, size);
105
106 CHECK(!pn_class_equals(clazz, a, b));
107 CHECK(pn_class_equals(clazz, a, a));
108 CHECK(pn_class_equals(clazz, b, b));
109 CHECK(!pn_class_equals(clazz, a, NULL));
110 CHECK(!pn_class_equals(clazz, NULL, a));
111
112 int rca = pn_class_refcount(clazz, a);
113 int rcb = pn_class_refcount(clazz, b);
114
115 CHECK((rca == -1 || rca == 1));
116 CHECK((rcb == -1 || rcb == 1));
117
118 pn_class_incref(clazz, a);
119
120 rca = pn_class_refcount(clazz, a);
121 CHECK((rca == -1 || rca == 2));
122
123 pn_class_decref(clazz, a);
124
125 rca = pn_class_refcount(clazz, a);
126 CHECK((rca == -1 || rca == 1));
127
128 pn_class_free(clazz, a);
129 pn_class_free(clazz, b);
130 }
131
132 TEST_CASE("object_class") {
133 test_class(PN_OBJECT, 0);
134 test_class(PN_VOID, 5);
135 test_class(&noop_class, 128);
136 }
137
138 static void test_new(size_t size, const pn_class_t *clazz) {
139 INFO("class=" << pn_class_name(clazz) << " size=" << size);
140 void *obj = pn_class_new(clazz, size);
141 REQUIRE(obj);
142 CHECK(pn_class_refcount(PN_OBJECT, obj) == 1);
143 CHECK(pn_class(obj) == clazz);
144 char *bytes = (char *)obj;
145 for (size_t i = 0; i < size; i++) {
146 // touch everything for valgrind
147 bytes[i] = i;
148 }
149 pn_free(obj);
150 }
151
152 TEST_CASE("object_class new") {
153 test_new(0, PN_OBJECT);
154 test_new(5, PN_OBJECT);
155 test_new(128, &noop_class);
156 }
157
158 static void finalizer(void *object) {
159 int **called = (int **)object;
160 (**called)++;
161 }
162
163 #define CID_finalizer CID_pn_object
164 #define finalizer_initialize NULL
165 #define finalizer_finalize finalizer
166 #define finalizer_hashcode NULL
167 #define finalizer_compare NULL
168 #define finalizer_inspect NULL
169
170 TEST_CASE("object_finalize") {
171 static pn_class_t clazz = PN_CLASS(finalizer);
172
173 int **obj = (int **)pn_class_new(&clazz, sizeof(int *));
174 REQUIRE(obj);
175
176 int called = 0;
177 *obj = &called;
178 pn_free(obj);
179
180 CHECK(called == 1);
181 }
182
183 TEST_CASE("object_free") {
184 // just to make sure it doesn't seg fault or anything
185 pn_free(NULL);
186 }
187
188 static uintptr_t hashcode(void *obj) { return (uintptr_t)obj; }
189
190 #define CID_hashcode CID_pn_object
191 #define hashcode_initialize NULL
192 #define hashcode_finalize NULL
193 #define hashcode_compare NULL
194 #define hashcode_hashcode hashcode
195 #define hashcode_inspect NULL
196
197 TEST_CASE("object_hashcode") {
198 static pn_class_t clazz = PN_CLASS(hashcode);
199 void *obj = pn_class_new(&clazz, 0);
200 REQUIRE(obj);
201 CHECK(pn_hashcode(obj) == (uintptr_t)obj);
202 CHECK(pn_hashcode(NULL) == 0);
203 pn_free(obj);
204 }
205
206 #define CID_compare CID_pn_object
207 #define compare_initialize NULL
208 #define compare_finalize NULL
209 #define compare_compare delta
210 #define compare_hashcode NULL
211 #define compare_inspect NULL
212
213 TEST_CASE("object_compare") {
214 static pn_class_t clazz = PN_CLASS(compare);
215
216 void *a = pn_class_new(&clazz, 0);
217 REQUIRE(a);
218 void *b = pn_class_new(&clazz, 0);
219 REQUIRE(b);
220
221 CHECK(pn_compare(a, b));
222 CHECK(!pn_equals(a, b));
223 CHECK(!pn_compare(a, a));
224 CHECK(pn_equals(a, a));
225 CHECK(!pn_compare(b, b));
226 CHECK(pn_equals(b, b));
227 CHECK(pn_compare(a, b) == (intptr_t)((uintptr_t)b - (uintptr_t)a));
228
229 CHECK(pn_compare(NULL, b));
230 CHECK(!pn_equals(NULL, b));
231
232 CHECK(pn_compare(a, NULL));
233 CHECK(!pn_equals(a, NULL));
234
235 CHECK(!pn_compare(NULL, NULL));
236 CHECK(pn_equals(NULL, NULL));
237
238 pn_free(a);
239 pn_free(b);
240 }
241
242 TEST_CASE("object_refcounting") {
243 int refs = 3;
244 void *obj = pn_class_new(PN_OBJECT, 0);
245
246 CHECK(pn_refcount(obj) == 1);
247
248 for (int i = 0; i < refs; i++) {
249 pn_incref(obj);
250 CHECK(pn_refcount(obj) == i + 2);
251 }
252
253 CHECK(pn_refcount(obj) == refs + 1);
254
255 for (int i = 0; i < refs; i++) {
256 pn_decref(obj);
257 CHECK(pn_refcount(obj) == refs - i);
258 }
259
260 CHECK(pn_refcount(obj) == 1);
261
262 pn_free(obj);
263 }
264
265 TEST_CASE("list") {
266 pn_list_t *list = pn_list(PN_WEAKREF, 0);
267 CHECK(pn_list_size(list) == 0);
268 CHECK(!pn_list_add(list, (void *)0));
269 CHECK(!pn_list_add(list, (void *)1));
270 CHECK(!pn_list_add(list, (void *)2));
271 CHECK(!pn_list_add(list, (void *)3));
272 CHECK(pn_list_get(list, 0) == (void *)0);
273 CHECK(pn_list_get(list, 1) == (void *)1);
274 CHECK(pn_list_get(list, 2) == (void *)2);
275 CHECK(pn_list_get(list, 3) == (void *)3);
276 CHECK(pn_list_size(list) == 4);
277 pn_list_del(list, 1, 2);
278 CHECK(pn_list_size(list) == 2);
279 CHECK(pn_list_get(list, 0) == (void *)0);
280 CHECK(pn_list_get(list, 1) == (void *)3);
281 pn_decref(list);
282 }
283
284 TEST_CASE("list_refcount") {
285 void *one = pn_class_new(PN_OBJECT, 0);
286 void *two = pn_class_new(PN_OBJECT, 0);
287 void *three = pn_class_new(PN_OBJECT, 0);
288 void *four = pn_class_new(PN_OBJECT, 0);
289
290 pn_list_t *list = pn_list(PN_OBJECT, 0);
291 CHECK(!pn_list_add(list, one));
292 CHECK(!pn_list_add(list, two));
293 CHECK(!pn_list_add(list, three));
294 CHECK(!pn_list_add(list, four));
295 CHECK(pn_list_get(list, 0) == one);
296 CHECK(pn_list_get(list, 1) == two);
297 CHECK(pn_list_get(list, 2) == three);
298 CHECK(pn_list_get(list, 3) == four);
299 CHECK(pn_list_size(list) == 4);
300
301 CHECK(pn_refcount(one) == 2);
302 CHECK(pn_refcount(two) == 2);
303 CHECK(pn_refcount(three) == 2);
304 CHECK(pn_refcount(four) == 2);
305
306 pn_list_del(list, 1, 2);
307 CHECK(pn_list_size(list) == 2);
308
309 CHECK(pn_refcount(one) == 2);
310 CHECK(pn_refcount(two) == 1);
311 CHECK(pn_refcount(three) == 1);
312 CHECK(pn_refcount(four) == 2);
313
314 CHECK(pn_list_get(list, 0) == one);
315 CHECK(pn_list_get(list, 1) == four);
316
317 CHECK(!pn_list_add(list, one));
318
319 CHECK(pn_list_size(list) == 3);
320 CHECK(pn_refcount(one) == 3);
321
322 pn_decref(list);
323
324 CHECK(pn_refcount(one) == 1);
325 CHECK(pn_refcount(two) == 1);
326 CHECK(pn_refcount(three) == 1);
327 CHECK(pn_refcount(four) == 1);
328
329 pn_decref(one);
330 pn_decref(two);
331 pn_decref(three);
332 pn_decref(four);
333 }
334
335 #define check_list_index(list, value, idx) \
336 CHECK(pn_list_index(list, value) == idx)
337
338 TEST_CASE("list_index") {
339 pn_list_t *l = pn_list(PN_WEAKREF, 0);
340 void *one = pn_string("one");
341 void *two = pn_string("two");
342 void *three = pn_string("three");
343 void *dup1 = pn_string("dup");
344 void *dup2 = pn_string("dup");
345 void *last = pn_string("last");
346
347 pn_list_add(l, one);
348 pn_list_add(l, two);
349 pn_list_add(l, three);
350 pn_list_add(l, dup1);
351 pn_list_add(l, dup2);
352 pn_list_add(l, last);
353
354 check_list_index(l, one, 0);
355 check_list_index(l, two, 1);
356 check_list_index(l, three, 2);
357 check_list_index(l, dup1, 3);
358 check_list_index(l, dup2, 3);
359 check_list_index(l, last, 5);
360
361 void *nonexistent = pn_string("nonexistent");
362
363 check_list_index(l, nonexistent, -1);
364
365 pn_free(l);
366 pn_free(one);
367 pn_free(two);
368 pn_free(three);
369 pn_free(dup1);
370 pn_free(dup2);
371 pn_free(last);
372 pn_free(nonexistent);
373 }
374
375 TEST_CASE("list_build") {
376 pn_list_t *l = build_list(0, pn_string("one"), pn_string("two"),
377 pn_string("three"), END);
378
379 REQUIRE(pn_list_size(l) == 3);
380
381 CHECK_THAT(pn_string_get((pn_string_t *)pn_list_get(l, 0)), Equals("one"));
382 CHECK_THAT(pn_string_get((pn_string_t *)pn_list_get(l, 1)), Equals("two"));
383 CHECK_THAT(pn_string_get((pn_string_t *)pn_list_get(l, 2)), Equals("three"));
384 pn_free(l);
385 }
386
387 TEST_CASE("map_build") {
388 pn_map_t *m = build_map(0.75, 0, pn_string("key"), pn_string("value"),
389 pn_string("key2"), pn_string("value2"), END);
390
391 CHECK(pn_map_size(m) == 2);
392
393 pn_string_t *key = pn_string(NULL);
394
395 pn_string_set(key, "key");
396 CHECK_THAT(pn_string_get((pn_string_t *)pn_map_get(m, key)), Equals("value"));
397 pn_string_set(key, "key2");
398 CHECK_THAT(pn_string_get((pn_string_t *)pn_map_get(m, key)),
399 Equals("value2"));
400
401 pn_free(m);
402 pn_free(key);
403 }
404
405 TEST_CASE("map_build_odd") {
406 pn_map_t *m =
407 build_map(0.75, 0, pn_string("key"), pn_string("value"),
408 pn_string("key2"), pn_string("value2"), pn_string("key3"), END);
409
410 CHECK(pn_map_size(m) == 3);
411
412 pn_string_t *key = pn_string(NULL);
413
414 pn_string_set(key, "key");
415 CHECK_THAT(pn_string_get((pn_string_t *)pn_map_get(m, key)), Equals("value"));
416 pn_string_set(key, "key2");
417 CHECK_THAT(pn_string_get((pn_string_t *)pn_map_get(m, key)),
418 Equals("value2"));
419 pn_string_set(key, "key3");
420 CHECK(pn_map_get(m, key) == NULL);
421
422 pn_free(m);
423 pn_free(key);
424 }
425
426 TEST_CASE("map") {
427 void *one = pn_class_new(PN_OBJECT, 0);
428 void *two = pn_class_new(PN_OBJECT, 0);
429 void *three = pn_class_new(PN_OBJECT, 0);
430
431 pn_map_t *map = pn_map(PN_OBJECT, PN_OBJECT, 4, 0.75);
432 CHECK(pn_map_size(map) == 0);
433
434 pn_string_t *key = pn_string("key");
435 pn_string_t *dup = pn_string("key");
436 pn_string_t *key1 = pn_string("key1");
437 pn_string_t *key2 = pn_string("key2");
438
439 CHECK(!pn_map_put(map, key, one));
440 CHECK(pn_map_size(map) == 1);
441 CHECK(!pn_map_put(map, key1, two));
442 CHECK(pn_map_size(map) == 2);
443 CHECK(!pn_map_put(map, key2, three));
444 CHECK(pn_map_size(map) == 3);
445
446 CHECK(pn_map_get(map, dup) == one);
447
448 CHECK(!pn_map_put(map, dup, one));
449 CHECK(pn_map_size(map) == 3);
450
451 CHECK(!pn_map_put(map, dup, two));
452 CHECK(pn_map_size(map) == 3);
453 CHECK(pn_map_get(map, dup) == two);
454
455 CHECK(pn_refcount(key) == 2);
456 CHECK(pn_refcount(dup) == 1);
457 CHECK(pn_refcount(key1) == 2);
458 CHECK(pn_refcount(key2) == 2);
459
460 CHECK(pn_refcount(one) == 1);
461 CHECK(pn_refcount(two) == 3);
462 CHECK(pn_refcount(three) == 2);
463
464 pn_map_del(map, key1);
465 CHECK(pn_map_size(map) == 2);
466
467 CHECK(pn_refcount(key) == 2);
468 CHECK(pn_refcount(dup) == 1);
469 CHECK(pn_refcount(key1) == 1);
470 CHECK(pn_refcount(key2) == 2);
471
472 CHECK(pn_refcount(one) == 1);
473 CHECK(pn_refcount(two) == 2);
474 CHECK(pn_refcount(three) == 2);
475
476 pn_decref(one);
477 pn_decref(two);
478 pn_decref(three);
479
480 pn_decref(key);
481 pn_decref(dup);
482 pn_decref(key1);
483 pn_decref(key2);
484
485 pn_decref(map);
486 }
487
488 TEST_CASE("hash") {
489 void *one = pn_class_new(PN_OBJECT, 0);
490 void *two = pn_class_new(PN_OBJECT, 0);
491 void *three = pn_class_new(PN_OBJECT, 0);
492
493 pn_hash_t *hash = pn_hash(PN_OBJECT, 4, 0.75);
494 pn_hash_put(hash, 0, NULL);
495 pn_hash_put(hash, 1, one);
496 pn_hash_put(hash, 2, two);
497 pn_hash_put(hash, 3, three);
498 pn_hash_put(hash, 4, one);
499 pn_hash_put(hash, 5, two);
500 pn_hash_put(hash, 6, three);
501 pn_hash_put(hash, 7, one);
502 pn_hash_put(hash, 8, two);
503 pn_hash_put(hash, 9, three);
504 pn_hash_put(hash, 10, one);
505 pn_hash_put(hash, 11, two);
506 pn_hash_put(hash, 12, three);
507 pn_hash_put(hash, 18, one);
508
509 CHECK(pn_hash_get(hash, 2) == two);
510 CHECK(pn_hash_get(hash, 5) == two);
511 CHECK(pn_hash_get(hash, 18) == one);
512 CHECK(pn_hash_get(hash, 0) == NULL);
513
514 CHECK(pn_hash_size(hash) == 14);
515
516 pn_hash_del(hash, 5);
517 CHECK(pn_hash_get(hash, 5) == NULL);
518 CHECK(pn_hash_size(hash) == 13);
519 pn_hash_del(hash, 18);
520 CHECK(pn_hash_get(hash, 18) == NULL);
521 CHECK(pn_hash_size(hash) == 12);
522
523 pn_decref(hash);
524
525 pn_decref(one);
526 pn_decref(two);
527 pn_decref(three);
528 }
529
530 // collider class: all objects have same hash, no two objects compare equal
531 static intptr_t collider_compare(void *a, void *b) {
532 if (a == b) return 0;
533 return (a > b) ? 1 : -1;
534 }
535
536 static uintptr_t collider_hashcode(void *obj) { return 23; }
537
538 #define CID_collider CID_pn_object
539 #define collider_initialize NULL
540 #define collider_finalize NULL
541 #define collider_inspect NULL
542
543 TEST_CASE("map_links") {
544 const pn_class_t collider_clazz = PN_CLASS(collider);
545 void *keys[3];
546 for (int i = 0; i < 3; i++) keys[i] = pn_class_new(&collider_clazz, 0);
547
548 // test deleting a head, middle link, tail
549
550 for (int delete_idx = 0; delete_idx < 3; delete_idx++) {
551 pn_map_t *map = pn_map(PN_WEAKREF, PN_WEAKREF, 0, 0.75);
552 // create a chain of entries that have same head (from identical key
553 // hashcode)
554 for (int i = 0; i < 3; i++) {
555 pn_map_put(map, keys[i], keys[i]);
556 }
557 pn_map_del(map, keys[delete_idx]);
558 for (int i = 0; i < 3; i++) {
559 void *value = (i == delete_idx) ? NULL : keys[i];
560 CHECK(pn_map_get(map, keys[i]) == value);
561 }
562 pn_free(map);
563 }
564 for (int i = 0; i < 3; i++) pn_free(keys[i]);
565 }
566
567 static void test_string(const char *value) {
568 size_t size = value ? strlen(value) : 0;
569
570 pn_string_t *str = pn_string(value);
571 CHECK_THAT(value, Equals(pn_string_get(str)));
572 CHECK(size == pn_string_size(str));
573
574 pn_string_t *strn = pn_stringn(value, size);
575 CHECK_THAT(value, Equals(pn_string_get(strn)));
576 CHECK(size == pn_string_size(strn));
577
578 pn_string_t *strset = pn_string(NULL);
579 pn_string_set(strset, value);
580 CHECK_THAT(value, Equals(pn_string_get(strset)));
581 CHECK(size == pn_string_size(strset));
582
583 pn_string_t *strsetn = pn_string(NULL);
584 pn_string_setn(strsetn, value, size);
585 CHECK_THAT(value, Equals(pn_string_get(strsetn)));
586 CHECK(size == pn_string_size(strsetn));
587
588 CHECK(pn_hashcode(str) == pn_hashcode(strn));
589 CHECK(pn_hashcode(str) == pn_hashcode(strset));
590 CHECK(pn_hashcode(str) == pn_hashcode(strsetn));
591
592 CHECK(!pn_compare(str, str));
593 CHECK(!pn_compare(str, strn));
594 CHECK(!pn_compare(str, strset));
595 CHECK(!pn_compare(str, strsetn));
596
597 pn_free(str);
598 pn_free(strn);
599 pn_free(strset);
600 pn_free(strsetn);
601 }
602
603 TEST_CASE("string_null") { test_string(NULL); }
604 TEST_CASE("string_empty") { test_string(""); }
605 TEST_CASE("string_simple") { test_string("this is a test"); }
606 TEST_CASE("string_long") {
607 test_string(
608 "012345678910111213151617181920212223242526272829303132333435363"
609 "738394041424344454647484950515253545556575859606162636465666768");
610 }
611
612 TEST_CASE("string embedded null") {
613 const char value[] = "this has an embedded \000 in it";
614 size_t size = sizeof(value);
615
616 pn_string_t *strn = pn_stringn(value, size);
617 CHECK_THAT(value, Equals(pn_string_get(strn)));
618 CHECK(pn_string_size(strn) == size);
619
620 pn_string_t *strsetn = pn_string(NULL);
621 pn_string_setn(strsetn, value, size);
622 CHECK_THAT(value, Equals(pn_string_get(strsetn)));
623 CHECK(pn_string_size(strsetn) == size);
624
625 CHECK(pn_hashcode(strn) == pn_hashcode(strsetn));
626 CHECK(!pn_compare(strn, strsetn));
627
628 pn_free(strn);
629 pn_free(strsetn);
630 }
631
632 TEST_CASE("string_format") {
633 pn_string_t *str = pn_string("");
634 CHECK(str);
635 int err = pn_string_format(str, "%s",
636 "this is a string that should be long "
637 "enough to force growth but just in case we'll "
638 "tack this other really long string on for the "
639 "heck of it");
640 CHECK(err == 0);
641 pn_free(str);
642 }
643
644 TEST_CASE("string_addf") {
645 pn_string_t *str = pn_string("hello ");
646 CHECK(str);
647 int err = pn_string_addf(str, "%s",
648 "this is a string that should be long "
649 "enough to force growth but just in case we'll "
650 "tack this other really long string on for the "
651 "heck of it");
652 CHECK(err == 0);
653 pn_free(str);
654 }
655
656 TEST_CASE("map_iteration") {
657 int n = 5;
658 pn_list_t *pairs = pn_list(PN_OBJECT, 2 * n);
659 for (int i = 0; i < n; i++) {
660 void *key = pn_class_new(PN_OBJECT, 0);
661 void *value = pn_class_new(PN_OBJECT, 0);
662 pn_list_add(pairs, key);
663 pn_list_add(pairs, value);
664 pn_decref(key);
665 pn_decref(value);
666 }
667
668 pn_map_t *map = pn_map(PN_OBJECT, PN_OBJECT, 0, 0.75);
669
670 CHECK(pn_map_head(map) == 0);
671
672 for (int i = 0; i < n; i++) {
673 pn_map_put(map, pn_list_get(pairs, 2 * i), pn_list_get(pairs, 2 * i + 1));
674 }
675
676 for (pn_handle_t entry = pn_map_head(map); entry;
677 entry = pn_map_next(map, entry)) {
678 void *key = pn_map_key(map, entry);
679 void *value = pn_map_value(map, entry);
680 ssize_t idx = pn_list_index(pairs, key);
681 CHECK(idx >= 0);
682
683 CHECK(pn_list_get(pairs, idx) == key);
684 CHECK(pn_list_get(pairs, idx + 1) == value);
685
686 pn_list_del(pairs, idx, 2);
687 }
688
689 CHECK(pn_list_size(pairs) == 0);
690
691 pn_decref(map);
692 pn_decref(pairs);
693 }
694
695 #define test_inspect(o, expected) \
696 do { \
697 pn_string_t *dst = pn_string(NULL); \
698 pn_inspect(o, dst); \
699 CHECK_THAT(expected, Equals(pn_string_get(dst))); \
700 pn_free(dst); \
701 } while (0)
702
703 TEST_CASE("list_inspect") {
704 pn_list_t *l = build_list(0, END);
705 test_inspect(l, "[]");
706 pn_free(l);
707
708 l = build_list(0, pn_string("one"), END);
709 test_inspect(l, "[\"one\"]");
710 pn_free(l);
711
712 l = build_list(0, pn_string("one"), pn_string("two"), END);
713 test_inspect(l, "[\"one\", \"two\"]");
714 pn_free(l);
715
716 l = build_list(0, pn_string("one"), pn_string("two"), pn_string("three"),
717 END);
718 test_inspect(l, "[\"one\", \"two\", \"three\"]");
719 pn_free(l);
720 }
721
722 TEST_CASE("map_inspect") {
723 // note that when there is more than one entry in a map, the order
724 // of the entries is dependent on the hashes involved, it will be
725 // deterministic though
726 pn_map_t *m = build_map(0.75, 0, END);
727 test_inspect(m, "{}");
728 pn_free(m);
729
730 m = build_map(0.75, 0, pn_string("key"), pn_string("value"), END);
731 test_inspect(m, "{\"key\": \"value\"}");
732 pn_free(m);
733
734 m = build_map(0.75, 0, pn_string("k1"), pn_string("v1"), pn_string("k2"),
735 pn_string("v2"), END);
736 test_inspect(m, "{\"k1\": \"v1\", \"k2\": \"v2\"}");
737 pn_free(m);
738
739 m = build_map(0.75, 0, pn_string("k1"), pn_string("v1"), pn_string("k2"),
740 pn_string("v2"), pn_string("k3"), pn_string("v3"), END);
741 test_inspect(m, "{\"k3\": \"v3\", \"k1\": \"v1\", \"k2\": \"v2\"}");
742 pn_free(m);
743 }
744
745 TEST_CASE("map_coalesced_chain") {
746 pn_hash_t *map = pn_hash(PN_OBJECT, 16, 0.75);
747 pn_string_t *values[9] = {pn_string("a"), pn_string("b"), pn_string("c"),
748 pn_string("d"), pn_string("e"), pn_string("f"),
749 pn_string("g"), pn_string("h"), pn_string("i")};
750 // add some items:
751 pn_hash_put(map, 1, values[0]);
752 pn_hash_put(map, 2, values[1]);
753 pn_hash_put(map, 3, values[2]);
754
755 // use up all non-addressable elements:
756 pn_hash_put(map, 14, values[3]);
757 pn_hash_put(map, 15, values[4]);
758 pn_hash_put(map, 16, values[5]);
759
760 // use an addressable element for a key that doesn't map to it:
761 pn_hash_put(map, 4, values[6]);
762 pn_hash_put(map, 17, values[7]);
763 CHECK(pn_hash_size(map) == 8);
764
765 // free up one non-addressable entry:
766 pn_hash_del(map, 16);
767 CHECK(pn_hash_get(map, 16) == NULL);
768 CHECK(pn_hash_size(map) == 7);
769
770 // add a key whose addressable slot is already taken (by 17),
771 // generating a coalesced chain:
772 pn_hash_put(map, 12, values[8]);
773
774 // remove an entry from the coalesced chain:
775 pn_hash_del(map, 4);
776 CHECK(pn_hash_get(map, 4) == NULL);
777
778 // test lookup of all entries:
779 CHECK_THAT(pn_string_get((pn_string_t *)pn_hash_get(map, 1)), Equals("a"));
780 CHECK_THAT(pn_string_get((pn_string_t *)pn_hash_get(map, 2)), Equals("b"));
781 CHECK_THAT(pn_string_get((pn_string_t *)pn_hash_get(map, 3)), Equals("c"));
782 CHECK_THAT(pn_string_get((pn_string_t *)pn_hash_get(map, 14)), Equals("d"));
783 CHECK_THAT(pn_string_get((pn_string_t *)pn_hash_get(map, 15)), Equals("e"));
784 CHECK_THAT(pn_string_get((pn_string_t *)pn_hash_get(map, 17)), Equals("h"));
785 CHECK_THAT(pn_string_get((pn_string_t *)pn_hash_get(map, 12)), Equals("i"));
786 CHECK(pn_hash_size(map) == 7);
787
788 // cleanup:
789 for (pn_handle_t i = pn_hash_head(map); i; i = pn_hash_head(map)) {
790 pn_hash_del(map, pn_hash_key(map, i));
791 }
792 CHECK(pn_hash_size(map) == 0);
793
794 for (size_t i = 0; i < 9; ++i) {
795 pn_free(values[i]);
796 }
797 pn_free(map);
798 }
799
800 TEST_CASE("map_coalesced_chain2") {
801 pn_hash_t *map = pn_hash(PN_OBJECT, 16, 0.75);
802 pn_string_t *values[10] = {pn_string("a"), pn_string("b"), pn_string("c"),
803 pn_string("d"), pn_string("e"), pn_string("f"),
804 pn_string("g"), pn_string("h"), pn_string("i"),
805 pn_string("j")};
806 // add some items:
807 pn_hash_put(map, 1, values[0]); // a
808 pn_hash_put(map, 2, values[1]); // b
809 pn_hash_put(map, 3, values[2]); // c
810
811 // use up all non-addressable elements:
812 pn_hash_put(map, 14, values[3]); // d
813 pn_hash_put(map, 15, values[4]); // e
814 pn_hash_put(map, 16, values[5]); // f
815 // take slot from addressable region
816 pn_hash_put(map, 29, values[6]); // g, goes into slot 12
817
818 // free up one non-addressable entry:
819 pn_hash_del(map, 14);
820 CHECK(pn_hash_get(map, 14) == NULL);
821
822 // add a key whose addressable slot is already taken (by 29),
823 // generating a coalesced chain:
824 pn_hash_put(map, 12, values[7]); // h
825 CHECK_THAT(pn_string_get((pn_string_t *)pn_hash_get(map, 12)), Equals("h"));
826 // delete from tail of coalesced chain:
827 pn_hash_del(map, 12);
828 CHECK(pn_hash_get(map, 12) == NULL);
829
830 // extend chain into cellar again, then coalesce again extending back
831 // into addressable region
832 pn_hash_put(map, 42, values[8]); // i
833 pn_hash_put(map, 25, values[9]); // j
834 // delete entry from coalesced chain, where next element in chain is
835 // in cellar:
836 CHECK_THAT(pn_string_get((pn_string_t *)pn_hash_get(map, 29)), Equals("g"));
837 pn_hash_del(map, 29);
838
839 // test lookup of all entries:
840 CHECK_THAT(pn_string_get((pn_string_t *)pn_hash_get(map, 1)), Equals("a"));
841 CHECK_THAT(pn_string_get((pn_string_t *)pn_hash_get(map, 2)), Equals("b"));
842 CHECK_THAT(pn_string_get((pn_string_t *)pn_hash_get(map, 3)), Equals("c"));
843 // d was deleted
844 CHECK_THAT(pn_string_get((pn_string_t *)pn_hash_get(map, 15)), Equals("e"));
845 CHECK_THAT(pn_string_get((pn_string_t *)pn_hash_get(map, 16)), Equals("f"));
846 // g was deleted, h was deleted
847 CHECK_THAT(pn_string_get((pn_string_t *)pn_hash_get(map, 42)), Equals("i"));
848 CHECK_THAT(pn_string_get((pn_string_t *)pn_hash_get(map, 25)), Equals("j"));
849 CHECK(pn_hash_size(map) == 7);
850
851 // cleanup:
852 for (pn_handle_t i = pn_hash_head(map); i; i = pn_hash_head(map)) {
853 pn_hash_del(map, pn_hash_key(map, i));
854 }
855 CHECK(pn_hash_size(map) == 0);
856
857 for (size_t i = 0; i < 10; ++i) {
858 pn_free(values[i]);
859 }
860 pn_free(map);
861 }
862
863 TEST_CASE("list_compare") {
864 pn_list_t *a = pn_list(PN_OBJECT, 0);
865 pn_list_t *b = pn_list(PN_OBJECT, 0);
866
867 CHECK(pn_equals(a, b));
868
869 void *one = pn_class_new(PN_OBJECT, 0);
870 void *two = pn_class_new(PN_OBJECT, 0);
871 void *three = pn_class_new(PN_OBJECT, 0);
872
873 pn_list_add(a, one);
874 CHECK(!pn_equals(a, b));
875 pn_list_add(b, one);
876 CHECK(pn_equals(a, b));
877
878 pn_list_add(b, two);
879 CHECK(!pn_equals(a, b));
880 pn_list_add(a, two);
881 CHECK(pn_equals(a, b));
882
883 pn_list_add(a, three);
884 CHECK(!pn_equals(a, b));
885 pn_list_add(b, three);
886 CHECK(pn_equals(a, b));
887
888 pn_free(a);
889 pn_free(b);
890 pn_free(one);
891 pn_free(two);
892 pn_free(three);
893 }
894
895 typedef struct {
896 pn_list_t *list;
897 size_t index;
898 } pn_it_state_t;
899
900 static void *pn_it_next(void *state) {
901 pn_it_state_t *it = (pn_it_state_t *)state;
902 if (it->index < pn_list_size(it->list)) {
903 return pn_list_get(it->list, it->index++);
904 } else {
905 return NULL;
906 }
907 }
908
909 TEST_CASE("list_iterator") {
910 pn_list_t *list = build_list(0, pn_string("one"), pn_string("two"),
911 pn_string("three"), pn_string("four"), END);
912 pn_iterator_t *it = pn_iterator();
913 pn_it_state_t *state =
914 (pn_it_state_t *)pn_iterator_start(it, pn_it_next, sizeof(pn_it_state_t));
915 state->list = list;
916 state->index = 0;
917
918 void *obj;
919 int index = 0;
920 while ((obj = pn_iterator_next(it))) {
921 CHECK(obj == pn_list_get(list, index));
922 ++index;
923 }
924 CHECK(index == 4);
925
926 pn_free(list);
927 pn_free(it);
928 }
929
930 TEST_CASE("list_heap") {
931 int size = 64;
932 pn_list_t *list = pn_list(PN_VOID, 0);
933
934 intptr_t min = 0;
935 intptr_t max = 0;
936
937 for (int i = 0; i < size; i++) {
938 intptr_t r = rand();
939
940 if (i == 0) {
941 min = r;
942 max = r;
943 } else {
944 if (r < min) {
945 min = r;
946 }
947 if (r > max) {
948 max = r;
949 }
950 }
951
952 pn_list_minpush(list, (void *)r);
953 }
954
955 intptr_t prev = (intptr_t)pn_list_minpop(list);
956 CHECK(prev == min);
957 CHECK(pn_list_size(list) == (size_t)(size - 1));
958 int count = 0;
959 while (pn_list_size(list)) {
960 intptr_t r = (intptr_t)pn_list_minpop(list);
961 CHECK(r >= prev);
962 prev = r;
963 count++;
964 }
965 CHECK(count == size - 1);
966 CHECK(prev == max);
967
968 pn_free(list);
969 }