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

/* tests Code Base 'd4skip' routine. */ 

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

#include "t4test.h"

#ifndef S4INDEX_OFF

static FIELD4INFO fields[] =
{
{"FLD",   'N',    13,    1 },
{"CHR",   'C',     8,    0 },
{ 0,       0 ,     0,    0 },
};

static TAG4INFO tags[] =
{
{"TAG",     "CHR",     "", 0, 0 },
{"TAG_TWO", "101-VAL(CHR)", "", 0, 0 },
{ 0,     0,   0, 0, 0 },
} ;

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

static void t4test_eof( DATA4 *data, int bof_cond, int eof_cond )
{
  int  rc ;

  rc = 0 ;
  
  if ( bof_cond )
  {
    if ( ! d4bof( data ) )
      rc = -1 ;
  }
  else
  {
    if ( d4bof( data ) )
      rc = -1 ;
  }
  if ( rc )
    t4severe( t4err_general, "01" ) ;
  
  if ( eof_cond )
  {
    if ( ! d4eof( data ) )
      rc = -1 ;
  }
  else
  {
    if ( d4eof( data ) )
      rc = -1 ;
  }
  if ( rc )
    t4severe( t4err_general, "02" ) ;
}

static void rec( DATA4 *data, long rec_no )
{
  if ( d4recno( data ) != rec_no )
    t4severe( t4err_recno, "03" ) ;
}

