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

   1.  Checks to make sure dBASE Function 'RECNO()' is working while indexing.
   */

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

#include  "t4test.h"

#ifndef S4INDEX_OFF

static FIELD4INFO test_fields[] =
{
{ "C" , 'C' , 100, 0 },
{ "CN", 'C' , 10, 0 },
{ "D", 'D', 8, 0 },
{ "M", 'M',10, 0 },
{ "N", 'N', 7, 2 },
{ 0,  0, 0, 0 },
} ;

static TAG4INFO tag_info[] =
{
  /* name, expression, filter, unique,descending */
{ "C_TAG",  "C", 0, 0, 0 },
{ "CN_TAG", "VAL(CN)", 0, 0, 0 },
{ "N_TAG",  "N", "", 0, 0 },
{ "D_TAG",  "D", 0, 0, 0 },
{ 0,0,0,0,0 },
} ;

#ifdef S4CLIPPER
static TAG4INFO tag_info2[] =
{
{ "C_TAG2", "C", 0, r4unique_continue, 0 },
{ 0,0,0,0,0 },
} ;
#endif

CODE4 cb ;
DATA4 *database ;
FIELD4 *c_field, *n_field, *m_field, *d_field, *cn_field ;

static int do_test( long num_recs, D4DISPLAY *disp )
{
  TAG4 *c_tag, *d_tag, *n_tag, *cn_tag ;
  long i_rec ;
  char buf[1000] ;
  int rc ;

#ifdef S4CLIPPER
  INDEX4 *test_index ;
#endif

  d4display_str( disp,  "Index Test (t4index2); ", 1 ) ;
  disp->y += 2 ;
  d4display_str( disp,  "        Number of Records: ", 1 ) ;
  d4display_num( disp, num_recs, 0 ) ;

  cb.mem_size_sort_buffer = 0x800 ;
  cb.safety = 0 ;

  database = d4create( &cb, "T4INDEX2", test_fields, tag_info ) ;
  if ( database == 0 )
    t4severe( t4err_data, "01" ) ;

  c_field = d4field( database, "C" ) ;
  d_field = d4field( database, "D" ) ;
  cn_field = d4field( database, "CN" ) ;
  n_field = d4field( database, "N" ) ;
  m_field = d4field( database, "M" ) ;

  if ( c_field == 0 || cn_field == 0 || d_field == 0 || n_field == 0 || m_field == 0 )
    t4severe( t4err_field, "02" ) ;

  d4opt_start( &cb ) ;

  if ( d4check( database ) != 0 )
    t4severe( t4err_check, "03" ) ;
  e4exit_test( &cb ) ;
  d4display_str( disp,  "Appending Record:   ", 1 ) ;

  for ( i_rec = 1L; i_rec <= num_recs; i_rec++ )
  {
    if ( d4display_quit( disp ) )
      return 1 ;
    if ( (i_rec % 25 == 0) || (i_rec == num_recs) )
    {
      disp->x = 0 ;
      d4display_str( disp,  "Appending Record:   ", 0 ) ;
      d4display_num( disp, i_rec, 0 ) ;
    }

    if ( d4append_start( database, 0 ) < 0 )
      return -1 ;

    if (i_rec % 3 == 0 )
      f4assign( c_field, "A" ) ;
    else
      f4assign( c_field, "B" ) ;

    f4assign_long( d_field, date4long( "19830208" ) + i_rec ) ;
    f4assign_long( n_field, i_rec ) ;
    f4assign_long( cn_field, i_rec ) ;
    if ( i_rec > 5 )
    {
      c4ltoa45( i_rec, buf, -6 ) ;
      f4memo_assign_n( m_field, buf, 6 ) ;
    }

    if ( d4append( database ) != 0 )
      t4severe( t4err_append, "04" ) ;
  }
  disp->y += 2 ;
  d4display_str( disp,  "          Checking Index File . . .", 1 ) ;

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

  c_tag = d4tag( database, "C_TAG" ) ;
  if ( c_tag == 0 )
    t4severe( t4err_tag, "06" ) ;

  n_tag = d4tag_prev( database, c_tag ) ;
  if ( n_tag )
    t4severe( t4err_tag, "07" ) ;

  c_tag = d4tag( database, "C_TAG" ) ;
  if ( c_tag == 0 )
    t4severe( t4err_tag, "08" ) ;

  d4tag_select( database, c_tag ) ;
  if ( num_recs >= 3 )
  {
    if( d4seek( database, "B" ) != 0 )
      t4severe( t4err_seek, "09" ) ;
  }

  cn_tag = d4tag_next( database, c_tag ) ;
  if ( cn_tag == 0 )
    t4severe( t4err_tag, "10" ) ;
  d4tag_select( database, cn_tag ) ;
  rc =  d4seek_double( database, 2.0 ) ;
  if (num_recs > 2 )
    if ( rc != 0 || d4recno(database) != 2 )
      t4severe( t4err_seek, "11" ) ;

  n_tag = d4tag_next( database, cn_tag ) ;
  if ( n_tag == 0 )
    t4severe( t4err_name, "12" ) ;
  d4tag_select( database, n_tag ) ;
  rc =  d4seek_double( database, 2.0 ) ;
  if (num_recs > 2 )
    if ( rc != 0 || d4recno(database) != 2 )
      t4severe( t4err_seek, "13" ) ;

  n_tag = d4tag_next( database, cn_tag ) ;
  if ( n_tag == 0 )
    t4severe( t4err_tag, "14" ) ;
  d4tag_select( database, n_tag ) ;
  rc =  d4seek_double( database, 2.0 ) ;
  if (num_recs > 2 )
    if ( rc != 0 || d4recno(database) != 2 )
      t4severe( t4err_seek, "15" ) ;

  d_tag = d4tag_next( database, n_tag ) ;
  if ( d_tag == 0 )
    t4severe( t4err_tag, "16" ) ;

  c_tag = d4tag_next( database, d_tag ) ;
  if ( c_tag )
    t4severe( t4err_tag, "17" ) ;

  d4tag_select( database, d_tag ) ;
  rc =  d4seek( database, "19830210") ;
  if ( num_recs > 1 )
    if ( rc != 0 || d4recno(database) != 2 )
      t4severe( t4err_seek, "18" ) ;

  disp->y += 2 ;
  d4display_str( disp,  "          Reindexing . . .", 1 ) ;
  
  if( d4reindex( database ) != 0 )
    t4severe( t4err_reindex, "19" ) ;

  if ( d4check( database ) != 0 )
    t4severe( t4err_reindex, "20" ) ;

  cn_tag = d4tag( database, "CN_TAG" ) ;
  if ( cn_tag == 0 )
    t4severe( t4err_tag, "21" ) ;
  d4tag_select( database, cn_tag ) ;
  rc =  d4seek_double( database, 2.0 ) ;
  if (num_recs > 2 )
    if ( rc != 0 || d4recno(database) != 2 )
      t4severe( t4err_seek, "22" ) ;

  n_tag = d4tag_next( database, cn_tag ) ;
  if ( n_tag == 0 )
    t4severe( t4err_tag, "23" ) ;
  d4tag_select( database, n_tag ) ;
  rc =  d4seek_double( database, 2.0 ) ;
  if (num_recs > 2 )
    if ( rc != 0 || d4recno(database) != 2 )
      t4severe( t4err_seek, "24" ) ;

  n_tag = d4tag_next( database, cn_tag ) ;
  if ( n_tag == 0 )
    t4severe( t4err_tag, "25" ) ;
  d4tag_select( database, n_tag ) ;
  rc =  d4seek_double( database, 2.0 ) ;
  if (num_recs > 2 )
    if ( rc != 0 || d4recno(database) != 2 )
      t4severe( t4err_seek, "26" ) ;

  d4tag_select( database, 0 ) ;  /* use record number ordering */
  if ( d4top( database ) < 0 )
    return -1 ;

  for( i_rec = 1L; i_rec < num_recs/2; i_rec++ )
  {
    f4assign( c_field, "Z") ;
    if ( d4skip( database, 1L ) < 0 )
      t4severe( t4err_skip, "27" ) ;
    if ( num_recs < 100 )
      if ( d4check( database )  != 0 )
        t4severe( t4err_check, "42" ) ;
  }

  disp->y += 2 ;

  d4display_str( disp,  "          Check of Index . . .", 1 ) ;
  if ( d4check( database )  != 0 )
    t4severe( t4err_check, "28" ) ;

  /* now reverse the process to make sure also works */
  for( i_rec = num_recs / 2; i_rec > 0L; i_rec-- )
  {
    if ( d4go( database, i_rec ) < 0 )
      t4severe( t4err_go, "29" ) ;
    f4assign( c_field, "A" ) ;
    if ( num_recs < 100 )
      if ( d4check( database )  != 0 )
        t4severe( t4err_check, "31" ) ;
  }

  disp->y += 2 ;

  d4display_str( disp,  "          Check of Index . . .", 1 ) ;
  if ( d4check( database )  != 0 )
    t4severe( t4err_check, "30" ) ;

#ifdef S4CLIPPER
  test_index = i4create( database, "T4INDEXX", tag_info2 ) ;
  if ( test_index == 0 )
    t4severe( t4err_data, "31" ) ;
#endif

  /* can't call d4check with this index since it has unique keys */
  for ( i_rec = 1 ; i_rec <= num_recs ; i_rec++ )
  {
    if ( d4go( database, i_rec ) < 0 )
      t4severe( t4err_go, "32" ) ;
    f4assign_long( c_field, i_rec ) ;
  }

  disp->y += 2 ;

  d4display_str( disp,  "          Check of Index . . .", 1 ) ;

#ifdef S4CLIPPER
  i4close( test_index ) ;
#endif

  if ( d4check( database )  != 0 )
    t4severe( t4err_check, "33" ) ;

#ifdef S4CLIPPER
  test_index = i4open( database, "T4INDEXX" ) ;
#endif

  for ( i_rec = 1 ; i_rec <= num_recs ; i_rec++ )
  {
    if ( d4go( database, i_rec ) < 0 )
      t4severe( t4err_go, "34" ) ;
    f4assign_long( c_field, 0 ) ;
    if ( num_recs < 100 )
      if ( d4check( database )  != 0 )
        t4severe( t4err_check, "40" ) ;
  }

#ifdef S4CLIPPER
  i4close( test_index ) ;
#endif

  disp->y += 2 ;

  d4display_str( disp,  "          Check of Index . . .", 1 ) ;
  if ( d4check( database )  != 0 )
    t4severe( t4err_check, "35" ) ;

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

  return 0 ;
}

