#!/usr/pkg/bin/ruby32 -w

#=================================================================================================
# Test cases of Curia for Ruby
#                                                       Copyright (C) 2000-2005 Mikio Hirabayashi
# This file is part of QDBM, Quick Database Manager.
# QDBM is free software; you can redistribute it and/or modify it under the terms of the GNU
# Lesser General Public License as published by the Free Software Foundation; either version
# 2.1 of the License or any later version.  QDBM 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 Lesser General Public License for more
# details.
# You should have received a copy of the GNU Lesser General Public License along with QDBM; if
# not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307 USA.
#=================================================================================================


require 'curia'


# main routine
def main()
  $0.gsub!(/.*\//, "")
  (ARGV.length >= 1) || usage()
  if(ARGV[0] == "write")
    rv = runwrite()
  elsif(ARGV[0] == "read")
    rv = runread()
  elsif(ARGV[0] == "misc")
    rv = runmisc()
  else
    usage()
  end
  return rv
end


# print the usage and exit
def usage()
  printf(STDERR, "%s: test cases for Curia for Ruby\n", $0)
  printf(STDERR, "\n")
  printf(STDERR, "usage:\n")
  printf(STDERR, "  %s write name rnum bnum dnum\n", $0)
  printf(STDERR, "  %s read name\n", $0)
  printf(STDERR, "  %s misc name\n", $0)
  printf(STDERR, "\n")
  exit(1)
end


# parse arguments of write command
def runwrite()
  name = nil
  rnum = -1
  bnum = -1
  dnum = -1
  i = 1
  while(i < ARGV.length)
    if(!name && ARGV[i] =~ /^-/)
      usage()
    elsif(!name)
      name = ARGV[i]
    elsif(rnum < 0)
      rnum = ARGV[i].to_i()
    elsif(bnum < 0)
      bnum = ARGV[i].to_i()
    elsif(dnum < 0)
      dnum = ARGV[i].to_i()
    else
      usage()
    end
    i += 1
  end
  (name && rnum > 0 && bnum > 0 && dnum > 0) || usage()
  dowrite(name, rnum, bnum, dnum)
  return 0
end


# parse arguments of read command
def runread()
  name = nil
  i = 1
  while(i < ARGV.length)
    if(!name && ARGV[i] =~ /^-/)
      usage()
    elsif(!name)
      name = ARGV[i]
    else
      usage()
    end
    i += 1
  end
  (name) || usage()
  doread(name)
  return 0
end


# parse arguments of misc command
def runmisc()
  name = nil
  i = 1
  while(i < ARGV.length)
    if(!name && ARGV[i] =~ /^-/)
      usage()
    elsif(!name)
      name = ARGV[i]
    else
      usage()
    end
    i += 1
  end
  (name) || usage()
  domisc(name)
  return 0
end


# perform write command
def dowrite(name, rnum, bnum, dnum)
  printf("<Writing Test>\n  name=%s  rnum=%d  bnum=%d  dnum=%d\n\n", name, rnum, bnum, dnum)
  curia = nil
  begin
    # open a database
    curia = Curia::new(name, Curia::OWRITER | Curia::OCREAT | Curia::OTRUNC, bnum, dnum)
    # loop for each record
    STDOUT.sync = true
    1.upto(rnum) do |i|
      buf = sprintf("%08d", i)
      # store a record
      curia.put(buf, buf)
      # print progression
      if(rnum > 250 && i % (rnum / 250) == 0)
        print(".")
        if(i == rnum || i % (rnum / 10) == 0)
          printf(" (%08d)\n", i)
        end
      end
    end
  rescue
    printf("%s: %s: %s\n", $0, name, $!)
    return 1
  ensure
    begin
      # close the database
      (curia) && curia.close()
    rescue
      return 1
    end
  end
  printf("ok\n\n")
  return 0
end


# perform read command
def doread(name)
  printf("<Reading Test>\n  name=%s\n\n", name)
  curia = nil
  begin
    # open a database
    curia = Curia::new(name)
    # get the number of record
    rnum = curia.rnum()
    # loop for each record
    STDOUT.sync = true
    1.upto(rnum) do |i|
      buf = sprintf("%08d", i)
      # store a record
      curia.get(buf)
      # print progression
      if(rnum > 250 && i % (rnum / 250) == 0)
        print(".")
        if(i == rnum || i % (rnum / 10) == 0)
          printf(" (%08d)\n", i)
        end
      end
    end
  rescue
    printf("%s: %s: %s\n", $0, name, $!)
    return 1
  ensure
    begin
      # close the database
      (curia) && curia.close()
    rescue
      return 1
    end
  end
  printf("ok\n\n")
  return 0
end


# perform misc command
def domisc(name)
  loopnum = 500
  bucketnum = 16
  divnum = 3
  threadnum = 10
  printf("<Miscellaneous Test>\n  name=%s\n\n", name)
  curia = nil
  begin
    # open the database
    printf("Creating a database ... ")
    curia = Curia::open("casket", Curia::OWRITER | Curia::OCREAT | Curia::OTRUNC,
                        bucketnum, divnum)
    printf("ok\n")
    # store records
    printf("Storing records ... ")
    1.upto(loopnum) do |i|
      buf =  sprintf("%08d", i)
      curia[buf] = buf
    end
    printf("ok\n")
    # retrieve records
    printf("Retrieving records ... ")
    1.upto(loopnum) do |i|
      buf =  sprintf("%08d", i)
      (curia[buf] == buf) || raise("key and value does not match")
    end
    printf("ok\n")
    # traverse records
    printf("Traversing records ... ")
    curia.each() do |key, val|
      (key == val) || raise("key and value does not match")
    end
    curia.keys()
    curia.values()
    printf("ok\n")
    # silent mode operations
    printf("Silent mode operations ... ")
    curia.silent = true
    curia.put("foo", "bar", Curia::DKEEP)
    curia.put("foo", "bar", Curia::DKEEP)
    curia.get("foo")
    curia.out("foo")
    curia.out("foo")
    curia.get("nil")
    curia.fetch("nil", "void");
    curia.keys()
    curia.values()
    printf("ok\n")
  rescue
    printf("%s: %s: %s\n", $0, name, $!)
    return 1
  ensure
    # close the database
    printf("Closing the database ... ")
    (curia) && curia.close()
    printf("ok\n")
  end
  # test iterator and threads
  printf("Processing with iterator and threads ... ")
  Curia::new("casket", Curia::OWRITER) do |curia|
    (curia.rnum() == loopnum) || raise("record number is invalid")
    curia.clear()
    threads = []
    1.upto(threadnum) do |i|
      t = Thread::new() do
        1.upto(loopnum) do |j|
          buf = sprintf("%08d", j)
          curia.put(buf, "*", Curia::DCAT)
        end
      end
      threads.push(t)
    end
    threads.each do |t|
      t.join()
    end
    1.upto(loopnum) do |i|
      buf = sprintf("%08d", i)
      (curia.vsiz(buf) == threadnum) || raise("thread writing is invalid")
    end
    (curia.index("*" * threadnum)) || raise("thread writing is invalid")
  end
  printf("ok\n")
  printf("all ok\n\n")
  return 0
end


# execute main
$0.gsub!(/.*\//, "")
exit(main())



# END OF FILE
