home *** CD-ROM | disk | FTP | other *** search
/ The CIA World Factbook 1992 / k3bimage.iso / sel / 02 / 0035 / test.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-02  |  10.6 KB  |  289 lines

  1. /****************************************************************************/
  2. /*     These routines test the data-compression and expansion routines      */
  3. /*     To alter:                                                            */
  4. /*     MAKE TEST                                                            */
  5. /*     ( use the MAKE file )                                                */
  6. /*                                                                          */
  7. /*     When compiling, you will get 3 warning messages about unused         */
  8. /*     parameters. This is because of the dummy screen() routine which      */
  9. /*     does nothing except keep the linker happy.                           */
  10. /*                                                                          */
  11. /*                                                                          */
  12. /*                                                                          */
  13. /*     When executed, this program uses the timer-tick for data and tests   */
  14. /*     compression/expansion for all data lengths used.                     */
  15. /*     It takes about 84 hours to execute.  It aborts on the first error.   */
  16. /*                                                                          */
  17. /*     Since one might like to stop it to use their computer, I added       */
  18. /*     routines so it may be restarted with the last buffer size.           */
  19. /*                                                                          */
  20. /*     This routine thoroughly tests both the file I/O and the compression  */
  21. /*     expansion, CRC routines to verify that they will NOT corrupt any     */
  22. /*     data. These tests are exhaustive. They were used to find the         */
  23. /*     MicroSoft stream-I/O bug and are the reason I changed to the         */
  24. /*     Unix-type "handle" file routines.                                    */
  25. /*                                                                          */
  26. /*                                                                          */
  27. /****************************************************************************/
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>                     /* Used for _free()                 */
  31. #include <string.h>
  32. #include <malloc.h>
  33. #include <time.h>
  34. #include "jmodem.h"
  35.  
  36. SYS syst;
  37. short main (void);
  38. char input[64];
  39. unsigned char *in_buf;
  40. unsigned char *out_buf;
  41. unsigned char *cmp_buf;
  42. unsigned char *sav_ptr;
  43. void show_hex(unsigned short, unsigned char *);
  44. char file_name[]="JMODEM.$$$";
  45. unsigned short status;
  46. unsigned short handle;
  47.  
  48. short main()
  49. {
  50.     unsigned short i,j,k,l;
  51.     short flag = 1;
  52.     short max = DAT_MAX;
  53.     in_buf  = (unsigned char *) malloc(DAT_LEN);        /* Allocate buffer */
  54.     if (in_buf == NULL)                                 /* If an error     */
  55.     {
  56.         printf("\nMemory error!");
  57.         return 1;
  58.     }
  59.     out_buf = (unsigned char *) malloc(DAT_LEN);        /* Allocate buffer */
  60.     if (out_buf == NULL)                                /* If an error     */
  61.     {
  62.         printf("\nMemory error!");
  63.         return 1;
  64.     }
  65.     input[0] = 0x00;
  66.     while (input[0] != 'C' && input[0] != 'F')
  67.     {
  68.         printf("\nTest CRC and compressor or File I/O? (C/F) ");
  69.         gets(input);
  70.         input[0] = input[0] & 95;
  71.     }
  72.  
  73.     switch (input[0])
  74.     {
  75.         case 'F':
  76.         {
  77.             l =  DAT_MAX;
  78.         printf("\nWant to check CRC / comp. after the file test? (Y/N) ");
  79.             gets(input);
  80.             if ( (input[0] & 95) == 'Y')
  81.                 flag = 0;
  82.         printf("\nBuffer size? (%d) ",DAT_MAX);
  83.         gets(input);
  84.             sscanf(input," %d",&l);
  85.             printf("\nWill write %d records.",l);
  86.             max = l;
  87.             sav_ptr = out_buf;
  88.             for (i=0; i<l; i++)
  89.                 *out_buf++ = (unsigned char) i;
  90.             out_buf = sav_ptr;
  91.             while (l)
  92.             {
  93.                 remove ("JMODEM.OLD");
  94.                 printf("\nOpening file %s for write.",file_name);
  95.                 status = file_io(CREATE, &handle, file_name, NULL) ;
  96.                 if (status)
  97.                 {
  98.                     printf("\nOpen failed~!");
  99.                     return(1);
  100.                 }
  101.                 printf("\nWriting file %s",file_name);
  102.                 for (i=0; i<100; i++)
  103.                 {
  104.                     printf("\nRecord %d,",i);
  105.                     status = file_io( WRITE ,            /* Function        */
  106.                                  &handle,                /* File handle     */
  107.                                  out_buf,                /* Where data is   */
  108.                                  l );                    /* Amount to write */
  109.                     if (status != l)
  110.                     {
  111.                         printf(" only wrote %d bytes!",status);
  112.                         return 1;
  113.                     }
  114.                     printf(" %d bytes okay.",status);
  115.                  }
  116.                  printf("\nClosing file");
  117.                  file_io(CLOSE,                          /* Function        */
  118.                        &handle,                          /* Open handle     */
  119.                        file_name,                        /* Name not used   */
  120.                           NULL);                         /* Buffer not used */
  121.  
  122.                 printf("\nOpening file %s for read.",file_name);
  123.                 status = file_io(OPEN_READ, &handle, file_name, NULL) ;
  124.                 if (status)
  125.                 {
  126.                     printf("\nOpen failed~!");
  127.                     return(1);
  128.                 }
  129.  
  130.                 printf("\nReading file %s",file_name);
  131.                 for (i=0; i<100; i++)
  132.                 {
  133.                     printf("\nRecord %d,",i);
  134.                     status = file_io( READ ,             /* Function        */
  135.                                  &handle,                /* File handle     */
  136.                                  in_buf,                 /* Where data is   */
  137.                                  l );                    /* Amount to write */
  138.                     if (status != l)
  139.                     {
  140.                         printf(" only read %d bytes!",status);
  141.                         return 1;
  142.                     }
  143.                     printf(" %d bytes okay,",status);
  144.  
  145.                     printf(" comparing,");
  146.                     k = memicmp (out_buf, in_buf,l);
  147.                     if (k)
  148.                     {
  149.                         printf(" did not compare!");
  150.                         return(1);
  151.                     }
  152.                     printf(" okay.");
  153.  
  154.                  }
  155.                  printf("\nClosing file");
  156.                  file_io(CLOSE,                          /* Function        */
  157.                        &handle,                          /* Open handle     */
  158.                    file_name,                        /* Name not used   */
  159.                           NULL);                         /* Buffer not used */
  160.             l--;
  161.             }
  162.             remove ("JMODEM.OLD");
  163.             remove ("JMODEM.$$$");
  164.             if (flag)
  165.                 return(0);
  166.         }
  167.         case 'C':
  168.         {
  169.             l = max;
  170.             cmp_buf = (unsigned char *) malloc(DAT_LEN); /* Allocate buffer */
  171.             if (cmp_buf == NULL)                         /* In an error     */
  172.             {
  173.                 printf("\nMemory error!");
  174.                 return 1;
  175.             }
  176.             printf("\nFilling the input buffer with consecutive bytes.");
  177.             sav_ptr = in_buf;                           /* Save            */
  178.             for (i=0; i< l; i++)
  179.                 *in_buf++ = (unsigned char) i;          /* Fill the buffer */
  180.  
  181.             in_buf = sav_ptr;
  182.             printf("\nAttempting to compress.");
  183.             j = encode ( l,in_buf, cmp_buf);            /* Compress the data*/
  184.             printf("\nEncoder returned %u.",j);         /* Won't compress   */
  185.             while (l--)
  186.             {
  187.                 in_buf = sav_ptr;
  188.                 printf("\nFilling the buffer with %u bytes.",l);
  189.                 for (i=0; i< l; i++)
  190.                     *in_buf++ = (unsigned char) clock();
  191.  
  192.                 in_buf = sav_ptr;
  193.   /*            show_hex(l,in_buf); */
  194.                 printf("\nCompressing,");
  195.                 j = encode ( l,in_buf, cmp_buf);
  196.                 printf(" returned %u.",j);
  197.   /*            show_hex(j,cmp_buf); */
  198.  
  199.                 in_buf = sav_ptr;
  200.                 printf("\nExpanding,");
  201.                 j = decode (j,cmp_buf,out_buf);
  202.  
  203.                 printf(" returned %u.",j);
  204.                 if (j != l)
  205.                     return(1);
  206.  
  207.                 printf("\nComparing,");
  208.                 k = memicmp (out_buf, in_buf,j);
  209.                 if (k)
  210.                 {
  211.                     printf(" did not compare!\n");
  212.                     return(1);
  213.                 }
  214.                 printf(" okay.");
  215.  
  216.                 printf("\nCalculating CRC.");        /* Calculate CRC    */
  217.                 k = calc_crc(SET_CRC,j,out_buf);
  218.                 printf("\nChecking CRC of %4.4X,",k);
  219.                 j = calc_crc(GET_CRC,j,out_buf);    /* Check CRC    */
  220.                 if (j)
  221.                 {
  222.                     printf(" bad CRC!\n");
  223.                     return(1);
  224.                 }
  225.                 printf(" okay.\n");
  226.  
  227.                 in_buf = sav_ptr;
  228.                 printf("\nFilling buffer with %u nulls.",l);
  229.                 for (i=0; i< l; i++)
  230.                     *in_buf++ = (unsigned char) 0;
  231.  
  232.                 in_buf = sav_ptr;
  233.                 printf("\nCompressing,");
  234.                 j = encode ( l,in_buf, cmp_buf);
  235.                 printf(" returned %u.",j);
  236.  
  237.                 in_buf = sav_ptr;
  238.                 printf("\nExpanding,");
  239.                 j = decode (j,cmp_buf,out_buf);
  240.  
  241.                 printf(" returned %u.",j);
  242.                 if (j != l)
  243.                     return(1);
  244.  
  245.                 printf("\nComparing,");
  246.                 k = memicmp (out_buf, in_buf,j);
  247.                 if (k)
  248.                 {
  249.                     printf(" did not compare!");
  250.                     return(1);
  251.                 }
  252.                 printf(" okay.");
  253.             }
  254.         }
  255.     }
  256.     return 0;
  257.  
  258. }
  259.  
  260. short screen(one,two,three)                      /* Dummy routine */
  261. short one;
  262. SYS *two;
  263. char *three;
  264. {
  265.  
  266.     return(0);
  267. }
  268.  
  269. void show_hex(len,string)
  270. unsigned short len;
  271. unsigned char *string;
  272. {
  273.     unsigned short i;
  274.  
  275.     while (len)
  276.     {
  277.         i = 16;
  278.         while ( (len) && (i) )
  279.             {
  280.             printf("%2.2X ",*string++);
  281.             len --;
  282.             i--;
  283.             }
  284.  
  285.     printf("\n");
  286.     }
  287.     return;
  288. }
  289.