static int  test_with_mem_check( long num_recs, D4DISPLAY *disp )
{
  d4init( &cb ) ;
  cb.hWnd = (unsigned) disp->hWnd ;

  if ( do_test( num_recs, disp ) )
    return 1 ;

#ifndef S4TEST_KEEP_FILES
  u4remove( "T4INDEX2.dbf" ) ;
#ifdef N4OTHER
  u4remove( "T4INDEX2.CGP" ) ;
#endif
#ifdef S4MDX
  u4remove( "T4INDEX2.mdx" ) ;
#endif
#ifdef S4FOX
  u4remove( "T4INDEX2.cdx" ) ;
  u4remove( "T4INDEX2.fpt" ) ;
#else
  u4remove( "T4INDEX2.dbt" ) ;
#endif
#ifdef S4CLIPPER
  u4remove( "C_TAG.NTX" ) ;
  u4remove( "CN_TAG.NTX" ) ;
  u4remove( "N_TAG.NTX" ) ;
  u4remove( "D_TAG.NTX" ) ;
  u4remove( "T4INDEXX.DBF" ) ;
  u4remove( "T4INDEXX.CGP" ) ;
  u4remove( "C_TAG2.NTX" ) ;
#endif
#ifdef S4NDX
  u4remove( "C_TAG.NDX" ) ;
  u4remove( "CN_TAG.NDX" ) ;
  u4remove( "N_TAG.NDX" ) ;
  u4remove( "D_TAG.NDX" ) ;
#endif
#endif

  d4init_undo(&cb) ;
  mem4reset() ;

#ifdef S4DEBUG
  mem4check_memory() ;

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

  return 0 ;
}

#endif

int S4FUNCTION t4test( D4DISPLAY *disp )
{
  long num_recs ;

#ifdef S4INDEX_OFF
  disp->y += 2 ;
  d4display_str( disp, "T4INDEX2:   REQUIRES INDEXING (S4INDEX_OFF DEFINED)", 1) ;
  d4display_str( disp, "", 1) ;
  return 1 ;
#else
#ifdef S4NO_PARMS
  num_recs = 500 ;
#else
  num_recs = atol( d4parsestring_nparm( &disp->parse_str ) ) ;
  
  if (num_recs == 0)
    t4severe( t4err_parm, "38" ) ;
#endif
  
#ifdef S4CLIPPER
  if ( test_with_mem_check( 9, disp ) )
    e4exit(&cb) ;
  
  if ( test_with_mem_check( 21, disp ) )
    e4exit(&cb) ;
  
  if ( test_with_mem_check( 45, disp ) )
    e4exit(&cb) ;

  if ( test_with_mem_check( 51, disp ) )
    e4exit(&cb) ;
  
  if ( test_with_mem_check( 54, disp ) )
    e4exit(&cb) ;
  
  if ( test_with_mem_check( 90, disp ) )
    e4exit(&cb) ;
  
  if ( test_with_mem_check( 400, disp ) )
    e4exit(&cb) ;
#endif
  
  if ( test_with_mem_check( num_recs, disp ) )
    e4exit(&cb) ;

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