]> wirehaze git hosting - ppos.git/blob - queue/testa-fila.c

wirehaze git hosting

add queue/
[ppos.git] / queue / testa-fila.c
1 // TAD filas genéricas
2 // Prof. Carlos A. Maziero, DINF UFPR
3 // Versão 1.0 - 09/2025
4
5 // ATENÇÃO: ESTE ARQUIVO NÃO DEVE SER ALTERADO;
6 // ALTERAÇÕES SERÃO DESCARTADAS NA CORREÇÃO.
7
8 // Teste do TAD fila genérica (fila de itens void *)
9 // usando uma fila de strings com nomes de frutas.
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <assert.h>
14 #include "queue.h"
15
16 // verifica o sistema operacional
17 #ifndef __linux__
18 #error "Este código foi planejado para ambientes Linux."
19 #endif
20
21 // imprime na tela um elemento da fila (chamada pela função queue_print)
22 void print_item(void *ptr)
23 {
24 printf("%s", (char *)ptr);
25 }
26
27 int main()
28 {
29 char *fruta[] = {"ameixa", "banana", "goiaba",
30 "laranja", "morango", "uva"};
31 int num_frutas = 6;
32 struct queue_t *q;
33 char *item;
34 int pos, status;
35
36 // Testes com fila inexistente
37 printf("1 Testes com uma fila inexistente\n");
38 q = NULL;
39
40 // tamanho deve ser ERROR
41 printf(" teste queue_size\n");
42 assert(queue_size(q) == ERROR);
43
44 // destruir deve retornar ERROR
45 printf(" teste queue_destroy\n");
46 status = queue_destroy(q);
47 assert(status == ERROR);
48
49 // operações com iterador devem retornar NULL
50 printf(" teste queue_head\n");
51 assert(queue_head(q) == NULL);
52 printf(" teste queue_next\n");
53 assert(queue_next(q) == NULL);
54 printf(" teste queue_item\n");
55 assert(queue_item(q) == NULL);
56
57 // inserção e remoção devem deve retornar ERROR
58 printf(" teste queue_add\n");
59 assert(queue_add(q, "teste") == ERROR);
60 printf(" teste queue_del\n");
61 assert(queue_del(q, "teste") == ERROR);
62
63 queue_print(" fila", q, print_item);
64
65 // Testes com fila existente, mas vazia
66 printf("2 Testes com uma fila existente mas vazia\n");
67
68 // cria a fila
69 printf(" teste queue_create\n");
70 q = queue_create();
71 assert(q);
72
73 // tamanho deve ser 0
74 printf(" teste queue_size\n");
75 assert(queue_size(q) == 0);
76
77 // operações com iterador devem retornar NULL
78 printf(" teste queue_head\n");
79 assert(queue_head(q) == NULL);
80 printf(" teste queue_next\n");
81 assert(queue_next(q) == NULL);
82 printf(" teste queue_item\n");
83 assert(queue_item(q) == NULL);
84
85 // remoção deve deve retornar NULL
86 printf(" teste queue_del\n");
87 assert(queue_del(q, "teste") == ERROR);
88
89 queue_print(" fila", q, print_item);
90
91 // Testes de inserção
92 printf("3 Testes de inserção\n");
93
94 for (int i = 0; i < num_frutas; i++)
95 {
96 item = fruta[i];
97 printf(" colocando %s\n", item);
98 assert(queue_add(q, item) == NOERROR);
99 assert(queue_size(q) == (i + 1));
100 queue_print(" fila", q, print_item);
101 }
102
103 printf("4 Testes do queue_print:\n");
104
105 queue_print(" fila", NULL, print_item);
106 queue_print(" fila", q, NULL);
107
108 printf("5 Testes do iterador:\n");
109
110 // iterador deve apontar para a primeira posição
111 item = queue_item(q);
112 pos = 0;
113
114 // testa se iterador aponta e se desloca corretamente
115 while (item)
116 {
117 printf(" iterador em %s (posição %d)\n", item, pos);
118 assert(item == fruta[pos]);
119 item = queue_next(q);
120 pos++;
121 }
122 printf(" iterador em %s\n", (char *)queue_item(q));
123
124 // testando queue_head
125 assert(queue_head(q) == fruta[0]);
126 assert(queue_item(q) == fruta[0]);
127
128 printf("6 Testes de remoção\n");
129
130 // testando queue_del com fila NULL
131 printf(" retirando de fila NULL\n");
132 queue_print(" fila antes ", q, print_item);
133 printf(" iterador em %s\n", (char *)queue_item(q));
134 assert(queue_del(NULL, fruta[0]) == ERROR);
135 queue_print(" fila depois", q, print_item);
136 printf(" iterador em %s\n", (char *)queue_item(q));
137 assert(queue_item(q) == fruta[0]);
138
139 // testando queue_del com item NULL
140 printf(" retirando item NULL\n");
141 queue_print(" fila antes ", q, print_item);
142 printf(" iterador em %s\n", (char *)queue_item(q));
143 assert(queue_del(q, NULL) == ERROR);
144 queue_print(" fila depois", q, print_item);
145 printf(" iterador em %s\n", (char *)queue_item(q));
146 assert(queue_item(q) == fruta[0]);
147
148 // testando queue_del com item que não está na fila
149 printf(" retirando acerola (que não está na fila)\n");
150 queue_print(" fila antes ", q, print_item);
151 printf(" iterador em %s\n", (char *)queue_item(q));
152 assert(queue_del(q, "acerola") == ERROR);
153 queue_print(" fila depois", q, print_item);
154 printf(" iterador em %s\n", (char *)queue_item(q));
155 assert(queue_item(q) == fruta[0]);
156
157 // testando queue_del com item no meio da fila
158 pos = num_frutas / 2;
159 printf(" retirando %s (no meio da fila)\n", fruta[pos]);
160 queue_print(" fila antes ", q, print_item);
161 printf(" iterador em %s\n", (char *)queue_item(q));
162 assert(queue_del(q, fruta[pos]) == NOERROR);
163 queue_print(" fila depois", q, print_item);
164 printf(" iterador em %s\n", (char *)queue_item(q));
165 assert(queue_item(q) == fruta[0]);
166
167 // testando queue_del com item no inicio da fila
168 pos = 0;
169 printf(" retirando %s (no início da fila)\n", fruta[pos]);
170 queue_print(" fila antes ", q, print_item);
171 printf(" iterador em %s\n", (char *)queue_item(q));
172 assert(queue_del(q, fruta[pos]) == NOERROR);
173 queue_print(" fila depois", q, print_item);
174 printf(" iterador em %s\n", (char *)queue_item(q));
175 assert(queue_item(q) == fruta[1]);
176
177 // testando queue_del com item no fim da fila
178 pos = num_frutas - 1;
179 printf(" retirando %s (no fim da fila)\n", fruta[pos]);
180 queue_print(" fila antes ", q, print_item);
181 printf(" iterador em %s\n", (char *)queue_item(q));
182 assert(queue_del(q, fruta[pos]) == NOERROR);
183 queue_print(" fila depois", q, print_item);
184 printf(" iterador em %s\n", (char *)queue_item(q));
185 assert(queue_item(q) == fruta[1]);
186
187 // testando queue_del com item apontado pelo iterador, no meio
188 queue_next(q);
189 pos = 2;
190 printf(" retirando sob o iterador\n");
191 queue_print(" fila antes ", q, print_item);
192 printf(" iterador em %s\n", (char *)queue_item(q));
193 assert(queue_item(q) == fruta[pos]);
194 assert(queue_del(q, fruta[pos]) == NOERROR);
195 queue_print(" fila depois", q, print_item);
196 printf(" iterador em %s\n", (char *)queue_item(q));
197 assert(queue_item(q) == fruta[4]);
198
199 // testando queue_del com item apontado pelo iterador, no fim
200 pos = 4;
201 printf(" retirando sob o iterador\n");
202 queue_print(" fila antes ", q, print_item);
203 printf(" iterador em %s\n", (char *)queue_item(q));
204 assert(queue_item(q) == fruta[pos]);
205 assert(queue_del(q, fruta[pos]) == NOERROR);
206 queue_print(" fila depois", q, print_item);
207 printf(" iterador em %s\n", (char *)queue_item(q));
208 assert(queue_item(q) == NULL);
209
210 // esvaziando a fila
211 printf("7 Esvaziando a fila\n");
212 queue_print(" fila", q, print_item);
213 item = queue_head(q);
214 while (item)
215 {
216 assert(queue_del(q, item) == NOERROR);
217 printf(" retirei %s\n", item);
218 queue_print(" fila", q, print_item);
219 item = queue_item(q);
220 }
221 assert(queue_size(q) == 0);
222
223 // testando queue_destroy
224 printf("8 Teste queue_destroy\n");
225 status = queue_destroy(q);
226 assert(status == NOERROR);
227
228 printf("Testes concluídos com sucesso!\n");
229 exit(0);
230 }