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

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

#include "t4test.h"

#ifndef S4INDEX_OFF

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

static FIELD4INFO fields[] =
{
{ "TEST", 'N', 15, 0 },
{ 0,0,0,0},
} ;

static TAG4INFO tags[] =
{
{ "TAG", "TEST", "", 0, 0 },
{ 0,0,0, 0, 0 },
} ;


static int test_write( D4DISPLAY *display, long num_recs, int do_open )
{
  long i_rec ;
  double d   =  60000000.0 ;
  double neg = -5.0 ;

  cb.safety =  0 ;

  if (do_open)
  {
    data = d4open( &cb, "T4WRITE" ) ;
    if ( d4zap( data, 1L, d4reccount( data ) ) != 0 )
      t4severe( t4err_zap, "01" ) ;
  }
  else
    data = d4create( &cb, "T4WRITE", fields, tags ) ;

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

  d4opt_start( &cb ) ;

  field =  d4field( data, "TEST" ) ;
  if ( field == 0 )
    t4severe( t4err_field, "03" ) ;

  f4assign_double( field, d ) ;

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

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

    if ( i_rec % 50 == 0 || i_rec == num_recs )
    {
      display->x = (int) 0 ;
      d4display_str( display,  "        Writing Record:  ", 0 ) ;
      d4display_num( display, i_rec, 0 ) ;
    }

    if ( d4append_start( data, 0 ) != 0 )
      t4severe( t4err_append, "04" ) ;
    if ( d4append( data ) != 0 )
      t4severe( t4err_append, "05" ) ;
  }

  tag = d4tag( data, "TAG" ) ;
  if ( tag == 0 )
    t4severe( t4err_tag, "06" ) ;
  d4tag_select( data, tag ) ;
  if (d4check( data ) < 0 )
    t4severe( t4err_check, "07" ) ;

  d -=  100000.0 ;

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

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

    if ( i_rec % 50 == 0 || i_rec == num_recs )
    {
      display->x = (int) 0 ;
      d4display_str( display,  "        Replacing Record:  ", 0 ) ;
      if ( d4recno( data ) != num_recs - i_rec + 1 )
        d4display_num( display, i_rec, 0 ) ;
    }

    if ( d4seek_double( data, d ) != r4after )
      t4severe( t4err_seek, "08" ) ;

#ifndef S4CLIPPER
#ifdef S4MDX
    if ( d4recno( data ) != num_recs - i_rec + 1 )
#endif
#ifdef S4NDX
      if ( d4recno( data ) != i_rec )
#endif
#ifdef S4FOX
        if ( d4recno( data ) != i_rec )
#endif
          t4severe( t4err_recno, "09" ) ;
#endif

    f4assign_double( field, neg ) ;
  }

  if (d4check( data ) < 0 )
    t4severe( t4err_check, "10" ) ;

  if ( d4close_all( &cb ) != 0 )
    t4severe( t4err_close, "11" ) ;

  return 0 ;
}


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

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

  if ( test_write( display, num_recs, 0 ) )
    return 1 ;
  if ( test_write( display, num_recs, 1 ) )
    return 1 ;

#ifndef S4TEST_KEEP_FILES
  u4remove( "T4WRITE.dbf" ) ;
#ifdef N4OTHER
  u4remove( "T4WRITE.CGP" ) ;
#endif
#ifdef S4MDX
  u4remove( "T4WRITE.mdx" ) ;
#endif
#ifdef S4FOX
  u4remove( "T4WRITE.cdx" ) ;
#endif
#ifdef S4CLIPPER
  u4remove( "TAG.NTX" ) ;
#endif
#ifdef S4NDX
  u4remove( "TAG.NDX" ) ;
#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 ;
}

#endif

int  S4FUNCTION t4test( D4DISPLAY *display )
{
#ifdef S4INDEX_OFF
  display->y += 2 ;
  d4display_str( display, "T4WRITE:   REQUIRES INDEXING (S4INDEX_OFF DEFINED)", 1) ;
  d4display_str( display, "", 1) ;
  return 1 ;
#else
  long num_recs = 1000 ;

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

  d4display_str( display,  "T4WRITE Test  ", 1 ) ;
  d4display_str( display,  " ", 1 ) ;
  d4display_str( display,  "Number of Recs:   ", 1 ) ;
  d4display_num( display,  (long) num_recs, 0 ) ;
  d4display_str( display,  "", 1 ) ;

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

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