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

  1.  
  2. /* t4pack_z.c  (c)Copyright Sequiter Software Inc., 1987-1990. All rights reserved.
  3.  
  4.    Tests Code Base packing/zapping/writing.
  5.  
  6. */
  7.  
  8. #include  "p4misc.h"
  9. #include  "d4all.h"
  10. #include  "w4.h"
  11. #include  "u4error.h"
  12.  
  13. #include <string.h>
  14. #include <stdlib.h>
  15.  
  16. static FIELD field_list[] =
  17. {
  18.    {"NUM",   'N',     11,    2,    0    },
  19.    {"CH ",   'C',     17,    0,    0    },
  20.    {"DT ",   'D',      7,    0,    0    },
  21. };
  22.  
  23. static void  do_test( long, long, int, int, int, int ) ;
  24.  
  25. static void  do_test( long num_recs, long num_delete, 
  26.                       int do_pack, int i_ref, int i_ref2, int i_ref3 )
  27. {
  28.    long  rec_num, delete_cycle, l_count, f_ref ;
  29.    int   i, c, rc ;
  30.    double  index_date, d ;
  31.  
  32.    w4( w4row()+1,0,  "Writing Record: " ) ;
  33.    c =  w4col() ;
  34.  
  35.    c4dt_julian( "19891220", &index_date ) ;
  36.  
  37.    for (rec_num = 1; rec_num <= num_recs; rec_num++)
  38.    {
  39.       w4long( w4row(),c, rec_num, 6 ) ;
  40.  
  41.       for ( i= 1; i <= f4num_fields(); i++ )
  42.       {
  43.          f_ref =  f4j_ref(i) ;
  44.  
  45.      switch( f4type(f_ref) )
  46.          {
  47.             case 'N':
  48.                d =  rec_num/5.0 ;
  49.                f4replace( f_ref, &d ) ;
  50.                break ;
  51.  
  52.             case 'C':
  53.             {
  54.                c4ltoa( rec_num, f4ptr(f_ref), f4width(f_ref) ) ;
  55.                break ;
  56.             }
  57.  
  58.             case 'D':
  59.                d =  index_date + rec_num/3 ;
  60.                c4dt_str( f4ptr(f_ref), &d ) ;
  61.                break ;
  62.          }
  63.       }
  64.  
  65.       if ( d4append() < 0 )
  66.       {
  67.      u4error( 0, "t4pack_z:  d4write", (char *) 0 ) ;
  68.          w4exit(1) ;
  69.       }
  70.    }
  71.  
  72.    w4( w4row()+1,0,  "Finished Writing Records") ;
  73.  
  74.    /*  Now for the pack or zap test. */
  75.    w4( w4row()+1,0,  "Num Records:  " ) ;
  76.    w4long( w4row(),w4col(), d4reccount(), 5 ) ;
  77.    w4( w4row()+1,0,  "Checking Index File") ;
  78.    if ( i4check(i_ref) < 0 )
  79.    {
  80.       u4error( 0, "t4pack_z:  i4check 1", (char *) 0 ) ;
  81.       w4exit(1) ;
  82.    }
  83.    if ( i4check(i_ref2) < 0 ) 
  84.    {
  85.       u4error( 0, "t4pack_z:  i4check 2", (char *) 0 ) ;
  86.       w4exit(1) ;
  87.    }
  88.    if ( i4check(i_ref3) < 0 )
  89.    {
  90.       u4error( 0, "t4pack_z:  i4check 3", (char *) 0 ) ;
  91.       w4exit(1) ;
  92.    }
  93.  
  94.    w4( w4row()+1,0,  "Delete Cycle, Records" ) ;
  95.  
  96.    delete_cycle = 0L ;
  97.  
  98.    while ( d4reccount() > 0 )
  99.    {
  100.       if ( d4reccount() != (num_recs - delete_cycle*num_delete) )
  101.       {
  102.      u4error( 0, "t4pack_z:  d4reccount", (char *) 0 ) ;
  103.          w4exit(1) ;
  104.       }
  105.  
  106.       delete_cycle ++ ;
  107.  
  108.       w4clear(w4row()+1) ;
  109.       w4long( w4row(),0,  delete_cycle, 5 ) ;
  110.       w4long( w4row(),w4col(), d4reccount(), 5 ) ;
  111.  
  112.       if ( do_pack )
  113.       {
  114.          for (l_count = 1; l_count <= num_delete && l_count <= d4reccount(); l_count++)
  115.          {
  116.             long del_rec ;
  117.           
  118.             if ( delete_cycle % 2L == 0L )
  119.                del_rec = l_count ;
  120.             else
  121.                del_rec =  d4reccount()+1-l_count ;
  122.  
  123.             if ( d4delete(  del_rec ) < 0 )
  124.             {
  125.            u4error( 0, "t4pack_z:  d4delete", (char *) 0 ) ;
  126.                w4exit(1) ;
  127.             }
  128.          }
  129.  
  130.          if ( do_pack == 2 )
  131.          {
  132.             w4( w4row(),w4col(), "  Extended Packing" ) ;
  133.             rc =  x4pack(0) ;
  134.          }
  135.          else
  136.          {
  137.             w4( w4row(),w4col(), "  Normal Packing" ) ;
  138.             rc =  d4pack() ;
  139.          }
  140.  
  141.          if ( rc < 0 )
  142.          {
  143.         u4error( 0, "t4pack_z:  d4pack", (char *) 0 ) ;
  144.             w4exit(1) ;
  145.          }
  146.       }
  147.       else
  148.       {
  149.          long  start ;
  150.  
  151.          w4( w4row(),w4col(), "  Zapping" ) ;
  152.  
  153.          if ( delete_cycle % 3L == 0L )
  154.             start =  1L ;
  155.          else
  156.          {
  157.             if ( delete_cycle % 3L == 1L )
  158.                start =  (num_delete+1L)/2 ;
  159.             else
  160.                start =  d4reccount() + 1L - num_delete ;
  161.          }
  162.  
  163.          if ( start+num_delete > d4reccount() )
  164.             start = d4reccount() +1L - num_delete ;
  165.  
  166.          if ( start <= 0L )  start = 1L ;
  167.  
  168.      if ( d4zap( start, start+num_delete-1 ) < 0 )
  169.          {
  170.         u4error( 0, "t4pack_z:  d4zap", (char *) 0 ) ;
  171.             w4exit(1) ;
  172.          }
  173.       }
  174.  
  175.       w4( w4row(),w4col(),  "  Checking") ;
  176.       if ( i4check(i_ref) < 0 ) 
  177.       {
  178.      u4error( 0, "t4pack_z:  i4check 4", (char *) 0 ) ;
  179.          w4exit(1) ;
  180.       }
  181.       if ( i4check(i_ref2) < 0 )
  182.       {
  183.      u4error( 0, "t4pack_z:  i4check 5", (char *) 0 ) ;
  184.          w4exit(1) ;
  185.       }
  186.       if ( i4check(i_ref3) < 0 ) 
  187.       {
  188.      u4error( 0, "t4pack_z:  i4check 6", (char *) 0 ) ;
  189.          w4exit(1) ;
  190.       }
  191.    }
  192. }
  193.  
  194. main( int argc, char **argv )
  195. {
  196.    int     i_ref, i_ref2, i_ref3, r ;
  197.    long    num_recs, num_delete ;
  198.  
  199.    #ifdef NO_HUGE
  200.       #ifdef IS_386
  201.          d4init() ;
  202.       #else
  203.          d4initialize( 4, 4, 20, 1000, 20000L ) ;
  204.       #endif
  205.    #else
  206.       d4init() ;
  207.    #endif
  208.    w4clear(-1) ;
  209.  
  210.    r = 0 ;
  211.  
  212.    if ( argc < 3 )
  213.    {
  214.       w4( r++,0, "Pack/Zap Test" ) ;
  215.       w4( r++,0, "t4pack_z  num_records  num_to_delete_at_a_time" ) ;
  216.       w4exit(0) ;
  217.    }
  218.  
  219.    num_recs =  atol( argv[1] ) ;
  220.  
  221.    r++ ;
  222.    w4( ++r,0, "Number of Records:" ) ;
  223.    w4long( r,w4col(), num_recs, 6) ;
  224.  
  225.    num_delete =  atol( argv[2] ) ;
  226.  
  227.    r++ ;
  228.    w4( ++r,0, "Number to Delete at a Time:" ) ;
  229.    w4long( r,w4col(), num_delete, 6) ;
  230.  
  231.    if ( d4create("t4pack",3,field_list,0) < 0 )
  232.    {
  233.       u4error( 0, "t4pack_z:  d4create", (char *) 0 ) ;
  234.       w4exit(1) ;
  235.    }
  236.  
  237.    x4filter( d4deleted ) ;
  238.  
  239.    if ( (i_ref = i4index("t4pack","CH",0,0)) < 0 ) 
  240.    {
  241.       u4error( 0, "t4pack_z:  i4index 1", (char *) 0 ) ;
  242.       w4exit(1) ;
  243.    }
  244.  
  245.    if ( (i_ref2 = i4index("t4pack2","NUM",0,0)) < 0)  
  246.    {
  247.       u4error( 0, "t4pack_z:  i4index 2", (char *) 0 ) ;
  248.       w4exit(1) ;
  249.    }
  250.  
  251.    if ( (i_ref3 = i4index("t4pack3","DT",0,0)) < 0 )  
  252.    {
  253.       u4error( 0, "t4pack_z:  i4index 3", (char *) 0 ) ;
  254.       w4exit(1) ;
  255.    }
  256.  
  257.    do_test( num_recs, num_delete, 2, i_ref, i_ref2, i_ref3 ) ;
  258.    do_test( num_recs, num_delete, 1, i_ref, i_ref2, i_ref3 ) ;
  259.    do_test( num_recs, num_delete, 0, i_ref, i_ref2, i_ref3 ) ;
  260.  
  261.    d4close_all() ;
  262.  
  263.  
  264.    #ifdef H4TEST
  265.    {
  266.       int  rc ;
  267.       d4init_undo() ;
  268.       rc = h4free_check(32000) ;
  269.       d4init() ;
  270.       if ( rc != 0 )
  271.       {
  272.          u4error( 0, "t4pack_z:  Memory items not freed", (char *) 0 );
  273.          w4exit(1) ;
  274.       }
  275.    }
  276.    #endif
  277.  
  278.    w4handle(1) ;
  279.    w4( w4row()+1,0, "t4pack_z:  SUCCESS" ) ;
  280.    w4exit(0) ;
  281. }
  282.  
  283.