diff options
author | (no author) <(no author)@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2002-03-22 04:48:57 +0000 |
---|---|---|
committer | (no author) <(no author)@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2002-03-22 04:48:57 +0000 |
commit | 7dc80f4f9c42752a6c870cff8a908f283b37cffa (patch) | |
tree | 8d73df58a238e3a123be05e138451e8a6373725d /ext/gdbm | |
parent | 89646912c2310e46b87d1c028d7960e08e80dbad (diff) |
This commit was manufactured by cvs2svn to create branch 'SHIGEO'.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/SHIGEO@2245 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/gdbm')
-rw-r--r-- | ext/gdbm/.cvsignore | 2 | ||||
-rw-r--r-- | ext/gdbm/MANIFEST | 6 | ||||
-rw-r--r-- | ext/gdbm/README | 1 | ||||
-rw-r--r-- | ext/gdbm/depend | 1 | ||||
-rw-r--r-- | ext/gdbm/extconf.rb | 7 | ||||
-rw-r--r-- | ext/gdbm/gdbm.c | 994 | ||||
-rw-r--r-- | ext/gdbm/testgdbm.rb | 663 |
7 files changed, 0 insertions, 1674 deletions
diff --git a/ext/gdbm/.cvsignore b/ext/gdbm/.cvsignore deleted file mode 100644 index fc802ff1c2..0000000000 --- a/ext/gdbm/.cvsignore +++ /dev/null @@ -1,2 +0,0 @@ -Makefile -mkmf.log diff --git a/ext/gdbm/MANIFEST b/ext/gdbm/MANIFEST deleted file mode 100644 index 1359b4fdaf..0000000000 --- a/ext/gdbm/MANIFEST +++ /dev/null @@ -1,6 +0,0 @@ -MANIFEST -README -depend -extconf.rb -gdbm.c -testgdbm.rb diff --git a/ext/gdbm/README b/ext/gdbm/README deleted file mode 100644 index df7a261c68..0000000000 --- a/ext/gdbm/README +++ /dev/null @@ -1 +0,0 @@ -gdbm ext-library for Ruby 1.3 or later diff --git a/ext/gdbm/depend b/ext/gdbm/depend deleted file mode 100644 index c080a81619..0000000000 --- a/ext/gdbm/depend +++ /dev/null @@ -1 +0,0 @@ -gdbm.o: gdbm.c $(hdrdir)/ruby.h $(topdir)/config.h $(hdrdir)/defines.h diff --git a/ext/gdbm/extconf.rb b/ext/gdbm/extconf.rb deleted file mode 100644 index 5a09492e5e..0000000000 --- a/ext/gdbm/extconf.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'mkmf' - -dir_config("gdbm") -if have_library("gdbm", "gdbm_open") and - have_header("gdbm.h") - create_makefile("gdbm") -end diff --git a/ext/gdbm/gdbm.c b/ext/gdbm/gdbm.c deleted file mode 100644 index d30f5dec7a..0000000000 --- a/ext/gdbm/gdbm.c +++ /dev/null @@ -1,994 +0,0 @@ -/************************************************ - - gdbm.c - - - $Author$ - $Date$ - modified at: Mon Jan 24 15:59:52 JST 1994 - -************************************************/ - -#include "ruby.h" - -#include <gdbm.h> -#include <fcntl.h> -#include <errno.h> - -static VALUE rb_cGDBM, rb_eGDBMError, rb_eGDBMFatalError; - -#define MY_BLOCK_SIZE (2048) -#define MY_FATAL_FUNC rb_gdbm_fatal -static void -rb_gdbm_fatal(msg) - char *msg; -{ - rb_raise(rb_eGDBMFatalError, msg); -} - -struct dbmdata { - int di_size; - GDBM_FILE di_dbm; -}; - -static void -closed_dbm() -{ - rb_raise(rb_eRuntimeError, "closed GDBM file"); -} - -#define GetDBM(obj, dbmp) {\ - Data_Get_Struct(obj, struct dbmdata, dbmp);\ - if (dbmp == 0) closed_dbm();\ - if (dbmp->di_dbm == 0) closed_dbm();\ -} - -static void -free_dbm(dbmp) - struct dbmdata *dbmp; -{ - if (dbmp) { - if (dbmp->di_dbm) gdbm_close(dbmp->di_dbm); - free(dbmp); - } -} - -static VALUE -fgdbm_close(obj) - VALUE obj; -{ - struct dbmdata *dbmp; - - GetDBM(obj, dbmp); - gdbm_close(dbmp->di_dbm); - dbmp->di_dbm = 0; - - return Qnil; -} - -static VALUE -fgdbm_s_new(argc, argv, klass) - int argc; - VALUE *argv; - VALUE klass; -{ - VALUE obj = Data_Wrap_Struct(klass, 0, free_dbm, 0); - rb_obj_call_init(obj, argc, argv); - return obj; -} - -static VALUE -fgdbm_initialize(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; -{ - VALUE file, vmode, vflags; - GDBM_FILE dbm; - struct dbmdata *dbmp; - int mode, flags = 0; - - if (rb_scan_args(argc, argv, "12", &file, &vmode, &vflags) == 1) { - mode = 0666; /* default value */ - } - else if (NIL_P(vmode)) { - mode = -1; /* return nil if DB not exist */ - } - else { - mode = NUM2INT(vmode); - } - - if (!NIL_P(vflags)) - flags = NUM2INT(vflags); - - SafeStringValue(file); - - dbm = 0; - if (mode >= 0) - dbm = gdbm_open(RSTRING(file)->ptr, MY_BLOCK_SIZE, - GDBM_WRCREAT|flags, mode, MY_FATAL_FUNC); - if (!dbm) - dbm = gdbm_open(RSTRING(file)->ptr, MY_BLOCK_SIZE, - GDBM_WRITER|flags, 0, MY_FATAL_FUNC); - if (!dbm) - dbm = gdbm_open(RSTRING(file)->ptr, MY_BLOCK_SIZE, - GDBM_READER|flags, 0, MY_FATAL_FUNC); - - if (!dbm) { - if (mode == -1) return Qnil; - - if (gdbm_errno == GDBM_FILE_OPEN_ERROR || - gdbm_errno == GDBM_CANT_BE_READER || - gdbm_errno == GDBM_CANT_BE_WRITER) - rb_sys_fail(RSTRING(file)->ptr); - else - rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno)); - } - - dbmp = ALLOC(struct dbmdata); - DATA_PTR(obj) = dbmp; - dbmp->di_dbm = dbm; - dbmp->di_size = -1; - - return obj; -} - -static VALUE -fgdbm_s_open(argc, argv, klass) - int argc; - VALUE *argv; - VALUE klass; -{ - VALUE obj = Data_Wrap_Struct(klass, 0, free_dbm, 0); - - if (NIL_P(fgdbm_initialize(argc, argv, obj))) { - return Qnil; - } - - if (rb_block_given_p()) { - return rb_ensure(rb_yield, obj, fgdbm_close, obj); - } - - return obj; -} - -static VALUE -rb_gdbm_fetch(dbm, key) - GDBM_FILE dbm; - datum key; -{ - datum val; - VALUE str; - - val = gdbm_fetch(dbm, key); - if (val.dptr == 0) - return Qnil; - - str = rb_obj_alloc(rb_cString); - RSTRING(str)->len = val.dsize; - RSTRING(str)->aux.capa = val.dsize; - RSTRING(str)->ptr = REALLOC_N(val.dptr,char,val.dsize+1); - RSTRING(str)->ptr[val.dsize] = '\0'; - - OBJ_TAINT(str); - return (VALUE)str; -} - -static VALUE -rb_gdbm_fetch2(dbm, keystr) - GDBM_FILE dbm; - VALUE keystr; -{ - datum key; - - StringValue(keystr); - key.dptr = RSTRING(keystr)->ptr; - key.dsize = RSTRING(keystr)->len; - - return rb_gdbm_fetch(dbm, key); -} - -static VALUE -rb_gdbm_fetch3(obj, keystr) - VALUE obj, keystr; -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - - GetDBM(obj, dbmp); - dbm = dbmp->di_dbm; - return rb_gdbm_fetch2(dbm, keystr); -} - -static VALUE -rb_gdbm_firstkey(dbm) - GDBM_FILE dbm; -{ - datum key; - VALUE str; - - key = gdbm_firstkey(dbm); - if (key.dptr == 0) - return Qnil; - - str = rb_obj_alloc(rb_cString); - RSTRING(str)->len = key.dsize; - RSTRING(str)->aux.capa = key.dsize; - RSTRING(str)->ptr = REALLOC_N(key.dptr,char,key.dsize+1); - RSTRING(str)->ptr[RSTRING(str)->len] = '\0'; - - OBJ_TAINT(str); - return str; -} - -static VALUE -rb_gdbm_nextkey(dbm, keystr) - GDBM_FILE dbm; - VALUE keystr; -{ - datum key, key2; - VALUE str; - - key.dptr = RSTRING(keystr)->ptr; - key.dsize = RSTRING(keystr)->len; - key2 = gdbm_nextkey(dbm, key); - if (key2.dptr == 0) - return Qnil; - - str = rb_obj_alloc(rb_cString); - RSTRING(str)->len = key2.dsize; - RSTRING(str)->aux.capa = key2.dsize; - RSTRING(str)->ptr = REALLOC_N(key2.dptr,char,key2.dsize+1); - RSTRING(str)->ptr[RSTRING(str)->len] = '\0'; - - OBJ_TAINT(str); - return str; -} - -static VALUE -fgdbm_fetch(obj, keystr, ifnone) - VALUE obj, keystr, ifnone; -{ - VALUE valstr; - - valstr = rb_gdbm_fetch3(obj, keystr); - if (NIL_P(valstr)) { - if (ifnone == Qnil && rb_block_given_p()) - return rb_yield(keystr); - return ifnone; - } - return valstr; -} - -static VALUE -fgdbm_aref(obj, keystr) - VALUE obj, keystr; -{ - return rb_gdbm_fetch3(obj, keystr); -} - -static VALUE -fgdbm_fetch_m(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; -{ - VALUE keystr, valstr, ifnone; - - rb_scan_args(argc, argv, "11", &keystr, &ifnone); - valstr = fgdbm_fetch(obj, keystr, ifnone); - if (argc == 1 && !rb_block_given_p() && NIL_P(valstr)) - rb_raise(rb_eIndexError, "key not found"); - - return valstr; -} - -static VALUE -fgdbm_index(obj, valstr) - VALUE obj, valstr; -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - VALUE keystr, valstr2; - - StringValue(valstr); - GetDBM(obj, dbmp); - dbm = dbmp->di_dbm; - for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr); - keystr = rb_gdbm_nextkey(dbm, keystr)) { - - valstr2 = rb_gdbm_fetch2(dbm, keystr); - if (!NIL_P(valstr2) && - RSTRING(valstr)->len == RSTRING(valstr2)->len && - memcmp(RSTRING(valstr)->ptr, RSTRING(valstr2)->ptr, - RSTRING(valstr)->len) == 0) { - return keystr; - } - } - return Qnil; -} - -static VALUE -fgdbm_indexes(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; -{ - VALUE new; - int i; - - new = rb_ary_new2(argc); - for (i=0; i<argc; i++) { - rb_ary_push(new, rb_gdbm_fetch3(obj, argv[i])); - } - - return new; -} - -static VALUE -fgdbm_select(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; -{ - VALUE new = rb_ary_new2(argc); - int i; - - if (rb_block_given_p()) { - GDBM_FILE dbm; - struct dbmdata *dbmp; - VALUE keystr; - - if (argc > 0) { - rb_raise(rb_eArgError, "wrong number arguments(%d for 0)", argc); - } - GetDBM(obj, dbmp); - dbm = dbmp->di_dbm; - - for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr); - keystr = rb_gdbm_nextkey(dbm, keystr)) { - VALUE assoc = rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr)); - - if (RTEST(rb_yield(assoc))) - rb_ary_push(new, assoc); - } - } - else { - for (i=0; i<argc; i++) { - rb_ary_push(new, rb_gdbm_fetch3(obj, argv[i])); - } - } - - return new; -} - -static VALUE -rb_gdbm_delete(obj, keystr) - VALUE obj, keystr; -{ - datum key; - struct dbmdata *dbmp; - GDBM_FILE dbm; - - rb_secure(4); - StringValue(keystr); - key.dptr = RSTRING(keystr)->ptr; - key.dsize = RSTRING(keystr)->len; - - GetDBM(obj, dbmp); - dbm = dbmp->di_dbm; - - if (!gdbm_exists(dbm, key)) { - return Qnil; - } - - if (gdbm_delete(dbm, key)) { - dbmp->di_size = -1; - rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno)); - } - else if (dbmp->di_size >= 0) { - dbmp->di_size--; - } - return obj; -} - -static VALUE -fgdbm_delete(obj, keystr) - VALUE obj, keystr; -{ - VALUE valstr; - - valstr = fgdbm_fetch(obj, keystr, Qnil); - rb_gdbm_delete(obj, keystr); - return valstr; -} - -static VALUE -fgdbm_shift(obj) - VALUE obj; -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - VALUE keystr, valstr; - - rb_secure(4); - GetDBM(obj, dbmp); - dbm = dbmp->di_dbm; - - keystr = rb_gdbm_firstkey(dbm); - if (NIL_P(keystr)) return Qnil; - valstr = rb_gdbm_fetch2(dbm, keystr); - rb_gdbm_delete(obj, keystr); - - return rb_assoc_new(keystr, valstr); -} - -static VALUE -fgdbm_delete_if(obj) - VALUE obj; -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - VALUE keystr, valstr; - VALUE ret, ary = rb_ary_new(); - int i, status = 0, n; - - rb_secure(4); - GetDBM(obj, dbmp); - dbm = dbmp->di_dbm; - n = dbmp->di_size; - dbmp->di_size = -1; - - for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr); - keystr = rb_gdbm_nextkey(dbm, keystr)) { - - valstr = rb_gdbm_fetch2(dbm, keystr); - ret = rb_protect(rb_yield, rb_assoc_new(keystr, valstr), &status); - if (status != 0) break; - if (RTEST(ret)) rb_ary_push(ary, keystr); - } - - for (i = 0; i < RARRAY(ary)->len; i++) - rb_gdbm_delete(obj, RARRAY(ary)->ptr[i]); - if (status) rb_jump_tag(status); - if (n > 0) dbmp->di_size = n - RARRAY(ary)->len; - - return obj; -} - -static VALUE -fgdbm_clear(obj) - VALUE obj; -{ - datum key, nextkey; - struct dbmdata *dbmp; - GDBM_FILE dbm; - - rb_secure(4); - GetDBM(obj, dbmp); - dbm = dbmp->di_dbm; - dbmp->di_size = -1; - -#if 0 - while (key = gdbm_firstkey(dbm), key.dptr) { - if (gdbm_delete(dbm, key)) { - free(key.dptr); - rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno)); - } - free(key.dptr); - } -#else - while (key = gdbm_firstkey(dbm), key.dptr) { - for (; key.dptr; key = nextkey) { - nextkey = gdbm_nextkey(dbm, key); - if (gdbm_delete(dbm, key)) { - free(key.dptr); - if (nextkey.dptr) free(nextkey.dptr); - rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno)); - } - free(key.dptr); - } - } -#endif - dbmp->di_size = 0; - - return obj; -} - -static VALUE -fgdbm_invert(obj) - VALUE obj; -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - VALUE keystr, valstr; - VALUE hash = rb_hash_new(); - - GetDBM(obj, dbmp); - dbm = dbmp->di_dbm; - for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr); - keystr = rb_gdbm_nextkey(dbm, keystr)) { - valstr = rb_gdbm_fetch2(dbm, keystr); - - rb_hash_aset(hash, valstr, keystr); - } - return hash; -} - -static VALUE -each_pair(obj) - VALUE obj; -{ - return rb_funcall(obj, rb_intern("each_pair"), 0, 0); -} - -static VALUE fgdbm_store _((VALUE,VALUE,VALUE)); - -static VALUE -update_i(pair, dbm) - VALUE pair, dbm; -{ - Check_Type(pair, T_ARRAY); - if (RARRAY(pair)->len < 2) { - rb_raise(rb_eArgError, "pair must be [key, value]"); - } - fgdbm_store(dbm, RARRAY(pair)->ptr[0], RARRAY(pair)->ptr[1]); - return Qnil; -} - -static VALUE -fgdbm_update(obj, other) - VALUE obj, other; -{ - rb_iterate(each_pair, other, update_i, obj); - return obj; -} - -static VALUE -fgdbm_replace(obj, other) - VALUE obj, other; -{ - fgdbm_clear(obj); - rb_iterate(each_pair, other, update_i, obj); - return obj; -} - -static VALUE -fgdbm_store(obj, keystr, valstr) - VALUE obj, keystr, valstr; -{ - datum key, val; - struct dbmdata *dbmp; - GDBM_FILE dbm; - - rb_secure(4); - StringValue(keystr); - key.dptr = RSTRING(keystr)->ptr; - key.dsize = RSTRING(keystr)->len; - - StringValue(valstr); - val.dptr = RSTRING(valstr)->ptr; - val.dsize = RSTRING(valstr)->len; - - GetDBM(obj, dbmp); - dbmp->di_size = -1; - dbm = dbmp->di_dbm; - if (gdbm_store(dbm, key, val, GDBM_REPLACE)) { - if (errno == EPERM) rb_sys_fail(0); - rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno)); - } - - return valstr; -} - -static VALUE -fgdbm_length(obj) - VALUE obj; -{ - datum key, nextkey; - struct dbmdata *dbmp; - GDBM_FILE dbm; - int i = 0; - - GetDBM(obj, dbmp); - if (dbmp->di_size > 0) return INT2FIX(dbmp->di_size); - dbm = dbmp->di_dbm; - - for (key = gdbm_firstkey(dbm); key.dptr; key = nextkey) { - nextkey = gdbm_nextkey(dbm, key); - free(key.dptr); - i++; - } - dbmp->di_size = i; - - return INT2FIX(i); -} - -static VALUE -fgdbm_empty_p(obj) - VALUE obj; -{ - datum key; - struct dbmdata *dbmp; - GDBM_FILE dbm; - - GetDBM(obj, dbmp); - if (dbmp->di_size < 0) { - dbm = dbmp->di_dbm; - - key = gdbm_firstkey(dbm); - if (key.dptr) { - free(key.dptr); - return Qfalse; - } - return Qtrue; - } - - if (dbmp->di_size == 0) return Qtrue; - return Qfalse; -} - -static VALUE -fgdbm_each_value(obj) - VALUE obj; -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - VALUE keystr; - - GetDBM(obj, dbmp); - dbm = dbmp->di_dbm; - - for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr); - keystr = rb_gdbm_nextkey(dbm, keystr)) { - - rb_yield(rb_gdbm_fetch2(dbm, keystr)); - } - return obj; -} - -static VALUE -fgdbm_each_key(obj) - VALUE obj; -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - VALUE keystr; - - GetDBM(obj, dbmp); - dbm = dbmp->di_dbm; - - for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr); - keystr = rb_gdbm_nextkey(dbm, keystr)) { - - rb_yield(keystr); - } - return obj; -} - -static VALUE -fgdbm_each_pair(obj) - VALUE obj; -{ - GDBM_FILE dbm; - struct dbmdata *dbmp; - VALUE keystr; - - GetDBM(obj, dbmp); - dbm = dbmp->di_dbm; - - for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr); - keystr = rb_gdbm_nextkey(dbm, keystr)) { - - rb_yield(rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr))); - } - - return obj; -} - -static VALUE -fgdbm_keys(obj) - VALUE obj; -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - VALUE keystr, ary; - - GetDBM(obj, dbmp); - dbm = dbmp->di_dbm; - - ary = rb_ary_new(); - for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr); - keystr = rb_gdbm_nextkey(dbm, keystr)) { - - rb_ary_push(ary, keystr); - } - - return ary; -} - -static VALUE -fgdbm_values(obj) - VALUE obj; -{ - datum key, nextkey; - struct dbmdata *dbmp; - GDBM_FILE dbm; - VALUE valstr, ary; - - GetDBM(obj, dbmp); - dbm = dbmp->di_dbm; - - ary = rb_ary_new(); - for (key = gdbm_firstkey(dbm); key.dptr; key = nextkey) { - nextkey = gdbm_nextkey(dbm, key); - valstr = rb_gdbm_fetch(dbm, key); - free(key.dptr); - rb_ary_push(ary, valstr); - } - - return ary; -} - -static VALUE -fgdbm_has_key(obj, keystr) - VALUE obj, keystr; -{ - datum key; - struct dbmdata *dbmp; - GDBM_FILE dbm; - - StringValue(keystr); - key.dptr = RSTRING(keystr)->ptr; - key.dsize = RSTRING(keystr)->len; - - GetDBM(obj, dbmp); - dbm = dbmp->di_dbm; - if (gdbm_exists(dbm, key)) - return Qtrue; - return Qfalse; -} - -static VALUE -fgdbm_has_value(obj, valstr) - VALUE obj, valstr; -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - VALUE keystr, valstr2; - - StringValue(valstr); - GetDBM(obj, dbmp); - dbm = dbmp->di_dbm; - for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr); - keystr = rb_gdbm_nextkey(dbm, keystr)) { - - valstr2 = rb_gdbm_fetch2(dbm, keystr); - - if (!NIL_P(valstr2) && - RSTRING(valstr)->len == RSTRING(valstr2)->len && - memcmp(RSTRING(valstr)->ptr, RSTRING(valstr2)->ptr, - RSTRING(valstr)->len) == 0) { - return Qtrue; - } - } - return Qfalse; -} - -static VALUE -fgdbm_to_a(obj) - VALUE obj; -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - VALUE keystr, ary; - - GetDBM(obj, dbmp); - dbm = dbmp->di_dbm; - - ary = rb_ary_new(); - for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr); - keystr = rb_gdbm_nextkey(dbm, keystr)) { - - rb_ary_push(ary, rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr))); - } - - return ary; -} - -static VALUE -fgdbm_reorganize(obj) - VALUE obj; -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - - rb_secure(4); - GetDBM(obj, dbmp); - dbm = dbmp->di_dbm; - gdbm_reorganize(dbm); - return obj; -} - -static VALUE -fgdbm_sync(obj) - VALUE obj; -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - - rb_secure(4); - GetDBM(obj, dbmp); - dbm = dbmp->di_dbm; - gdbm_sync(dbm); - return obj; -} - -static VALUE -fgdbm_set_cachesize(obj, val) - VALUE obj, val; -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - int optval; - - GetDBM(obj, dbmp); - dbm = dbmp->di_dbm; - - optval = FIX2INT(val); - if (gdbm_setopt(dbm, GDBM_CACHESIZE, &optval, sizeof(optval)) == -1) { - rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno)); - } - return val; -} - -static VALUE -fgdbm_set_fastmode(obj, val) - VALUE obj, val; -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - int optval; - - GetDBM(obj, dbmp); - dbm = dbmp->di_dbm; - - optval = 0; - if (RTEST(val)) - optval = 1; - - if (gdbm_setopt(dbm, GDBM_FASTMODE, &optval, sizeof(optval)) == -1) { - rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno)); - } - return val; -} - -static VALUE -fgdbm_set_syncmode(obj, val) - VALUE obj, val; -{ -#if !defined(GDBM_SYNCMODE) - fgdbm_set_fastmode(obj, RTEST(val) ? Qfalse : Qtrue); - return val; -#else - struct dbmdata *dbmp; - GDBM_FILE dbm; - int optval; - - GetDBM(obj, dbmp); - dbm = dbmp->di_dbm; - - optval = 0; - if (RTEST(val)) - optval = 1; - - if (gdbm_setopt(dbm, GDBM_FASTMODE, &optval, sizeof(optval)) == -1) { - rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno)); - } - return val; -#endif -} - -static VALUE -fgdbm_to_hash(obj) - VALUE obj; -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - VALUE keystr, hash; - - GetDBM(obj, dbmp); - dbm = dbmp->di_dbm; - - hash = rb_hash_new(); - for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr); - keystr = rb_gdbm_nextkey(dbm, keystr)) { - - rb_hash_aset(hash, keystr, rb_gdbm_fetch2(dbm, keystr)); - } - - return hash; -} - -static VALUE -fgdbm_reject(obj) - VALUE obj; -{ - return rb_hash_delete_if(fgdbm_to_hash(obj)); -} - -void -Init_gdbm() -{ - rb_cGDBM = rb_define_class("GDBM", rb_cObject); - rb_eGDBMError = rb_define_class("GDBMError", rb_eStandardError); - rb_eGDBMFatalError = rb_define_class("GDBMFatalError", rb_eException); - rb_include_module(rb_cGDBM, rb_mEnumerable); - - rb_define_singleton_method(rb_cGDBM, "new", fgdbm_s_new, -1); - rb_define_singleton_method(rb_cGDBM, "open", fgdbm_s_open, -1); - - rb_define_method(rb_cGDBM, "initialize", fgdbm_initialize, -1); - rb_define_method(rb_cGDBM, "close", fgdbm_close, 0); - rb_define_method(rb_cGDBM, "[]", fgdbm_aref, 1); - rb_define_method(rb_cGDBM, "fetch", fgdbm_fetch_m, -1); - rb_define_method(rb_cGDBM, "[]=", fgdbm_store, 2); - rb_define_method(rb_cGDBM, "store", fgdbm_store, 2); - rb_define_method(rb_cGDBM, "index", fgdbm_index, 1); - rb_define_method(rb_cGDBM, "indexes", fgdbm_indexes, -1); - rb_define_method(rb_cGDBM, "indices", fgdbm_indexes, -1); - rb_define_method(rb_cGDBM, "select", fgdbm_select, -1); - rb_define_method(rb_cGDBM, "length", fgdbm_length, 0); - rb_define_method(rb_cGDBM, "size", fgdbm_length, 0); - rb_define_method(rb_cGDBM, "empty?", fgdbm_empty_p, 0); - rb_define_method(rb_cGDBM, "each", fgdbm_each_pair, 0); - rb_define_method(rb_cGDBM, "each_value", fgdbm_each_value, 0); - rb_define_method(rb_cGDBM, "each_key", fgdbm_each_key, 0); - rb_define_method(rb_cGDBM, "each_pair", fgdbm_each_pair, 0); - rb_define_method(rb_cGDBM, "keys", fgdbm_keys, 0); - rb_define_method(rb_cGDBM, "values", fgdbm_values, 0); - rb_define_method(rb_cGDBM, "shift", fgdbm_shift, 0); - rb_define_method(rb_cGDBM, "delete", fgdbm_delete, 1); - rb_define_method(rb_cGDBM, "delete_if", fgdbm_delete_if, 0); - rb_define_method(rb_cGDBM, "reject!", fgdbm_delete_if, 0); - rb_define_method(rb_cGDBM, "reject", fgdbm_reject, 0); - rb_define_method(rb_cGDBM, "clear", fgdbm_clear, 0); - rb_define_method(rb_cGDBM,"invert", fgdbm_invert, 0); - rb_define_method(rb_cGDBM,"update", fgdbm_update, 1); - rb_define_method(rb_cGDBM,"replace", fgdbm_replace, 1); - rb_define_method(rb_cGDBM,"reorganize", fgdbm_reorganize, 0); - rb_define_method(rb_cGDBM,"sync", fgdbm_sync, 0); - /* rb_define_method(rb_cGDBM,"setopt", fgdbm_setopt, 2); */ - rb_define_method(rb_cGDBM,"cachesize=", fgdbm_set_cachesize, 1); - rb_define_method(rb_cGDBM,"fastmode=", fgdbm_set_fastmode, 1); - rb_define_method(rb_cGDBM,"syncmode=", fgdbm_set_syncmode, 1); - - rb_define_method(rb_cGDBM, "include?", fgdbm_has_key, 1); - rb_define_method(rb_cGDBM, "has_key?", fgdbm_has_key, 1); - rb_define_method(rb_cGDBM, "member?", fgdbm_has_key, 1); - rb_define_method(rb_cGDBM, "has_value?", fgdbm_has_value, 1); - rb_define_method(rb_cGDBM, "key?", fgdbm_has_key, 1); - rb_define_method(rb_cGDBM, "value?", fgdbm_has_value, 1); - - rb_define_method(rb_cGDBM, "to_a", fgdbm_to_a, 0); - rb_define_method(rb_cGDBM, "to_hash", fgdbm_to_hash, 0); - - /* flags for gdbm_opn() */ - /* - rb_define_const(rb_cGDBM, "READER", INT2FIX(GDBM_READER)); - rb_define_const(rb_cGDBM, "WRITER", INT2FIX(GDBM_WRITER)); - rb_define_const(rb_cGDBM, "WRCREAT", INT2FIX(GDBM_WRCREAT)); - rb_define_const(rb_cGDBM, "NEWDB", INT2FIX(GDBM_NEWDB)); - */ - rb_define_const(rb_cGDBM, "FAST", INT2FIX(GDBM_FAST)); - /* this flag is obsolete in gdbm 1.8. - On gdbm 1.8, fast mode is default behavior. */ - - /* gdbm version 1.8 specific */ -#if defined(GDBM_SYNC) - rb_define_const(rb_cGDBM, "SYNC", INT2FIX(GDBM_SYNC)); -#endif -#if defined(GDBM_NOLOCK) - rb_define_const(rb_cGDBM, "NOLOCK", INT2FIX(GDBM_NOLOCK)); -#endif - rb_define_const(rb_cGDBM, "VERSION", rb_str_new2(gdbm_version)); -} diff --git a/ext/gdbm/testgdbm.rb b/ext/gdbm/testgdbm.rb deleted file mode 100644 index 524d3f8ca3..0000000000 --- a/ext/gdbm/testgdbm.rb +++ /dev/null @@ -1,663 +0,0 @@ -require 'runit/testcase' -require 'runit/cui/testrunner' - -if $".grep(/\bgdbm.so\b/).empty? - begin - require './gdbm' - rescue LoadError - require 'gdbm' - end -end - -def uname_s - require 'rbconfig' - case Config::CONFIG['host_os'] - when 'cygwin' - require 'Win32API' - uname = Win32API.new 'cygwin1', 'uname', 'P', 'I' - utsname = ' ' * 100 - raise 'cannot get system name' if uname.call(utsname) == -1 - - utsname.unpack('A20' * 5)[0] - else - Config::CONFIG['host_os'] - end -end - -SYSTEM = uname_s - -class TestGDBM < RUNIT::TestCase - def setup - @path = "tmptest_gdbm_" - assert_instance_of(GDBM, @gdbm = GDBM.new(@path)) - - # prepare to make readonly GDBM file - GDBM.open("tmptest_gdbm_rdonly", 0400) {|gdbm| - gdbm['foo'] = 'FOO' - } - assert_instance_of(GDBM, @gdbm_rdonly = GDBM.new("tmptest_gdbm_rdonly", nil)) - end - def teardown - assert_nil(@gdbm.close) - assert_nil(@gdbm_rdonly.close) - GC.start - File.delete *Dir.glob("tmptest_gdbm*").to_a - p Dir.glob("tmptest_gdbm*") if $DEBUG - end - - def check_size(expect, gdbm=@gdbm) - assert_equals(expect, gdbm.size) - n = 0 - gdbm.each { n+=1 } - assert_equals(expect, n) - if expect == 0 - assert_equals(true, gdbm.empty?) - else - assert_equals(false, gdbm.empty?) - end - end - - def test_version - STDERR.print GDBM::VERSION - end - - def test_s_new_has_no_block - # GDBM.new ignore the block - foo = true - assert_instance_of(GDBM, gdbm = GDBM.new("tmptest_gdbm") { foo = false }) - assert_equals(foo, true) - assert_nil(gdbm.close) - end - def test_s_open_create_new - return if /^CYGWIN_9/ =~ SYSTEM - - save_mask = File.umask(0) - begin - assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm")) - gdbm.close - assert_equals(File.stat("tmptest_gdbm").mode & 0777, 0666) - assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm2", 0644)) - gdbm.close - assert_equals(File.stat("tmptest_gdbm2").mode & 0777, 0644) - ensure - File.umask save_mask - end - end - def test_s_open_no_create - # this test is failed on libgdbm 1.8.0 - assert_nil(gdbm = GDBM.open("tmptest_gdbm", nil)) - ensure - gdbm.close if gdbm - end - def test_s_open_3rd_arg - assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644, - GDBM::FAST)) - gdbm.close - - # gdbm 1.8.0 specific - if defined? GDBM::SYNC - assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644, - GDBM::SYNC)) - gdbm.close - end - # gdbm 1.8.0 specific - if defined? GDBM::NOLOCK - assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644, - GDBM::NOLOCK)) - gdbm.close - end - end - def test_s_open_with_block - assert_equals(GDBM.open("tmptest_gdbm") { :foo }, :foo) - end - def test_s_open_lock - fork() { - assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644)) - sleep 2 - } - begin - sleep 1 - assert_exception(Errno::EWOULDBLOCK) { - begin - assert_instance_of(GDBM, gdbm2 = GDBM.open("tmptest_gdbm", 0644)) - rescue Errno::EAGAIN, Errno::EACCES - raise Errno::EWOULDBLOCK - end - } - ensure - Process.wait - end - end - -=begin - # Is it guaranteed on many OS? - def test_s_open_lock_one_process - # locking on one process - assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644)) - assert_exception(Errno::EWOULDBLOCK) { - begin - GDBM.open("tmptest_gdbm", 0644) - rescue Errno::EAGAIN - raise Errno::EWOULDBLOCK - end - } - end -=end - - def test_s_open_nolock - # gdbm 1.8.0 specific - if not defined? GDBM::NOLOCK - return - end - - fork() { - assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644, - GDBM::NOLOCK)) - sleep 2 - } - sleep 1 - begin - gdbm2 = nil - assert_no_exception(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) { - assert_instance_of(GDBM, gdbm2 = GDBM.open("tmptest_gdbm", 0644)) - } - ensure - Process.wait - gdbm2.close if gdbm2 - end - - p Dir.glob("tmptest_gdbm*") if $DEBUG - - fork() { - assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644)) - sleep 2 - } - begin - sleep 1 - gdbm2 = nil - assert_no_exception(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) { - # this test is failed on Cygwin98 (???) - assert_instance_of(GDBM, gdbm2 = GDBM.open("tmptest_gdbm", 0644, - GDBM::NOLOCK)) - } - ensure - Process.wait - gdbm2.close if gdbm2 - end - end - - def test_s_open_error - assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0)) - assert_exception(Errno::EACCES) { - GDBM.open("tmptest_gdbm", 0) - } - gdbm.close - end - - def test_close - assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm")) - assert_nil(gdbm.close) - - # closed GDBM file - assert_exception(RuntimeError) { gdbm.close } - end - - def test_aref - assert_equals('bar', @gdbm['foo'] = 'bar') - assert_equals('bar', @gdbm['foo']) - - assert_nil(@gdbm['bar']) - end - - def test_fetch - assert_equals('bar', @gdbm['foo']='bar') - assert_equals('bar', @gdbm.fetch('foo')) - - # key not found - assert_exception(IndexError) { - @gdbm.fetch('bar') - } - - # test for `ifnone' arg - assert_equals('baz', @gdbm.fetch('bar', 'baz')) - - # test for `ifnone' block - assert_equals('foobar', @gdbm.fetch('bar') {|key| 'foo' + key }) - end - - def test_aset - num = 0 - 2.times {|i| - assert_equals('foo', @gdbm['foo'] = 'foo') - assert_equals('foo', @gdbm['foo']) - assert_equals('bar', @gdbm['foo'] = 'bar') - assert_equals('bar', @gdbm['foo']) - - num += 1 if i == 0 - assert_equals(num, @gdbm.size) - - # assign nil - assert_equals('', @gdbm['bar'] = '') - assert_equals('', @gdbm['bar']) - - num += 1 if i == 0 - assert_equals(num, @gdbm.size) - - # empty string - assert_equals('', @gdbm[''] = '') - assert_equals('', @gdbm['']) - - num += 1 if i == 0 - assert_equals(num, @gdbm.size) - - # Fixnum - assert_equals('200', @gdbm['100'] = '200') - assert_equals('200', @gdbm['100']) - - num += 1 if i == 0 - assert_equals(num, @gdbm.size) - - # Big key and value - assert_equals('y' * 100, @gdbm['x' * 100] = 'y' * 100) - assert_equals('y' * 100, @gdbm['x' * 100]) - - num += 1 if i == 0 - assert_equals(num, @gdbm.size) - } - end - - def test_index - assert_equals('bar', @gdbm['foo'] = 'bar') - assert_equals('foo', @gdbm.index('bar')) - assert_nil(@gdbm['bar']) - end - - def test_indexes - keys = %w(foo bar baz) - values = %w(FOO BAR BAZ) - @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values - assert_equals(values.reverse, @gdbm.indexes(*keys.reverse)) - end - - def test_select - keys = %w(foo bar baz) - values = %w(FOO BAR BAZ) - @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values - assert_equals(values.reverse, @gdbm.select(*keys.reverse)) - end - - def test_select_with_block - keys = %w(foo bar baz) - values = %w(FOO BAR BAZ) - @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values - ret = @gdbm.select {|k,v| - assert_equals(k.upcase, v) - k != "bar" - } - assert_equals([['baz', 'BAZ'], ['foo', 'FOO']], - ret.sort) - end - - def test_length - num = 10 - assert_equals(0, @gdbm.size) - num.times {|i| - i = i.to_s - @gdbm[i] = i - } - assert_equals(num, @gdbm.size) - - @gdbm.shift - - assert_equals(num - 1, @gdbm.size) - end - - def test_empty? - assert_equals(true, @gdbm.empty?) - @gdbm['foo'] = 'FOO' - assert_equals(false, @gdbm.empty?) - end - - def test_each_pair - n = 0 - @gdbm.each_pair { n += 1 } - assert_equals(0, n) - - keys = %w(foo bar baz) - values = %w(FOO BAR BAZ) - - @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values - - n = 0 - ret = @gdbm.each_pair {|key, val| - assert_not_nil(i = keys.index(key)) - assert_equals(val, values[i]) - - n += 1 - } - assert_equals(keys.size, n) - assert_equals(@gdbm, ret) - end - - def test_each_value - n = 0 - @gdbm.each_value { n += 1 } - assert_equals(0, n) - - keys = %w(foo bar baz) - values = %w(FOO BAR BAZ) - - @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values - - n = 0 - ret = @gdbm.each_value {|val| - assert_not_nil(key = @gdbm.index(val)) - assert_not_nil(i = keys.index(key)) - assert_equals(val, values[i]) - - n += 1 - } - assert_equals(keys.size, n) - assert_equals(@gdbm, ret) - end - - def test_each_key - n = 0 - @gdbm.each_key { n += 1 } - assert_equals(0, n) - - keys = %w(foo bar baz) - values = %w(FOO BAR BAZ) - - @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values - - n = 0 - ret = @gdbm.each_key {|key| - assert_not_nil(i = keys.index(key)) - assert_equals(@gdbm[key], values[i]) - - n += 1 - } - assert_equals(keys.size, n) - assert_equals(@gdbm, ret) - end - - def test_keys - assert_equals([], @gdbm.keys) - - keys = %w(foo bar baz) - values = %w(FOO BAR BAZ) - - @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values - - assert_equals(keys.sort, @gdbm.keys.sort) - assert_equals(values.sort, @gdbm.values.sort) - end - - def test_values - test_keys - end - - def test_shift - assert_nil(@gdbm.shift) - assert_equals(0, @gdbm.size) - - keys = %w(foo bar baz) - values = %w(FOO BAR BAZ) - - @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values - - ret_keys = [] - ret_values = [] - while ret = @gdbm.shift - ret_keys.push ret[0] - ret_values.push ret[1] - - assert_equals(keys.size - ret_keys.size, @gdbm.size) - end - - assert_equals(keys.sort, ret_keys.sort) - assert_equals(values.sort, ret_values.sort) - end - - def test_delete - keys = %w(foo bar baz) - values = %w(FOO BAR BAZ) - key = keys[1] - - assert_nil(@gdbm.delete(key)) - assert_equals(0, @gdbm.size) - - @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values - - assert_equals('BAR', @gdbm.delete(key)) - assert_nil(@gdbm[key]) - assert_equals(2, @gdbm.size) - - assert_nil(@gdbm.delete(key)) - - if /^CYGWIN_9/ !~ SYSTEM - assert_exception(GDBMError) { - @gdbm_rdonly.delete("foo") - } - - assert_nil(@gdbm_rdonly.delete("bar")) - end - end - def test_delete_with_block - key = 'no called block' - @gdbm[key] = 'foo' - assert_equals('foo', @gdbm.delete(key) {|k| k.replace 'called block'}) - assert_equals('no called block', key) - assert_equals(0, @gdbm.size) - - key = 'no called block' - assert_equals(:blockval, - @gdbm.delete(key) {|k| k.replace 'called block'; :blockval}) - assert_equals('called block', key) - assert_equals(0, @gdbm.size) - end - - def test_delete_if - v = "0" - 100.times {@gdbm[v] = v; v = v.next} - - ret = @gdbm.delete_if {|key, val| key.to_i < 50} - assert_equals(@gdbm, ret) - check_size(50, @gdbm) - - ret = @gdbm.delete_if {|key, val| key.to_i >= 50} - assert_equals(@gdbm, ret) - check_size(0, @gdbm) - - # break - v = "0" - 100.times {@gdbm[v] = v; v = v.next} - check_size(100, @gdbm) - n = 0; - @gdbm.delete_if {|key, val| - break if n > 50 - n+=1 - true - } - assert_equals(51, n) - check_size(49, @gdbm) - - @gdbm.clear - - # raise - v = "0" - 100.times {@gdbm[v] = v; v = v.next} - check_size(100, @gdbm) - n = 0; - begin - @gdbm.delete_if {|key, val| - raise "runtime error" if n > 50 - n+=1 - true - } - rescue - end - assert_equals(51, n) - check_size(49, @gdbm) - end - - def test_reject - v = "0" - 100.times {@gdbm[v] = v; v = v.next} - - hash = @gdbm.reject {|key, val| key.to_i < 50} - assert_instance_of(Hash, hash) - assert_equals(100, @gdbm.size) - - assert_equals(50, hash.size) - hash.each_pair {|key,val| - assert_equals(false, key.to_i < 50) - assert_equals(key, val) - } - - hash = @gdbm.reject {|key, val| key.to_i < 100} - assert_instance_of(Hash, hash) - assert_equals(true, hash.empty?) - end - - def test_clear - v = "1" - 100.times {v = v.next; @gdbm[v] = v} - - assert_equals(@gdbm, @gdbm.clear) - - # validate GDBM#size - i = 0 - @gdbm.each { i += 1 } - assert_equals(@gdbm.size, i) - assert_equals(0, i) - end - - def test_invert - v = "0" - 100.times {@gdbm[v] = v; v = v.next} - - hash = @gdbm.invert - assert_instance_of(Hash, hash) - assert_equals(100, hash.size) - hash.each_pair {|key, val| - assert_equals(key.to_i, val.to_i) - } - end - - def test_update - hash = {} - v = "0" - 100.times {v = v.next; hash[v] = v} - - @gdbm["101"] = "101" - @gdbm.update hash - assert_equals(101, @gdbm.size) - @gdbm.each_pair {|key, val| - assert_equals(key.to_i, val.to_i) - } - end - - def test_replace - hash = {} - v = "0" - 100.times {v = v.next; hash[v] = v} - - @gdbm["101"] = "101" - @gdbm.replace hash - assert_equals(100, @gdbm.size) - @gdbm.each_pair {|key, val| - assert_equals(key.to_i, val.to_i) - } - end - - def test_reorganize - size1 = File.size(@path) - i = "1" - 1000.times {i = i.next; @gdbm[i] = i} - @gdbm.clear - @gdbm.sync - - size2 = File.size(@path) - @gdbm.reorganize - size3 = File.size(@path) - - # p [size1, size2, size3] - assert_equals(true, size1 < size2) - # this test is failed on Cygwin98. `GDBM version 1.8.0, as of May 19, 1999' - assert_equals(true, size3 < size2) - assert_equals(size1, size3) - end - - def test_sync - assert_instance_of(GDBM, gdbm = GDBM.open('tmptest_gdbm', 0666, GDBM::FAST)) - assert_equals(gdbm.sync, gdbm) - gdbm.close - assert_instance_of(GDBM, gdbm = GDBM.open('tmptest_gdbm', 0666)) - assert_equals(gdbm.sync, gdbm) - gdbm.close - end - - def test_cachesize= - assert_equals(@gdbm.cachesize = 1024, 1024) - end - - def test_fastmode= - assert_equals(@gdbm.fastmode = true, true) - end - - def test_syncmode= - assert_equals(@gdbm.syncmode = true, true) - end - - def test_haskey? - assert_equals('bar', @gdbm['foo']='bar') - assert_equals(true, @gdbm.has_key?('foo')) - assert_equals(false, @gdbm.has_key?('bar')) - end - - def test_has_value? - assert_equals('bar', @gdbm['foo']='bar') - assert_equals(true, @gdbm.has_value?('bar')) - assert_equals(false, @gdbm.has_value?('foo')) - end - - def test_to_a - v = "0" - 100.times {v = v.next; @gdbm[v] = v} - - ary = @gdbm.to_a - assert_instance_of(Array, ary) - assert_equals(100, ary.size) - ary.each {|key,val| - assert_equals(key.to_i, val.to_i) - } - end - - def test_to_hash - v = "0" - 100.times {v = v.next; @gdbm[v] = v} - - hash = @gdbm.to_hash - assert_instance_of(Hash, hash) - assert_equals(100, hash.size) - hash.each {|key,val| - assert_equals(key.to_i, val.to_i) - } - end -end - -if $0 == __FILE__ - if ARGV.size == 0 - suite = RUNIT::TestSuite.new - suite.add_test(TestGDBM.suite) - else - suite = RUNIT::TestSuite.new - ARGV.each do |testmethod| - suite.add_test(TestGDBM.new(testmethod)) - end - end - - RUNIT::CUI::TestRunner.run(suite) -end |