home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_11 / 2n11029a < prev    next >
Text File  |  1991-09-13  |  3KB  |  119 lines

  1. /* ----------------------------------------------------
  2.  *  LISTING 7
  3.  *
  4.  *  Filename:           btest.c
  5.  *  Summary:            test cache file i/o interface
  6.  *  Author:             T.W. Nelson
  7.  *  Compile options:    
  8.  *  Version:            1.00
  9.  *  Date:               05-Sep-1991
  10.  *  Notes:
  11.  *
  12.  *  Source code Copyright (c) 1991 T.W. Nelson. May be
  13.  *  used only for non-commercial purposes with
  14.  *  appropriate acknowledgement of copyright.
  15.  * ------------------------------------------------- */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <time.h>
  20. #include <bios.h>
  21. #include <io.h>
  22. #include "bfile.h"
  23.  
  24. #define BMAX      8     //#cache blocks
  25. #define BSIZE   512     //block size in bytes
  26.  
  27. BFILE *bf;
  28. char buf[BSIZE];
  29. int Fblocks;
  30.  
  31. void do_random(void)
  32. {
  33.     //read and write random blocks from file .....
  34.     long blockno;
  35.  
  36.     printf( "Random block read/write test routine.\n");
  37.     printf( "Press any key to quit...\n\n" );
  38.     randomize();        //initialize random# generator
  39.  
  40.     while( bioskey(1) == 0 )    {
  41.         blockno = random( Fblocks );
  42.         read_b( bf, (void *) buf, (ulong) blockno );
  43.         write_b( bf, (void *) buf, (ulong) blockno );
  44.     }
  45.  
  46.     bioskey(0);         //remove keypress from buffer
  47. }
  48.  
  49. int btest(void)
  50. {
  51.     int     option;
  52.     static long num = -1L;
  53.  
  54.     printf("\n0=quit, 1=auto r/w, 2=read, 3=write: ");
  55.     scanf( "%d", &option );
  56.  
  57.     switch( option )
  58.     {
  59.     case    0:
  60.         return 0;       //quit
  61.     case    1:
  62.         do_random();    //random read/write
  63.         return 1;
  64.     case    2:
  65.         printf( "Enter block to read: " );
  66.         scanf( "%ld", &num );
  67.         if( (int) num >= Fblocks )
  68.             printf("Block# out of range\a\n");
  69.         else
  70.             read_b( bf, (void *) buf, (ulong) num);
  71.         return 1;
  72.     case    3:
  73.         if( num != -1L )    {
  74.             write_b( bf, (void *) buf, (ulong) num);
  75.             printf("Block %ld written\n", num );
  76.         }
  77.         return 1;
  78.     default:
  79.         return 1;
  80.     }
  81. }
  82.  
  83. void print_status(void)
  84. {
  85.     chdr_t *p = 0;
  86.  
  87.     printf("Cache stats: hits=%ld miss=%ld adds=%ld\n",
  88.             bf->cac.hits, bf->cac.miss, bf->cac.adds );
  89.     printf( "Block#  Acc   Stat  MFRU=%ld  LFRU=%ld\n",
  90.                bf->cac.mfru->bnum, bf->cac.lfru->bnum);
  91.     printf( "------- ----- -----\n" );
  92.  
  93.     for( p = bf->cac.mfru; p != GROUND; p = p->next ) {
  94.             printf( "%-7ld %-5ld %-4u\n", p->bnum,
  95.                                           p->acc,
  96.                                           p->stat );
  97.     }
  98. }
  99.  
  100. main( int argc, char **argv )
  101. {
  102.     if( argc <= 1 )     {
  103.             printf("Need file name argument...\n");
  104.             return 1;
  105.     }
  106.  
  107.     bf = open_b( argv[1], BMAX, BSIZE, 0);
  108.     Fblocks = (int) (filelength( bf->fd ) / BSIZE);
  109.  
  110.     while( btest() )
  111.             print_status();
  112.  
  113.     close_b( bf );
  114.  
  115.     return 0;
  116. }
  117.  
  118. /* ---- End of File -------------------------------- */
  119.