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

/* Tests CodeBase d4seek() function. */

#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 FIELD4INFO fields2[] =
{
{ "FIELD1", 'C', 5, 0 },
{ "FIELD2", 'C', 4, 0 },
{ 0, 0,0,0 },
} ;

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

static TAG4INFO tags2[] =
{
#ifndef N4OTHER
{ "TAG_NAME", "FIELD1+FIELD2", ".NOT. DELETED()", 0, 0 },
#else
{ "TAG_NAME", "FIELD1+FIELD2", "", 0, 0 },
#endif
{ "TAG2", "FIELD1", "", 0, 0 },
{ 0,0,0, 0, 0 },
} ;

CODE4 cb;
DATA4 *data;
INDEX4 *test_index ;
FIELD4 *field, *field1, *field2 ;


static long power( long val, long p )
{
  long r ;

  r = 1L ;

  while ( --p >= 0 )
    r *= val ;

  return r ;
}

static void do_seek( DATA4 *data )
{
  int i ;
  i = d4seek( data, "  " ) ;
  if ( (d4recno(data) != 3) ||  (i != 0) )
    t4severe( t4err_seek, "01" ) ;

  i = d4seek( data, "  1  222 " ) ;
  if ( (d4recno(data) != 2) ||  (i != 0) )
    t4severe( t4err_seek, "02" ) ;

  i = d4seek( data, "  1  222 " ) ;
  if ( (d4recno(data) != 2) ||  (i != 0) )
    t4severe( t4err_seek, "03" ) ;

  i = d4seek( data, "123456789" ) ;
  if ( (d4recno(data) != 7) ||  (i != 0) )
    t4severe( t4err_seek, "04" ) ;

  /* do some 'not found' seeks */
  i = d4seek( data, " 1      2" ) ;
  if ( (d4recno(data) != 8) ||  (i != r4after) )
    t4severe( t4err_seek, "05" ) ;

  i = d4seek( data, "     111 " ) ;
  if ( (d4recno(data) != 1) ||  (i != r4after) )
    t4severe( t4err_seek, "06" ) ;

  i = d4seek( data, "123456799" ) ;
  if ( (d4recno(data) != 13) ||  (i != r4eof) )
    t4severe( t4err_seek, "07" ) ;
}

