| 1 |
#! /usr/bin/ruby -w |
| 2 |
|
| 3 |
#================================================================================================= |
| 4 |
# Test cases of Curia for Ruby |
| 5 |
# Copyright (C) 2000-2005 Mikio Hirabayashi |
| 6 |
# This file is part of QDBM, Quick Database Manager. |
| 7 |
# QDBM is free software; you can redistribute it and/or modify it under the terms of the GNU |
| 8 |
# Lesser General Public License as published by the Free Software Foundation; either version |
| 9 |
# 2.1 of the License or any later version. QDBM is distributed in the hope that it will be |
| 10 |
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 |
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 12 |
# details. |
| 13 |
# You should have received a copy of the GNU Lesser General Public License along with QDBM; if |
| 14 |
# not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 15 |
# 02111-1307 USA. |
| 16 |
#================================================================================================= |
| 17 |
|
| 18 |
|
| 19 |
require 'curia' |
| 20 |
|
| 21 |
|
| 22 |
# main routine |
| 23 |
def main() |
| 24 |
$0.gsub!(/.*\//, "") |
| 25 |
(ARGV.length >= 1) || usage() |
| 26 |
if(ARGV[0] == "write") |
| 27 |
rv = runwrite() |
| 28 |
elsif(ARGV[0] == "read") |
| 29 |
rv = runread() |
| 30 |
elsif(ARGV[0] == "misc") |
| 31 |
rv = runmisc() |
| 32 |
else |
| 33 |
usage() |
| 34 |
end |
| 35 |
return rv |
| 36 |
end |
| 37 |
|
| 38 |
|
| 39 |
# print the usage and exit |
| 40 |
def usage() |
| 41 |
printf(STDERR, "%s: test cases for Curia for Ruby\n", $0) |
| 42 |
printf(STDERR, "\n") |
| 43 |
printf(STDERR, "usage:\n") |
| 44 |
printf(STDERR, " %s write name rnum bnum dnum\n", $0) |
| 45 |
printf(STDERR, " %s read name\n", $0) |
| 46 |
printf(STDERR, " %s misc name\n", $0) |
| 47 |
printf(STDERR, "\n") |
| 48 |
exit(1) |
| 49 |
end |
| 50 |
|
| 51 |
|
| 52 |
# parse arguments of write command |
| 53 |
def runwrite() |
| 54 |
name = nil |
| 55 |
rnum = -1 |
| 56 |
bnum = -1 |
| 57 |
dnum = -1 |
| 58 |
i = 1 |
| 59 |
while(i < ARGV.length) |
| 60 |
if(!name && ARGV[i] =~ /^-/) |
| 61 |
usage() |
| 62 |
elsif(!name) |
| 63 |
name = ARGV[i] |
| 64 |
elsif(rnum < 0) |
| 65 |
rnum = ARGV[i].to_i() |
| 66 |
elsif(bnum < 0) |
| 67 |
bnum = ARGV[i].to_i() |
| 68 |
elsif(dnum < 0) |
| 69 |
dnum = ARGV[i].to_i() |
| 70 |
else |
| 71 |
usage() |
| 72 |
end |
| 73 |
i += 1 |
| 74 |
end |
| 75 |
(name && rnum > 0 && bnum > 0 && dnum > 0) || usage() |
| 76 |
dowrite(name, rnum, bnum, dnum) |
| 77 |
return 0 |
| 78 |
end |
| 79 |
|
| 80 |
|
| 81 |
# parse arguments of read command |
| 82 |
def runread() |
| 83 |
name = nil |
| 84 |
i = 1 |
| 85 |
while(i < ARGV.length) |
| 86 |
if(!name && ARGV[i] =~ /^-/) |
| 87 |
usage() |
| 88 |
elsif(!name) |
| 89 |
name = ARGV[i] |
| 90 |
else |
| 91 |
usage() |
| 92 |
end |
| 93 |
i += 1 |
| 94 |
end |
| 95 |
(name) || usage() |
| 96 |
doread(name) |
| 97 |
return 0 |
| 98 |
end |
| 99 |
|
| 100 |
|
| 101 |
# parse arguments of misc command |
| 102 |
def runmisc() |
| 103 |
name = nil |
| 104 |
i = 1 |
| 105 |
while(i < ARGV.length) |
| 106 |
if(!name && ARGV[i] =~ /^-/) |
| 107 |
usage() |
| 108 |
elsif(!name) |
| 109 |
name = ARGV[i] |
| 110 |
else |
| 111 |
usage() |
| 112 |
end |
| 113 |
i += 1 |
| 114 |
end |
| 115 |
(name) || usage() |
| 116 |
domisc(name) |
| 117 |
return 0 |
| 118 |
end |
| 119 |
|
| 120 |
|
| 121 |
# perform write command |
| 122 |
def dowrite(name, rnum, bnum, dnum) |
| 123 |
printf("<Writing Test>\n name=%s rnum=%d bnum=%d dnum=%d\n\n", name, rnum, bnum, dnum) |
| 124 |
curia = nil |
| 125 |
begin |
| 126 |
# open a database |
| 127 |
curia = Curia::new(name, Curia::OWRITER | Curia::OCREAT | Curia::OTRUNC, bnum, dnum) |
| 128 |
# loop for each record |
| 129 |
STDOUT.sync = true |
| 130 |
1.upto(rnum) do |i| |
| 131 |
buf = sprintf("%08d", i) |
| 132 |
# store a record |
| 133 |
curia.put(buf, buf) |
| 134 |
# print progression |
| 135 |
if(rnum > 250 && i % (rnum / 250) == 0) |
| 136 |
print(".") |
| 137 |
if(i == rnum || i % (rnum / 10) == 0) |
| 138 |
printf(" (%08d)\n", i) |
| 139 |
end |
| 140 |
end |
| 141 |
end |
| 142 |
rescue |
| 143 |
printf("%s: %s: %s\n", $0, name, $!) |
| 144 |
return 1 |
| 145 |
ensure |
| 146 |
begin |
| 147 |
# close the database |
| 148 |
(curia) && curia.close() |
| 149 |
rescue |
| 150 |
return 1 |
| 151 |
end |
| 152 |
end |
| 153 |
printf("ok\n\n") |
| 154 |
return 0 |
| 155 |
end |
| 156 |
|
| 157 |
|
| 158 |
# perform read command |
| 159 |
def doread(name) |
| 160 |
printf("<Reading Test>\n name=%s\n\n", name) |
| 161 |
curia = nil |
| 162 |
begin |
| 163 |
# open a database |
| 164 |
curia = Curia::new(name) |
| 165 |
# get the number of record |
| 166 |
rnum = curia.rnum() |
| 167 |
# loop for each record |
| 168 |
STDOUT.sync = true |
| 169 |
1.upto(rnum) do |i| |
| 170 |
buf = sprintf("%08d", i) |
| 171 |
# store a record |
| 172 |
curia.get(buf) |
| 173 |
# print progression |
| 174 |
if(rnum > 250 && i % (rnum / 250) == 0) |
| 175 |
print(".") |
| 176 |
if(i == rnum || i % (rnum / 10) == 0) |
| 177 |
printf(" (%08d)\n", i) |
| 178 |
end |
| 179 |
end |
| 180 |
end |
| 181 |
rescue |
| 182 |
printf("%s: %s: %s\n", $0, name, $!) |
| 183 |
return 1 |
| 184 |
ensure |
| 185 |
begin |
| 186 |
# close the database |
| 187 |
(curia) && curia.close() |
| 188 |
rescue |
| 189 |
return 1 |
| 190 |
end |
| 191 |
end |
| 192 |
printf("ok\n\n") |
| 193 |
return 0 |
| 194 |
end |
| 195 |
|
| 196 |
|
| 197 |
# perform misc command |
| 198 |
def domisc(name) |
| 199 |
loopnum = 500 |
| 200 |
bucketnum = 16 |
| 201 |
divnum = 3 |
| 202 |
threadnum = 10 |
| 203 |
printf("<Miscellaneous Test>\n name=%s\n\n", name) |
| 204 |
curia = nil |
| 205 |
begin |
| 206 |
# open the database |
| 207 |
printf("Creating a database ... ") |
| 208 |
curia = Curia::open("casket", Curia::OWRITER | Curia::OCREAT | Curia::OTRUNC, |
| 209 |
bucketnum, divnum) |
| 210 |
printf("ok\n") |
| 211 |
# store records |
| 212 |
printf("Storing records ... ") |
| 213 |
1.upto(loopnum) do |i| |
| 214 |
buf = sprintf("%08d", i) |
| 215 |
curia[buf] = buf |
| 216 |
end |
| 217 |
printf("ok\n") |
| 218 |
# retrieve records |
| 219 |
printf("Retrieving records ... ") |
| 220 |
1.upto(loopnum) do |i| |
| 221 |
buf = sprintf("%08d", i) |
| 222 |
(curia[buf] == buf) || raise("key and value does not match") |
| 223 |
end |
| 224 |
printf("ok\n") |
| 225 |
# traverse records |
| 226 |
printf("Traversing records ... ") |
| 227 |
curia.each() do |key, val| |
| 228 |
(key == val) || raise("key and value does not match") |
| 229 |
end |
| 230 |
curia.keys() |
| 231 |
curia.values() |
| 232 |
printf("ok\n") |
| 233 |
# silent mode operations |
| 234 |
printf("Silent mode operations ... ") |
| 235 |
curia.silent = true |
| 236 |
curia.put("foo", "bar", Curia::DKEEP) |
| 237 |
curia.put("foo", "bar", Curia::DKEEP) |
| 238 |
curia.get("foo") |
| 239 |
curia.out("foo") |
| 240 |
curia.out("foo") |
| 241 |
curia.get("nil") |
| 242 |
curia.fetch("nil", "void"); |
| 243 |
curia.keys() |
| 244 |
curia.values() |
| 245 |
printf("ok\n") |
| 246 |
rescue |
| 247 |
printf("%s: %s: %s\n", $0, name, $!) |
| 248 |
return 1 |
| 249 |
ensure |
| 250 |
# close the database |
| 251 |
printf("Closing the database ... ") |
| 252 |
(curia) && curia.close() |
| 253 |
printf("ok\n") |
| 254 |
end |
| 255 |
# test iterator and threads |
| 256 |
printf("Processing with iterator and threads ... ") |
| 257 |
Curia::new("casket", Curia::OWRITER) do |curia| |
| 258 |
(curia.rnum() == loopnum) || raise("record number is invalid") |
| 259 |
curia.clear() |
| 260 |
threads = [] |
| 261 |
1.upto(threadnum) do |i| |
| 262 |
t = Thread::new() do |
| 263 |
1.upto(loopnum) do |j| |
| 264 |
buf = sprintf("%08d", j) |
| 265 |
curia.put(buf, "*", Curia::DCAT) |
| 266 |
end |
| 267 |
end |
| 268 |
threads.push(t) |
| 269 |
end |
| 270 |
threads.each do |t| |
| 271 |
t.join() |
| 272 |
end |
| 273 |
1.upto(loopnum) do |i| |
| 274 |
buf = sprintf("%08d", i) |
| 275 |
(curia.vsiz(buf) == threadnum) || raise("thread writing is invalid") |
| 276 |
end |
| 277 |
(curia.index("*" * threadnum)) || raise("thread writing is invalid") |
| 278 |
end |
| 279 |
printf("ok\n") |
| 280 |
printf("all ok\n\n") |
| 281 |
return 0 |
| 282 |
end |
| 283 |
|
| 284 |
|
| 285 |
# execute main |
| 286 |
$0.gsub!(/.*\//, "") |
| 287 |
exit(main()) |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
# END OF FILE |