summaryrefslogtreecommitdiff
path: root/src/tests/xb_test_ndx.cpp
blob: b8b97f6bf5fdd413152f2ea9fc178e88c6083e02 (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* xb_test_ndx.cpp

XBase64 Software Library

Copyright (c) 1997,2003,2014,2022 Gary A Kunkel

The xb64 software library is covered under the terms of the GPL Version 3, 2007 license.

Email Contact:

    XDB-devel@lists.sourceforge.net
    XDB-users@lists.sourceforge.net

*/

// This program tests the class xbIxNdx

// usage:   xb_test_ndx QUITE|NORMAL|VERBOSE



// fix me - this program needs to test GetUnique

#include "xbase.h"

using namespace xb;

#include "tstfuncs.cpp"


int main( int argCnt, char **av )
{
  int iRc = 0;
  int iRc2 = 0;
  int iPo = 1;          /* print option */
                       /* 0 - QUIET    */
                       /* 1 - NORMAL   */
                       /* 2 - VERBOSE  */

  if( argCnt > 1 ) {
    if( av[1][0] == 'Q' )
      iPo = 0;
    else if( av[1][0] == 'V' )
      iPo = 2;
  }


  xbSchema MyV3Record[] = 
  {
    { "CFLD",       XB_CHAR_FLD,     30, 0 },
    { "DFLD",       XB_DATE_FLD,      8, 0 },
    { "NFLD",       XB_NUMERIC_FLD,  12, 0 },
    { "",0,0,0 }
  };

  xbXBase x;
  xbString sMsg;

  #ifdef XB_LOGGING_SUPPORT
  x.EnableMsgLogging();
  if( iPo ){
    std::cout << "Logfile is [" << x.GetLogFqFileName().Str() << "]" << std::endl;
  }
  sMsg.Sprintf( "Program [%s] initializing...", av[0] );
  x.WriteLogMessage( sMsg );
  x.SetLogSize( 1000000 );
  #endif

  x.SetDataDirectory( PROJECT_DATA_DIR );
  x.EnableMsgLogging();

  #ifdef XB_LOCKING_SUPPORT
  x.DisableDefaultAutoLock();
  #endif

  InitTime();

  char c;
  xbString s;
  xbInt32 lRecCnt = 0;
  iRc = 0;


  if( iPo > 0 )
    std::cout << "Default Data Directory is [" << x.GetDataDirectory().Str() << "]" << std::endl;

  xbDbf *V3Dbf = new xbDbf3( &x );

  xbIx *ixPtr;
  void *ndx;

  iRc2 = V3Dbf->CreateTable( "TestNdx.DBF", "TestNdx", MyV3Record, XB_OVERLAY, XB_MULTI_USER );
  iRc += TestMethod( iPo, "CreateTable()", (xbInt32) iRc2, XB_NO_ERROR );

  if( iRc2 )
    x.DisplayError( iRc2 );


  iRc2 = V3Dbf->CreateTag( "NDX", "TestNdxC.NDX", "CFLD", "", 0, 0, XB_OVERLAY, &ixPtr, &ndx );
  iRc += TestMethod( iPo, "CreateTag()", (xbInt32) iRc2, XB_NO_ERROR );
  if( iRc2 )
    x.DisplayError( iRc2 );

  iRc2 = V3Dbf->AssociateIndex( "NDX", "TestNdxC.NDX", 0 );
  iRc += TestMethod( iPo, "Associate()", (xbInt32) iRc2, XB_NO_ERROR );

  iRc2 = V3Dbf->CreateTag( "NDX", "TestNdxD.NDX", "DFLD", "", 0, 0, XB_OVERLAY, &ixPtr, &ndx );
  iRc += TestMethod( iPo, "CreateTag()", (xbInt32) iRc2, XB_NO_ERROR );
  if( iRc2 )
    x.DisplayError( iRc2 );

  iRc2 = V3Dbf->AssociateIndex( "NDX", "TestNdxD.NDX", 0 );
  iRc += TestMethod( iPo, "Associate()", (xbInt32) iRc2, XB_NO_ERROR );

  iRc2 = V3Dbf->CreateTag( "NDX", "TestNdxN.NDX", "NFLD", "", 0, 0, XB_OVERLAY, &ixPtr, &ndx );
  iRc += TestMethod( iPo, "CreateTag()", (xbInt32) iRc2, XB_NO_ERROR );
  if( iRc2 )
    x.DisplayError( iRc2 );

  iRc2 = V3Dbf->AssociateIndex( "NDX", "TestNdxN.NDX", 0 );
  iRc += TestMethod( iPo, "Associate()", (xbInt32) iRc2, XB_NO_ERROR );

  #ifdef XB_LOCKING_SUPPORT
  iRc += TestMethod( iPo, "LockTable()", V3Dbf->LockTable( XB_LOCK ), XB_NO_ERROR );
  #endif

  iRc += TestMethod( iPo, "GetCurTagName()", V3Dbf->GetCurTagName().Str(), "TestNdxC", 8 );
  iRc += TestMethod( iPo, "SetCurTag()", V3Dbf->SetCurTag( "TestNdxN" ), XB_NO_ERROR );
  iRc += TestMethod( iPo, "GetCurTagName()", V3Dbf->GetCurTagName().Str(), "TestNdxN", 8 );
  xbDouble dbl = 100;
  iRc += TestMethod( iPo, "Find( 100 )", V3Dbf->Find( dbl ), XB_NOT_FOUND );

  iRc += TestMethod( iPo, "SetCurTag()", V3Dbf->SetCurTag( "TestNdxD" ), XB_NO_ERROR );
  iRc += TestMethod( iPo, "GetCurTagName()", V3Dbf->GetCurTagName().Str(), "TestNdxD", 8 );
  xbDate dt;
  iRc += TestMethod( iPo, "Find( dt )", V3Dbf->Find( dt ), XB_NOT_FOUND );

  iRc += TestMethod( iPo, "GetFirstKey()",    ixPtr->GetFirstKey(), XB_EMPTY );
  iRc += TestMethod( iPo, "GetNextKey()",     ixPtr->GetFirstKey(), XB_EMPTY );
  iRc += TestMethod( iPo, "GetLasttKey()",    ixPtr->GetLastKey(),  XB_EMPTY );
  iRc += TestMethod( iPo, "GetPrevKey()",     ixPtr->GetPrevKey(),  XB_EMPTY );
  iRc += TestMethod( iPo, "FindKey()",        ixPtr->FindKey( NULL, "19611101", 8, 0 ), XB_NOT_FOUND );

  iRc += TestMethod( iPo, "BlankRecord()",    V3Dbf->BlankRecord(),                     XB_NO_ERROR );
  iRc += TestMethod( iPo, "Putfield()",       V3Dbf->PutField( "CFLD", "AAA" ),         XB_NO_ERROR );
  iRc += TestMethod( iPo, "Putfield()",       V3Dbf->PutField( "DFLD", "19611109" ),    XB_NO_ERROR );
  iRc += TestMethod( iPo, "PutfieldDouble()", V3Dbf->PutDoubleField( "NFLD", 50 ), XB_NO_ERROR );

  iRc += TestMethod( iPo, "AppendRecord()",   V3Dbf->AppendRecord(),                    XB_NO_ERROR );

  xbDate d( "19890209" );

  //for( xbUInt16 i = 0; i < 35 && iRc == XB_NO_ERROR; i++ ){
  for( xbUInt16 i = 0; i < 2 && iRc == XB_NO_ERROR; i++ ){
    for( xbUInt16 j = 0; j < 35 && iRc == XB_NO_ERROR; j++ ){ 
      c = j + 65;
      s = c;
      s.PadRight( c, (xbUInt32) i + 1 );
      V3Dbf->BlankRecord();
      V3Dbf->PutField( "CFLD", s );
      V3Dbf->PutLongField( "NFLD", ++lRecCnt );
      V3Dbf->PutField( "DFLD", d.Str() );
      d++;

      iRc = V3Dbf->AppendRecord();
    }
  }

  iRc += TestMethod( iPo, "SetCurTag()", V3Dbf->SetCurTag( "TestNdxN" ), XB_NO_ERROR );
  iRc += TestMethod( iPo, "GetCurTagName()", V3Dbf->GetCurTagName().Str(), "TestNdxN", 8 );

  dbl = 55.0;
  iRc += TestMethod( iPo, "Find( 55.0 )", V3Dbf->Find( dbl ), XB_NO_ERROR );

  iRc += TestMethod( iPo, "SetCurTag()", V3Dbf->SetCurTag( "TestNdxC" ), XB_NO_ERROR );
  iRc += TestMethod( iPo, "GetCurTagName()", V3Dbf->GetCurTagName().Str(), "TestNdxC", 8 );

  s = "AAA";
  iRc += TestMethod( iPo, "Find( 'AAA' )", V3Dbf->Find( s ), XB_NO_ERROR );

  s = "AzAA";
  iRc += TestMethod( iPo, "Find( 'AzAA' )", V3Dbf->Find( s ), XB_NOT_FOUND );

  iRc += TestMethod( iPo, "SetCurTag()", V3Dbf->SetCurTag( "TestNdxD" ), XB_NO_ERROR );
  iRc += TestMethod( iPo, "GetCurTagName()", V3Dbf->GetCurTagName().Str(), "TestNdxD", 8 );

  iRc += TestMethod( iPo, "Find( '19890420' )", V3Dbf->Find(  d ), XB_NOT_FOUND );

  V3Dbf->GetRecord( 26 );
  V3Dbf->PutField( "CFLD", "AAA" );
  V3Dbf->PutRecord( 26 );

  V3Dbf->GetRecord( 14 );
  V3Dbf->PutField( "CFLD", "AAAA" );
  V3Dbf->PutRecord( 14 );

  V3Dbf->GetRecord( 11 );
  V3Dbf->PutField( "CFLD", "III" );
  V3Dbf->PutRecord( 11 );

  V3Dbf->GetRecord( 25 );
  V3Dbf->PutField( "CFLD", "DDD" );
  V3Dbf->PutRecord( 25 );

  V3Dbf->GetRecord( 12 );
  V3Dbf->PutField( "CFLD", "EEE" );
  V3Dbf->PutRecord( 12 );

  V3Dbf->GetRecord( 26 );
  V3Dbf->PutField( "CFLD", "CCC" );
  V3Dbf->PutRecord( 26 );

  V3Dbf->GetRecord( 13 );
  V3Dbf->PutField( "CFLD", "CCCC" );
  V3Dbf->PutRecord( 13 );

  V3Dbf->GetRecord( 27 );
  V3Dbf->PutField( "CFLD", "AAA" );
  V3Dbf->PutRecord( 27 );

  V3Dbf->GetRecord( 28 );
  V3Dbf->PutField( "CFLD", "CCC" );
  V3Dbf->PutRecord( 28 );

  V3Dbf->GetRecord( 24 );
  V3Dbf->PutField( "CFLD", "FFF" );
  V3Dbf->PutRecord( 24 );

  V3Dbf->GetRecord( 10 );
  V3Dbf->PutField( "CFLD", "HHH" );
  V3Dbf->PutRecord( 10 );


  for( xbUInt16 i = 0; i < 35 && iRc == XB_NO_ERROR; i++ ){
    for( xbUInt16 j = 0; j < 14 && iRc == XB_NO_ERROR; j++ ){ 
      c = j + 65;
      s = c;
      s.PadRight( c, (xbUInt32) i + 1 );
      V3Dbf->BlankRecord();
      V3Dbf->PutField( "CFLD", s );
      V3Dbf->PutLongField( "NFLD", ++lRecCnt );
      V3Dbf->PutField( "DFLD", d.Str() );
      d++;
      iRc = V3Dbf->AppendRecord();
    }
  }

  for( xbUInt16 i = 0; i < 2 && iRc == XB_NO_ERROR; i++ ){
    for( xbUInt16 j = 0; j < 14 && iRc == XB_NO_ERROR; j++ ){ 
      c = j + 65;
      s = c;
      s.PadRight( c, (xbUInt32) i + 1 );

      V3Dbf->BlankRecord();
      V3Dbf->PutField( "CFLD", s );
      V3Dbf->PutLongField( "NFLD", ++lRecCnt );
      V3Dbf->PutField( "DFLD", d.Str() );
      d++;

      iRc = V3Dbf->AppendRecord();
    }
  }

  V3Dbf->GetRecord( 26 );
  V3Dbf->PutField( "CFLD", "AAA" );
  V3Dbf->PutRecord( 26 );

  V3Dbf->GetRecord( 14 );
  V3Dbf->PutField( "CFLD", "AAAA" );
  V3Dbf->PutRecord( 14 );

  V3Dbf->GetRecord( 11 );
  V3Dbf->PutField( "CFLD", "III" );
  V3Dbf->PutRecord( 11 );

  V3Dbf->GetRecord( 25 );
  V3Dbf->PutField( "CFLD", "DDD" );
  V3Dbf->PutRecord( 25 );

  V3Dbf->GetRecord( 12 );
  V3Dbf->PutField( "CFLD", "EEE" );
  V3Dbf->PutRecord( 12 );

  V3Dbf->GetRecord( 26 );
  V3Dbf->PutField( "CFLD", "CCC" );
  V3Dbf->PutRecord( 26 );

  V3Dbf->GetRecord( 13 );
  V3Dbf->PutField( "CFLD", "CCCC" );
  V3Dbf->PutRecord( 13 );

  V3Dbf->GetRecord( 27 );
  V3Dbf->PutField( "CFLD", "AAA" );
  V3Dbf->PutRecord( 27 );

  V3Dbf->GetRecord( 28 );
  V3Dbf->PutField( "CFLD", "CCC" );
  V3Dbf->PutRecord( 28 );

  V3Dbf->GetRecord( 24 );
  V3Dbf->PutField( "CFLD", "FFF" );
  V3Dbf->PutRecord( 24 );

  V3Dbf->GetRecord( 10 );
  V3Dbf->PutField( "CFLD", "HHH" );
  V3Dbf->PutRecord( 10 );

  // just in case there are any issues outstanding
  V3Dbf->Abort();

  xbIxList *ixl = V3Dbf->GetIxList();
  xbIxNdx *ix;
  xbString sTagName;
  while( ixl ){
    if( *ixl->sFmt == "NDX" ){
      ix = (xbIxNdx *) ixl->ix;
      //ix->GetTagName( 0, sTagName );
      sMsg.Sprintf( "CheckTagIntegrity() - [%s]", ix->GetTagName(ix->GetCurTag()).Str());
      iRc += TestMethod( iPo, sMsg, ix->CheckTagIntegrity( ix->GetCurTag(), 2 ), XB_NO_ERROR );
      ixl = ixl->next;
    }
  }

  #ifdef XB_LOCKING_SUPPORT
  iRc += TestMethod( iPo, "LockTable()", V3Dbf->LockTable( XB_UNLOCK ), XB_NO_ERROR );
  #endif

  xbString sDir;
  V3Dbf->GetFileDirPart( sDir );
  xbString sDbfName;
  xbString sInfName;
  sDbfName.Sprintf( "%sTestNdxR.DBF", sDir.Str());
  sInfName.Sprintf( "%sTestNdxR.INF", sDir.Str());

  V3Dbf->xbRemove( sDbfName );
  V3Dbf->xbRemove( sInfName );

  iRc += TestMethod( iPo, "Rename()",  V3Dbf->Rename( "TestNdxR.DBF" ), XB_NO_ERROR );

  iRc += TestMethod( iPo, "DeleteTable()", V3Dbf->DeleteTable(), XB_NO_ERROR );
  //  iRc += TestMethod( iPo, "Close()", V3Dbf->Close(), XB_NO_ERROR );  // did a delete instead of close

  delete V3Dbf;

  x.SetUniqueKeyOpt( XB_EMULATE_DBASE );
  V3Dbf = new xbDbf3( &x );

  iRc2 = V3Dbf->CreateTable( "TestNdx.DBF", "TestNdx", MyV3Record, XB_OVERLAY, XB_MULTI_USER );
  iRc += TestMethod( iPo, "CreateTable()", (xbInt32) iRc2, XB_NO_ERROR );

  if( iRc2 )
    x.DisplayError( iRc2 );

  iRc2 = V3Dbf->CreateTag( "NDX", "TestNdxC.NDX", "CFLD", "", 0, xbTrue, XB_OVERLAY, &ixPtr, &ndx );
  iRc += TestMethod( iPo, "CreateTag()", (xbInt32) iRc2, XB_NO_ERROR );
  if( iRc2 )
    x.DisplayError( iRc2 );

  iRc2 = V3Dbf->AssociateIndex( "NDX", "TestNdxC.NDX", 0 );
  iRc += TestMethod( iPo, "Associate()", (xbInt32) iRc2, XB_NO_ERROR );


  iRc += TestMethod( iPo, "BlankRecord()",    V3Dbf->BlankRecord(),                     XB_NO_ERROR );
  iRc += TestMethod( iPo, "Putfield()",       V3Dbf->PutField( "CFLD", "AAA" ),         XB_NO_ERROR );
  iRc += TestMethod( iPo, "Putfield()",       V3Dbf->PutField( "DFLD", "19611109" ),    XB_NO_ERROR );
  iRc += TestMethod( iPo, "PutfieldDouble()", V3Dbf->PutDoubleField( "NFLD", 50 ), XB_NO_ERROR );
  iRc += TestMethod( iPo, "AppendRecord()",   V3Dbf->AppendRecord(),                    XB_NO_ERROR );

  iRc += TestMethod( iPo, "BlankRecord()",    V3Dbf->BlankRecord(),                     XB_NO_ERROR );
  iRc += TestMethod( iPo, "Putfield()",       V3Dbf->PutField( "CFLD", "BBB" ),         XB_NO_ERROR );
  iRc += TestMethod( iPo, "Putfield()",       V3Dbf->PutField( "DFLD", "19611109" ),    XB_NO_ERROR );
  iRc += TestMethod( iPo, "PutfieldDouble()", V3Dbf->PutDoubleField( "NFLD", 50 ), XB_NO_ERROR );
  iRc += TestMethod( iPo, "AppendRecord()",   V3Dbf->AppendRecord(),                    XB_NO_ERROR );

  iRc += TestMethod( iPo, "BlankRecord()",    V3Dbf->BlankRecord(),                     XB_NO_ERROR );
  iRc += TestMethod( iPo, "Putfield()",       V3Dbf->PutField( "CFLD", "BBB" ),         XB_NO_ERROR );
  iRc += TestMethod( iPo, "Putfield()",       V3Dbf->PutField( "DFLD", "19611109" ),    XB_NO_ERROR );
  iRc += TestMethod( iPo, "PutfieldDouble()", V3Dbf->PutDoubleField( "NFLD", 50 ), XB_NO_ERROR );
  iRc += TestMethod( iPo, "AppendRecord()",   V3Dbf->AppendRecord(),                    XB_NO_ERROR );


  ixl = V3Dbf->GetIxList();
  while( ixl ){
    if( *ixl->sFmt == "NDX" ){
      ix = (xbIxNdx *) ixl->ix;
      //ix->GetTagName( 0, sTagName );
      sMsg.Sprintf( "CheckTagIntegrity() - [%s]", ix->GetTagName(ix->GetCurTag()).Str());
      iRc += TestMethod( iPo, sMsg, ix->CheckTagIntegrity( ix->GetCurTag(), 2 ), XB_NO_ERROR );
      ixl = ixl->next;
    }
  }

  iRc += TestMethod( iPo, "DeleteTable()", V3Dbf->DeleteTable(), XB_NO_ERROR );

  if( iPo > 0 || iRc < 0 )
    fprintf( stdout, "Total Errors = %d\n", iRc * -1 );

  #ifdef XB_LOGGING_SUPPORT
  sMsg.Sprintf( "Program [%s] terminating with [%d] errors...", av[0], iRc * -1 );
  x.WriteLogMessage( sMsg, 2 );
  #endif

  return iRc;
}