/*************************************************************************************************
 * dptest
 *                                                       Copyright(c) 2000-2003 Mikio Hirabayashi
 *************************************************************************************************/


#include <depot.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#undef TRUE
#define TRUE           1                 /* boolean true */
#undef FALSE
#define FALSE          0                 /* boolean false */

#define RECBUFSIZ      32                /* buffer for records */
#define RANDDATAMIN    4                 /* minimum length of buffer */
#define RANDDATAMAX    12                /* maximum length of buffer */


/* global variables */
const char *progname;                    /* program name */


/* function prototypes */
int main(int argc, char **argv);
void usage(void);
int runwrite(int argc, char **argv);
int runread(int argc, char **argv);
int runcombo(int argc, char **argv);
int runrand(int argc, char **argv);
int runwicked(int argc, char **argv);
void pdperror(const char *name);
int setrandstr(char *buf, int radix);
int dowrite(const char *name, int rnum, int bnum, int repeat, int align);
int doread(const char *name);
int docombo(const char *name);
int dorand(const char *name, int rnum, int bnum, int repeat, int align, int poor);
int dowicked(const char *name, int rnum);


/* main routine */
int main(int argc, char **argv){
  int rv;
  progname = argv[0];
  if(argc < 2) usage();
  rv = 0;
  if(!strcmp(argv[1], "write")){
    rv = runwrite(argc, argv);
  } else if(!strcmp(argv[1], "read")){
    rv = runread(argc, argv);
  } else if(!strcmp(argv[1], "combo")){
    rv = runcombo(argc, argv);
  } else if(!strcmp(argv[1], "rand")){
    rv = runrand(argc, argv);
  } else if(!strcmp(argv[1], "wicked")){
    rv = runwicked(argc, argv);
  } else {
    usage();
  }
  return rv;
}


/* print the usage and exit */
void usage(void){
  fprintf(stderr, "%s: test cases for Depot\n", progname);
  fprintf(stderr, "\n");
  fprintf(stderr, "usage:\n");
  fprintf(stderr, "  %s write [-cat num] [-align num] name rnum bnum\n", progname);
  fprintf(stderr, "  %s read name\n", progname);
  fprintf(stderr, "  %s combo name\n", progname);
  fprintf(stderr, "  %s rand [-cat num] [-align num] [-poor] name rnum bnum\n", progname);
  fprintf(stderr, "  %s wicked name rnum\n", progname);
  exit(1);
}


/* parse arguments of write command */
int runwrite(int argc, char **argv){
  char *name, *rstr, *bstr;
  int i, rnum, bnum, repeat, align, rv;
  name = NULL;
  rstr = NULL;
  bstr = NULL;
  rnum = 0;
  bnum = 0;
  repeat = 1;
  align = 0;
  for(i = 2; i < argc; i++){
    if(!name && argv[i][0] == '-'){
      if(!strcmp(argv[i], "-cat")){
        if(++i >= argc) usage();
        repeat = atoi(argv[i]);
      } else if(!strcmp(argv[i], "-align")){
        if(++i >= argc) usage();
        align = atoi(argv[i]);
      } else {
        usage();
      }
    } else if(!name){
      name = argv[i];
    } else if(!rstr){
      rstr = argv[i];
    } else if(!bstr){
      bstr = argv[i];
    } else {
      usage();
    }
  }
  if(!name || !rstr || !bstr) usage();
  rnum = atoi(rstr);
  bnum = atoi(bstr);
  if(rnum < 1 || bnum < 1 || repeat < 1 || align < 0) usage();
  rv = dowrite(name, rnum, bnum, repeat, align);
  return rv;
}


/* parse arguments of read command */
int runread(int argc, char **argv){
  char *name;
  int i, rv;
  name = NULL;
  for(i = 2; i < argc; i++){
    if(!name && argv[i][0] == '-'){
      usage();
    } else if(!name){
      name = argv[i];
    } else {
      usage();
    }
  }
  if(!name) usage();
  rv = doread(name);
  return rv;
}


