00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00040
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
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
00064
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 }