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

  1.  
  2. /* t4sort2.c   (c)Copyright Sequiter Software Inc., 1987-1990.  All rights reserved. */
  3.  
  4. /* Create three databases.
  5.  
  6.    Sort using an index file lookups on the other databases.
  7. */
  8.  
  9. #include "p4misc.h"
  10. #include "d4all.h"
  11. #include "w4.h"
  12. #include "u4error.h"
  13.  
  14. #include <stdlib.h>
  15.  
  16. int  t4sort2(long,long) ;
  17.  
  18. main(int argc, char **argv)
  19. {
  20.    long  num_rec, num_alloc ;
  21.  
  22.    #ifdef NO_HUGE
  23.       #ifdef IS_386
  24.          d4init() ;
  25.       #else
  26.          d4initialize( 4, 4, 20, 1000, 20000L ) ;
  27.       #endif
  28.    #else
  29.       d4init() ;
  30.    #endif
  31.    w4clear(-1) ;
  32.  
  33.    if ( argc < 2 )
  34.    {
  35.       w4(0,0, "t4sort2  Num_Records [MemAlloc(Default 8000)]") ;
  36.       w4(1,0, "t4sort2  Test 'x4sort' routine") ;
  37.       w4cursor(2,0) ;
  38.       w4exit(1) ;
  39.    }
  40.  
  41.    num_rec =  atol( argv[1] ) ;
  42.  
  43.    if ( argc > 2 )
  44.       num_alloc =  atol( argv[2] ) ;
  45.    else
  46.       num_alloc =  8000 ;
  47.  
  48.    w4( w4row()+1,0, "Num Records:" ) ;  w4long( w4row(),w4col(), num_rec, 8 ) ;
  49.    w4( w4row()+1,0, "Mem Alloc:  " ) ;  w4long( w4row(),w4col(), num_alloc,8 ) ;
  50.  
  51.    t4sort2( num_rec, num_alloc ) ;
  52.    d4close_all() ;
  53.  
  54.    #ifdef H4TEST
  55.    {
  56.       int  rc ;
  57.       d4init_undo() ;
  58.       rc = h4free_check(32000) ;
  59.       d4init() ;
  60.       if ( rc != 0 )
  61.       {
  62.          u4error( 0, "t4sort2:  Memory items not freed", (char *) 0 );
  63.          w4exit(1) ;
  64.       }
  65.    }
  66.    #endif
  67.  
  68.    w4handle(1) ;
  69.    w4( w4row()+1,0, "t4sort2:  SUCCESS" ) ;
  70.  
  71.    w4exit(0) ;
  72. }
  73.  
  74. void check_base(long), check_index(long) ;
  75.  
  76. static FIELD  start[] =
  77. {
  78.    { "VAL",   'C',  6, 0, 0 },
  79. } ;
  80.  
  81. static FIELD  t4look_a[] =
  82. {
  83.    { "VAL",   'C', 6, 0, 0 },
  84. } ;
  85.  
  86. static FIELD  t4look_b[] =
  87. {
  88.    { "VAL",  'C', 6, 0, 0 },
  89. } ;
  90.  
  91.  
  92. /*
  93.    Each index file is indexed according to record number ordering.
  94.  
  95.    'T4LOOK_B' - has 'num_rec' records.
  96.               - Reverse Order
  97.  
  98.    'T4LOOK_A' - has 'num_rec*2' records.
  99.               - Regular Order
  100.  
  101.    'T4START'  - has 'num_rec*4' records.
  102.               - Sort   T4LOOK_B->VAL + T4LOOK_A->VAL + STR(RECNO())
  103.               - Reverse Order
  104.  
  105.     Example:  8 Records
  106.  
  107.        Rec 8 (1) -> Rec 4 (4) -> Rec 2 (1)
  108.        Rec 4 (5) -> Rec 2 (2) -> Rec 1 (2)
  109. */
  110.  
  111. int  t4sort2( long num_recs, long mem_alloc )
  112. {
  113.    int  start_ref, new_ref, t4look_a_ref, i_t4look_a_ref,
  114.         t4look_b_ref, i_t4look_b_ref ;
  115.    long f_ref, i_rec ;
  116.  
  117.    d4buf_init( mem_alloc, mem_alloc, 1L ) ;
  118.    start_ref     =  d4create( "t4start",  1, start, 0 ) ;
  119.  
  120.    t4look_a_ref  =  d4create( "T4LOOK_A", 1, t4look_a, 0 ) ;
  121.    i_t4look_a_ref=  i4index(  "t4look_a", "STR(RECNO(),6,0)", 0, 0 ) ;
  122.  
  123.    t4look_b_ref  =  d4create( "t4look_b", 1, t4look_b, 0 ) ;
  124.    i_t4look_b_ref=  i4index(  "t4look_b", "STR(RECNO(),6,0)", 0, 0 ) ;
  125.  
  126.    /* Define two relations, first to lookup in 'T4LOOK_A.DBF' and
  127.       another recursive relation to lookup in 'T4LOOK_B.DBF'.
  128.    */
  129.    d4select( start_ref ) ;
  130.    d4buf_total( 200L, 3, 1 ) ;
  131.    d4buf_unit( 5L ) ;
  132.    x4relate( "STR((RECNO()+.1)/2,6,0)", t4look_a_ref, i_t4look_a_ref, -1L ) ;
  133.  
  134.    d4select( t4look_a_ref ) ;
  135.    d4buf_total( 20L, 3, 1 ) ;
  136.    d4buf_unit( 15L ) ;
  137.    x4relate( "STR((RECNO()+.1)/2,6,0)", t4look_b_ref, i_t4look_b_ref, -1L ) ;
  138.  
  139.    d4select( t4look_b_ref ) ;
  140.    d4buf_total( 70L, 2, 1 ) ;
  141.    d4buf_unit( 25L ) ;
  142.  
  143.    /* Now add the data. */
  144.    d4select( start_ref ) ;
  145.    f_ref =  f4ref( "VAL" ) ;
  146.    for ( i_rec = 1L; i_rec <= 4*num_recs; i_rec++ )
  147.    {
  148.       f4r_long( f_ref, 4*num_recs+1L-i_rec ) ;
  149.       d4append() ;
  150.    }
  151.  
  152.    d4select( t4look_a_ref ) ;
  153.    f_ref =  f4ref( "VAL" ) ;
  154.    for ( i_rec = 1L; i_rec <= 2*num_recs; i_rec++ )
  155.    {
  156.       f4r_long( f_ref, i_rec ) ;
  157.       d4append() ;
  158.    }
  159.  
  160.    d4select( t4look_b_ref ) ;
  161.    f_ref =  f4ref( "VAL" ) ;
  162.    for ( i_rec = 1L; i_rec <= num_recs; i_rec++ )
  163.    {
  164.       f4r_long( f_ref, num_recs+1L-i_rec ) ;
  165.       d4append() ;
  166.    }
  167.  
  168.    d4select( start_ref ) ;
  169.    new_ref = x4sort( "T4SORT2", "T4LOOK_B->VAL + T4LOOK_A->VAL + VAL", 1L, 0) ;
  170.  
  171.    d4select( new_ref ) ;
  172.    check_base( num_recs ) ;
  173.  
  174.    d4select( start_ref ) ;
  175.    i4index_filter( "t4start", "T4LOOK_B->VAL + T4LOOK_A->VAL + VAL", 1, 0, 
  176.                    x4relate_do ) ;
  177.    check_index( num_recs ) ;
  178.  
  179.    if ( d4skip(1L) != 3 )
  180.    {
  181.       u4error( 0, "t4sort2", (char *) 0 ) ;
  182.       w4exit(1) ;
  183.    }
  184.  
  185.    return 0 ;
  186. }
  187.  
  188.  
  189. void  check_index( long n )
  190. {
  191.    long  i,j,k ;
  192.  
  193.    d4top() ;
  194.  
  195.    for ( i=n; i>=1; i-- )
  196.    {
  197.       for ( j=i*2-1; j <= i*2; j++ )
  198.       {
  199.      for ( k=j*2; k>= j*2-1; k-- )
  200.      {
  201.             if ( d4recno() != k  || d4eof())
  202.             {
  203.                u4error( 0, "t4sort2: checker", (char *) 0 ) ;
  204.                w4exit(1) ;
  205.             }
  206.         d4skip(1L) ;
  207.      }
  208.       }
  209.    }
  210.    if ( ! d4eof())
  211.    {
  212.       u4error( 0, "t4sort2", (char *) 0 ) ;
  213.       w4exit(1) ;
  214.    }
  215. }
  216.  
  217. void  check_base( long n )
  218. {
  219.    long  i,j,k ;
  220.  
  221.    d4top() ;
  222.    for ( i=n; i>=1; i-- )
  223.    {
  224.       for ( j=i*2-1; j <= i*2; j++ )
  225.       {
  226.      for ( k=j*2; k>= j*2-1; k-- )
  227.      {
  228.         long  val ;
  229.  
  230.         val = f4long(f4ref("VAL")) ;
  231.         
  232.             if ( d4reccount()-val+1L != k || d4eof() )
  233.             {
  234.                u4error( 0, "t4sort2: checker", (char *) 0 ) ;
  235.                w4exit(1) ;
  236.             }
  237.         d4skip(1L) ;
  238.      }
  239.       }
  240.    }
  241.    if ( ! d4eof())
  242.    {
  243.       u4error( 0, "t4sort2", (char *) 0 ) ;
  244.       w4exit(1) ;
  245.    }
  246. }
  247.