/* parse arguments of combo command */
int runcombo(int argc, char **argv){
  char *name;
  int i, rv;
  name = NULL;
  for(i = 2; i < argc; i++){
    if(!name && argv[i][0] == '-'){
      usage();
    } else if(!name){
      name = argv[i];
    } else {
      usage();
    }
  }
  if(!name) usage();
  rv = docombo(name);
  return rv;
}


/* parse arguments of rand command */
int runrand(int argc, char **argv){
  char *name, *rstr, *bstr;
  int i, rnum, bnum, repeat, align, bp, rv;
  name = NULL;
  rstr = NULL;
  bstr = NULL;
  rnum = 0;
  bnum = 0;
  repeat = 1;
  align = 0;
  bp = FALSE;
  for(i = 2; i < argc; i++){
    if(!name && argv[i][0] == '-'){
      if(!strcmp(argv[i], "-cat")){
        if(++i >= argc) usage();
        repeat = atoi(argv[i]);
      } else if(!strcmp(argv[i], "-align")){
        if(++i >= argc) usage();
        align = atoi(argv[i]);
      } else if(!strcmp(argv[i], "-poor")){
        bp = TRUE;
      } else {
        usage();
      }
    } else if(!name){
      name = argv[i];
    } else if(!rstr){
      rstr = argv[i];
    } else if(!bstr){
      bstr = argv[i];
    } else {
      usage();
    }
  }
  if(!name || !rstr || !bstr) usage();
  rnum = atoi(rstr);
  bnum = atoi(bstr);
  if(rnum < 1 || bnum < 1 || repeat < 1 || align < 0) usage();
  rv = dorand(name, rnum, bnum, repeat, align, bp);
  return rv;
}


/* parse arguments of wicked command */
int runwicked(int argc, char **argv){

  char *name, *rstr;
  int i, rnum, rv;
  name = NULL;
  rstr = NULL;
  rnum = 0;
  for(i = 2; i < argc; i++){
    if(!name && argv[i][0] == '-'){
      usage();
    } else if(!name){
      name = argv[i];
    } else if(!rstr){
      rstr = argv[i];
    } else {
      usage();
    }
  }
  if(!name || !rstr) usage();
  rnum = atoi(rstr);
  if(rnum < 1) usage();
  rv = dowicked(name, rnum);
  return rv;
}


/* print an error message */
void pdperror(const char *name){
  fprintf(stderr, "%s: %s: %s\n", progname, name, dperrmsg(dpecode));
}


/* set random pattern and return the length */
int setrandstr(char *buf, int radix){
  int i, len;
  len = rand() % (RANDDATAMAX - RANDDATAMIN) + RANDDATAMIN + 1;
  for(i = 0; i < len; i++){
    buf[i] = rand() % radix;
  }
  buf[len] = '\0';
  return len;
}


/* perform write command */
int dowrite(const char *name, int rnum, int bnum, int repeat, int align){
  DEPOT *depot;
  int i, ri, err, len;
  char buf[RECBUFSIZ];
  printf("<Writing Test>\n  name=%s  rnum=%d  bnum=%d  repeat=%d  align=%d\n\n",
         name, rnum, bnum, repeat, align);
  /* open a database */
  if(!(depot = dpopen(name, DP_OWRITER | DP_OCREAT | DP_OTRUNC, bnum))){
    pdperror(name);
    return 1;
  }
  /* set alignment */
  if(align > 0){
    if(!dpsetalign(depot, align)){
      pdperror(name);
      dpclose(depot);
      return 1;
    }
  }
  /* loop for concatenation */
  err = FALSE;
  for(ri = 1; ri <= repeat; ri++){
    if(repeat > 1) printf("Repeat: %d\n", ri);
    /* loop for each record */
    for(i = 1; i <= rnum; i++){
      len = sprintf(buf, "%08d", i);
      /* store a record */
      if(!dpput(depot, buf, len, buf, len, DP_DCAT)){
        pdperror(name);
        err = TRUE;
        break;
      }
      /* print progression */
      if(rnum > 250 && i % (rnum / 250) == 0){
        putchar('.');
        fflush(stdout);
        if(i == rnum || i % (rnum / 10) == 0){
          printf(" (%08d)\n", i);
          fflush(stdout);
        }
      }
    }
    if(err) break;
  }
  /* close the database */
  if(!dpclose(depot)){
    pdperror(name);
    return 1;
  }
  if(!err) printf("ok\n\n");
  return err ? 1 : 0;
}


