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


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

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


/* 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);
void pdperror(const char *name);
int dowrite(const char *name, int rnum, int bnum, int dnum, int repeat, int align, int lob);
int doread(const char *name, int lob);
int docombo(const char *name);


/* 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 {
    usage();
  }
  return rv;
}


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


/* parse arguments of write command */
int runwrite(int argc, char **argv){
  char *name, *rstr, *bstr, *dstr;
  int i, rnum, bnum, dnum, repeat, align, lob, rv;
  name = NULL;
  rstr = NULL;
  bstr = NULL;
  dstr = NULL;
  rnum = 0;
  bnum = 0;
  dnum = 0;
  repeat = 1;
  align = 0;
  lob = 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], "-lob")){
        lob = TRUE;
      } else {
        usage();
      }
    } else if(!name){
      name = argv[i];
    } else if(!rstr){
      rstr = argv[i];
    } else if(!bstr){
      bstr = argv[i];
    } else if(!dstr){
      dstr = argv[i];
    } else {
      usage();
    }
  }
  if(!name || !rstr || !bstr || !dstr) usage();
  rnum = atoi(rstr);
  bnum = atoi(bstr);
  dnum = atoi(dstr);
  if(rnum < 1 || bnum < 1 || dnum < 1 || repeat < 1 || align < 0) usage();
  rv = dowrite(name, rnum, bnum, dnum, repeat, align, lob);
  return rv;
}


/* parse arguments of read command */
int runread(int argc, char **argv){
  char *name;
  int i, lob, rv;
  name = NULL;
  lob = FALSE;
  for(i = 2; i < argc; i++){
    if(!name && argv[i][0] == '-'){
      if(!strcmp(argv[i], "-lob")){
        lob = TRUE;
      } else {
        usage();
      }
    } else if(!name){
      name = argv[i];
    } else {
      usage();
    }
  }
  if(!name) usage();
  rv = doread(name, lob);
  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;
}


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


