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

  1.  
  2. /* t4sort.c   (c)Copyright Sequiter Software Inc., 1987-1990.  All rights reserved. */
  3.  
  4. #include "p4misc.h"
  5. #include "d4all.h"
  6. #include "w4.h"
  7. #include "u4error.h"
  8.  
  9. #include <stdlib.h>
  10.  
  11. int   t4sort(long,long), filter_odd(void), filter_over_100(void) ;
  12.  
  13. int  filter_odd()
  14. {
  15.    if ( d4recno()%2 == 1 )  return 1 ;
  16.    return 0 ;
  17. }
  18.  
  19. int  filter_over_100()
  20. {
  21.    if ( f4long(f4j_ref(1)) > 100L )  return 2 ;  /* Generate EOF Condition. */
  22.    return 0 ;
  23. }
  24.  
  25. static FIELD  fields[] =
  26. {
  27.    { "NUM",  'N',  8, 0, 0 },
  28. } ;
  29.  
  30. int  t4sort( long num_rec, long mem_alloc )
  31. {
  32.    int  rc, ref, sort_ref2, sort_ref3, sort_ref4 ;
  33.    long i_rec, f_ref, f_ref2, f_ref3, f_ref4 ;
  34.  
  35.    w4clear(-1) ;
  36.  
  37.    ref =  d4create( "t4sort", 1, fields, 0 ) ;
  38.    f_ref =  f4ref( "NUM" ) ;
  39.  
  40.    d4buf_init( mem_alloc, mem_alloc, 0L ) ;
  41.    d4buf_total( 100L, 1, 1 ) ;
  42.    d4buf_unit( 100L ) ; 
  43.  
  44.    for ( i_rec = 1L; i_rec <= num_rec; i_rec++ )
  45.    {
  46.       d4append_blank() ;
  47.       f4r_long( f_ref, num_rec-i_rec+1L ) ;
  48.    }
  49.  
  50.    sort_ref2 =  x4sort( "t4sort2", "NUM", 1L, 0 ) ;
  51.    d4select( sort_ref2) ;
  52.    if ( sort_ref2 < 0 )  w4exit(-1) ;
  53.    f_ref2 =  f4ref( "NUM" ) ;
  54.  
  55.    if ( num_rec != d4reccount() )
  56.    {
  57.       u4error( 0, "t4sort: Error in t4sort2", (char *) 0 );
  58.       w4exit(-1) ;
  59.    }
  60.  
  61.    for ( i_rec = 1L; i_rec <= num_rec; i_rec++ )
  62.    {
  63.       d4go( i_rec ) ;
  64.       if ( f4long( f_ref2) != i_rec )
  65.       {
  66.      u4error( 0, "t4sort: Error in t4sort2", (char *) 0 );
  67.      w4exit(-1) ;
  68.       }
  69.    }
  70.  
  71.  
  72.    /* Sort filtering Even Records. */
  73.    d4select( ref ) ;
  74.    x4filter( filter_odd ) ;
  75.  
  76.    sort_ref3 =  x4sort( "t4sort3", "NUM", 1L, 0 ) ;
  77.    d4select( sort_ref3 ) ;
  78.    if ( sort_ref3 < 0 )  w4exit(-1) ;
  79.    f_ref3 =  f4ref( "NUM" ) ;
  80.  
  81.    if ( num_rec/2 != d4reccount()   )
  82.    {
  83.       u4error( 0, "t4sort: Error in t4sort3", (char *) 0 );
  84.       w4exit(-1) ;
  85.    }
  86.  
  87.    for ( i_rec = 1L; i_rec <= d4reccount(); i_rec++ )
  88.    {
  89.       d4go( i_rec) ;
  90.  
  91.       if ( f4long( f_ref3) != i_rec*2 - (num_rec+1)%2 )
  92.       {
  93.      u4error( 0, "t4sort: Error in t4sort3", (char *) 0 );
  94.      w4exit(-1) ;
  95.       }
  96.    }
  97.  
  98.  
  99.    d4select( ref ) ;
  100.    x4filter_reset() ;
  101.    x4filter( filter_over_100 ) ;
  102.  
  103.    sort_ref4 =  x4sort( "t4sort4", "NUM", 3L, 0 ) ;
  104.    d4select( sort_ref4) ;
  105.    if ( sort_ref4 < 0 )  w4exit(-1) ;
  106.    f_ref4 =  f4ref( "NUM" ) ;
  107.  
  108.    rc =  d4top() ;
  109.    if ( d4reccount() > 100L || d4reccount() >= num_rec )
  110.    {
  111.       u4error( 0, "t4sort: Error in t4sort4", (char *) 0 );
  112.       w4exit(-1) ;
  113.    }
  114.  
  115.    for ( rc = d4top(); rc != 3; rc = d4skip(1L) ) 
  116.    {
  117.       if ( f4long( f_ref4) != d4recno() )
  118.       {
  119.      u4error( 0, "t4sort: Error in t4sort4", (char *) 0 );
  120.      w4exit(-1) ;
  121.       }
  122.    }
  123.  
  124.    d4close_all() ;
  125.  
  126.    return 0 ;
  127. }
  128.  
  129.  
  130. main( int argc, char **argv )
  131. {
  132.    long  num_rec, num_alloc ;
  133.  
  134.    #ifdef NO_HUGE
  135.       #ifdef IS_386
  136.          d4init() ;
  137.       #else
  138.          d4initialize( 4, 4, 20, 1000, 20000L ) ;
  139.       #endif
  140.    #else
  141.       d4init() ;
  142.    #endif
  143.    w4clear(-1) ;
  144.  
  145.    if ( argc < 2 )
  146.    {
  147.       w4(0,0, "t4sort  Num_Records [MemAlloc(Default 8000)]") ;
  148.       w4(1,0, "t4sort  Test 'x4sort' routine") ;
  149.       w4cursor(2,0) ;
  150.       w4exit(1) ;
  151.    }
  152.  
  153.    num_rec =  atol( argv[1] ) ;
  154.  
  155.    if ( argc > 2 )
  156.       num_alloc =  atol( argv[2] ) ;
  157.    else
  158.       num_alloc =  8000 ;
  159.  
  160.    w4( w4row()+1,0, "Num Records:" ) ;  w4long( w4row(),w4col(), num_rec, 8 ) ;
  161.    w4( w4row()+1,0, "Mem Alloc:  " ) ;  w4long( w4row(),w4col(), num_alloc,8 ) ;
  162.  
  163.    t4sort( num_rec, num_alloc ) ;
  164.  
  165.    #ifdef H4TEST
  166.    {
  167.       int  rc ;
  168.       d4init_undo() ;
  169.       rc = h4free_check(32000) ;
  170.       d4init() ;
  171.       if ( rc != 0 )
  172.       {
  173.          u4error( 0, "t4sort:  Memory items not freed", (char *) 0 );
  174.          w4exit(1) ;
  175.       }
  176.    }
  177.    #endif
  178.  
  179.    w4handle(1) ;
  180.    w4( w4row()+1,0, "t4sort:  SUCCESS" ) ;
  181.    w4exit(0) ;
  182. }
  183.