
/******************************************/
/*Title   : bisearch.h                    */
/*Descr   : BiSearch arithmetic Tester    */
/*Author  : Liu Wei                       */
/*Create  : 2006-12-13                    */
/*Version : R1V00_01                      */
/*Modify  : 2006-12-13                    */
/******************************************/
#include "../../iptrans/src/include/iptrans.h"
#include "./include/bisearch.h"

FILE *fp = NULL;
BiSearchArray bsearch_array; 


int GetIdleNode(int *nDUIndex)
{
	  if( bsearch_array.nArrayCount >= bsearch_array.nArrayTotalLen )
	      return 0; 
	  *nDUIndex = (rand()*19)%bsearch_array.nArrayTotalLen;
	  return 1;
}

void logarray ( BiSearchArray * pBiArray, char *op )
{
    BiSearch *pBS;
    LL  lastkey = 0;
    int nPos = -1;
    char buf[4096];
    char tmp_str[512];
    int i;
    buf[0] = '\0';

    sprintf ( buf, "Array Contents:[total :%d]\n" , pBiArray->nArrayCount);
    for ( i = 0; i < pBiArray->nArrayTotalLen; i++ )
    {
        pBS = pBiArray->pBiArray + i;
        sprintf ( tmp_str, "{%-3u,(%-3llu,%-3u)}  ", i, pBS->llKey, pBS->nDUPosIndex );
        strcat ( buf, tmp_str );
        if( i % 6 == 5 )
        {
            strcat ( buf, "\n" );
        }
        if( lastkey > pBS->llKey && i < pBiArray->nArrayCount )
            nPos = i;
        lastkey = pBS->llKey;
    }
    //if( nPos != -1 )
    {
        sprintf ( tmp_str, "\nERR Pos: %d\n\n", nPos );
        strcat ( buf, tmp_str );
        fprintf ( fp, buf );
    }
}

#define log()  logarray( &bsearch_array , op)

int main ( int argc, char *argv[] )
{

    char op[32];
    char buff[64];
    int i;
    srand((int)time(0));

    if( ( fp = fopen ( "./log.txt", "w" ) ) == NULL )
    {
        printf ( "Open File Fail\n" );
        return 0;
    }
    fprintf ( fp, "======================\n" );
    fprintf ( fp, "||  BiSearch Test   ||\n" );
    fprintf ( fp, "======================\n\n\n" );

    for ( i = 0; i < 32; i++ )
    {
        op[i] = 'A' + ( i * 3 + 11 ) % 26;
        buff[i] = op[i] + ( i * 7 + 19 ) % 26;
    }

    BISearchReg ( &bsearch_array, 64 ,GetIdleNode );

    for ( i = 0; i < 30; i++ )
    {
        int key = ( ( i * 11 ) % 5 ) * ( i % 5 ) - 3 * ( i % 11 ) + 39;
        int value = (3 * i + 1)%bsearch_array.nArrayTotalLen;

        fprintf ( fp, "Insert(%-3d, %-3d)\n", key, value );
        BISInsert ( &bsearch_array, key, value );
        log();
    }
    log();

    for ( i = 0; i < 128; i++ )
    {
        int k = ( ( i * 11 ) % 7 ) * ( i % 9 ) - 3 * ( i % 13 ) + i * ( rand (  ) % 13 * 19 ) + 61;

        k = k % 999;
        fprintf ( fp, "=====>>Insert(%d, %d)\n", k, 2 * i );
        BISInsert ( &bsearch_array, k, (2 * i+rand() % 13 * 19 + i*19)%bsearch_array.nArrayTotalLen);
        fprintf ( fp, "\n" );
        log();

        k = ( ( 20 - i ) % 3 ) * 9 - ( 3 * ( 20 - i ) ) + 9*128;
        k = k % 999;
        fprintf ( fp, "search index: %d" , k );
        if( -1 != GetDUIndex( &bsearch_array ,k )  )
        {
           fprintf ( fp, "<<=====Delete Key %d\n", k );
           BISDelete ( &bsearch_array , k );
        }
        else
        	 fprintf ( fp, "<<=====Delete Fail , not found [%d]", k );
        fprintf ( fp, "\n" );
        log();
        
    }
    
    for( i = 0 ; i < 128 ; i++ )
    {
    	  int nDUIndex = 0;
    	  int k = ( ( i * 11 ) % 7 ) * ( i % 9 ) - 3 * ( i % 13 ) + i * ( rand (  ) % 13 * 19 ) + 61;
    	  
    	  k = k % 999;
    	  BISSearchMng( &bsearch_array , BIS_INSERT , k , &nDUIndex );
    	  fprintf ( fp, "=====>>Insert(%d, %d)\n", k, nDUIndex );
    	  fprintf ( fp, "\n" );
        log();
    	  
    	  k = ( ( i * 11 ) % 7 ) * ( i % 9 ) - 3 * ( i % 13 ) + i * ( rand (  ) % 13 * 19 ) + 61;
    	  k = k % 999;
    	  fprintf ( fp, "search index: %d" , k );
        if( -1 != GetDUIndex( &bsearch_array ,k )  )
        {
           fprintf ( fp, "<<=====Delete Key %d\n", k );
           BISSearchMng( &bsearch_array , BIS_DELETE , k ,&nDUIndex);
        }
        else
        	 fprintf ( fp, "<<=====Delete Fail , not found [%d]", k );
    	 
    	  fprintf ( fp, "\n" );
        log();
    	  
    }    
    //fclose ( fp );
    BISearchUnReg ( &bsearch_array);
    return 0;
}
