Advertisement
LeonidR

Test per Calcolatrice (dal prof)

Dec 17th, 2013
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.19 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <stdbool.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdarg.h>
  9. #include <errno.h>
  10. #include <time.h>
  11. #include <unistd.h>
  12.  
  13. #define PIPE_IN "calcPipeIn"
  14. #define PIPE_OUT "calcPipeOut"
  15. #define MAXEXP 100
  16. #define NT 6
  17. #define DEBUG 1
  18.  
  19. void die(char * s) {
  20.     perror(s);
  21.     exit(1);
  22. }
  23.  
  24. // output abilitato se DEBUG == 1
  25. void d_print(char *s, ...) {
  26.     va_list ap;
  27.  
  28.     if (DEBUG) {  
  29.         va_start(ap, s);
  30.         vprintf(s,ap);
  31.         va_end(ap);
  32.     }    
  33. }
  34.  
  35. int main() {
  36.     int pin,pout,nums,n,i,j,k,r,fail=false,rread;
  37.     char sn[MAXEXP],ris[MAXEXP];
  38.  
  39.     // apre le pipe
  40.     if( ( pin=open(PIPE_IN,O_RDWR) ) < 0 ||  ( pout=open(PIPE_OUT,O_RDWR | O_NONBLOCK) ) < 0 )
  41.         die("Errore apertura pipe");
  42.  
  43.     srand(time(NULL)); // inizializza il generatore random
  44.  
  45.     d_print("=== INIZIO TEST ===\n");
  46.      
  47.     // genera NT espressioni le invia e controlla il risultato ricevuto
  48.     for(i=0;i<NT;i++) {
  49.         r=0; // azzera la somma (per il test)
  50.  
  51.         nums = (int) ((float)rand() / RAND_MAX * 9) + 1; // lunghezza espressione
  52.          
  53.         d_print("Espressione %i composta da %i numeri: ",i,nums);
  54.          
  55.         // genera i nums numeri e li invia come stringhe separati da +
  56.         // l'ultimo numero e' terminato con #
  57.         for(j=0;j<nums;j++) {
  58.             // sceglie un numero a caso
  59.             n = (int) ((float)rand() / RAND_MAX * 100);
  60.             d_print("%i ",n);
  61.             r += n; // aggiorna la somma (per il test successivo)
  62.             sprintf(sn,"%i",n); // crea la stringa
  63.             write(pin,sn,strlen(sn)); // la invia sulla pipe
  64.             if (j==nums-1) // e' l'ultimo numero?
  65.                 write(pin,"#",1); // termina l'espressione
  66.             else
  67.                 write(pin,"+",1); // aggiunge il simbolo +
  68.         }
  69.         d_print("\n  somma: %i",r);
  70.          
  71.         // controlliamo il risultato dalla pipe
  72.         d_print("\n  leggo dalla pipe ... :"); fflush(stdout);
  73.         k=0;
  74.          
  75.         // leggo un risultato. La lettura e' non bloccante (vedi la open)
  76.         while(k<MAXEXP-1 && (rread=read(pout,&ris[k],1))>0 && ris[k] != '#') k++;
  77.          
  78.         // se per caso non ha letto nulla ritenta dopo un secondo
  79.         if (rread < 0 && errno ==EAGAIN) {
  80.             sleep(1);
  81.             while(k<MAXEXP-1 && (rread=read(pout,&ris[k],1))>0 && ris[k] != '#') k++;
  82.         }          
  83.            
  84.          
  85.         if (rread < 0 && errno ==EAGAIN) {
  86.             // anche al secondo tentativo non ho letto nulla
  87.             d_print("FAIL! timeout\n");
  88.             fail = true;  
  89.         } else if (atoi(ris) == r)
  90.             // ho letto la somma corretta
  91.             d_print("OK\n");
  92.         else {
  93.             // ho letto un valore errato, stampo la stringa letta dalla pipe
  94.             ris[k+1] ='\0';
  95.             d_print("FAIL! Ho letto %s\n",ris);
  96.             fail = true;              
  97.         }
  98.     }    
  99.      
  100.     if (fail) {
  101.         d_print("=== TEST FALLITO ===\n");
  102.         exit(1);
  103.     }else{
  104.         d_print("=== TEST SUPERATO ===\n");
  105.         exit(0);
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement