ppos

Usermode educational operating system.
Log | Files | Refs

map-test.c (3278B)


      1 // PingPongOS - PingPong Operating System
      2 // Prof. Carlos A. Maziero, DINF UFPR
      3 // Versão 2.1 -- 07/2026
      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 mapa de objetos
      9 
     10 #include <stdio.h>
     11 #include <assert.h>
     12 #include "map.h"
     13 
     14 char *fruta[] = {"banana", "uva", "morango", "goiaba",
     15                  "laranja", "pera", "melancia", "jaca"};
     16 
     17 int main()
     18 {
     19     struct map_t *a;
     20     int id;
     21     char *obj;
     22 
     23     // create
     24     a = map_create(5);
     25     map_print("map", a);
     26     assert(a);
     27 
     28     printf("\n");
     29 
     30     // put
     31     obj = fruta[0];
     32     id = map_put(a, obj);
     33     assert(id >= 0);
     34     printf("Object \"%s\" registered under ID %d\n", obj, id);
     35     map_print("map", a);
     36 
     37     // store
     38     obj = fruta[1];
     39     id = map_put(a, obj);
     40     assert(id >= 0);
     41     printf("Object \"%s\" registered under ID %d\n", obj, id);
     42     map_print("map", a);
     43 
     44     // store
     45     obj = fruta[2];
     46     id = map_put(a, obj);
     47     assert(id >= 0);
     48     printf("Object \"%s\" registered under ID %d\n", obj, id);
     49     map_print("map", a);
     50 
     51     // store
     52     obj = fruta[3];
     53     id = map_put(a, obj);
     54     assert(id >= 0);
     55     printf("Object \"%s\" registered under ID %d\n", obj, id);
     56     map_print("map", a);
     57 
     58     // store
     59     obj = fruta[4];
     60     id = map_put(a, obj);
     61     assert(id >= 0);
     62     printf("Object \"%s\" registered under ID %d\n", obj, id);
     63     map_print("map", a);
     64 
     65     // store (error)
     66     obj = fruta[5];
     67     id = map_put(a, obj);
     68     assert(id < 0);
     69     printf("Object \"%s\" not registered\n", obj);
     70     map_print("map", a);
     71 
     72     printf("\n");
     73 
     74     // retrieve
     75     id = 0;
     76     obj = map_get(a, id);
     77     assert(obj == fruta[0]);
     78     printf("Object under ID %d is \"%s\"\n", id, obj);
     79     map_print("map", a);
     80 
     81     // retrieve
     82     id = 4;
     83     obj = map_get(a, id);
     84     assert(obj == fruta[4]);
     85     printf("Object under ID %d is \"%s\"\n", id, obj);
     86     map_print("map", a);
     87 
     88     // retrieve (error)
     89     id = 5;
     90     obj = map_get(a, id);
     91     assert(obj == NULL);
     92     printf("ID %d is not associated to an object\n", id);
     93     map_print("map", a);
     94 
     95     printf("\n");
     96 
     97     // release
     98     id = 3;
     99     obj = map_del(a, id);
    100     assert(obj == fruta[3]);
    101     printf("ID %d deleted had object \"%s\"\n", id, obj);
    102     map_print("map", a);
    103 
    104     // release
    105     id = 1;
    106     obj = map_del(a, id);
    107     assert(obj == fruta[1]);
    108     printf("ID %d deleted had object \"%s\"\n", id, obj);
    109     map_print("map", a);
    110 
    111     // release (error)
    112     id = 5;
    113     obj = map_del(a, id);
    114     assert(obj == NULL);
    115     printf("ID %d is not associated to an object\n", id);
    116     map_print("map", a);
    117 
    118     printf("\n");
    119 
    120     // store
    121     obj = fruta[6];
    122     id = map_put(a, obj);
    123     assert(id == 1);
    124     printf("Object \"%s\" registered under ID %d\n", obj, id);
    125     map_print("map", a);
    126 
    127     // store
    128     obj = fruta[5];
    129     id = map_put(a, obj);
    130     assert(id == 3);
    131     printf("Object \"%s\" registered under ID %d\n", obj, id);
    132     map_print("map", a);
    133 
    134     // store (error)
    135     obj = fruta[7];
    136     id = map_put(a, obj);
    137     assert(id == -1);
    138     printf("Object \"%s\" not registered\n", obj);
    139     map_print("map", a);
    140 
    141     printf("\n");
    142 
    143     map_destroy(a);
    144     printf("Map destroyed\n");
    145 
    146     printf("Testes concluídos com sucesso!\n");
    147     return(0);
    148 }
    149