/* perform read command */
int doread(const char *name){
  DEPOT *depot;
  int i, rnum, err, len;
  char buf[RECBUFSIZ], *val;
  printf("<Reading Test>\n  name=%s\n\n", name);
  /* open a database */
  if(!(depot = dpopen(name, DP_OREADER, -1))){
    pdperror(name);
    return 1;
  }
  /* get the number of records */
  rnum = dprnum(depot);
  err = FALSE;
  /* loop for each record */
  for(i = 1; i <= rnum; i++){
    len = sprintf(buf, "%08d", i);
    if(!(val = dpget(depot, buf, len, 0, -1, NULL))){
      pdperror(name);
      err = TRUE;
      break;
    }
    free(val);
    /* print progression */
    if(rnum > 250 && i % (rnum / 250) == 0){
      putchar('.');
      fflush(stdout);
      if(i == rnum || i % (rnum / 10) == 0){
        printf(" (%08d)\n", i);
        fflush(stdout);
      }
    }
  }
  /* close the database */
  if(!dpclose(depot)){
    pdperror(name);
    return 1;
  }
  if(!err) printf("ok\n\n");
  return err ? 1 : 0;
}


/* perform combo command */
int docombo(const char *name){
  DEPOT *depot;
  char buf[RECBUFSIZ], wbuf[RECBUFSIZ], *vbuf;
  int i, len, wlen, vsiz;
  printf("<Combination Test>\n  name=%s\n\n", name);
  printf("Creating a database with bnum 3 ... ");
  if(!(depot = dpopen(name, DP_OWRITER | DP_OCREAT | DP_OTRUNC, 3))){
    pdperror(name);
    return 1;
  }
  printf("ok\n");
  printf("Setting alignment as 16 ... ");
  if(!dpsetalign(depot, 16)){
    pdperror(name);
    dpclose(depot);
    return 1;
  }
  printf("ok\n");
  printf("Adding 20 records ... ");
  for(i = 1; i <= 20; i++){
    len = sprintf(buf, "%08d", i);
    if(!dpput(depot, buf, len, buf, len, DP_DOVER)){
      pdperror(name);
      dpclose(depot);
      return 1;
    }
  }
  printf("ok\n");
  printf("Checking records ... ");
  for(i = 1; i <= 20; i++){
    len = sprintf(buf, "%08d", i);
    if(!(vbuf = dpget(depot, buf, len, 0, -1, &vsiz))){
      pdperror(name);
      dpclose(depot);
      return 1;
    }
    free(vbuf);
    if(vsiz != dpvsiz(depot, buf, len)){
      fprintf(stderr, "%s: %s: invalid vsiz\n", progname, name);
      dpclose(depot);
      return 1;
    }
  }
  printf("ok\n");
  printf("Overwriting top 10 records without moving rooms ... ");
  for(i = 1; i <= 10; i++){
    len = sprintf(buf, "%08d", i);
    if(!dpput(depot, buf, len, buf, len, DP_DOVER)){
      pdperror(name);
      dpclose(depot);
      return 1;
    }
  }
  printf("ok\n");
  printf("Overwriting top 5 records with moving rooms ... ");
  for(i = 1; i <= 5; i++){
    len = sprintf(buf, "%08d", i);
    wlen = sprintf(wbuf, "%024d", i);
    if(!dpput(depot, buf, len, wbuf, wlen, DP_DOVER)){
      pdperror(name);
      dpclose(depot);
      return 1;
    }
  }
  printf("ok\n");
  printf("Overwriting top 15 records in concatenation with moving rooms ... ");
  for(i = 1; i <= 15; i++){
    len = sprintf(buf, "%08d", i);
    wlen = sprintf(wbuf, "========================");
    if(!dpput(depot, buf, len, wbuf, wlen, DP_DCAT)){
      pdperror(name);
      dpclose(depot);
      return 1;
    }
  }
  printf("ok\n");
  printf("Checking records ... ");
  for(i = 1; i <= 20; i++){
    len = sprintf(buf, "%08d", i);
    if(!(vbuf = dpget(depot, buf, len, 0, -1, &vsiz))){
      pdperror(name);
      dpclose(depot);
      return 1;
    }
    free(vbuf);
    if(vsiz != dpvsiz(depot, buf, len)){
      fprintf(stderr, "%s: %s: invalid vsiz\n", progname, name);
      dpclose(depot);
      return 1;
    }
  }
  printf("ok\n");
  printf("Deleting top 10 records ... ");
  for(i = 1; i <= 10; i++){
    len = sprintf(buf, "%08d", i);
    if(!dpout(depot, buf, len)){
      pdperror(name);
      dpclose(depot);
      return 1;
    }
  }
  printf("ok\n");
  printf("Checking deleted records ... ");
  for(i = 1; i <= 10; i++){
    len = sprintf(buf, "%08d", i);
    vbuf = dpget(depot, buf, len, 0, -1, &vsiz);
    free(vbuf);
    if(vbuf || dpecode != DP_ENOITEM){
      fprintf(stderr, "%s: %s: deleting failed\n", progname, name);
      dpclose(depot);
      return 1;
    }
  }
  printf("ok\n");
  printf("Overwriting top 15 records in concatenation with moving rooms ... ");
  for(i = 1; i <= 15; i++){
    len = sprintf(buf, "%08d", i);
    wlen = sprintf(wbuf, "========================");
    if(!dpput(depot, buf, len, wbuf, wlen, DP_DCAT)){
      pdperror(name);
      dpclose(depot);
      return 1;
    }
  }
  printf("ok\n");
  printf("Checking records ... ");
  for(i = 1; i <= 20; i++){
    len = sprintf(buf, "%08d", i);
    if(!(vbuf = dpget(depot, buf, len, 0, -1, &vsiz))){
      pdperror(name);
      dpclose(depot);
      return 1;
    }
    free(vbuf);
    if(vsiz != dpvsiz(depot, buf, len)){
      fprintf(stderr, "%s: %s: invalid vsiz\n", progname, name);
      dpclose(depot);
      return 1;
    }
  }
  printf("ok\n");
  printf("Optimizing database ... ");
  if(!dpoptimize(depot, -1)){
    pdperror(name);
    dpclose(depot);
    return 1;
  }
  printf("ok\n");
  printf("Checking records ... ");
  for(i = 1; i <= 20; i++){
    len = sprintf(buf, "%08d", i);
    if(!(vbuf = dpget(depot, buf, len, 0, -1, &vsiz))){
      pdperror(name);
      dpclose(depot);
      return 1;
    }
    free(vbuf);
    if(vsiz != dpvsiz(depot, buf, len)){
      fprintf(stderr, "%s: %s: invalid vsiz\n", progname, name);
      dpclose(depot);
      return 1;
    }
  }
  printf("ok\n");
  printf("Closing the database ... ");
  if(!dpclose(depot)){
    pdperror(name);
    return 1;
  }
  printf("ok\n");
  printf("all ok\n\n");
  return 0;
}


