home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c160 / 1.ddi / TEST / T4WRITE2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-22  |  3.4 KB  |  167 lines

  1.  
  2. /* t4write2.c
  3.  
  4.    (c)Copyright Sequiter Software Inc., 1987-1990. All rights reserved.
  5.  
  6.    Tests Code Base 'd4seek' and 'd4write' routines.
  7.  
  8.    1.  Create databases 'd4learn.dbf' and 'd4learn.ndx'.
  9.  
  10.    2.  Write 'n' records with even values
  11.        2*reccount - 2*n
  12.        Ex.  For 5 records      8, 6, 4, 2, 0
  13.        
  14.  
  15.    3.  Seek for a record which has not yet been searched for.
  16.        Write corresponding Odd Value.   2*n-1
  17.        Ex.  For 5 records   1, 3, 5, 7, 9
  18.  
  19.    4.  Check the index file.
  20. */
  21.  
  22. #include  "p4misc.h"
  23. #include  "d4all.h"
  24. #include  "w4.h"
  25. #include  "u4error.h"
  26.  
  27. #include <string.h>
  28. #include <stdlib.h>
  29.  
  30. static FIELD  fields[]=
  31. {
  32.    { "TEST", 'C', 10, 0, 0 },
  33. } ;
  34.  
  35.  
  36. main( int argc, char **argv )
  37. {
  38.    int  r, width, i_ref, rc, c ;
  39.    long n, i_rec, odd_rec ;
  40.    char *ptr ;
  41.    char  buf[1000] ;
  42.  
  43.    #ifdef NO_HUGE
  44.       #ifdef IS_386
  45.          d4init() ;
  46.       #else
  47.          d4initialize( 4, 4, 20, 1000, 20000L ) ;
  48.       #endif
  49.    #else
  50.       d4init() ;
  51.    #endif
  52.    w4clear(-1) ;
  53.  
  54.    r = 0 ;
  55.  
  56.    if ( argc < 2 )
  57.    {
  58.       w4( r++,0, "Write/Seek Test" ) ;
  59.       w4( r++,0, "t4write2  num_records" ) ;
  60.       w4exit(0) ;
  61.    }
  62.  
  63.    n =  atol( argv[1] ) ;
  64.    r++ ;
  65.    w4(   ++r,0, "Number of Records:" ) ;
  66.    w4long( r,w4col(), n, 6)  ;
  67.    r++ ;
  68.  
  69.    d4create( "t4write2", 1, fields, 0 ) ;
  70.    ptr   =  f4ptr(   f4ref( "TEST") ) ;
  71.    width =  f4width( f4ref( "TEST") ) ;
  72.  
  73.    w4( w4row()+1,0, "Writing Rec:" ) ;
  74.    c =  w4col() ;
  75.  
  76.    for ( i_rec = 1L; i_rec <= n; i_rec++ )
  77.    {
  78.       w4long( w4row(),c, i_rec, 6 ) ;
  79.       c4ltoa( 2*n - 2*i_rec, ptr, width ) ;
  80.       d4write(0L) ;
  81.    }
  82.  
  83.    i_ref =  i4index( "t4write2", "TEST", 0, 0 ) ;
  84.    
  85.    /* Ex. 5 records:  2,3,1,4,0 (ignored),5;
  86.       Ex. 4 records:  2,3,1,4
  87.    */
  88.    if (  n & 1L )
  89.       odd_rec = n ;
  90.    else 
  91.       odd_rec = n+1 ;
  92.  
  93.    w4( w4row()+1,0, "Seeking Rec:" ) ;
  94.    c =  w4col() ;
  95.  
  96.    for ( i_rec = n/2; i_rec >= 0; )
  97.    {
  98.       if ( i_rec > 0 && i_rec <= n )
  99.       {
  100.          w4long( w4row(),c, i_rec, 6 ) ;
  101.  
  102.          c4ltoa( 2*n - 2*i_rec, buf, width ) ;
  103.      if ( d4seek( buf ) != 0 )
  104.          {
  105.         u4error( 0, "t4write2:  d4seek", (char *) 0 ) ;
  106.             w4exit(1) ;
  107.          }
  108.  
  109.      c4ltoa( 2*i_rec-1, ptr, width ) ;
  110.      d4write( d4recno() ) ;
  111.       }
  112.  
  113.       if ( i_rec <= n/2 )
  114.          i_rec =  odd_rec-i_rec ;
  115.       else
  116.       {
  117.      i_rec =  odd_rec-i_rec ;
  118.      i_rec -- ;
  119.       }
  120.    }
  121.  
  122.    w4( w4row()+1,0, "Checking Index File" ) ;
  123.    i4check( i_ref ) ;
  124.  
  125.    w4( w4row()+1,0, "Checking Record:" ) ;
  126.    c =  w4col() ;
  127.  
  128.    for ( i_rec = 1L, rc =  d4top(); rc != 3; i_rec++, rc = d4skip(1L) ) 
  129.    {
  130.       w4long( w4row(),c, i_rec, 6 ) ;
  131.  
  132.       if ( d4recno() != i_rec )
  133.       {
  134.      u4error( 0, "t4write2:  Seek/Write Test Fail, Final Check", (char *) 0 ) ;
  135.          w4exit(1) ;
  136.       }
  137.  
  138.       c4ltoa( 2*i_rec-1, buf, width ) ;
  139.       if ( memcmp( ptr, buf, width ) != 0 )
  140.       {
  141.      u4error( 0, "t4write2:  Seek/Write Test Fail, Final Check", (char *) 0 ) ;
  142.          w4exit(1) ;
  143.       }
  144.    }
  145.  
  146.    d4close_all() ;
  147.  
  148.  
  149.    #ifdef H4TEST
  150.    {
  151.       int  rc ;
  152.       d4init_undo() ;
  153.       rc = h4free_check(32000) ;
  154.       d4init() ;
  155.       if ( rc != 0 )
  156.       {
  157.          u4error( 0, "t4write2:  Memory items not freed", (char *) 0 );
  158.          w4exit(1) ;
  159.       }
  160.    }
  161.    #endif
  162.  
  163.    w4handle(1) ;
  164.    w4( w4row()+1,0, "t4write2:  SUCCESS" ) ;
  165.    w4exit(0) ;
  166. }
  167.