/* t4sort.c   (c)Copyright Sequiter Software Inc., 1990-1993.  All rights reserved. */
/* Tests sorting. */

#include "d4all.h"
#ifdef __TURBOC__
#pragma hdrstop
#endif

#include "t4test.h"

CODE4 cb;
DATA4 *data;
FIELD4 *field ;
TAG4 *tag ;
SORT4 sort ;

static int test_sort( D4DISPLAY *display, long num_sort )
{
  char sort_val[7] ;
  long sort_record_no, i_rec ;
  char *other_ptr, *data_ptr ;
  int rc ;

  /* sort4init() must be called before sort4put(). */
  /* The sort data length is one; there is no extra data */

  if ( sort4init( &sort, &cb, 6, 0 ) != 0 )
    t4severe( t4err_sort, "01" ) ;

  d4display_str( display,  "        Putting Item:   ", 1 ) ;

  for ( i_rec = 1; i_rec <= num_sort; i_rec++ )
  {
    if ( d4display_quit( display ) )
      return 1 ;

    if ( i_rec % 2000 == 0 || i_rec == num_sort )
    {
      display->x = (int) 0 ;
      d4display_str( display,  "        Putting Item:   ", 0 ) ;
      d4display_num( display, i_rec, 0 ) ;
    }

    c4ltoa45( i_rec, sort_val, 6 ) ;
    sort_val[6] = '\0' ;

    if ( sort4put( &sort, i_rec, sort_val, "" ) != 0 )
      t4severe( t4err_sort, "02" ) ;
  }

  /* Specify that there will be no more calls to sort4put() */

  d4display_str( display,  "        Initializing for gets . . .   ", 1 ) ;

  if ( sort4get_init( &sort ) < 0 )
    t4severe( t4err_sort, "03" ) ;

  for ( i_rec = 1; i_rec <= num_sort; i_rec++ )
  {
    if ( d4display_quit( display ) )
      return 1 ;

    if ( i_rec % 2000 == 0 || i_rec == num_sort )
    {
      display->x = (int) 0 ;
      d4display_str( display,  "        Getting Item:   ", 0 ) ;
      d4display_num( display, i_rec, 0 ) ;
    }

    rc = sort4get( &sort, &sort_record_no, (void **) &data_ptr, (void **) &other_ptr ) ;
    if ( rc < 0 )
      t4severe( t4err_sort, "04" ) ;

    c4ltoa45( i_rec, sort_val, 6 ) ;

    if ( memcmp( data_ptr, sort_val, 6) != 0 )
      t4severe( t4err_sort, "05" ) ;

    if ( sort_record_no != i_rec )
      t4severe( t4err_sort, "06" ) ;
  }

  rc = sort4get( &sort, &sort_record_no, (void **) &data_ptr, (void **) &other_ptr ) ;
  if ( rc < 0 )
    t4severe( t4err_sort, "07" ) ;

  if ( d4close_all( &cb ) != 0 )
    t4severe( t4err_close, "08" ) ;
  return 0 ;
}


static int  test_with_mem_check( D4DISPLAY *display, long num_sort )
{
  d4init( &cb ) ;

  cb.hWnd = (unsigned) display->hWnd ;
#ifdef S4DLL
  cb.hInst = (unsigned) display->hInst ;
#endif

  if ( test_sort( display, num_sort ) ) return 1 ;

#ifdef S4DEBUG
  mem4check_memory() ;
#endif

  mem4reset() ;

#ifdef S4DEBUG

#ifndef S4DLL
  if ( mem4free_check(100) != 0 )
    t4severe( t4err_memory, "09" ) ;
#endif
#endif

  return 0 ;
}


int  S4FUNCTION t4test( D4DISPLAY *display )
{
  long num_sort = 20000L ;

#ifndef S4NO_PARMS
  num_sort = atol( d4parsestring_nparm( &display->parse_str ) ) ;
  if ( num_sort <= 0 )
    num_sort = 70000L ;
#endif

  d4display_str( display,  "T4SORT Test  ", 1 ) ;
  d4display_str( display,  " ", 1 ) ;
  d4display_str( display,  "Number to Sort:   ", 1 ) ;
  d4display_num( display,  (long) num_sort, 0 ) ;
  d4display_str( display,  "", 1 ) ;

  if ( test_with_mem_check( display, num_sort ) )
    t4severe( t4err_general, "10" ) ;

  display->y += 2 ;
  d4display_str( display, "T4SORT:   SUCCESS", 1 ) ;
  d4display_str( display, "", 1) ;
  return 1 ;
}
