ppos

Usermode educational operating system.
Log | Files | Refs

queue-test.c (7467B)


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