/* perform write command */
int dowrite(const char *name, int rnum, int bnum, int dnum, int repeat, int align, int lob){
  CURIA *curia;
  int i, ri, err, len;
  char buf[32];
  printf("<Writing Test>\n  name=%s  rnum=%d  bnum=%d  dnum=%d  repeat=%d  align=%d  lob=%d\n\n",
         name, rnum, bnum, dnum, repeat, align, lob);
  /* open a database */
  if(!(curia = cropen(name, CR_OWRITER | CR_OCREAT | CR_OTRUNC, bnum, dnum))){
    pdperror(name);
    return 1;
  }
  /* set alignment */
  if(align > 0){
    if(!crsetalign(curia, align)){
      pdperror(name);
      crclose(curia);
      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(lob){
        if(!crputlob(curia, buf, len, buf, len, CR_DCAT)){
          pdperror(name);
          err = TRUE;
          break;
        }
      } else {
        if(!crput(curia, buf, len, buf, len, CR_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(!crclose(curia)){
    pdperror(name);
    return 1;
  }
  if(!err) printf("ok\n\n");
  return err ? 1 : 0;
}


/* perform read command */
int doread(const char *name, int lob){
  CURIA *curia;
  int i, rnum, err, len;
  char buf[32], *val;
  printf("<Reading Test>\n  name=%s  lob=%d\n\n", name, lob);
  /* open a database */
  if(!(curia = cropen(name, CR_OREADER, -1, -1))){
    pdperror(name);
    perror("cropen");
    return 1;
  }
  /* get the number of records */
  if(lob){
    rnum = crrnumlob(curia);
  } else {
    rnum = crrnum(curia);
  }
  err = FALSE;
  /* loop for each record */
  for(i = 1; i <= rnum; i++){
    len = sprintf(buf, "%08d", i);
    if(lob){
      if(!(val = crgetlob(curia, buf, len, 0, -1, NULL))){
        pdperror(name);
        err = TRUE;
        break;
      }
    } else {
      if(!(val = crget(curia, 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(!crclose(curia)){
    pdperror(name);
    return 1;
  }
  if(!err) printf("ok\n\n");
  return err ? 1 : 0;
}


/* perform combo command */
int docombo(const char *name){
  CURIA *curia;
  char buf[32], wbuf[32], *vbuf;
  int i, len, wlen, vsiz;
  printf("<Combination Test>\n  name=%s\n\n", name);
  printf("Creating a database with bnum 3 ... ");
  if(!(curia = cropen(name, CR_OWRITER | CR_OCREAT | CR_OTRUNC, 3, 3))){
    pdperror(name);
    return 1;
  }
  printf("ok\n");
  printf("Setting alignment as 16 ... ");
  if(!crsetalign(curia, 16)){
    pdperror(name);
    crclose(curia);
    return 1;
  }
  printf("ok\n");
  printf("Adding 20 records ... ");
  for(i = 1; i <= 20; i++){
    len = sprintf(buf, "%08d", i);
    if(!crput(curia, buf, len, buf, len, CR_DOVER)){
      pdperror(name);
      crclose(curia);
      return 1;
    }
  }
  printf("ok\n");
  printf("Checking records ... ");
  for(i = 1; i <= 20; i++){
    len = sprintf(buf, "%08d", i);
    if(!(vbuf = crget(curia, buf, len, 0, -1, &vsiz))){
      pdperror(name);
      crclose(curia);
      return 1;
    }
    free(vbuf);
    if(vsiz != crvsiz(curia, buf, len)){
      fprintf(stderr, "%s: %s: invalid vsiz\n", progname, name);
      crclose(curia);
      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(!crput(curia, buf, len, buf, len, CR_DOVER)){
      pdperror(name);
      crclose(curia);
      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(!crput(curia, buf, len, wbuf, wlen, CR_DOVER)){
      pdperror(name);
      crclose(curia);
      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(!crput(curia, buf, len, wbuf, wlen, CR_DCAT)){
      pdperror(name);
      crclose(curia);
      return 1;
    }
  }
  printf("ok\n");
  printf("Checking records ... ");
  for(i = 1; i <= 20; i++){
    len = sprintf(buf, "%08d", i);
    if(!(vbuf = crget(curia, buf, len, 0, -1, &vsiz))){
      pdperror(name);
      crclose(curia);
      return 1;
    }
    free(vbuf);
    if(vsiz != crvsiz(curia, buf, len)){
      fprintf(stderr, "%s: %s: invalid vsiz\n", progname, name);
      crclose(curia);
      return 1;
    }
  }
  printf("ok\n");
  printf("Deleting top 10 records ... ");
  for(i = 1; i <= 10; i++){
    len = sprintf(buf, "%08d", i);
    if(!crout(curia, buf, len)){
      pdperror(name);
      crclose(curia);
      return 1;
    }
  }
  printf("ok\n");
  printf("Checking deleted records ... ");
  for(i = 1; i <= 10; i++){
    len = sprintf(buf, "%08d", i);
    vbuf = crget(curia, buf, len, 0, -1, &vsiz);
    free(vbuf);
    if(vbuf || dpecode != DP_ENOITEM){
      fprintf(stderr, "%s: %s: deleting failed\n", progname, name);
      crclose(curia);
      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(!crput(curia, buf, len, wbuf, wlen, CR_DCAT)){
      pdperror(name);
      crclose(curia);
      return 1;
    }
  }
  printf("ok\n");
  printf("Checking records ... ");
  for(i = 1; i <= 20; i++){
    len = sprintf(buf, "%08d", i);
    if(!(vbuf = crget(curia, buf, len, 0, -1, &vsiz))){
      pdperror(name);
      crclose(curia);
      return 1;
    }
    free(vbuf);
    if(vsiz != crvsiz(curia, buf, len)){
      fprintf(stderr, "%s: %s: invalid vsiz\n", progname, name);
      crclose(curia);
      return 1;
    }
  }
  printf("ok\n");
  printf("Optimizing database ... ");
  if(!croptimize(curia, -1)){
    pdperror(name);
    crclose(curia);
    return 1;
  }
  printf("ok\n");
  printf("Checking records ... ");
  for(i = 1; i <= 20; i++){
    len = sprintf(buf, "%08d", i);
    if(!(vbuf = crget(curia, buf, len, 0, -1, &vsiz))){
      pdperror(name);
      crclose(curia);
      return 1;
    }
    free(vbuf);
    if(vsiz != crvsiz(curia, buf, len)){
      fprintf(stderr, "%s: %s: invalid vsiz\n", progname, name);
      crclose(curia);
      return 1;
    }
  }
  printf("ok\n");
  printf("Closing database ... ");
  if(!crclose(curia)){
    pdperror(name);
    return 1;
  }
  printf("ok\n");
  printf("all ok\n\n");
  return 0;
}



/* END OF FILE */
