home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / comm / jmodem.zip / TEST.C < prev    next >
Text File  |  1990-06-20  |  11KB  |  299 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. #if defined (TURBOC)
  33.     #include <alloc.h>
  34. #else
  35.     #include <malloc.h>
  36. #endif
  37. #include <time.h>
  38. #define SCREEN
  39. #include "jmodem.h"
  40.  
  41. unsigned char far *tick = (unsigned char far *) 0x46C;
  42. SYS syst;
  43. short main (void);
  44. char input[64];
  45. unsigned char *in_buf;
  46. unsigned char *out_buf;
  47. unsigned char *cmp_buf;
  48. unsigned char *sav_ptr;
  49. void show_hex(unsigned short, unsigned char *);
  50. char file_name[]="JMODEM.$$$";
  51. unsigned short status;
  52. short handle;
  53.  
  54. short main()
  55. {
  56.     unsigned short i,j,k,l;
  57.     short flag = 1;
  58.     short max = DAT_MAX;
  59.     l =  DAT_MAX;
  60.     printf("\nBuffer size? (%d) ",DAT_MAX);
  61.     gets(input);
  62.     sscanf(input," %d",&l);
  63.     max = l;
  64.  
  65.     in_buf  = (unsigned char *) malloc(l);             /* Allocate buffer */
  66.     if (in_buf == NULL)                                 /* If an error     */
  67.     {
  68.         printf("\nMemory error!");
  69.         return 1;
  70.     }
  71.     out_buf = (unsigned char *) malloc(l);              /* Allocate buffer */
  72.     if (out_buf == NULL)                                /* If an error     */
  73.     {
  74.         printf("\nMemory error!");
  75.         return 1;
  76.     }
  77.  
  78.     printf("Will write %d records.",l);
  79.     input[0] = 0x00;
  80.     while (input[0] != 'C' && input[0] != 'F')
  81.     {
  82.         printf("\nTest CRC and compressor or File I/O? (C/F) ");
  83.         gets(input);
  84.         input[0] = input[0] & 95;
  85.     }
  86.     switch (input[0])
  87.     {
  88.         case 'F':
  89.         {
  90.         printf("\nWant to check CRC / comp. after the file test? (Y/N) ");
  91.             gets(input);
  92.             if ( (input[0] & 95) == 'Y')
  93.                 flag = 0;
  94.             sav_ptr = out_buf;
  95.             for (i=0; i<l; i++)
  96.                 *out_buf++ = (unsigned char) i;
  97.             out_buf = sav_ptr;
  98.             while (l)
  99.             {
  100.                 remove ("JMODEM.OLD");
  101.                 printf("\nOpening file %s for write.",file_name);
  102.                 status = file_io(CREATE, &handle, file_name, NULL) ;
  103.                 if (status)
  104.                 {
  105.                     printf("\nOpen failed~!");
  106.                     return(1);
  107.                 }
  108.                 printf("\nWriting file %s",file_name);
  109.                 for (i=0; i<100; i++)
  110.                 {
  111.                     printf("\nRecord %d,",i);
  112.                     status = file_io( WRITE ,            /* Function        */
  113.                                  &handle,                /* File handle     */
  114.                                  out_buf,                /* Where data is   */
  115.                                  l );                    /* Amount to write */
  116.                     if (status != l)
  117.                     {
  118.                         printf(" only wrote %d bytes!",status);
  119.                         return 1;
  120.                     }
  121.                     printf(" %d bytes okay.",status);
  122.                  }
  123.                  printf("\nClosing file");
  124.                  file_io(CLOSE,                          /* Function        */
  125.                        &handle,                          /* Open handle     */
  126.                        file_name,                        /* Name not used   */
  127.                           NULL);                         /* Buffer not used */
  128.  
  129.                 printf("\nOpening file %s for read.",file_name);
  130.                 status = file_io(OPEN_READ, &handle, file_name, NULL) ;
  131.                 if (status)
  132.                 {
  133.                     printf("\nOpen failed~!");
  134.                     return(1);
  135.                 }
  136.  
  137.                 printf("\nReading file %s",file_name);
  138.                 for (i=0; i<100; i++)
  139.                 {
  140.                     printf("\nRecord %d,",i);
  141.                     status = file_io( READ ,             /* Function        */
  142.                                  &handle,                /* File handle     */
  143.                                  in_buf,                 /* Where data is   */
  144.                                  l );                    /* Amount to write */
  145.                     if (status != l)
  146.                     {
  147.                         printf(" only read %d bytes!",status);
  148.                         return 1;
  149.                     }
  150.                     printf(" %d bytes okay,",status);
  151.  
  152.                     printf(" comparing,");
  153.                     k = memicmp (out_buf, in_buf,l);
  154.                     if (k)
  155.                     {
  156.                         printf(" did not compare!");
  157.                         return(1);
  158.                     }
  159.                     printf(" okay.");
  160.  
  161.                  }
  162.                  printf("\nClosing file");
  163.                  file_io(CLOSE,                          /* Function        */
  164.                        &handle,                          /* Open handle     */
  165.                    file_name,                        /* Name not used   */
  166.                           NULL);                         /* Buffer not used */
  167.             l--;
  168.             }
  169.             remove ("JMODEM.OLD");
  170.             remove ("JMODEM.$$$");
  171.             if (flag)
  172.                 return(0);
  173.         }
  174.         case 'C':
  175.         {
  176.             l = max;
  177.             cmp_buf = (unsigned char *) malloc(DAT_LEN); /* Allocate buffer */
  178.             if (cmp_buf == NULL)                         /* In an error     */
  179.             {
  180.                 printf("\nMemory error!");
  181.                 return 1;
  182.             }
  183.             printf("\nFilling the input buffer with consecutive bytes.");
  184.             sav_ptr = in_buf;                           /* Save            */
  185.             for (i=0; i< l; i++)
  186.                 *in_buf++ = (unsigned char) i;          /* Fill the buffer */
  187.  
  188.             in_buf = sav_ptr;
  189.             printf("\nAttempting to compress.");
  190.             j = encode ( l,in_buf, cmp_buf);            /* Compress the data*/
  191.             printf("\nEncoder returned %u.",j);         /* Won't compress   */
  192.  
  193.             while (l--)
  194.             {
  195.                 in_buf = sav_ptr;
  196.                 printf("\nFilling the buffer with %u bytes.",l);
  197.                 for (i=0; i< l; i++)
  198.                 {
  199.                     *in_buf++ = *tick;
  200.                     j = 8;
  201.                     while (j--);                        /* Waste some time */
  202.                 }
  203.                 in_buf = sav_ptr;
  204. /*              show_hex(l,in_buf); */
  205.                 printf("\nCompressing,");
  206.                 j = encode ( l,in_buf, cmp_buf);
  207.                 printf(" returned %u.",j);
  208.   /*            show_hex(j,cmp_buf); */
  209.  
  210.                 in_buf = sav_ptr;
  211.                 printf("\nExpanding,");
  212.                 j = decode (j,cmp_buf,out_buf);
  213.  
  214.                 printf(" returned %u.",j);
  215.                 if (j != l)
  216.                     return(1);
  217.  
  218.                 printf("\nComparing,");
  219.                 k = memicmp (out_buf, in_buf,j);
  220.                 if (k)
  221.                 {
  222.                     printf(" did not compare!\n");
  223.                     return(1);
  224.                 }
  225.                 printf(" okay.");
  226.  
  227.                 printf("\nCalculating CRC.");       /* Calculate CRC    */
  228.                 k = calc_crc(SET_CRC,j,out_buf);
  229.                 printf("\nChecking CRC of %4.4X,",k);
  230.                 j = calc_crc(GET_CRC,j,out_buf);    /* Check CRC    */
  231.                 if (j)
  232.                 {
  233.                     printf(" bad CRC!\n");
  234.                     return(1);
  235.                 }
  236.                 printf(" okay.\n");
  237.  
  238.                 in_buf = sav_ptr;
  239.                 printf("\nFilling buffer with %u nulls.",l);
  240.                 for (i=0; i< l; i++)
  241.                     *in_buf++ = (unsigned char) 0;
  242.  
  243.                 in_buf = sav_ptr;
  244.                 printf("\nCompressing,");
  245.                 j = encode ( l,in_buf, cmp_buf);
  246.                 printf(" returned %u.",j);
  247.  
  248.                 in_buf = sav_ptr;
  249.                 printf("\nExpanding,");
  250.                 j = decode (j,cmp_buf,out_buf);
  251.  
  252.                 printf(" returned %u.",j);
  253.                 if (j != l)
  254.                     return(1);
  255.  
  256.                 printf("\nComparing,");
  257.                 k = memicmp (out_buf, in_buf,j);
  258.                 if (k)
  259.                 {
  260.                     printf(" did not compare!");
  261.                     return(1);
  262.                 }
  263.                 printf(" okay.\n");
  264.             }
  265.         }
  266.     }
  267.     return 0;
  268.  
  269. }
  270. /* Dummy routine to keep the linker happy. */
  271.  
  272. void screen (one, two)
  273. short one;
  274. SYS *two;
  275. {
  276.     one = (short) two;
  277. }
  278.  
  279. void show_hex(len,string)
  280. unsigned short len;
  281. unsigned char *string;
  282. {
  283.     unsigned short i;
  284.  
  285.     while (len)
  286.     {
  287.         i = 16;
  288.         while ( (len) && (i) )
  289.             {
  290.             printf("%2.2X ",*string++);
  291.             len --;
  292.             i--;
  293.             }
  294.  
  295.     printf("\n");
  296.     }
  297.     return;
  298. }
  299.