PLplot 5.15.0
Loading...
Searching...
No Matches
bhunt_search_test.c
Go to the documentation of this file.
1//
2// Copyright (C) 2009 Alan W. Irwin
3//
4// This file is part of PLplot.
5//
6// PLplot is free software; you can redistribute it and/or modify
7// it under the terms of the GNU Library General Public License as published
8// by the Free Software Foundation; either version 2 of the License, or
9// (at your option) any later version.
10//
11// PLplot is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU Library General Public License for more details.
15//
16// You should have received a copy of the GNU Library General Public License
17// along with PLplot; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19//
20//
21#include <stdio.h>
22#include <stdlib.h>
23#include "qsastimeP.h"
24
25int gedouble( double *number1, double *number2 );
26
28int gedouble( double *number1, double *number2 )
29{
31 return ( *number1 >= *number2 );
32}
33
34int main()
35{
36 int i, j, iswitch, ifrandom, ifhunt, ntable, offset, multiplier, ntest, index;
37 double *table, test;
38
39 // size of ordered table of data and offset and multiplier to determine
40 // number and value of test values used to search the ordered table.
41 scanf( "%i %i %i", &ntable, &offset, &multiplier );
42 ntest = abs( multiplier ) * ( ntable - 1 ) + 1;
43 printf( "ntable, offset, multiplier, ntest = %i, %i, %i, %i\n", ntable, offset, multiplier, ntest );
44
45 table = (double *) malloc( ntable * sizeof ( double ) );
46 if ( table == NULL )
47 {
48 printf( "Could not malloc desired memory\n" );
49 return 1;
50 }
51
52
53 // Generate ordered table to be searched.
54 for ( i = 0; i < ntable; i++ )
55 {
56 table[i] = (double) i;
57 }
58
59 for ( iswitch = 0; iswitch < 4; iswitch++ )
60 {
61 ifrandom = ( iswitch & 0x1 ) == 0x1;
62 ifhunt = ( iswitch & 0x2 ) == 0x2;
63 // Generate series of test values (not necessarily ordered) to be used
64 // as keys for searching the table array).
65 index = -40;
67 for ( i = 0; i < ntest; i++ )
68 {
69 if ( ifrandom )
70 {
71 j = (int) ( (double) ntest * (double) rand() / ( ( (double) RAND_MAX ) + 1. ) );
72 }
73 else
74 {
75 j = i;
76 }
77
78 test = offset + (double) j / (double) multiplier;
79 if ( !ifhunt )
80 index = -40;
81 bhunt_search( &test, table, ntable, sizeof ( double ), &index, ( int ( * )( const void *, const void * ) )gedouble );
82 if ( index < -1 || index > ntable - 1 )
83 {
84 printf( "ERROR: test = %20.16f lead to an invalid index of %i\n", test, index );
85 return 1;
86 }
87
88 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 ) ) )
89 {
90 if ( index == -1 )
91 {
92 printf( "ERROR for index == -1, test = %20.16f, table[index+1] = %20.16f\n", test, table[index + 1] );
93 return 1;
94 }
95 else if ( index > -1 && index < ntable - 1 )
96 {
97 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] );
98 return 1;
99 }
100 else if ( index == ntable - 1 )
101 {
102 printf( "ERROR for index == ntable - 1, table[index] = %20.16f, test = %20.16f\n", table[index], test );
103 return 1;
104 }
105 else
106 {
107 printf( "Internal logic ERROR\n" );
108 return 1;
109 }
110 }
111 }
112 printf( "Average number of gedouble calls per bhunt_search call = %f for ifhunt, ifrandom = %i,%i\n", (double) count_gedouble / (double) ntest, ifhunt, ifrandom );
113 }
114 printf( "Successful completion of bhunt_search test\n" );
115
116 free( (void *) table );
117 return 0;
118}
int count_gedouble
int gedouble(double *number1, double *number2)
int main()
void bhunt_search(const void *key, const void *base, int n, size_t size, int *low, int(*ge)(const void *keyval, const void *datum))
Definition qsastime.c:1312