397 CxMempool *src = cxMempoolCreateSimple(4); |
397 CxMempool *src = cxMempoolCreateSimple(4); |
398 CxMempool *dest = cxMempoolCreateSimple(4); |
398 CxMempool *dest = cxMempoolCreateSimple(4); |
399 CX_TEST_DO { |
399 CX_TEST_DO { |
400 // allocate the first object |
400 // allocate the first object |
401 int *c = cxMalloc(src->allocator, sizeof(int)); |
401 int *c = cxMalloc(src->allocator, sizeof(int)); |
|
402 // check that the destructor functions are also transferred |
|
403 cxMempoolSetDestructor(c, test_mempool_destructor); |
|
404 // allocate the second object |
|
405 c = cxMalloc(src->allocator, sizeof(int)); |
|
406 cxMempoolSetDestructor(c, test_mempool_destructor); |
|
407 |
|
408 |
|
409 // check source pool |
|
410 CX_TEST_ASSERT(src->size == 2); |
|
411 CX_TEST_ASSERT(src->registered_size == 0); |
|
412 const CxAllocator *old_allocator = src->allocator; |
|
413 CX_TEST_ASSERT(old_allocator->data == src); |
|
414 |
|
415 // perform transfer |
|
416 int result = cxMempoolTransfer(src, dest); |
|
417 CX_TEST_ASSERT(result == 0); |
|
418 |
|
419 // check transfer |
|
420 CX_TEST_ASSERT(src->size == 0); |
|
421 CX_TEST_ASSERT(dest->size == 2); |
|
422 CX_TEST_ASSERT(src->registered_size == 0); |
|
423 CX_TEST_ASSERT(dest->registered_size == 1); // the old allocator |
|
424 CX_TEST_ASSERT(src->allocator != old_allocator); |
|
425 CX_TEST_ASSERT(old_allocator->data == dest); |
|
426 |
|
427 // verify that destroying old pool does nothing |
|
428 test_mempool_destructor_called = 0; |
|
429 cxMempoolFree(src); |
|
430 CX_TEST_ASSERT(test_mempool_destructor_called == 0); |
|
431 |
|
432 // cover illegal arguments |
|
433 result = cxMempoolTransfer(dest, dest); |
|
434 CX_TEST_ASSERT(result != 0); |
|
435 |
|
436 // verify that destroying new pool calls the destructors |
|
437 // but only two times (the old allocator has a different destructor) |
|
438 cxMempoolFree(dest); |
|
439 CX_TEST_ASSERT(test_mempool_destructor_called == 2); |
|
440 } |
|
441 } |
|
442 |
|
443 CX_TEST(test_mempool_transfer_with_foreign_memory) { |
|
444 CxMempool *src = cxMempoolCreateSimple(4); |
|
445 CxMempool *dest = cxMempoolCreateSimple(4); |
|
446 CX_TEST_DO { |
|
447 // allocate the first object |
|
448 int *c = cxMalloc(src->allocator, sizeof(int)); |
402 // allocate the second object |
449 // allocate the second object |
403 c = cxMalloc(src->allocator, sizeof(int)); |
450 c = cxMalloc(src->allocator, sizeof(int)); |
404 // check that the destructor functions are also transferred |
451 // check that the destructor functions are also transferred |
405 cxMempoolSetDestructor(c, test_mempool_destructor); |
452 cxMempoolSetDestructor(c, test_mempool_destructor); |
406 // register foreign object |
453 // register foreign object |
428 // verify that destroying old pool does nothing |
475 // verify that destroying old pool does nothing |
429 test_mempool_destructor_called = 0; |
476 test_mempool_destructor_called = 0; |
430 cxMempoolFree(src); |
477 cxMempoolFree(src); |
431 CX_TEST_ASSERT(test_mempool_destructor_called == 0); |
478 CX_TEST_ASSERT(test_mempool_destructor_called == 0); |
432 |
479 |
433 // cover illegal arguments |
|
434 result = cxMempoolTransfer(dest, dest); |
|
435 CX_TEST_ASSERT(result != 0); |
|
436 |
|
437 // verify that destroying new pool calls the destructors |
480 // verify that destroying new pool calls the destructors |
438 // but only two times (the old allocator has a different destructor) |
481 // but only two times (the old allocator has a different destructor) |
439 cxMempoolFree(dest); |
482 cxMempoolFree(dest); |
440 CX_TEST_ASSERT(test_mempool_destructor_called == 2); |
483 CX_TEST_ASSERT(test_mempool_destructor_called == 2); |
441 |
484 |
442 // free the foreign object |
485 // free the foreign object |
443 free(c); |
486 free(c); |
444 } |
487 } |
|
488 } |
|
489 |
|
490 CX_TEST(test_mempool_transfer_foreign_memory_only) { |
|
491 CxMempool *src = cxMempoolCreateSimple(4); |
|
492 CxMempool *dest = cxMempoolCreateSimple(4); |
|
493 int *a = malloc(sizeof(int)); |
|
494 int *b = malloc(sizeof(int)); |
|
495 CX_TEST_DO { |
|
496 // register foreign objects |
|
497 cxMempoolRegister(src, a, test_mempool_destructor); |
|
498 cxMempoolRegister(src, b, test_mempool_destructor); |
|
499 |
|
500 // check source pool |
|
501 CX_TEST_ASSERT(src->size == 0); |
|
502 CX_TEST_ASSERT(src->registered_size == 2); |
|
503 const CxAllocator *old_allocator = src->allocator; |
|
504 CX_TEST_ASSERT(old_allocator->data == src); |
|
505 |
|
506 // perform transfer |
|
507 int result = cxMempoolTransfer(src, dest); |
|
508 CX_TEST_ASSERT(result == 0); |
|
509 |
|
510 // check transfer |
|
511 CX_TEST_ASSERT(src->size == 0); |
|
512 CX_TEST_ASSERT(dest->size == 0); |
|
513 CX_TEST_ASSERT(src->registered_size == 0); |
|
514 CX_TEST_ASSERT(dest->registered_size == 3); // 2 objects + old allocator |
|
515 CX_TEST_ASSERT(src->allocator != old_allocator); |
|
516 CX_TEST_ASSERT(old_allocator->data == dest); |
|
517 |
|
518 // verify that destroying old pool does nothing |
|
519 test_mempool_destructor_called = 0; |
|
520 cxMempoolFree(src); |
|
521 CX_TEST_ASSERT(test_mempool_destructor_called == 0); |
|
522 |
|
523 // verify that destroying new pool calls the destructors |
|
524 // but only two times (the old allocator has a different destructor) |
|
525 cxMempoolFree(dest); |
|
526 CX_TEST_ASSERT(test_mempool_destructor_called == 2); |
|
527 } |
|
528 free(a); |
|
529 free(b); |
445 } |
530 } |
446 |
531 |
447 CX_TEST(test_mempool_transfer_object) { |
532 CX_TEST(test_mempool_transfer_object) { |
448 CxMempool *src = cxMempoolCreateSimple(4); |
533 CxMempool *src = cxMempoolCreateSimple(4); |
449 CxMempool *dest = cxMempoolCreateSimple(4); |
534 CxMempool *dest = cxMempoolCreateSimple(4); |
516 cx_test_register(suite, test_mempool_global_destructors); |
601 cx_test_register(suite, test_mempool_global_destructors); |
517 cx_test_register(suite, test_mempool_global_destructors2); |
602 cx_test_register(suite, test_mempool_global_destructors2); |
518 cx_test_register(suite, test_mempool_register); |
603 cx_test_register(suite, test_mempool_register); |
519 cx_test_register(suite, test_mempool_register2); |
604 cx_test_register(suite, test_mempool_register2); |
520 cx_test_register(suite, test_mempool_transfer); |
605 cx_test_register(suite, test_mempool_transfer); |
|
606 cx_test_register(suite, test_mempool_transfer_with_foreign_memory); |
|
607 cx_test_register(suite, test_mempool_transfer_foreign_memory_only); |
521 cx_test_register(suite, test_mempool_transfer_object); |
608 cx_test_register(suite, test_mempool_transfer_object); |
522 |
609 |
523 return suite; |
610 return suite; |
524 } |
611 } |