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