/* t4index.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 S4WINDOWS
#ifndef S4OS2
#ifndef S4UNIX
#include <dos.h>
#endif
#endif
#endif

#ifndef S4INDEX_OFF

static FIELD4INFO test_fields[] =
{
{ "C", 'C', 100, 0 },
{ "C2", 'C', 8, 0 },
{ 0,  0, 0, 0 },
} ;

static TAG4INFO tag_info[] =
{
  /* name, expression, filter, unique,descending */
{ "C", "C", "", 0, 0 },
{ "C2", "C2", "", 0, 0 },
{ "R", "RECNO()", "", 0, 0 },
{ 0,  0,  0, 0, 0 },

};

CODE4 cb;
DATA4 *data;
INDEX4 *test_index, *index_check ;

static int do_test( long num_recs, D4DISPLAY *disp, int do_open )
{
  long i_rec ;

  cb.hWnd = (unsigned) disp->hWnd ;

  if ( d4display_quit( disp ) )
    return 1 ;
  d4display_str( disp,  "  Index Test: part one ", 1 ) ;
  disp->y += 2 ;
  d4display_str( disp,  "        Number of Records: ", 1 ) ;
  d4display_num( disp, num_recs, 0 ) ;
  d4display_str( disp, " ", 1 ) ;

  cb.safety = 0 ;

  if (do_open)
  {
    cb.auto_open = 0 ;
    if( do_open == 2 )
    {
#ifndef S4WIN32
#ifndef S4OS2
#ifdef __TURBOC__
      _chmod( "T4INDEX.DBF", 1, _A_RDONLY ) ;
#endif
#endif
#ifdef _MSC_VER
      _dos_setfileattr( "T4INDEX.DBF", _A_RDONLY ) ;
#endif
#endif
    }
    data = d4open( &cb, "T4INDEX" ) ;
    if( do_open != 2 )
      if ( d4zap( data, 1L, d4reccount( data ) ) != 0 )
        t4severe( t4err_zap, "01" ) ;
  }
  else
    data = d4create( &cb, "T4INDEX", test_fields, 0);

  if ( cb.error_code || data == 0 )
    t4severe( t4err_data, "02" ) ;

  d4opt_start( &cb ) ;

  if( do_open != 2 )
    for ( i_rec = 1L; i_rec <= num_recs; i_rec++ )
    {
      if ( (i_rec % 50 == 0) || (i_rec == num_recs) )
      {
        d4display_str( disp,  "        Writing Record:   ", 0 ) ;
        d4display_num( disp, i_rec, 0 ) ;
        d4display_str( disp,  "      ", 0 ) ;
        disp->x = (int) 0 ;
      }

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

  if ( d4display_quit( disp ) )
    return 1 ;
  disp->y += 2 ;
  d4display_str( disp,  "          Creating Index File . . .", 1 ) ;

  if( do_open == 2 )
    test_index =  i4open( data, "T4INDEX" ) ;
  else
    test_index =  i4create( data, "T4INDEX", tag_info ) ;
  if ( test_index == 0 )
    t4severe( t4err_data, "04" ) ;
  

  d4display_str( disp,  "          Checking Index File . . .", 1 ) ;
  if ( i4check( test_index )  != 0 )
    t4severe( t4err_check, "05" ) ;

  index_check = d4index( data, "T4INDEX" );
  if (index_check == 0)
    t4severe( t4err_check, "06" ) ;

  index_check = d4index( data, "S4INDEX" ); /* doesn't exist */
  if (index_check != 0)
    t4severe( t4err_check, "07" ) ;

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

  if( do_open == 2 )
  {
#ifndef S4WIN32
#ifdef __TURBOC__
    _chmod( "T4INDEX.DBF", 1, 0 ) ;
#endif
#ifdef _MSC_VER
    _dos_setfileattr( "T4INDEX.DBF", 0 ) ;
#endif
#endif
  }

  return 0 ;
}

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

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

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

  d4init_undo( &cb ) ;
  mem4reset() ;

#ifdef S4DEBUG
  mem4check_memory() ;

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

  return 0 ;
}

#endif

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

#ifdef S4INDEX_OFF
  disp->y += 2 ;
  d4display_str( disp, "T4INDEX:   REQUIRES INDEXING (S4INDEX_OFF DEFINED)", 1) ;
  d4display_str( disp, "", 1) ;
  return 1 ;
#else
#ifdef S4NO_PARMS
  num_recs = 1000 ;
#else
  num_recs = atol( d4parsestring_nparm( &disp->parse_str ) ) ;
  
  if (num_recs == 0)
    t4severe( t4err_parm, "11" ) ;
#endif

  if ( test_with_mem_check( num_recs, disp, 0 ) )
    t4severe( t4err_general, "12" ) ;
  if ( test_with_mem_check( num_recs, disp, 1 ) )
    t4severe( t4err_general, "13" ) ;

  /* now check read_attrib... */
#ifndef S4WINDOWS
  if ( test_with_mem_check( num_recs, disp, 2 ) )
    t4severe( t4err_general, "14" ) ;
#endif

#ifndef S4TEST_KEEP_FILES
  u4remove( "T4INDEX.dbf" ) ;
#ifdef N4OTHER
  u4remove( "T4INDEX.CGP" ) ;
#endif
#ifdef S4MDX
  u4remove( "T4INDEX.mdx" ) ;
#endif
#ifdef S4FOX
  u4remove( "T4INDEX.cdx" ) ;
#endif
#ifdef S4CLIPPER
  u4remove( "C.NTX" ) ;
  u4remove( "C2.NTX" ) ;
  u4remove( "R.NTX" ) ;
#endif
#ifdef S4NDX
  u4remove( "C.NDX" ) ;
  u4remove( "C2.NDX" ) ;
  u4remove( "R.NDX" ) ;
#endif
#endif

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

