Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > PriorityQueue
view TestPriorityQueue.c @ 6:cafa35875bab
changed brch tag filename
| author | Me@portablequad |
|---|---|
| date | Mon, 13 Feb 2012 10:35:10 -0800 |
| parents | 9ecc993a29ea |
| children |
line source
1 #include <stdbool.h>
2 #include <stdio.h>
3 #include <stdlib.h>
6 #include "CUnit/Basic.h"
7 #include "PriorityQueue.h"
9 #ifndef DEBUG
10 #define DEBUG
11 #endif
13 static PrioQueueStruc *Q;
14 static char *first,*second,*third;
16 int init_suite (void) {
17 if (NULL == (Q= makePrioQ())) return -1;
18 else return 0;
19 }
21 int clean_suite (void) {
22 return 0;
23 }
25 void testInsertAElement (void) {
27 first= malloc(3*sizeof(char));
28 first= "foo";
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 }
38 void testInsertOneElement (void) {
40 first= malloc(3*sizeof(char));
41 first= "ten";
43 CU_ASSERT(true == insertPrioQ(first,10,Q));
44 printPrioQ(Q);
45 }
47 void testPopOneElement (void) {
48 popPrioQ(Q);
49 }
51 void testInsertSorted (void) {
52 second= malloc(4*sizeof(char));
53 second= "five";
55 CU_ASSERT(true == insertPrioQ(second,5,Q));
56 printPrioQ(Q);
57 }
59 void testInsertAndHeapify (void) {
60 third= malloc(6*sizeof(char));
61 third= "twenty";
63 CU_ASSERT(true == insertPrioQ(third,20,Q));
64 printPrioQ(Q);
65 }
67 void testGetFirstElement (void) {
68 void* elem;
70 elem= getFirstPrioQ(Q);
71 CU_ASSERT(elem == third);
72 printPrioQ(Q);
73 }
75 void testSwap (void) {
76 swap(Q,0,2);
77 printPrioQ(Q);
78 swap(Q,2,0);
79 printPrioQ(Q);
80 }
82 void testPop (void) {
83 void *elem;
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 }
99 void testPopEmptyQueue (void) {
100 void *elem;
101 CU_ASSERT (NULL == (elem= popPrioQ(Q)));
102 }
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 }
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 }
125 int main () {
126 CU_pSuite pSuite= NULL;
128 if (CUE_SUCCESS != CU_initialize_registry()) return CU_get_error();
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 }
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)) {
150 CU_cleanup_registry();
151 return CU_get_error();
152 }
155 CU_basic_set_mode(CU_BRM_VERBOSE);
156 CU_basic_run_tests();
157 CU_cleanup_registry();
158 return CU_get_error();
159 }
