home *** CD-ROM | disk | FTP | other *** search
-
- /* t4sort.c (c)Copyright Sequiter Software Inc., 1987-1990. All rights reserved. */
-
- #include "p4misc.h"
- #include "d4all.h"
- #include "w4.h"
- #include "u4error.h"
-
- #include <stdlib.h>
-
- int t4sort(long,long), filter_odd(void), filter_over_100(void) ;
-
- int filter_odd()
- {
- if ( d4recno()%2 == 1 ) return 1 ;
- return 0 ;
- }
-
- int filter_over_100()
- {
- if ( f4long(f4j_ref(1)) > 100L ) return 2 ; /* Generate EOF Condition. */
- return 0 ;
- }
-
- static FIELD fields[] =
- {
- { "NUM", 'N', 8, 0, 0 },
- } ;
-
- int t4sort( long num_rec, long mem_alloc )
- {
- int rc, ref, sort_ref2, sort_ref3, sort_ref4 ;
- long i_rec, f_ref, f_ref2, f_ref3, f_ref4 ;
-
- w4clear(-1) ;
-
- ref = d4create( "t4sort", 1, fields, 0 ) ;
- f_ref = f4ref( "NUM" ) ;
-
- d4buf_init( mem_alloc, mem_alloc, 0L ) ;
- d4buf_total( 100L, 1, 1 ) ;
- d4buf_unit( 100L ) ;
-
- for ( i_rec = 1L; i_rec <= num_rec; i_rec++ )
- {
- d4append_blank() ;
- f4r_long( f_ref, num_rec-i_rec+1L ) ;
- }
-
- sort_ref2 = x4sort( "t4sort2", "NUM", 1L, 0 ) ;
- d4select( sort_ref2) ;
- if ( sort_ref2 < 0 ) w4exit(-1) ;
- f_ref2 = f4ref( "NUM" ) ;
-
- if ( num_rec != d4reccount() )
- {
- u4error( 0, "t4sort: Error in t4sort2", (char *) 0 );
- w4exit(-1) ;
- }
-
- for ( i_rec = 1L; i_rec <= num_rec; i_rec++ )
- {
- d4go( i_rec ) ;
- if ( f4long( f_ref2) != i_rec )
- {
- u4error( 0, "t4sort: Error in t4sort2", (char *) 0 );
- w4exit(-1) ;
- }
- }
-
-
- /* Sort filtering Even Records. */
- d4select( ref ) ;
- x4filter( filter_odd ) ;
-
- sort_ref3 = x4sort( "t4sort3", "NUM", 1L, 0 ) ;
- d4select( sort_ref3 ) ;
- if ( sort_ref3 < 0 ) w4exit(-1) ;
- f_ref3 = f4ref( "NUM" ) ;
-
- if ( num_rec/2 != d4reccount() )
- {
- u4error( 0, "t4sort: Error in t4sort3", (char *) 0 );
- w4exit(-1) ;
- }
-
- for ( i_rec = 1L; i_rec <= d4reccount(); i_rec++ )
- {
- d4go( i_rec) ;
-
- if ( f4long( f_ref3) != i_rec*2 - (num_rec+1)%2 )
- {
- u4error( 0, "t4sort: Error in t4sort3", (char *) 0 );
- w4exit(-1) ;
- }
- }
-
-
- d4select( ref ) ;
- x4filter_reset() ;
- x4filter( filter_over_100 ) ;
-
- sort_ref4 = x4sort( "t4sort4", "NUM", 3L, 0 ) ;
- d4select( sort_ref4) ;
- if ( sort_ref4 < 0 ) w4exit(-1) ;
- f_ref4 = f4ref( "NUM" ) ;
-
- rc = d4top() ;
- if ( d4reccount() > 100L || d4reccount() >= num_rec )
- {
- u4error( 0, "t4sort: Error in t4sort4", (char *) 0 );
- w4exit(-1) ;
- }
-
- for ( rc = d4top(); rc != 3; rc = d4skip(1L) )
- {
- if ( f4long( f_ref4) != d4recno() )
- {
- u4error( 0, "t4sort: Error in t4sort4", (char *) 0 );
- w4exit(-1) ;
- }
- }
-
- d4close_all() ;
-
- return 0 ;
- }
-
-
- main( int argc, char **argv )
- {
- long num_rec, num_alloc ;
-
- #ifdef NO_HUGE
- #ifdef IS_386
- d4init() ;
- #else
- d4initialize( 4, 4, 20, 1000, 20000L ) ;
- #endif
- #else
- d4init() ;
- #endif
- w4clear(-1) ;
-
- if ( argc < 2 )
- {
- w4(0,0, "t4sort Num_Records [MemAlloc(Default 8000)]") ;
- w4(1,0, "t4sort Test 'x4sort' routine") ;
- w4cursor(2,0) ;
- w4exit(1) ;
- }
-
- num_rec = atol( argv[1] ) ;
-
- if ( argc > 2 )
- num_alloc = atol( argv[2] ) ;
- else
- num_alloc = 8000 ;
-
- w4( w4row()+1,0, "Num Records:" ) ; w4long( w4row(),w4col(), num_rec, 8 ) ;
- w4( w4row()+1,0, "Mem Alloc: " ) ; w4long( w4row(),w4col(), num_alloc,8 ) ;
-
- t4sort( num_rec, num_alloc ) ;
-
- #ifdef H4TEST
- {
- int rc ;
- d4init_undo() ;
- rc = h4free_check(32000) ;
- d4init() ;
- if ( rc != 0 )
- {
- u4error( 0, "t4sort: Memory items not freed", (char *) 0 );
- w4exit(1) ;
- }
- }
- #endif
-
- w4handle(1) ;
- w4( w4row()+1,0, "t4sort: SUCCESS" ) ;
- w4exit(0) ;
- }
-