static int  test_seek( D4DISPLAY *display, long left )
{
  long num_digits, num_recs, on_rec, i ;
  long limit, extra ;
  int  rc ; 
  unsigned on_digits ;
  char buff[200] ;

  cb.safety = 0 ;

  data = d4create( &cb, "T4SEEK", fields, 0 ) ;
  if ( data == 0 )
    t4severe( t4err_data, "08" ) ;

  d4opt_start( &cb ) ;

  for ( num_digits = 0; left> 0; num_digits++ )
    left /= 10 ;

  if ( num_digits <= 0 )  num_digits = 1 ;

  for ( num_recs = 10, i=1; i< num_digits; i++ )
    num_recs  *= 10 ;

  num_recs-- ;

  if ( num_digits > fields[0].len )
    t4severe( t4err_field, "09" ) ;

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

  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, "11" ) ;
    c4ltoa45( (long) on_rec, buff, (int) -num_digits ) ;
    f4assign_n( field, buff, (int) num_digits ) ;
    if ( d4append( data ) < 0 )
      t4severe( t4err_append, "12" ) ;
  }

  d4display_str( display,  "        Indexing . . .    ", 1 ) ;
  test_index = i4create( data, "T4SEEK", tags ) ;

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

  for ( on_digits = 1; on_digits <= (unsigned)num_digits; on_digits++ )
  {
    limit  = power( 10L, on_digits ) ;
    extra  = power( 10L, num_digits-on_digits ) ;
    d4display_str( display,  " Seeking Value:   ", 1 ) ;
    display->x = 0;

    for ( i=0; i< limit; i++ )
    {
      if ( d4display_quit( display ) )  return 1 ;

#ifdef _MSC_VER
#pragma warning( disable : 4146 )
#endif

      c4ltoa45( (long) i, buff,  (int)-on_digits ) ;

#ifdef _MSC_VER
#pragma warning( default : 4146 )
#endif

      buff[on_digits] = '\0' ;

      d4display_str( display,  " Seeking Value:   ", 0 ) ;
      d4display_num( display, i, 0 ) ;

      rc = d4seek( data, buff ) ;
      d4display_str( display,  "      Record:   ", 0 ) ;
      d4display_num( display, d4recno( data ), 0 ) ;
      display->x = 0;

      if ( rc != 0 )
      {
        if ( rc != r4after || d4recno( data ) != 1 )
          t4severe( t4err_recno, "14" ) ;
      }
      if ( d4recno( data ) != i*extra && d4recno( data ) != 1 )
        t4severe( t4err_recno, "15" ) ;
    }
  }

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

  cb.safety = 0 ;

  d4display_str( display,  "T4SEEK Test: Part TWO  \n", 1 ) ;

  data = d4create( &cb, "T4SEEK2", fields2, tags2 ) ;
  if ( data == 0 )
    t4severe( t4err_data, "17" ) ;

  field1 = d4field( data, "FIELD1" ) ;     /* assign pointers to fields */
  field2 = d4field( data, "FIELD2" ) ;     /* assign pointers to fields */
  if ( field1 == 0 || field2 == 0 )
    t4severe( t4err_field, "18" ) ;

  if ( d4append_start( data, 0 ) != 0 )
    t4severe( t4err_append, "19" ) ;
  f4assign( field1, "   " ) ;
  f4assign( field2, "1111" ) ;
  if ( d4append( data ) != 0 )
    t4severe( t4err_append, "20" ) ;

  if ( d4append_start( data, 0 ) != 0 )
    t4severe( t4err_append, "21" ) ;
  f4assign( field1, "  1" ) ;
  f4assign( field2, "222" ) ;
  if ( d4append( data ) != 0 )
    t4severe( t4err_append, "22" ) ;

  if ( d4append_start( data, 0 ) != 0 )
    t4severe( t4err_append, "23" ) ;
  f4assign( field1, "   " ) ;
  f4assign( field2, "   " ) ;
  if ( d4append( data ) != 0 )
    t4severe( t4err_append, "24" ) ;

  if ( d4append_start( data, 0 ) != 0 )
    t4severe( t4err_append, "25" ) ;
  f4assign( field1, " 1 " ) ;
  f4assign( field2, "223" ) ;
  if ( d4append( data ) != 0 )
    t4severe( t4err_append, "26" ) ;

  if ( d4append_start( data, 0 ) != 0 )
    t4severe( t4err_append, "27" ) ;
  f4assign( field1, " 1 " ) ;
  f4assign( field2, "    " ) ;
  if ( d4append( data ) != 0 )
    t4severe( t4err_append, "28" ) ;

  if ( d4append_start( data, 0 ) != 0 )
    t4severe( t4err_append, "29" ) ;
  f4assign( field1, " 1 " ) ;
  f4assign( field2, "224" ) ;
  if ( d4append( data ) != 0 )
    t4severe( t4err_append, "30" ) ;

  if ( d4append_start( data, 0 ) != 0 )
    t4severe( t4err_append, "31" ) ;
  f4assign( field1, "12345" ) ;
  f4assign( field2, "6789" ) ;
  if ( d4append( data ) != 0 )
    t4severe( t4err_append, "32" ) ;

  if ( d4append_start( data, 0 ) != 0 )
    t4severe( t4err_append, "33" ) ;
  f4assign( field1, " 1 " ) ;
  f4assign( field2, "   3" ) ;
  if ( d4append( data ) != 0 )
    t4severe( t4err_append, "34" ) ;

  if ( d4append_start( data, 0 ) != 0 )
    t4severe( t4err_append, "35" ) ;
  f4assign( field1, "   " ) ;
  f4assign( field2, " 111" ) ;
  if ( d4append( data ) != 0 )
    t4severe( t4err_append, "36" ) ;

  if ( d4append_start( data, 0 ) != 0 )
    t4severe( t4err_append, "37" ) ;
  f4assign( field1, " 3 " ) ;
  f4assign( field2, "333" ) ;
  if ( d4append( data ) != 0 )
    t4severe( t4err_append, "38" ) ;

  if ( d4append_start( data, 0 ) != 0 )
    t4severe( t4err_append, "39" ) ;
  f4assign( field1, "    1" ) ;
  f4assign( field2, "7777" ) ;
  if ( d4append( data ) != 0 )
    t4severe( t4err_append, "40" ) ;

  if ( d4append_start( data, 0 ) != 0 )
    t4severe( t4err_append, "41" ) ;
  f4assign( field1, " 1 " ) ;
  f4assign( field2, "333" ) ;
  if ( d4append( data ) != 0 )
    t4severe( t4err_append, "42" ) ;

  d4tag_select( data, d4tag_default( data ) ) ;

  /* do some basic checks on database to ensure tag order is correct */
  if ( d4top( data ) != 0 )
    t4severe( t4err_go, "43" ) ;
  if ( memcmp( f4ptr(field2), "    ", 4 ) )
    t4severe( t4err_field, "44" ) ;

  if ( d4skip( data, 4 ) != 0 )
    t4severe( t4err_skip, "45" ) ;
  if ( memcmp( f4ptr(field2), "222 ", 4 ) )
    t4severe( t4err_field, "46" ) ;

  if ( d4skip( data, 5 ) != 0 )
    t4severe( t4err_skip, "47" ) ;
  if ( memcmp( f4ptr(field2), "333 ", 4 ) )
    t4severe( t4err_field, "48" ) ;

  if ( d4skip( data, -4 ) != 0 )
    t4severe( t4err_skip, "49" ) ;
  if ( memcmp( f4ptr(field2), "    ", 4 ) )
    t4severe( t4err_field, "50" ) ;

  d4delete( data ) ;

  if ( d4skip( data, -2 ) != 0 )
    t4severe( t4err_skip, "51" ) ;
  if ( memcmp( f4ptr(field2), "7777", 4 ) )
    t4severe( t4err_field, "52" ) ;

  if ( d4skip( data, 2 ) != 0 )
    t4severe( t4err_skip, "53" ) ;
