home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / db02_src.zip / rtest.cc < prev    next >
C/C++ Source or Header  |  1993-11-05  |  4KB  |  175 lines

  1. /**************************************************************************
  2.  * Source Id :
  3.  *
  4.  * $Id: rtest.cc,v 1.7 1993/05/26 01:01:39 kevinl Exp $
  5.  *-------------------------------------------------------------------------
  6.  * Project Notes :
  7.  *
  8.  *  Diamond Base
  9.  *  ============
  10.  *      A solid database implementation, spurred on by the continuing
  11.  *  Metal (Lead) Base saga.
  12.  *
  13.  *  Project Team :
  14.  *        A. Davison
  15.  *        K. Lentin
  16.  *        D. Platt
  17.  *
  18.  *    Project Commenced : 05-02-1993
  19.  *
  20.  *-------------------------------------------------------------------------
  21.  *  Module Notes :
  22.  *
  23.  *  Testing rserv. Bulldozing it into the ground actually.
  24.  *
  25.  *  Original Author : Mad Daz.
  26.  *
  27.  *-------------------------------------------------------------------------
  28.  * Revision History:
  29.  *
  30.  * $Log
  31.  **************************************************************************/
  32.  
  33. #include <iostream.h>
  34. #ifdef __BORLANDC__
  35. #include <string.h>
  36. #else
  37. #include <unistd.h>
  38. #endif
  39. #include <stdlib.h>
  40. #include <stdio.h>
  41. #include "rserv.h"
  42. #include "rand.h"
  43. #ifndef MALLOC_H_MISSING
  44. #include <malloc.h>
  45. #endif
  46.  
  47. void fillBuffer(long seed,char *buffer,int length)
  48. {
  49.     // Fill a buffer with random characters
  50.     srand(seed);
  51.     for(int i=0;i<length;i++) {
  52.         buffer[i] = rand()&255;
  53.     }
  54. }
  55.  
  56. int checkBuffer(long seed,char *buffer,int length)
  57. {
  58.     // Check the contents of a buffer against a string of random characters.
  59.     srand(seed);
  60.     char t;
  61.     for(int i=0;i<length;i++) {
  62.         t = rand()&255;
  63.         if (buffer[i] != t) return 0;
  64.     }
  65.     return 1;
  66. }
  67.  
  68. int
  69. main(int argc,char **argv)
  70. {
  71.     if (argc==2) {
  72.         if (!strcmp("init",argv[1])) {
  73.             recServer    rs;
  74.             if (rs.createDb("testdb2",13,112)) {
  75.                 cerr << "Create failed" << endl;
  76.             }
  77.         } else if (!strcmp("bash",argv[1])) {
  78.             // Do some random record creation and integrity checking.
  79.             // we will have up to 5000 records, which can be active
  80.             // or inactive (seed == -1) - each record has a seed associated
  81.             // with it which can be used to check the integrity of the record.
  82.             randcl MyRand(21);
  83.             recServer rs("testdb2",112);
  84.  
  85.             long headerSeed = MyRand.ulong();
  86.             long seeds[5000];
  87.             long where[5000];
  88.             long numActive = 0;
  89.             long iters = 0;
  90.             char leader[112];
  91.  
  92.             fillBuffer(headerSeed,leader,112);
  93.             if (!rs.writeData(0,leader,112)) {
  94.                 cerr << "Failed to write leader" << endl;
  95.                 exit(1);
  96.             }
  97.  
  98.             do {
  99.                 // Pick a random operation:
  100.                 char buffer[13];
  101.  
  102.                 if (MyRand.ulong()%2 || !numActive) {
  103.                     long newRec;
  104.                     if (rs.newRec(newRec)) {
  105.                         cerr << "Error creating record when numActive==" <<
  106.                             numActive << " " << iters << endl;
  107.                         exit(1);
  108.                     }
  109.                     where[numActive] = newRec;
  110.                     seeds[numActive] = MyRand.ulong();
  111.                     fillBuffer(seeds[numActive],buffer,13);
  112.                     rs.putRec(where[numActive],buffer);
  113.                     numActive++;
  114.                 } else {
  115.                     // Check an existing record to establish it's integrity
  116.                     int which = MyRand.ulong()%numActive;
  117.                     if (rs.getRec(where[which],buffer)) {
  118.                         cerr << "Retrieval failed for record #" << where[which]
  119.                             << " " << iters << endl;
  120.                         exit(1);
  121.                     }
  122.                     if (!checkBuffer(seeds[which],buffer,13)) {
  123.                         cerr << "Integrity check failed for record #" << 
  124.                             where[which] << " " << iters << endl;
  125.                         exit(1);
  126.                     }
  127.                 }
  128.                 iters++;
  129.                 if (!iters%10) {
  130.                     // Check header integrity
  131.                     memset(leader,0,112);
  132.                     if (!rs.readData(0,leader,112)) {
  133.                         cerr << "Error reading leader" << endl;
  134.                         exit(1);
  135.                     }
  136.                     if (!checkBuffer(headerSeed,leader,112)) {
  137.                         cerr << "Leader failed integrity check" << endl;
  138.                         exit(1);
  139.                     }
  140.                 }
  141.             } while(numActive<5000);
  142.             cout << "Done!" << endl;
  143.         } else if (!strcmp("big_add",argv[1])) {
  144.             recServer rs("testdb2",112);
  145.             int i;
  146.             char buffer[123];
  147.             for(i=0;i<5000;i++) {
  148.                 long idx;
  149.                 if (rs.newRec(idx)) {
  150.                     cerr << "New failed on iteration " << i << endl;
  151.                     exit(1);
  152.                 }
  153.                 sprintf(buffer,"!%d! This is the abode of record number !%d!",i,i);
  154.                 if (rs.putRec(idx,buffer)) {
  155.                     cerr << "Put failed on iteration " << i << endl;
  156.                     exit(1);
  157.                 }
  158.                 cout << ".";
  159.             }
  160.  
  161.             memset(buffer,' ',122);
  162.             // Doing fetches now:
  163.             for(i=0;i<5000;i++) {
  164.                 if (rs.getRec(i,buffer)) {
  165.                     cerr << "get failed on iteration " << i << endl;
  166.                     exit(1);
  167.                 }
  168.                 cout << "<" ;
  169.             }
  170.         }
  171.     } else {
  172.         cout << "Usage " << argv[0] << " [init|bash|big_add]" << endl;
  173.     }
  174. }
  175.