• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

bhunt_search_test.c

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2009 Alan W. Irwin
00003 //
00004 // This file is part of PLplot.
00005 //
00006 // PLplot is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU Library General Public License as published
00008 // by the Free Software Foundation; either version 2 of the License, or
00009 // (at your option) any later version.
00010 //
00011 // PLplot is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU Library General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Library General Public License
00017 // along with PLplot; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019 //
00020 //
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include "qsastimeP.h"
00024 
00025 int gedouble( double *number1, double *number2 );
00026 
00027 int count_gedouble;
00028 int gedouble( double *number1, double *number2 )
00029 {
00030     count_gedouble++;
00031     return ( *number1 >= *number2 );
00032 }
00033 
00034 int main()
00035 {
00036     int    i, j, iswitch, ifrandom, ifhunt, ntable, offset, multiplier, ntest, index;
00037     double *table, test;
00038 
00039     // size of ordered table of data and offset and multiplier to determine
00040     // number and value of test values used to search the ordered table.
00041     scanf( "%i %i %i", &ntable, &offset, &multiplier );
00042     ntest = abs( multiplier ) * ( ntable - 1 ) + 1;
00043     printf( "ntable, offset, multiplier, ntest = %i, %i, %i, %i\n", ntable, offset, multiplier, ntest );
00044 
00045     table = (double *) malloc( ntable * sizeof ( double ) );
00046     if ( table == NULL )
00047     {
00048         printf( "Could not malloc desired memory\n" );
00049         return 1;
00050     }
00051 
00052 
00053     // Generate ordered table to be searched.
00054     for ( i = 0; i < ntable; i++ )
00055     {
00056         table[i] = (double) i;
00057     }
00058 
00059     for ( iswitch = 0; iswitch < 4; iswitch++ )
00060     {
00061         ifrandom = ( iswitch & 0x1 ) == 0x1;
00062         ifhunt   = ( iswitch & 0x2 ) == 0x2;
00063         // Generate series of test values (not necessarily ordered) to be used
00064         // as keys for searching the table array).
00065         index          = -40;
00066         count_gedouble = 0;
00067         for ( i = 0; i < ntest; i++ )
00068         {
00069             if ( ifrandom )
00070             {
00071                 j = (int) ( (double) ntest * (double) rand() / ( ( (double) RAND_MAX ) + 1. ) );
00072             }
00073             else
00074             {
00075                 j = i;
00076             }
00077 
00078             test = offset + (double) j / (double) multiplier;
00079             if ( !ifhunt )
00080                 index = -40;
00081             bhunt_search( &test, table, ntable, sizeof ( double ), &index, ( int ( * )( const void *, const void * ) )gedouble );
00082             if ( index < -1 || index > ntable - 1 )
00083             {
00084                 printf( "ERROR: test = %20.16f lead to an invalid index of %i\n", test, index );
00085                 return 1;
00086             }
00087 
00088             if ( !( ( index == -1 && test < table[index + 1] ) || ( index > -1 && index < ntable - 1 && table[index] <= test && test < table[index + 1] ) || ( index == ntable - 1 && table[index] <= test ) ) )
00089             {
00090                 if ( index == -1 )
00091                 {
00092                     printf( "ERROR for index == -1,  test = %20.16f, table[index+1] = %20.16f\n", test, table[index + 1] );
00093                     return 1;
00094                 }
00095                 else if ( index > -1 && index < ntable - 1 )
00096                 {
00097                     printf( "ERROR for index > -1 && index < ntable-1,  table[index] = %20.16f, test = %20.16f, table[index+1] = %20.16f\n", table[index], test, table[index + 1] );
00098                     return 1;
00099                 }
00100                 else if ( index == ntable - 1 )
00101                 {
00102                     printf( "ERROR for index == ntable - 1, table[index] = %20.16f, test = %20.16f\n", table[index], test );
00103                     return 1;
00104                 }
00105                 else
00106                 {
00107                     printf( "Internal logic ERROR\n" );
00108                     return 1;
00109                 }
00110             }
00111         }
00112         printf( "Average number of gedouble calls per bhunt_search call = %f for ifhunt, ifrandom = %i,%i\n", (double) count_gedouble / (double) ntest, ifhunt, ifrandom );
00113     }
00114     printf( "Successful completion of bhunt_search test\n" );
00115 
00116     free( (void *) table );
00117     return 0;
00118 }

Generated on Wed Oct 12 2011 20:42:21 for PLplot by  doxygen 1.7.1