#ifndef N4OTHER
  if ( memcmp( f4ptr(field2), "   3", 4 ) )
#else
    if ( memcmp( f4ptr(field2), "    ", 4 ) )
#endif
      t4severe( t4err_field, "54" ) ;

  if ( d4go( data, 7 ) != 0 )
    t4severe( t4err_go, "55" ) ;
  if ( memcmp( f4ptr(field2), "6789", 4 ) )
    t4severe( t4err_field, "56" ) ;

  if ( d4skip( data, 1 ) != r4eof )
    t4severe( t4err_skip, "57" ) ;
  if ( (d4recno(data) != d4reccount(data)+1) ||  (!d4eof(data)) )
    t4severe( t4err_recno, "58" ) ;

  if ( d4top( data ) != 0 )
    t4severe( t4err_go, "59" ) ;
  if (d4recno(data) != 3)
    t4severe( t4err_recno, "60" ) ;

  if ( d4skip( data, -1 ) != r4bof )
    t4severe( t4err_skip, "61" ) ;

  /* test seeking */
  do_seek( data ) ;

  if ( d4go( data, 4 ) != 0 )
    t4severe( t4err_go, "62" ) ;
  d4delete( data ) ;
  if ( d4go( data, 9 ) != 0 )
    t4severe( t4err_go, "63" ) ;
  d4delete( data ) ;