/* perform rand command */
int dorand(const char *name, int rnum, int bnum, int repeat, int align, int poor){
  DEPOT *depot;
  int i, ri, err, len;
  char buf[RECBUFSIZ];
  printf("<Random Writing Test>\n  name=%s  rnum=%d  bnum=%d  repeat=%d  align=%d\n\n",
         name, rnum, bnum, repeat, align);
  srand(time(NULL));
  if(!(depot = dpopen(name, DP_OWRITER | DP_OCREAT | DP_OTRUNC, bnum))){
    pdperror(name);
    return 1;
  }
  if(align > 0){
    if(!dpsetalign(depot, align)){
      pdperror(name);
      dpclose(depot);
      return 1;
    }
  }
  err = FALSE;
  for(ri = 1; ri <= repeat; ri++){
    if(repeat > 1) printf("Repeat: %d\n", ri);
    for(i = 1; i <= rnum; i++){
      len = setrandstr(buf, poor ? 2 : 256);
      if(!dpput(depot, buf, len, buf, len, DP_DCAT)){
        pdperror(name);
        err = TRUE;
        break;
      }
      if(rnum > 250 && i % (rnum / 250) == 0){
        putchar('.');
        fflush(stdout);
        if(i == rnum || i % (rnum / 10) == 0){
          printf(" (%08d)\n", i);
          fflush(stdout);
        }
      }
    }
    if(err) break;
  }
  if(!dpclose(depot)){
    pdperror(name);
    return 1;
  }
  if(!err) printf("ok\n\n");
  return err ? 1 : 0;
}


