123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704 |
- /**
- \example test/heap.c
- */
- #include <string.h>
- #include <stdio.h>
- #include "lub/heap.h"
- #include "lub/test.h"
- /*************************************************************
- * TEST CODE
- ************************************************************* */
- int testseq = 0;
- /*
- * This is the main entry point for this executable
- */
- #define SIZEOF_HEAP_T (140)
- #define SIZEOF_ALLOC_BLOCK_T ( 8)
- #define SIZEOF_NODE_T ( 20)
- #define SIZEOF_CONTEXT_T (104)
-
- #define RAW_SIZE (SIZEOF_HEAP_T + 132)
- /* allow space to assign a leak detection context */
- #define ACTUAL_SIZE (RAW_SIZE)
- char segment1[ACTUAL_SIZE];
- char segment2[ACTUAL_SIZE*4];
- #define LARGE_SIZE 1024 * 400
- char large_seg[LARGE_SIZE];
- #define MIN_ALLOC 12
- typedef struct test_node_s test_node_t;
- struct test_node_s
- {
- test_node_t *next;
- };
- static test_node_t *first_node;
- /*--------------------------------------------------------- */
- /*
- * Returns a pseudo random number based on the input.
- * This will given 2^15 concequtive numbers generate the entire
- * number space in a psedo random manner.
- */
- static int
- random(int i)
- {
- /* multiply the first prime after 2^14 and mask to 15 bits */
- return (16411*i) & (32767);
- }
- /*--------------------------------------------------------- */
- static void
- create_nodes(lub_heap_t *heap,
- int count)
- {
- test_node_t *node;
- int i;
-
- lub_test_seq_log(LUB_TEST_NORMAL,"Create % random sized blocks",count);
- first_node = NULL;
- for(i = 0; i < count; i++)
- {
- union
- {
- test_node_t **node_ptr;
- char **char_ptr;
- } tmp;
- /* pick a random size upto 400 bytes */
- size_t size = 1 + 400 * random(i) / 32768;
- node = NULL;
- tmp.node_ptr = &node;
- lub_heap_realloc(heap,tmp.char_ptr,size,LUB_HEAP_ALIGN_NATIVE);
-
- if(NULL != node)
- {
- /* put this into the linked list for later */
- node->next = first_node;
- first_node = node;
- }
- else
- {
- lub_test_seq_log(LUB_TEST_NORMAL,"ONLY MANAGED TO CREATE %d BLOCKS",i);
- break;
- }
- }
- lub_test_seq_log(LUB_TEST_NORMAL,"Dump the details...");
- lub_heap_show(heap,BOOL_TRUE);
- }
- /*--------------------------------------------------------- */
- static void
- test_performance(void)
- {
- lub_heap_t *heap;
-
- lub_test_seq_begin(++testseq,"Check the performance...");
- heap = lub_heap_create(large_seg,sizeof(large_seg));
- lub_test_check(NULL != heap,"Check creation of 400KB heap");
-
- create_nodes(heap,1000);
-
- lub_test_seq_log(LUB_TEST_NORMAL,"Now free every other node...");
- {
- test_node_t **ptr;
- int i = 0;
- for(ptr = &first_node;
- *ptr;
- i++,ptr = &(*ptr)->next)
- {
- if(i % 2)
- {
- char *tmp = (char*)*ptr;
- /* remove from the linked list */
- *ptr = (*ptr)->next;
-
- /* and free the node */
- lub_heap_realloc(heap,&tmp,0,LUB_HEAP_ALIGN_NATIVE);
- }
- }
- }
- lub_test_seq_log(LUB_TEST_NORMAL,"Dump the details...");
- lub_heap_show(heap,BOOL_TRUE);
- create_nodes(heap,500);
-
- lub_heap_destroy(heap);
- lub_test_seq_end();
- }
- /*--------------------------------------------------------- */
- static void
- test_stats_check(lub_heap_t *this)
- {
- lub_heap_stats_t stats;
- unsigned total_memory,used_memory;
-
- lub_heap__get_stats(this,&stats);
- total_memory = stats.segs_bytes
- - stats.static_bytes;
- used_memory = stats.free_bytes
- + stats.free_overhead
- + stats.alloc_bytes
- + stats.alloc_overhead;
-
- lub_test_check_int(total_memory,used_memory,
- "Check that the (segs_bytes - static_bytes) equals the free and allocated bytes + overheads");
- if(total_memory != used_memory)
- {
- lub_heap_show(this,BOOL_TRUE);
- }
- }
- /*--------------------------------------------------------- */
- static void
- test_taint_check(char *ptr,size_t size)
- {
- bool_t tainted = BOOL_TRUE;
- if(ptr && (LUB_HEAP_ZERO_ALLOC != ptr))
- {
- while(size--)
- {
- if((unsigned char)*ptr != 0xaa)
- {
- tainted = BOOL_FALSE;
- break;
- }
- }
- }
- lub_test_check(tainted,"Check memory has been tainted");
- }
- /*--------------------------------------------------------- */
- void
- test_main(unsigned frame_count)
- {
- lub_heap_t *heap;
- char *tmp;
- char *ptr1 = NULL;
- char *ptr2 = NULL;
- char *ptr3 = NULL;
- lub_heap_status_t result;
- lub_heap_stats_t stats;
- size_t actual_size = RAW_SIZE;
-
- if(frame_count)
- {
- actual_size = ACTUAL_SIZE;
- }
-
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_create(segment1,%d)",actual_size);
-
- heap = lub_heap_create(segment1,actual_size);
- lub_test_check(NULL != heap,"Check heap created correctly");
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- test_stats_check(heap);
- lub_test_seq_log(LUB_TEST_NORMAL,"Configured to collect %d frames for leak detection",frame_count);
- lub_heap__set_framecount(frame_count);
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(1,stats.free_blocks,"Check the number of free blocks");
-
- lub_test_seq_end();
- /*----------------------------------------------------- */
-
- lub_test_seq_begin(++testseq,"lub_heap_add_segment(segment2,%d)",actual_size*2);
-
- lub_heap_add_segment(heap,segment2,actual_size*2);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- test_stats_check(heap);
-
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(1,stats.free_blocks,"Check the two adjacent segments get merged");
-
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_static_alloc(heap,10)");
-
- tmp = lub_heap_static_alloc(heap,10);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(NULL != tmp,"Check static allocation works");
- test_taint_check(tmp,12);
- test_stats_check(heap);
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(1,stats.static_blocks,"Check the number of static blocks");
- lub_test_check_int(12,stats.static_bytes,"Check static_bytes has gone up");
- if (tmp)
- memset(tmp,0x11,10);
-
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr1,10,LUB_HEAP_ALIGN_NATIVE)");
-
- result = lub_heap_realloc(heap,&ptr1,10,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_OK == result,"Check dynamic allocation says it works");
- lub_test_check(NULL != ptr1,"Check dynamic allocation actually works");
- test_taint_check(ptr1,10);
- test_stats_check(heap);
-
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(1,stats.alloc_blocks,"Check the number of dynamic blocks");
- lub_test_check_int(MIN_ALLOC,stats.alloc_bytes,"Check alloc bytes has gone up");
- if(ptr1)
- memset(ptr1,0x22,10);
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_static_alloc(heap,10)");
- tmp = lub_heap_static_alloc(heap,10);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(NULL != tmp,"Check static allocation works");
- test_taint_check(tmp,10);
- test_stats_check(heap);
-
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(2,stats.static_blocks,"Check the number of static blocks");
- lub_test_check_int(2*MIN_ALLOC,stats.static_bytes,"Check static bytes has gone up");
- if(tmp)
- memset(tmp,0x33,10);
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_static_alloc(heap,200)");
-
- tmp = lub_heap_static_alloc(heap,200);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(NULL != tmp,"Check static allocation from second segment works");
- test_taint_check(tmp,200);
- test_stats_check(heap);
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(3,stats.static_blocks,"Check the number of static blocks");
- lub_test_check_int(200+2*MIN_ALLOC,stats.static_bytes,"Check static_bytes has gone up");
- if(tmp)
- memset(tmp,0x99,200);
-
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr1,30,LUB_HEAP_ALIGN_NATIVE)");
- tmp = ptr1;
- result = lub_heap_realloc(heap,&ptr1,30,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_OK == result,"Check growing an allocation works");
- lub_test_check(ptr1 == tmp,"Check growing an allocation keeps the same pointer");
- test_taint_check(ptr1+10,20);
- test_stats_check(heap);
-
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(1,stats.alloc_blocks,"Check the number of dynamic blocks");
- lub_test_check_int(20 + MIN_ALLOC,stats.alloc_bytes,"Check alloc bytes has gone up");
- if(ptr1)
- memset(ptr1,0x44,30);
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr1,30,LUB_HEAP_ALIGN_NATIVE)");
- tmp = ptr1;
- result = lub_heap_realloc(heap,&ptr1,30,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_OK == result,"Check asking for the same size works");
- lub_test_check(ptr1 == tmp,"Check it keeps the same pointer");
- test_stats_check(heap);
-
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(1,stats.alloc_blocks,"Check the number of dynamic blocks");
- lub_test_check_int(20+MIN_ALLOC,stats.alloc_bytes,"Check alloc bytes");
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr1,4,LUB_HEAP_ALIGN_NATIVE)");
- tmp = ptr1;
- result = lub_heap_realloc(heap,&ptr1,4,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_OK == result,"Check shrinking an allocation works");
- lub_test_check(ptr1 == tmp,"Check shrinking an allocation retains pointer");
- test_taint_check(ptr1+4,MIN_ALLOC-4);
- test_stats_check(heap);
-
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(1,stats.alloc_blocks,"Check the number of dynamic blocks");
- if(ptr1)
- memset(ptr1,0x55,4);
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr1,100,LUB_HEAP_ALIGN_NATIVE)");
- tmp = ptr1;
- result = lub_heap_realloc(heap,&ptr1,104,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_OK == result,"Check growing and moving works");
- lub_test_check(ptr1 != tmp,"Check growing and moving changes the pointer");
- test_taint_check(ptr1+4,100);
- test_stats_check(heap);
-
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(1,stats.alloc_blocks,"Check the number of dynamic blocks");
- if(ptr1)
- memset(ptr1,0x55,100);
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr1,0,LUB_HEAP_ALIGN_NATIVE)");
- tmp = ptr1;
- result = lub_heap_realloc(heap,&ptr1,0,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_OK == result,"Check releasing an allocation says it works");
- lub_test_check(LUB_HEAP_ZERO_ALLOC == ptr1,"Check releasing an allocation actually works");
- test_stats_check(heap);
-
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(0,stats.alloc_blocks,"Check the number of dynamic blocks");
- lub_test_check(BOOL_TRUE == lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr1,20,LUB_HEAP_ALIGN_NATIVE)");
- /* now test with multiple dynamic blocks to exercise different recovery mechanisms */
- result = lub_heap_realloc(heap,&ptr1,20,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_OK == result,"Check dynamic allocation (1) says it works");
- lub_test_check(NULL != ptr1,"Check dynamic allocation (1) actually works");
- test_taint_check(ptr1,20);
- test_stats_check(heap);
-
- if(ptr1)
- memset(ptr1,0x66,20);
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr2,20,LUB_HEAP_ALIGN_NATIVE)");
- result = lub_heap_realloc(heap,&ptr2,30,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_OK == result,"Check dynamic allocation (2) says it works");
- lub_test_check(NULL != ptr2,"Check dynamic allocation (2) actually works");
- test_taint_check(ptr2,32);
- test_stats_check(heap);
-
- if(ptr2)
- memset(ptr2,0x77,20);
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr3,1,LUB_HEAP_ALIGN_NATIVE)");
- result = lub_heap_realloc(heap,&ptr3,1,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_OK == result,"Check dynamic allocation (3) says it works");
- lub_test_check(NULL != ptr3,"Check dynamic allocation (3) actually works");
- test_taint_check(ptr3,8);
- test_stats_check(heap);
-
- if(ptr3)
- memset(ptr3,0x88,1);
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(3,stats.alloc_blocks,"Check the number of dynamic blocks");
- lub_test_check_int(3,stats.static_blocks,"Check the number static blocks");
- lub_test_check_int(1,stats.free_blocks,"Check the number of free blocks");
- lub_test_check_int(60,stats.alloc_bytes,"Check alloc bytes has gone up");
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr2,8,LUB_HEAP_ALIGN_NATIVE)");
- /* shrink the second block to create a new free block */
- tmp = ptr2;
- result = lub_heap_realloc(heap,&ptr2,8,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_OK == result,"Check shrinking (2) says it works");
- lub_test_check(tmp == ptr2,"Check shrinking (2) maintains the pointer");
- test_stats_check(heap);
-
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(3,stats.alloc_blocks,"Check the number of dynamic blocks");
- lub_test_check_int(2,stats.free_blocks,"Check the number of free blocks");
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr2,0,LUB_HEAP_ALIGN_NATIVE)");
- /* release the second block to cause fragmentation */
- tmp = ptr2;
- result = lub_heap_realloc(heap,&ptr2,0,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_OK == result,"Check releasing an allocation (2) says it works");
- lub_test_check(LUB_HEAP_ZERO_ALLOC == ptr2,"Check releasing an allocation (2) actually works");
- test_stats_check(heap);
-
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(2,stats.alloc_blocks,"Check the number of dynamic blocks");
- lub_test_check_int(2,stats.free_blocks,"Check the number of free blocks");
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr2,0,LUB_HEAP_ALIGN_NATIVE)");
- /* deliberatly double free the memory */
- ptr2 = tmp;
- result = lub_heap_realloc(heap,&ptr2,0,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_DOUBLE_FREE == result,"Check double free is spotted");
- lub_test_check(tmp == ptr2,"Check pointer hasn't changed.");
- test_stats_check(heap);
- ptr2 = NULL;
-
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(2,stats.alloc_blocks,"Check the number of dynamic blocks");
- lub_test_check_int(2,stats.free_blocks,"Check the number of free blocks");
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr1,24,LUB_HEAP_ALIGN_NATIVE)");
- /* extend the lower block */
- tmp = ptr1;
- result = lub_heap_realloc(heap,&ptr1,24,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_OK == result,"Check growing allocation (1) says it works");
- lub_test_check(NULL != ptr1,"Check growing allocation (1) actually works");
- lub_test_check(tmp == ptr1,"Check growing allocation (1) didn't move block");
- test_taint_check(ptr1+20,4);
- test_stats_check(heap);
-
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(2,stats.alloc_blocks,"Check the number of dynamic blocks");
- lub_test_check_int(2,stats.free_blocks,"Check the number of free blocks");
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr1,48,LUB_HEAP_ALIGN_NATIVE)");
- /* extend the lower block to absorb the free block */
- tmp = ptr1;
- result = lub_heap_realloc(heap,&ptr1,48,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_OK == result,"Check growing allocation (1) says it works");
- lub_test_check(NULL != ptr1,"Check growing allocation (1) actually works");
- lub_test_check(tmp == ptr1,"Check growing allocation (1) didn't move block");
- test_taint_check(ptr1+20,16);
- test_stats_check(heap);
-
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(2,stats.alloc_blocks,"Check the number of dynamic blocks");
- lub_test_check_int(1,stats.free_blocks,"Check the number of free blocks");
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr1,0,LUB_HEAP_ALIGN_NATIVE)");
- /* release the lower block */
- tmp = ptr1;
- result = lub_heap_realloc(heap,&ptr1,0,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_OK == result,"Check releasing (1) says it works");
- lub_test_check(LUB_HEAP_ZERO_ALLOC == ptr1,"Check releasing memory (1) actually works");
- test_stats_check(heap);
-
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(1,stats.alloc_blocks,"Check the number of dynamic blocks");
- lub_test_check_int(2,stats.free_blocks,"Check the number of free blocks");
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr3,0,LUB_HEAP_ALIGN_NATIVE)");
- /* release the upper block */
- tmp = ptr3;
- result = lub_heap_realloc(heap,&ptr3,0,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_OK == result,"Check releasing (3) says it works");
- lub_test_check(LUB_HEAP_ZERO_ALLOC == ptr3,"Check releasing memory (3) actually works");
- test_stats_check(heap);
-
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(0,stats.alloc_blocks,"Check the number of dynamic blocks");
- lub_test_check_int(1,stats.free_blocks,"Check the number of free blocks");
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr1,12,LUB_HEAP_ALIGN_NATIVE)");
- /* now test extending blocks downwards */
- result = lub_heap_realloc(heap,&ptr1,12,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_OK == result,"Check allocation (1) says it works");
- lub_test_check(NULL != ptr1,"Check allocation memory (1) actually works");
- test_stats_check(heap);
-
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(1,stats.alloc_blocks,"Check the number of dynamic blocks");
- lub_test_check_int(1,stats.free_blocks,"Check the number of free blocks");
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr2,12,LUB_HEAP_ALIGN_NATIVE)");
- /* now test extending blocks downwards */
- result = lub_heap_realloc(heap,&ptr2,12,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_OK == result,"Check allocation (2) says it works");
- lub_test_check(NULL != ptr2,"Check allocation memory (2) actually works");
- test_stats_check(heap);
-
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(2,stats.alloc_blocks,"Check the number of dynamic blocks");
- lub_test_check_int(1,stats.free_blocks,"Check the number of free blocks");
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr3,12,LUB_HEAP_ALIGN_NATIVE)");
- /* now test extending blocks downwards */
- result = lub_heap_realloc(heap,&ptr3,12,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_OK == result,"Check allocation (1) says it works");
- lub_test_check(NULL != ptr3,"Check allocation memory (1) actually works");
- test_stats_check(heap);
-
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(3,stats.alloc_blocks,"Check the number of dynamic blocks");
- lub_test_check_int(1,stats.free_blocks,"Check the number of free blocks");
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr1,0,LUB_HEAP_ALIGN_NATIVE)");
- /* release the lower block */
- tmp = ptr1;
- result = lub_heap_realloc(heap,&ptr1,0,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_OK == result,"Check releasing (1) says it works");
- lub_test_check(LUB_HEAP_ZERO_ALLOC == ptr1,"Check releasing memory (1) actually works");
- test_stats_check(heap);
-
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(2,stats.alloc_blocks,"Check the number of dynamic blocks");
- lub_test_check_int(2,stats.free_blocks,"Check the number of free blocks");
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr2,16,LUB_HEAP_ALIGN_NATIVE)");
- /* now extend the middle block (downwards) */
- tmp = ptr2;
- result = lub_heap_realloc(heap,&ptr2,16,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_OK == result,"Check extending downwards (2) says it works");
- lub_test_check(tmp != ptr2,"Check extending downwards (2) changes pointer");
- test_stats_check(heap);
-
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(2,stats.alloc_blocks,"Check the number of dynamic blocks");
- lub_test_check_int(1,stats.free_blocks,"Check the number of free blocks");
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr2,20,LUB_HEAP_ALIGN_NATIVE)");
- /* now extend the middle block (downwards) */
- tmp = ptr2;
- result = lub_heap_realloc(heap,&ptr2,20,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_OK == result,"Check extending downwards (2) says it works");
- lub_test_check(tmp != ptr2,"Check extending downwards (2) changes pointer");
- test_stats_check(heap);
-
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(2,stats.alloc_blocks,"Check the number of dynamic blocks");
- lub_test_check_int(2,stats.free_blocks,"Check the number of free blocks");
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr2,0,LUB_HEAP_ALIGN_NATIVE)");
- /* now set the pointer to NULL */
- ptr2 = NULL;
- result = lub_heap_realloc(heap,&ptr2,0,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_OK == result,"Check that releasing NULL and allocating nothing says it works");
- lub_test_check(LUB_HEAP_ZERO_ALLOC == ptr2,"Check pointer is LUB_HEAP_ALLOC_ZERO");
- test_stats_check(heap);
-
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(2,stats.alloc_blocks,"Check the number of dynamic blocks");
- lub_test_check_int(1,stats.free_blocks,"Check the number of free blocks");
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_realloc(heap,&ptr2,-1,LUB_HEAP_ALIGN_NATIVE)");
- result = lub_heap_realloc(heap,&ptr2,-1,LUB_HEAP_ALIGN_NATIVE);
- lub_test_check(lub_heap_check_memory(heap),"Check integrity of heap");
- lub_test_check(LUB_HEAP_FAILED == result,"Check that allocating large size near 2^32 fails");
- lub_test_check(NULL == ptr2,"Check pointer is unchanged");
- test_stats_check(heap);
-
- lub_heap__get_stats(heap,&stats);
- lub_test_check_int(2,stats.alloc_blocks,"Check the number of dynamic blocks");
- lub_test_check_int(1,stats.free_blocks,"Check the number of free blocks");
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_show()");
- lub_heap_show(heap,BOOL_TRUE);
-
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_show()");
- lub_heap_show(heap,BOOL_TRUE);
-
- lub_test_seq_end();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_show_leaks()");
- lub_heap_leak_report(LUB_HEAP_SHOW_ALL,"");
- lub_test_seq_end();
- /*----------------------------------------------------- */
- test_performance();
- /*----------------------------------------------------- */
- lub_test_seq_begin(++testseq,"lub_heap_destroy()");
- lub_heap_destroy(heap);
- lub_test_seq_end();
- /*----------------------------------------------------- */
- }
- /*--------------------------------------------------------- */
- int
- main(int argc, const char *argv[])
- {
- int status;
- lub_heap_init(argv[0]);
-
- lub_test_parse_command_line(argc,argv);
- lub_test_begin("lub_heap");
- lub_heap_taint(BOOL_TRUE);
- lub_heap_check(BOOL_TRUE);
- /* first of all test with leak detection switched off */
- test_main(0);
- #if 1
- /* now test with leak detection switched on */
- test_main(1); /* a single context to use */
- #endif
- /* tidy up */
- status = lub_test_get_status();
- lub_test_end();
-
- return status;
- }
- /*--------------------------------------------------------- */
|