summaryrefslogtreecommitdiff
path: root/libtest/indextst.cpp
blob: c3a74befdccfe02ad42dc04d8bd99cb1f61b649c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*  indextst.cpp 

    Xbase project source code

    This program creates a sample database and multiple indices.
    It tests the index logic.

    Copyright (C) 1997,2003  Gary A Kunkel
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


    Contact:
    
     Email:
    
      xbase64-devel@lists.sourceforge.net
      xbase64-users@lists.sourceforge.net
      
      
     Regular Mail:
     
       XBase Support
       149C South Main St
       Keller Texas, 76248     
       USA
*/

#include "xbase64/xbase64.h"

int CheckIndex(
#ifdef XB_INDEX_NDX
               xbNdx *indx1, xbNdx *indx2, xbNdx *indx3
#endif
#if defined( XB_INDEX_NDX ) && defined ( XB_INDEX_NTX )
               ,
#endif
#ifdef XB_INDEX_NTX
               xbNtx *intx1, xbNtx *intx2, xbNtx *intx3
#endif
              );

int main()
{
  xbShort f1, f2, f3, rc, sts = 0;
  char charbuf[10];

  xbSchema MyRecord[] =
  {
    { "CHARFLD1",  XB_CHAR_FLD,      6, 0 },
    { "CHARFLD2",  XB_CHAR_FLD,      6, 0 },
    { "NUMFLD1",   XB_NUMERIC_FLD,   6, 0 },
    { "",0,0,0 }
  };

  /* define the classes */
  xbXBase x;			/* initialize xbase  */
  xbDbf MyFile( &x );		/* class for table   */

#ifdef XB_INDEX_NDX
  xbNdx indx1( &MyFile );	/* class for ndx index 1 */
  xbNdx indx2( &MyFile );	/* class for ndx index 2 */
  xbNdx indx3( &MyFile );	/* class for ndx index 3 */
#endif

#ifdef XB_INDEX_NTX
  xbNtx intx1( &MyFile );	/* class for ntx index 1 */
  xbNtx intx2( &MyFile );	/* class for ntx index 2 */
  xbNtx intx3( &MyFile );	/* class for ntx index 3 */
#endif

#ifndef XBASE_DEBUG
  std::cout << "XBASE_DEBUG support option not compiled into library";
  return 1;
#endif


  std::cout << "Creating test database and indices" << std::endl;
  if(( rc = MyFile.CreateDatabase( "IXTEST.DBF", MyRecord, XB_OVERLAY ))
        != XB_NO_ERROR )
     std::cout << "Error creating database = " << rc << "\n";
  else
  {
#ifdef XB_INDEX_NDX
     if(( rc = indx1.CreateIndex(
       "IXNDX1.NDX", "CHARFLD1", XB_NOT_UNIQUE, XB_OVERLAY )) != XB_NO_ERROR )
     {
        std::cout << "Error creating index 1 = " << rc << std::endl;
        exit( 1 );
     }

     if(( rc = indx2.CreateIndex(
       "IXNDX2.NDX", "CHARFLD1+CHARFLD2", XB_NOT_UNIQUE, XB_OVERLAY )) != XB_NO_ERROR )
     {
        std::cout << "Error creating index 2 = " << rc << std::endl;
        exit( 1 );
     }

     if(( rc = indx3.CreateIndex(
       "IXNDX3.NDX", "NUMFLD1", XB_NOT_UNIQUE, XB_OVERLAY )) != XB_NO_ERROR )
     {
        std::cout << "Error creating index 3 = " << rc << std::endl;
        exit( 1 );
     }
#endif


#ifdef XB_INDEX_NTX
     if(( rc = intx1.CreateIndex(
       "IXNTX1.NTX", "CHARFLD1", XB_NOT_UNIQUE, XB_OVERLAY )) != XB_NO_ERROR )
     {
        std::cout << "Error creating index 4 = " << rc << std::endl;
        exit( 1 );
     }

     if(( rc = intx2.CreateIndex( 
       "IXNTX2.NTX", "CHARFLD1+CHARFLD2", XB_NOT_UNIQUE, XB_OVERLAY )) != XB_NO_ERROR )
     {
        std::cout << "Error creating index 5 = " << rc << std::endl;
        exit( 1 );
     }

     if(( rc = intx3.CreateIndex( 
       "IXNTX3.NTX", "NUMFLD1", XB_NOT_UNIQUE, XB_OVERLAY )) != XB_NO_ERROR )
     {
        std::cout << "Error creating index 6 = " << rc << std::endl;
        exit( 1 );
     }
#endif
  }

  f1 = MyFile.GetFieldNo( "CHARFLD1" );
  f2 = MyFile.GetFieldNo( "CHARFLD2" );
  f3 = MyFile.GetFieldNo( "NUMFLD1" ); 

  std::cout << "Populating database and indices with data" << std::endl;
  std::cout << ".";
  std::cout.flush();
  for( int i = 0; i < 10000; i++ ){
    if( i % 100 == 0){
      std::cout << ".";
      std::cout.flush();
    }
    memset( charbuf, 0x00, 10 );
    sprintf( charbuf, "%d", i );
    MyFile.BlankRecord();
    MyFile.PutField( f1, charbuf );
    MyFile.PutField( f2, charbuf );
    MyFile.PutLongField( f3, i );
    MyFile.AppendRecord();


  }
  std::cout << " Done." << std::endl;

  // Check all indices.
  sts += CheckIndex(
#ifdef XB_INDEX_NDX
                    &indx1, &indx2, &indx3
#endif
#if defined( XB_INDEX_NDX ) && defined( XB_INDEX_NTX )
                    ,
#endif
#ifdef XB_INDEX_NTX
                    &intx1, &intx2, &intx3
#endif
                    );
  //
  // Appending a "Z" to the records will cause the index delete
  // functions to be used.
  //
  // - Bob Cotton <bob@synxis.com>
  //
  std::cout << "Reversing records." << std::endl;
  std::cout << ".";
  std::cout.flush();
    for( xbLong j = 1; j < MyFile.NoOfRecords(); j++ ){
      if (j  % 1000 == 0)
      {
          std::cout << ".";
          std::cout.flush();
      }
      MyFile.GetRecord(j);
      memset( charbuf, 0x00, 10 );
      sprintf( charbuf, "Z%d", (int)j-1 );
      MyFile.PutField( f1, charbuf );
      MyFile.PutField( f2, charbuf );
      MyFile.PutLongField( f3, j-1 );
      MyFile.PutRecord();
  }
  std::cout << " Done." << std::endl;
  // Check all indices
  sts += CheckIndex(
#ifdef XB_INDEX_NDX
                     &indx1, &indx2, &indx3
#endif
#if defined( XB_INDEX_NDX ) && defined( XB_INDEX_NTX )
                     ,
#endif
#ifdef XB_INDEX_NTX
                    &intx1, &intx2, &intx3
#endif
                    );
    

  std::cout << "Index testing completed" << std::endl;std::cout.flush();
  MyFile.CloseDatabase();   /* Close database and associated indexes */
  return sts;
}     