static int  test_skip( D4DISPLAY *display, int do_open )
{
  char  buff[9] ;
  long  i_rec ;

  cb.safety = 0 ;

  if ( do_open )
  {
    cb.auto_open = 0 ;
    data = d4open( &cb, "T4SKIP" ) ;
    d4zap( data, 1L, d4reccount( data ) ) ;
  }
  else
    data = d4create( &cb, "T4SKIP", fields, 0 ) ;

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

  d4opt_start( &cb ) ;

  /* First Execute Skips on an Empty Database */
  if ( d4bottom( data ) != r4eof )
    t4severe( t4err_go, "05" ) ;
  t4test_eof( data, 1, 1 ) ;    rec( data, 1L ) ;

  if ( d4top( data ) != r4eof )
    t4severe( t4err_go, "06" ) ;
  t4test_eof( data, 1, 1 ) ;    rec( data, 1L ) ;

  if ( d4skip( data, -1L ) != r4bof )
    t4severe( t4err_skip, "07" ) ;
  t4test_eof( data, 1, 1 ) ;    rec( data, 1L ) ;

  if ( d4skip( data, 1L )  != r4eof )
    t4severe( t4err_skip, "08" ) ;
  t4test_eof( data, 1, 1 ) ;    rec( data, 1L ) ;

  if (do_open)
  {
    test_index = i4open( data, "T4SKIP" ) ;
    if( d4reindex( data ) != 0 )
      t4severe( t4err_reindex, "09" ) ;
  }
  else
    test_index = i4create( data, "T4SKIP", tags ) ;

  if ( test_index == 0 )
    t4severe( t4err_data, "10" ) ;

  t4test_eof( data, 1, 1 ) ;
  rec( data, 1L ) ;

  /* Index File Skips on an Empty Database */
  if ( d4bottom( data ) != r4eof )
    t4severe( t4err_go, "11" ) ;
  t4test_eof( data, 1, 1 ) ;    rec( data, 1L ) ;

  if ( d4top( data ) != r4eof )
    t4severe( t4err_go, "12" ) ;
  t4test_eof( data, 1, 1 ) ;    rec( data, 1L ) ;

  if ( d4skip( data, -1L ) != r4bof )
    t4severe( t4err_skip, "13" ) ;
  t4test_eof( data, 1, 1 ) ;
  rec( data, 1L ) ;

  if ( d4skip( data, 1L ) != r4eof )
    t4severe( t4err_skip, "14" ) ;
  t4test_eof( data, 1, 1 ) ;
  rec( data, 1L ) ;

  /* Index File Skips on a One Record Database */
  if ( d4append_blank( data ) < 0 )
    t4severe( t4err_append, "15" ) ;

  t4test_eof( data, 0, 0 ) ;
  rec( data, 1L ) ;

  if ( d4skip( data,  1L ) != r4eof )
    t4severe( t4err_skip, "16" ) ;
  t4test_eof( data, 0, 1 ) ;
  rec( data, 2L ) ;

  if ( d4skip( data, -1L ) != 0 )
    t4severe( t4err_skip, "17" ) ;
  t4test_eof( data, 0, 0 ) ;
  rec( data, 1L ) ;

  if ( d4skip( data, -1L ) != r4bof )
    t4severe( t4err_skip, "18" ) ;
  t4test_eof( data, 1,0 ) ;
  rec( data, 1L ) ;

  /* Database Skips on a One Record Database */
  tag = d4tag( data, "TAG" ) ;
  if ( tag == 0 )
    t4severe( t4err_tag, "19" ) ;
  d4tag_select( data, tag ) ;

  if ( d4skip( data,  1L ) != r4eof )
    t4severe( t4err_skip, "20" ) ;
  t4test_eof( data, 0, 1 ) ;
  rec( data, 2L ) ;

  if ( d4skip( data, -1L ) != 0 )
    t4severe( t4err_skip, "21" ) ;
  t4test_eof( data, 0, 0 ) ;
  rec( data, 1L ) ;

  if ( d4skip( data, -1L ) != r4bof )
    t4severe( t4err_skip, "22" ) ;
  t4test_eof( data, 1,0 ) ;
  rec( data, 1L ) ;

  if ( d4zap( data, 1L, d4reccount( data ) ) != 0 )
    t4severe( t4err_zap, "23" ) ;
  t4test_eof( data, 1, 1 ) ;

  d4display_str( display,  "  Writing 100 Records . . .  ", 1 ) ;

  /* Write  100  Records */
  for ( i_rec = 1; i_rec <= 100; i_rec++ )
  {
    if ( d4display_quit( display ) )  return 1 ;

    if ( d4append_start( data, 0 ) < 0 )
      t4severe( t4err_append, "24" ) ;
    f4assign_long( d4field_j( data, 2 ), (i_rec+1)/3 ) ;
    c4ltoa45( (long) (101 - i_rec), buff, 8 ) ;
    f4assign_n( d4field( data, "CHR" ), buff, 8 ) ;
    if ( d4append( data ) < 0 )
      t4severe( t4err_append, "25" ) ;

    t4test_eof( data, 0, 0 ) ;
    rec( data, i_rec ) ;
  }

  if ( d4top( data ) != 0 )
    t4severe( t4err_go, "26" ) ;
  if ( d4recno( data ) != 100 )
    t4severe( t4err_recno, "27" ) ;

  if ( d4skip( data, 60L ) != 0 )
    t4severe( t4err_skip, "28" ) ;
  if ( d4recno( data ) != 40 )
    t4severe( t4err_recno, "29" ) ;

  if ( d4skip( data, 22L ) != 0 )
    t4severe( t4err_skip, "30" ) ;
  if ( d4recno( data ) != 18 )
    t4severe( t4err_recno, "31" ) ;

  if ( d4skip( data, -74L ) != 0 )
    t4severe( t4err_skip, "32" ) ;
  if ( d4recno( data ) != 92 )
    t4severe( t4err_recno, "33" ) ;

  if ( d4skip( data, -20L ) != r4bof )
    t4severe( t4err_skip, "34" ) ;
  if ( !d4bof( data ) )
    t4severe( t4err_general, "35" ) ;

  /* Skip Test Without Index File */

  d4tag_select( data, 0 ) ;

  if ( d4bottom( data ) != 0 )
    t4severe( t4err_go, "36" ) ;

  if ( d4skip( data, -99L ) != 0 )
    t4severe( t4err_skip, "37" ) ;
  t4test_eof( data, 0, 0 ) ;
  rec( data, 1L ) ;

  if ( d4skip( data, -1L ) != r4bof )
    t4severe( t4err_skip, "38" ) ;
  t4test_eof( data, 1,0 ) ;
  rec( data, 1L ) ;

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

    if ( d4skip( data, 1L ) != 0 )
      t4severe( t4err_skip, "39" ) ;
    t4test_eof( data, 0, 0 ) ;
    rec( data, i_rec ) ;
  }

  if ( d4skip( data, 1L ) != r4eof )
    t4severe( t4err_skip, "40" ) ;
  t4test_eof( data, 0, 1 ) ;
  rec( data, 101L ) ;

  for ( i_rec = 100; i_rec >=1; i_rec-- )
  {
    if ( d4display_quit( display ) )  return 1 ;

    if ( d4skip( data, -1L ) != 0 )
      t4severe( t4err_skip, "41" ) ;
    t4test_eof( data, 0, 0 ) ;
    rec( data, i_rec ) ;
  }

  /* Skip Test With an Index File */

  d4tag_select( data, d4tag( data, "TAG_TWO" ) ) ;

  if ( d4bottom( data ) != 0 )
    t4severe( t4err_go, "42" ) ;
  t4test_eof( data, 0, 0 ) ;
  rec( data, 100L ) ;

  if ( d4skip( data, -99L ) != 0 )
    t4severe( t4err_skip, "43" ) ;
  t4test_eof( data, 0, 0 ) ;
  rec( data, 1L ) ;

  if ( d4skip( data, -1L ) != r4bof )
    t4severe( t4err_skip, "44" ) ;
  t4test_eof( data, 1,0 ) ;
  rec( data, 1L ) ;

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

    if ( d4skip( data, 1L ) != 0 )
      t4severe( t4err_skip, "45" ) ;
    t4test_eof( data, 0, 0 ) ;
    rec( data, i_rec ) ;
  }

  if ( d4skip( data, 1L ) != r4eof )
    t4severe( t4err_skip, "46" ) ;
  t4test_eof( data, 0, 1 ) ;
  rec( data, 101L ) ;

  for ( i_rec = 100; i_rec >=1; i_rec-- )
  {
    if ( d4display_quit( display ) )
      return 1 ;

    if ( d4skip( data, -1L ) != 0 )
      t4severe( t4err_skip, "47" ) ;
    t4test_eof( data, 0, 0 ) ;
    rec( data, i_rec ) ;
  }

  if ( d4close_all( &cb ) < 0 )
    t4severe( t4err_close, "48" ) ;

  return 0 ;
}

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

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

  if ( test_skip( display, 0 ) )
    return 1 ;
  if ( test_skip( display, 1 ) )
    return 1 ;

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

  d4init_undo(&cb) ;
  mem4reset() ;

#ifdef S4DEBUG
  mem4check_memory() ;

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

  return 0 ;
}

#endif

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

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

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