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

/* Tests Code Base 'd4position' routine. */

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

#ifndef S4INDEX_OFF

static FIELD4INFO fields[] =
{
{ "FIELD", 'C', 25, 0 },
{ 0, 0,0,0 },
} ;

static TAG4INFO tags[] =
{
{ "TAG_NAME", "FIELD", "", 0, 0 },
{ 0,0,0, 0, 0 },
} ;

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

static int test_position( D4DISPLAY *display ) ;

static int  test_main( D4DISPLAY *display, long num_recs, int do_open )
{
  long on_rec ;

  cb.safety = 0 ;

  if ( do_open )
  {
    data = d4open( &cb, "T4POSITI" ) ;
    d4zap( data, 1L, d4reccount( data ) ) ;
  }
  else
    data = d4create( &cb, "T4POSITI", fields, tags ) ;

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

  d4opt_start( &cb ) ;

  field = d4field( data, "FIELD" ) ;
  if ( field == 0 )
    t4severe( t4err_field, "02" ) ;

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

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

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

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

    f4assign_long( field, on_rec ) ;
    if ( d4append(data) < 0 )
      t4severe( t4err_append, "04" ) ;
  }

  if ( d4check( data ) != 0 )
    t4severe( t4err_check, "05" ) ;

  d4tag_select( data, 0 ) ;
  if ( test_position( display ) )
    return 1 ;

  tag = d4tag( data, "TAG_NAME" ) ;
  if ( tag == 0 )
    t4severe( t4err_tag, "06" ) ;
  d4tag_select( data, tag ) ;
  if ( test_position( display ) )
    return 1 ;

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


static int  test_position( D4DISPLAY *display )
{
  long count, r, top_rec, bottom_rec ;
  double pos ;
  double last_pos = -.1 ;

  count = d4reccount( data ) ;

  for ( r = 1L; r <= count; r++ )
  {
    if ( d4display_quit( display ) )
      return 1 ;

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

    d4position2( data, &pos ) ;

    if ( pos <= last_pos )
      t4severe( t4err_position, "09" ) ;

    if ( d4position_set( data, pos )  != 0 )
      t4severe( t4err_position, "10" ) ;

    if ( d4recno( data ) != r )
      t4severe( t4err_recno, "11" ) ;

    if ( d4recno( data ) != r )
      t4severe( t4err_recno, "12" ) ;

    pos = last_pos ;
  }

  /* EOF, BOF testing */
  if ( d4top( data )  != 0 )
    t4severe( t4err_go, "13" ) ;
  top_rec = d4recno( data ) ;

  if ( d4position_set( data, 0.0 ) != 0 )
    t4severe( t4err_position, "14" ) ;
  if ( top_rec != d4recno( data ) )
    t4severe( t4err_recno, "15" ) ;

  if ( d4bottom( data )  != 0 )
    t4severe( t4err_go, "16" ) ;
  bottom_rec = d4recno( data ) ;

  if ( d4position_set( data, 1.0 ) != 0 )
    t4severe( t4err_position, "17" ) ;
  if ( bottom_rec != d4recno( data ) )
    t4severe( t4err_recno, "18" ) ;

  if ( d4skip( data, 1L ) != r4eof )
    t4severe( t4err_skip, "19" ) ;

  d4position2( data, &pos );
  if ( pos <= 1.0 )
    t4severe( t4err_position, "20" ) ;

  if ( d4position_set( data, 1.1 ) != r4eof )
    t4severe( t4err_position, "21" ) ;

  if ( ! d4eof( data ) )
    t4severe( t4err_general, "22" ) ;

  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_main( display, num_recs, 0 ) )
    return 1 ;
  if ( test_main( display, num_recs, 1 ) )
    return 1 ;

#ifndef S4TEST_KEEP_FILES
  u4remove( "T4POSITI.dbf" ) ;
#ifdef N4OTHER
  u4remove( "T4POSITI.CGP" ) ;
#endif
#ifdef S4MDX
  u4remove( "T4POSITI.mdx" ) ;
#endif
#ifdef S4FOX
  u4remove( "T4POSITI.cdx" ) ;
#endif
#ifdef S4CLIPPER
  u4remove( "TAG_NAME.NTX" ) ;
#endif
#ifdef S4NDX
  u4remove( "TAG_NAME.NDX" ) ;
#endif
#endif

  d4init_undo(&cb) ;
  mem4reset() ;

#ifdef S4DEBUG
  mem4check_memory() ;

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

  return 0 ;
}

#endif

int S4FUNCTION t4test( D4DISPLAY *display )
{
#ifdef S4INDEX_OFF
  display->y += 2 ;
  d4display_str( display, "T4POSITI:   REQUIRES INDEXING (S4INDEX_OFF DEFINED)", 1) ;
  d4display_str( display, "", 1) ;
  return 1 ;
#else
#ifdef S4CLIPPER
  d4display_str( display,  "T4POSITI: not for CLIPPER format ", 1 ) ;
  return 0 ;
#else
  long num_recs = 1000 ;
  
#ifndef S4NO_PARMS
  num_recs = atol( d4parsestring_nparm( &display->parse_str ) ) ;
  if ( num_recs <= 0 )
    t4severe( t4err_parm, "24" ) ;
#endif
  
  d4display_str( display,  "T4POSITION 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 ) )
    t4severe( t4err_general, "25" ) ;
  
  display->y += 2 ;
  d4display_str( display, "T4POSITION:   SUCCESS", 1) ;
  d4display_str( display, "", 1) ;
  return 1 ;
#endif
#endif
}