Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > PriorityQueue
comparison TestPriorityQueue.c @ 0:9ecc993a29ea
initial heap implementation
| author | hausers |
|---|---|
| date | Thu, 09 Feb 2012 11:48:03 +0100 |
| parents | |
| children | 500d0e2fabfb |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:e94503253952 |
|---|---|
| 1 #include <stdbool.h> | |
| 2 #include <stdio.h> | |
| 3 #include <stdlib.h> | |
| 4 | |
| 5 | |
| 6 #include "CUnit/Basic.h" | |
| 7 #include "PriorityQueue.h" | |
| 8 | |
| 9 #ifndef DEBUG | |
| 10 #define DEBUG | |
| 11 #endif | |
| 12 | |
| 13 static PrioQueueStruc *Q; | |
| 14 static char *first,*second,*third; | |
| 15 | |
| 16 int init_suite (void) { | |
| 17 if (NULL == (Q= makeVMSPrioQ())) return -1; | |
| 18 else return 0; | |
| 19 } | |
| 20 | |
| 21 int clean_suite (void) { | |
| 22 return 0; | |
| 23 } | |
| 24 | |
| 25 void testInsertAElement (void) { | |
| 26 | |
| 27 first= malloc(3*sizeof(char)); | |
| 28 first= "foo"; | |
| 29 | |
| 30 printf("SIZE = %d | ",Q->size); | |
| 31 printf("MAXSIZE = %d | ",Q->maxSize); | |
| 32 CU_ASSERT(true == insertPrioQ(first,0,Q)); | |
| 33 printPrioQ(Q); | |
| 34 printf("SIZE = %d | ",Q->size); | |
| 35 printf("MAXSIZE = %d | ",Q->maxSize); | |
| 36 } | |
| 37 | |
| 38 void testInsertOneElement (void) { | |
| 39 | |
| 40 first= malloc(3*sizeof(char)); | |
| 41 first= "ten"; | |
| 42 | |
| 43 CU_ASSERT(true == insertPrioQ(first,10,Q)); | |
| 44 printPrioQ(Q); | |
| 45 } | |
| 46 | |
| 47 void testPopOneElement (void) { | |
| 48 popPrioQ(Q); | |
| 49 } | |
| 50 | |
| 51 void testInsertSorted (void) { | |
| 52 second= malloc(4*sizeof(char)); | |
| 53 second= "five"; | |
| 54 | |
| 55 CU_ASSERT(true == insertPrioQ(second,5,Q)); | |
| 56 printPrioQ(Q); | |
| 57 } | |
| 58 | |
| 59 void testInsertAndHeapify (void) { | |
| 60 third= malloc(6*sizeof(char)); | |
| 61 third= "twenty"; | |
| 62 | |
| 63 CU_ASSERT(true == insertPrioQ(third,20,Q)); | |
| 64 printPrioQ(Q); | |
| 65 } | |
| 66 | |
| 67 void testGetFirstElement (void) { | |
| 68 void* elem; | |
| 69 | |
| 70 elem= getFirstPrioQ(Q); | |
| 71 CU_ASSERT(elem == third); | |
| 72 printPrioQ(Q); | |
| 73 } | |
| 74 | |
| 75 void testSwap (void) { | |
| 76 swap(Q,0,2); | |
| 77 printPrioQ(Q); | |
| 78 swap(Q,2,0); | |
| 79 printPrioQ(Q); | |
| 80 } | |
| 81 | |
| 82 void testPop (void) { | |
| 83 void *elem; | |
| 84 | |
| 85 elem= popPrioQ(Q); | |
| 86 CU_ASSERT(elem == third); | |
| 87 printPrioQ(Q); | |
| 88 printf("size = %d",Q->size); | |
| 89 elem= popPrioQ(Q); | |
| 90 CU_ASSERT(elem == first); | |
| 91 printPrioQ(Q); | |
| 92 printf("size = %d",Q->size); | |
| 93 elem= popPrioQ(Q); | |
| 94 CU_ASSERT(elem == second); | |
| 95 printPrioQ(Q); | |
| 96 printf("size = %d",Q->size); | |
| 97 } | |
| 98 | |
| 99 void testPopEmptyQueue (void) { | |
| 100 void *elem; | |
| 101 CU_ASSERT (NULL == (elem= popPrioQ(Q))); | |
| 102 } | |
| 103 | |
| 104 void testEnlargeQueue (void) { | |
| 105 int i; | |
| 106 char *elem; | |
| 107 for (i= 0; i<1163; i++) { | |
| 108 elem= malloc(5*sizeof(char)); | |
| 109 sprintf(elem,"%d",i); | |
| 110 CU_ASSERT(true == insertPrioQ(elem,i,Q)); | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 | |
| 115 void testPopEnlargedQueue (void) { | |
| 116 int i; | |
| 117 char *elem; | |
| 118 for (i= 1162; i>= 0; i--) { | |
| 119 elem= popPrioQ(Q); | |
| 120 CU_ASSERT(i == atoi(elem)); | |
| 121 } | |
| 122 CU_ASSERT(NULL == popPrioQ(Q)); | |
| 123 } | |
| 124 | |
| 125 int main () { | |
| 126 CU_pSuite pSuite= NULL; | |
| 127 | |
| 128 if (CUE_SUCCESS != CU_initialize_registry()) return CU_get_error(); | |
| 129 | |
| 130 pSuite= CU_add_suite("Simple Test Suite",init_suite, clean_suite); | |
| 131 if (NULL == pSuite) { | |
| 132 CU_cleanup_registry(); | |
| 133 return CU_get_error(); | |
| 134 } | |
| 135 | |
| 136 if (NULL == CU_add_test(pSuite,"test of insert first element",testInsertOneElement) || | |
| 137 NULL == CU_add_test(pSuite,"test of insert sorted",testInsertSorted) || | |
| 138 NULL == CU_add_test(pSuite,"test of insert and heapify",testInsertAndHeapify) || | |
| 139 NULL == CU_add_test(pSuite,"test of swap",testSwap) || | |
| 140 NULL == CU_add_test(pSuite,"test of get top element",testGetFirstElement) || | |
| 141 NULL == CU_add_test(pSuite,"test of pop elements",testPop) || | |
| 142 NULL == CU_add_test(pSuite,"test of pop an empty queue",testPopEmptyQueue) || | |
| 143 NULL == CU_add_test(pSuite,"test of again a element",testInsertAElement) || | |
| 144 NULL == CU_add_test(pSuite,"test of remove the element",testPopOneElement) || | |
| 145 NULL == CU_add_test(pSuite,"test of enlarge queue",testEnlargeQueue) || | |
| 146 NULL == CU_add_test(pSuite,"test of pop enlarged queue",testPopEnlargedQueue)) { | |
| 147 | |
| 148 | |
| 149 | |
| 150 CU_cleanup_registry(); | |
| 151 return CU_get_error(); | |
| 152 } | |
| 153 | |
| 154 | |
| 155 CU_basic_set_mode(CU_BRM_VERBOSE); | |
| 156 CU_basic_run_tests(); | |
| 157 CU_cleanup_registry(); | |
| 158 return CU_get_error(); | |
| 159 } |
