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

/* Tests CodeBase++ Date class. */

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

#include  "t4test.h"

static FIELD4INFO field_list[] =
{
{"NUM", 'N',  11,  2  },
{"CH ", 'C',  17,  0  },
{"DT ", 'D',   8,  0  },
{ 0,0,0,0 },
} ;

#ifndef S4INDEX_OFF
static TAG4INFO tag_list[] =
{
{ "T_NUM", "NUM", 0, 0, 0 },
{ "T_CH",   "CH", 0, 0, 0 },
{ "T_DT",   "DT", 0, 0, 0 },
{ 0,0,0,0,0 },
} ;
#endif

CODE4 cb ;
DATA4  *data ;
FIELD4 *field ;

static int do_test( D4DISPLAY *display, int do_pack, long num_recs, long num_delete, int do_open )
{
  int  rc, i, delete_cycle ;
  char *dt = "19891220" ;
  long index_date, rec_num, l_count, del_rec, start ;

  index_date = date4long( dt ) ;

  cb.safety =  0 ;

  if ( do_open )
    data = d4open( &cb, "T4PACK_Z" ) ;
  else
  {
#ifdef S4INDEX_OFF
    data = d4create( &cb, "T4PACK_Z", field_list, 0 ) ;
#else
    data = d4create( &cb, "T4PACK_Z", field_list, tag_list ) ;
#endif
  }

  if ( data == 0 )
    t4severe( t4err_data, "01" ) ;

  d4opt_start( &cb ) ;

  d4display_str( display,  "        Appending Record:   ", 1 ) ;

  for ( rec_num = 1; rec_num <= num_recs; rec_num++)
  {
    if ( d4display_quit( display ) )  return 1 ;

    if ( rec_num % 50 == 0 )
    {
      display->x = (int) 0 ;
      d4display_str( display,  "        Appending Record:   ", 0 ) ;
      d4display_num( display, rec_num, 0 ) ;
    }

    if ( d4append_start( data, 0 ) < 0 )
      t4severe( t4err_append, "02" ) ;

    for ( i= 1 ; i <= d4num_fields( data ) ; i++ )
    {
      field = d4field_j( data, i ) ;
      if ( field == 0 )
        t4severe( t4err_field, "03" ) ;

      switch( f4type( field ) )
      {
      case  'N':
        f4assign_double( field, (double) rec_num / 5 ) ;
        break ;
      case  'C':
        f4assign_long( field, rec_num ) ;
        break ;
      case  'D':
        f4assign_long( field, (long)( index_date + rec_num / 3 ) ) ;
        break ;
      default:
        t4severe( t4err_field, "04" ) ;
      }
    }

    if ( d4append( data ) < 0 )
      t4severe( t4err_append, "05" ) ;
  }

  d4display_str( display,  "  ", 1 ) ;
  d4display_str( display,  "        Checking Index File. . . ", 1 ) ;
  d4display_wait( display ) ;
  rc = d4check( data ) ;
  d4display_start() ;
  if ( rc != 0 )
    t4severe( t4err_check, "06" ) ;

  delete_cycle = 0L ;

  while ( d4reccount( data ) > 0 )
  {
    if ( d4display_quit( display ) )
      return 1 ;

    if ( d4reccount( data ) != ( num_recs - delete_cycle * num_delete ) )
      t4severe( t4err_count, "07" ) ;

    delete_cycle++ ;

    if ( do_pack )
    {
      d4display_str( display,  "        Packing.  Current Reccount: ", 1 ) ;
      d4display_num( display, (long) d4reccount( data ), 0 ) ;

      for (l_count=1; l_count<=num_delete && l_count<=d4reccount(data); l_count++)
      {
        if ( d4display_quit( display ) )
          return 1 ;

        if ( delete_cycle % 2L == 0L )
          del_rec = l_count ;
        else
          del_rec =  d4reccount( data )+1-l_count ;

        if ( d4go( data, del_rec ) != 0 )
          t4severe( t4err_go, "08" ) ;

        d4delete( data ) ;
      }

      if ( d4pack( data ) != 0 )
        t4severe( t4err_pack, "09" ) ;
    }
    else
    {
      d4display_str( display,  "        Zapping.  Current Reccount: ", 1 ) ;
      d4display_num( display, (long) d4reccount( data ), 0 ) ;

      if ( delete_cycle % 3L == 0L )
        start =  1L ;
      else
      {
        if ( delete_cycle % 3L == 1L )
          start =  (num_delete+1L)/2 ;
        else
          start =  d4reccount( data ) + 1L - num_delete ;
      }

      if ( ( start + num_delete ) > d4reccount( data ) )
        start = d4reccount( data ) +1L - num_delete ;

      if ( start <= 0L )
        start = 1L ;

      d4display_wait( display ) ;
      rc = d4zap( data, start, start + num_delete - 1  ) ;
      d4display_start() ;
      if ( rc != 0 )
        t4severe( t4err_zap, "10" ) ;
    }

    d4display_str( display,  "        Checking Index File. . . ", 1 ) ;
    d4display_wait( display ) ;
    rc = d4check( data ) ;
    d4display_start() ;
    if ( rc != 0 )
      t4severe( t4err_check, "11" ) ;
  }
  d4close( data );
  return 0 ;
}


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

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

  if ( do_test( display, 1, num_recs, num_delete, 0 ) )
    return 1 ;
  if ( do_test( display, 0, num_recs, num_delete, 1 ) )
    return 1 ;

#ifndef S4TEST_KEEP_FILES
  u4remove( "T4PACK_Z.dbf" ) ;
#ifdef S4INDEX_OFF
#ifdef N4OTHER
  u4remove( "T4PACK_Z.CGP" ) ;
#endif
#ifdef S4MDX
  u4remove( "T4PACK_Z.mdx" ) ;
#endif
#ifdef S4FOX
  u4remove( "T4PACK_Z.cdx" ) ;
#endif
#ifdef S4CLIPPER
  u4remove( "T_NUM.NTX" ) ;
  u4remove( "T_CH.NTX" ) ;
  u4remove( "T_DT.NTX" ) ;
#endif
#ifdef S4NDX
  u4remove( "T_NUM.NDX" ) ;
  u4remove( "T_CH.NDX" ) ;
  u4remove( "T_DT.NDX" ) ;
#endif
#endif
#endif

  d4init_undo(&cb) ;
  mem4reset() ;

#ifdef S4DEBUG
  mem4check_memory() ;

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

  return 0 ;
}

int  S4FUNCTION t4test( D4DISPLAY *display )
{
  long num_recs = 1000 ;
  long num_delete = 100 ;

#ifndef S4NO_PARMS
  num_recs = atol( d4parsestring_nparm( &display->parse_str ) ) ;
  num_delete = atol( d4parsestring_nparm( &display->parse_str ) ) ;
  
  if ( num_recs <= 0 || num_delete <= 0 )
    t4severe( t4err_general, "13" ) ;
#endif

  d4display_str( display,  "T4PACK_Z Test  ", 1 ) ;
  d4display_str( display,  " ", 1 ) ;
  d4display_str( display,  "Number of Recs:   ", 1 ) ;
  d4display_num( display,  (long) num_recs, 0 ) ;
  d4display_str( display,  "Number for Deletion:   ", 1 ) ;
  d4display_num( display,  (long) num_delete, 0 ) ;
  d4display_str( display,  "", 1 ) ;

  if ( test_with_mem_check( display, num_recs, num_delete ) )
    e4exit(&cb) ;

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