int CheckIndex(
#ifdef XB_INDEX_NDX
    xbNdx *indx1,
    xbNdx *indx2,
    xbNdx *indx3
#endif
#if defined( XB_INDEX_NDX ) && defined( XB_INDEX_NTX )
   ,
#endif
#ifdef XB_INDEX_NTX
    xbNtx *intx1,
    xbNtx *intx2,
    xbNtx *intx3
#endif
    )
{
    int rc;
    int sts = 0;
    
#if defined( XB_INDEX_NDX ) && defined( XBASE_DEBUG )
    std::cout << "Testing NDX index 1 "; std::cout.flush();
    if(( rc = indx1->CheckIndexIntegrity(0)) != XB_NO_ERROR ){
        std::cout << "Error " << rc << " with index indx1" << std::endl;
        sts++;
    }
    else
        std::cout << "OK" << std::endl;

    std::cout << "Testing NDX index 2 ";std::cout.flush();
    if(( rc = indx2->CheckIndexIntegrity(0)) != XB_NO_ERROR ){
        std::cout << "Error " << rc << " with index indx2" << std::endl;
        sts++;
    }
    else
        std::cout << "OK" << std::endl;


    std::cout << "Testing NDX index 3 ";std::cout.flush();
    if(( rc = indx3->CheckIndexIntegrity(0)) != XB_NO_ERROR ){
        std::cout << "Error " << rc << " with index indx3" << std::endl;
        sts++;
    }
    else
        std::cout << "OK" << std::endl;
#endif
#if defined( XB_INDEX_NTX ) && defined( XBASE_DEBUG )
    std::cout << "Testing NTX index 1 ";std::cout.flush();
    if(( rc = intx1->CheckIndexIntegrity(0)) != XB_NO_ERROR ){
        std::cout << "Error " << rc << " with index intx1" << std::endl;
        sts++;
    }
    else
        std::cout << "OK" << std::endl;

    std::cout << "Testing NTX index 2 ";std::cout.flush();
    if(( rc = intx2->CheckIndexIntegrity(0)) != XB_NO_ERROR ){
        std::cout << "Error " << rc << " with index intx2" << std::endl;
        sts++;
    }
    else
        std::cout << "OK" << std::endl;

    std::cout << "Testing NTX index 3 ";std::cout.flush();
    if(( rc = intx3->CheckIndexIntegrity(0)) != XB_NO_ERROR ){
        std::cout << "Error " << rc << " with index intx3" << std::endl;
        sts++;
    }
    else
        std::cout << "OK" << std::endl;
#endif
    return sts;

}