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
|
#! /usr/bin/ruby -w
#=================================================================================================
# Test cases of Depot 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 'depot'
# main routine
def main()
$0 = $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 Depot for Ruby\n", $0)
printf(STDERR, "\n")
printf(STDERR, "usage:\n")
printf(STDERR, " %s write name rnum bnum\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
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()
else
usage()
end
i += 1
end
(name && rnum > 0 && bnum > 0) || usage()
dowrite(name, rnum, bnum)
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)
printf("<Writing Test>\n name=%s rnum=%d bnum=%d\n\n", name, rnum, bnum)
depot = nil
begin
# open a database
depot = Depot::new(name, Depot::OWRITER | Depot::OCREAT | Depot::OTRUNC, bnum)
# loop for each record
STDOUT.sync = true
1.upto(rnum) do |i|
buf = sprintf("%08d", i)
# store a record
depot.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
(depot) && depot.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)
depot = nil
begin
# open a database
depot = Depot::new(name)
# get the number of record
rnum = depot.rnum()
# loop for each record
STDOUT.sync = true
1.upto(rnum) do |i|
buf = sprintf("%08d", i)
# store a record
depot.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
(depot) && depot.close()
rescue
return 1
end
end
printf("ok\n\n")
return 0
end
# perform misc command
def domisc(name)
loopnum = 500
bucketnum = 16
threadnum = 10
printf("<Miscellaneous Test>\n name=%s\n\n", name)
depot = nil
begin
# open the database
printf("Creating a database ... ")
depot = Depot::open("casket", Depot::OWRITER | Depot::OCREAT | Depot::OTRUNC, bucketnum)
printf("ok\n")
# store records
printf("Storing records ... ")
1.upto(loopnum) do |i|
buf = sprintf("%08d", i)
depot[buf] = buf
end
printf("ok\n")
# retrieve records
printf("Retrieving records ... ")
1.upto(loopnum) do |i|
buf = sprintf("%08d", i)
(depot[buf] == buf) || raise("key and value does not match")
end
printf("ok\n")
# traverse records
printf("Traversing records ... ")
depot.each() do |key, val|
(key == val) || raise("key and value does not match")
end
depot.keys()
depot.values()
printf("ok\n")
# silent mode operations
printf("Silent mode operations ... ")
depot.silent = true
depot.put("foo", "bar", Depot::DKEEP)
depot.put("foo", "bar", Depot::DKEEP)
depot.get("foo")
depot.out("foo")
depot.out("foo")
depot.get("nil")
depot.fetch("nil", "void");
depot.keys()
depot.values()
printf("ok\n")
rescue
printf("%s: %s: %s\n", $0, name, $!)
return 1
ensure
# close the database
printf("Closing the database ... ")
(depot) && depot.close()
printf("ok\n")
end
# test iterator and threads
printf("Processing with iterator and threads ... ")
Depot::new("casket", Depot::OWRITER) do |depot|
(depot.rnum() == loopnum) || raise("record number is invalid")
depot.clear()
threads = []
1.upto(threadnum) do |i|
t = Thread::new() do
1.upto(loopnum) do |j|
buf = sprintf("%08d", j)
depot.put(buf, "*", Depot::DCAT)
end
end
threads.push(t)
end
threads.each do |t|
t.join()
end
1.upto(loopnum) do |i|
buf = sprintf("%08d", i)
(depot.vsiz(buf) == threadnum) || raise("thread writing is invalid")
end
(depot.index("*" * threadnum)) || raise("thread writing is invalid")
end
printf("ok\n")
printf("all ok\n\n")
return 0
end
# execute main
$0 = $0.gsub(/.*\//, "")
exit(main())
# END OF FILE
|