#ifndef N4OTHER
  do_seek( data ) ;
  
  i = d4seek( data, " 1   223 " ) ;
  if ( (d4recno(data) != 6) ||  (i != r4after) )
    t4severe( t4err_recno, "64" ) ;
  
  i = d4seek( data, "      111" ) ;
  if ( (d4recno(data) != 1) ||  (i != r4after) )
    t4severe( t4err_recno, "65" ) ;
  
  /* delete all records so tag is empty */
  if ( d4bottom( data ) != 0 )
    t4severe( t4err_go, "66" ) ;
  for ( ;; )
  {
    if ( d4eof( data ) )
      break ;
    d4delete( data ) ;
    if ( d4skip( data, -1L ) < 0 )
      t4severe( t4err_skip, "67" ) ;
  }

  /* check empty tag */
  if ( d4go( data, 4 ) != 0 )
    t4severe( t4err_go, "68" ) ;
  if ( d4skip( data, -1 ) != r4eof )
    t4severe( t4err_skip, "69" ) ;
  if ( (d4recno(data) != d4reccount(data)+1) ||  (!d4eof(data)) || (!d4bof(data)) )
    t4severe( t4err_recno, "70" ) ;
  if ( d4go( data, 6 ) != 0 )
    t4severe( t4err_go, "71" ) ;
  if ( d4skip( data, 1 ) != r4eof )
    t4severe( t4err_skip, "72" ) ;
  if ( (d4recno(data) != d4reccount(data)+1) ||  (!d4eof(data)) || (!d4bof(data)) )
    t4severe( t4err_recno, "73" ) ;
  if ( d4seek( data, " 1   333 " ) != r4eof )
    t4severe( t4err_seek, "74" ) ;
  
  /* recall one record */
  if ( d4go( data, 6 ) != 0 )
    t4severe( t4err_go, "75" ) ;
  d4recall( data ) ;
  
  if ( d4go( data, 2 ) != 0 )
    t4severe( t4err_go, "76" ) ;
  if ( d4skip( data, -1 ) != r4bof )
    t4severe( t4err_skip, "77" ) ;
  if ( (d4recno(data) != 6)  ||  (!d4bof(data)) )
    t4severe( t4err_recno, "78" ) ;
  if ( d4go( data, 8 ) != 0 )
    t4severe( t4err_go, "79" ) ;
  if ( d4skip( data, 1 ) != 0 )
    t4severe( t4err_skip, "80" ) ;
  if ( (d4recno(data) != 6) ||  (d4eof(data)) || (d4bof(data)) )
    t4severe( t4err_recno, "81" ) ;
  if ( d4skip( data, 1 ) != r4eof )
    t4severe( t4err_skip, "82" ) ;
  if ( (d4recno(data) != d4reccount(data)+1) ||  (!d4eof(data)) || (d4bof(data)) )
    t4severe( t4err_recno, "83" ) ;
  
  if ( d4seek( data, " 1   224 " ) != 0 )
    t4severe( t4err_seek, "84" ) ;

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

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

  if ( d4append_start( data, 0 ) != 0 )
    t4severe( t4err_append, "86" ) ;
  f4assign( field1, "ZZZZZ" ) ;
  if ( d4append( data ) != 0 )
    t4severe( t4err_append, "87" ) ;

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

  if ( d4append_start( data, 0 ) != 0 )
    t4severe( t4err_append, "89" ) ;
  f4assign( field1, "YYYYY" ) ;
  if ( d4append( data ) != 0 )
    t4severe( t4err_append, "90" ) ;

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

  if ( d4append_start( data, 0 ) != 0 )
    t4severe( t4err_append, "92" ) ;
  f4assign( field1, "VVVVV" ) ;
  if ( d4append( data ) != 0 )
    t4severe( t4err_append, "93" ) ;

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

  if ( d4append_start( data, 0 ) != 0 )
    t4severe( t4err_append, "95" ) ;
  f4assign( field1, "XXXXX" ) ;
  if ( d4append( data ) != 0 )
    t4severe( t4err_append, "96" ) ;

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

  if ( d4bottom( data ) != 0 )
    t4severe( t4err_go, "98" ) ;
  if ( memcmp( f4str( field1 ), "ZZZZZ", 5 ) != 0 )
    t4severe( t4err_field, "99" ) ;
  f4assign( field1, "VVVVV" ) ;

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

  if ( d4bottom( data ) != 0 )
    t4severe( t4err_go, "101" ) ;
  if ( memcmp( f4str( field1 ), "YYYYY", 5 ) != 0 )
    t4severe( t4err_field, "102" ) ;
  f4assign( field1, "VVVVV" ) ;

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

  if ( d4bottom( data ) != 0 )
    t4severe( t4err_go, "104" ) ;
  if ( memcmp( f4str( field1 ), "XXXXX", 5 ) != 0 )
    t4severe( t4err_field, "105" ) ;
  f4assign( field1, "VVVVV" ) ;

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

  if ( d4append_start( data, 0 ) != 0 )
    t4severe( t4err_append, "107" ) ;
  f4assign( field1, "AAAAA" ) ;
  if ( d4append( data ) != 0 )
    t4severe( t4err_append, "108" ) ;

  if ( d4append_start( data, 0 ) != 0 )
    t4severe( t4err_append, "109" ) ;
  f4assign( field1, "AA  B" ) ;
  if ( d4append( data ) != 0 )
    t4severe( t4err_append, "110" ) ;

  if ( d4seek( data, "AA " ) != 0 )
    t4severe( t4err_seek, "111" ) ;
  if ( memcmp( f4str( field1 ), "AA  B", 5 ) != 0 )
    t4severe( t4err_field, "112" ) ;

  if ( d4check( data ) != 0 )
    t4severe( t4err_check, "113" ) ;
#endif

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


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

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

  if ( test_seek( display, left ) )
    return 1 ;

#ifndef S4TEST_KEEP_FILES
  u4remove( "T4SEEK.dbf" ) ;
  u4remove( "T4SEEK2.dbf" ) ;
#ifdef N4OTHER
  u4remove( "T4SEEK.CGP" ) ;
  u4remove( "T4SEEK2.CGP" ) ;
#endif
#ifdef S4MDX
  u4remove( "T4SEEK.mdx" ) ;
  u4remove( "T4SEEK2.mdx" ) ;
#endif
#ifdef S4FOX
  u4remove( "T4SEEK.cdx" ) ;
  u4remove( "T4SEEK2.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, "115" ) ;
#endif
#endif

  return 0 ;
}

#endif

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

#ifndef S4NO_PARMS
  left = atol( d4parsestring_nparm( &display->parse_str ) ) ;
  if ( left <= 0 )
    t4severe( t4err_parm, "116" ) ;
#endif

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

  if ( test_with_mem_check( display, left ) )
    t4severe( t4err_memory, "117" ) ;

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