/* perform wicked command */
int dowicked(const char *name, int rnum){
  DEPOT *depot;
  int i, len, err;
  char buf[32], *val;
  printf("<Wicked Writing Test>\n  name=%s  rnum=%d\n\n", name, rnum);
  srand(time(NULL));
  err = FALSE;
  if(!(depot = dpopen(name, DP_OWRITER | DP_OCREAT | DP_OTRUNC, rnum / 10))){
    pdperror(name);
    return 1;
  }
  if(!dpsetalign(depot, 16)){
    pdperror(name);
    err = TRUE;
  }
  for(i = 1; i <= rnum; i++){
    len = sprintf(buf, "%08d", rand() % rnum + 1);
    switch(rand() % 16){
    case 0:
      putchar('O');
      if(!dpput(depot, buf, len, buf, len, DP_DOVER)) err = TRUE;
      break;
    case 1:
      putchar('K');
      if(!dpput(depot, buf, len, buf, len, DP_DKEEP) && dpecode != DP_EKEEP) err = TRUE;
      break;
    case 2:
     putchar('D');
      if(!dpout(depot, buf, len) && dpecode != DP_ENOITEM) err = TRUE;
      break;
    case 3:
      putchar('V');
      if(dpvsiz(depot, buf, len) == -1 && dpecode != DP_ENOITEM) err = TRUE;
      break;
    default:
      putchar('C');
      if(!dpput(depot, buf, len, buf, len, DP_DCAT)) err = TRUE;
      break;
    }
    if(i % 50 == 0) putchar('\n');
    if(err){
      pdperror(name);
      break;
    }
  }
  if(!dpoptimize(depot, -1)){
    pdperror(name);
    err = TRUE;
  }
  for(i = 1; i <= rnum; i++){
    len = sprintf(buf, "%08d", i);
    if(!dpput(depot, buf, len, ":", -1, DP_DCAT)){
      pdperror(name);
      err = TRUE;
      break;
    }
    putchar(':');
    if(i % 50 == 0) putchar('\n');
  }
  if(!dpoptimize(depot, -1)){
    pdperror(name);
    err = TRUE;
  }
  for(i = 1; i <= rnum; i++){
    len = sprintf(buf, "%08d", i);
    if(!(val = dpget(depot, buf, len, 0, -1, NULL))){
      pdperror(name);
      err = TRUE;
      break;
    }
    free(val);
    putchar('=');
    if(i % 50 == 0) putchar('\n');
  }
  if(!dpiterinit(depot)){
    pdperror(name);
    err = TRUE;
  }
  for(i = 1; i <= rnum; i++){
    len = sprintf(buf, "%08d", i);
    if(!(val = dpiternext(depot, NULL))){
      pdperror(name);
      err = TRUE;
      break;
    }
    free(val);
    putchar('@');
    if(i % 50 == 0) putchar('\n');
  }
  if(!dpclose(depot)){
    pdperror(name);
    return 1;
  }
  if(!err) printf("ok\n\n");
  return err ? 1 : 0;
}



/* END OF FILE */
