From 5bcbd1d5d3e74f07220cbda4014d408f97447e63 Mon Sep 17 00:00:00 2001 From: guy Decoux Date: Sat, 28 Feb 2009 20:20:42 +0100 Subject: mmap-0.2.5 --- b.rb | 0 doc/b.rb | 109 ++++++++++++++++ doc/mmap.rb | 396 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ docs/b.rb | 109 ---------------- docs/mmap.rb | 396 -------------------------------------------------------- ext/runtest.rb | 11 ++ extconf.rb | 24 ++-- mmap.c | 37 ++++-- test/mmapt.rb | 380 +++++++++++++++++++++++++++++++++++++++++++++++++++++ test/runit_.rb | 45 +++++++ tests/mmapt.rb | 362 --------------------------------------------------- tests/runit_.rb | 44 ------- 12 files changed, 979 insertions(+), 934 deletions(-) mode change 100755 => 100644 b.rb create mode 100755 doc/b.rb create mode 100644 doc/mmap.rb delete mode 100755 docs/b.rb delete mode 100644 docs/mmap.rb create mode 100644 ext/runtest.rb create mode 100644 test/mmapt.rb create mode 100644 test/runit_.rb delete mode 100644 tests/mmapt.rb delete mode 100644 tests/runit_.rb diff --git a/b.rb b/b.rb old mode 100755 new mode 100644 diff --git a/doc/b.rb b/doc/b.rb new file mode 100755 index 0000000..0839bb3 --- /dev/null +++ b/doc/b.rb @@ -0,0 +1,109 @@ +#!/usr/bin/ruby + +def yield_or_not(primary) + text = primary.sub(/\{\s*\|([^|]+)\|[^}]*\}/, '') + if text != primary + "def #{text}yield #$1\nend" + else + "def #{text}end" + end +end + +def normalize(text) + norm = text.gsub(/\(\(\|([^|]+)\|\)\)/, '\\1') + norm.gsub!(/\(\(\{/, '') + norm.gsub!(/\}\)\)/, '') + norm.gsub!(/\(\(<([^|>]+)[^>]*>\)\)/, '\\1') + norm.gsub!(/^\s*:\s/, ' * ') + norm +end + +def intern_def(text, names, fout) + fout.puts "##{normalize(text.join('#'))}" + fout.puts yield_or_not(names[0]) + if names.size > 1 + n = names[0].chomp.sub(/\(.*/, '') + names[1 .. -1].each do |na| + nd = na.chomp.sub(/\(.*/, '') + if nd != n + fout.puts "#same than #{n}" + fout.puts yield_or_not(na) + end + end + end +end + +def output_def(text, names, keywords, fout) + if ! names.empty? + keywords.each do |k| + fout.puts k + intern_def(text, names, fout) + fout.puts "end" if k != "" + end + end +end + +def loop_file(file, fout) + text, keywords, names = [], [""], [] + comment = false + rep, indent, vide = '', -1, nil + IO.foreach(file) do |line| + if /^#\^/ =~ line + comment = ! comment + next + end + if comment + fout.puts "# #{normalize(line)}" + next + end + case line + when /^\s*$/ + vide = true + text.push line + when /^#\^/ + comment = ! comment + when /^##/ + line[0] = ?\s + fout.puts line + when /^#\s*(.+?)\s*$/ + keyword = $1 + output_def(text, names, keywords, fout) + text, names = [], [] + keywords = keyword.split(/\s*##\s*/) + if keywords.size == 1 + fout.puts keywords[0] + keywords = [""] + end + when /^#/ + when /^---/ + name = $' + if vide + output_def(text, names, keywords, fout) + text, names = [], [] + rep, indent, vide = '', -1, false + end + names.push name + else + vide = false + if line.sub!(/^(\s*): /, '* ') + indent += ($1 <=> rep) + rep = $1 + else + line.sub!(/^#{rep}/, '') + end + if indent >= 0 + line = (' ' * indent) + line + else + line.sub!(/\A\s*/, '') + end + text.push line + end + end +end + +File.open("#{ARGV[0]}.rb", "w") do |fout| + loop_file("../#{ARGV[0]}.rd", fout) + Dir['*.rd'].each do |file| + loop_file(file, fout) + end +end diff --git a/doc/mmap.rb b/doc/mmap.rb new file mode 100644 index 0000000..2c585a0 --- /dev/null +++ b/doc/mmap.rb @@ -0,0 +1,396 @@ +# The Mmap class implement memory-mapped file objects +# +# Most of these methods have the same syntax than the methods of String +# +# === WARNING +# === The variables $' and $` are not available with gsub! and sub! +class Mmap + include Comparable + include Enumerable + class << self + + #disable paging of all pages mapped. flag can be + #Mmap::MCL_CURRENT or Mmap::MCL_FUTURE + # + def lockall(flag) + end + + #create a new Mmap object + # + #* file + # + # Pathname of the file, if nil is given an anonymous map + # is created Mmanp::MAP_ANON + # + #* mode + # + # Mode to open the file, it can be "r", "w", "rw", "a" + # + #* protection + # + # specify the nature of the mapping + # + # * Mmap::MAP_SHARED + # + # Creates a mapping that's shared with all other processes + # mapping the same areas of the file. + # The default value is Mmap::MAP_SHARED + # + # * Mmap::MAP_PRIVATE + # + # Creates a private copy-on-write mapping, so changes to the + # contents of the mmap object will be private to this process + # + #* options + # + # Hash. If one of the options length or offset + # is specified it will not possible to modify the size of + # the mapped file. + # + # length:: maps length bytes from the file + # + # offset:: the mapping begin at offset + # + # advice:: the type of the access (see #madvise) + # + # + def new(file, mode = "r", protection = Mmap::MAP_SHARED, options = {}) + end + + #reenable paging + # + def unlockall + end + end + + #add count bytes to the file (i.e. pre-extend the file) + # + def extend(count) + end + + #advice can have the value Mmap::MADV_NORMAL, + #Mmap::MADV_RANDOM, Mmap::MADV_SEQUENTIAL, + #Mmap::MADV_WILLNEED, Mmap::MADV_DONTNEED + # + def madvise(advice) + end + + #change the mode, value must be "r", "w" or "rw" + # + def mprotect(mode) + end + + #disable paging + # + def mlock + end + + #flush the file + # + def msync + end + #same than msync + def flush + end + + #reenable paging + # + def munlock + end + + #terminate the association + def munmap + end + # + #=== Other methods with the same syntax than for the class String + # + # + + #comparison + # + def ==(other) + end + + #comparison + # + def >(other) + end + + #comparison + # + def >=(other) + end + + #comparison + # + def <(other) + end + + #comparison + # + def <=(other) + end + + #used for case comparison + # + def ===(other) + end + + #append other to self + # + def <<(other) + end + + #return an index of the match + # + def =~(other) + end + + #Element reference - with the following syntax + # + #self[nth] + # + #retrieve the nth character + # + #self[start..last] + # + #return a substring from start to last + # + #self[start, length] + # + #return a substring of lenght characters from start + # + def [](args) + end + + + # Element assignement - with the following syntax + # + # self[nth] = val + # + # change the nth character with val + # + # self[start..last] = val + # + # change substring from start to last with val + # + # self[start, len] = val + # + # replace length characters from start with val. + # + def []=(args) + end + + #comparison : return -1, 0, 1 + # + def self <=> other + end + + # only with ruby >= 1.7.1 + def casecmp(other) + end + + #append the contents of other + # + def concat(other) + end + + #change the first character to uppercase letter + # + def capitalize! + end + + #chop off the last character + # + def chop! + end + + #chop off the line ending character, specified by rs + # + def chomp!(rs = $/) + end + + #each parameter defines a set of character to count + # + def count(o1 [, o2, ...]) + end + + #crypt with salt + # + def crypt(salt) + end + + #delete every characters included in str + # + def delete!(str) + end + + #change all uppercase character to lowercase character + # + def downcase! + end + + #iterate on each byte + # + def each_byte + yield char + end + + #iterate on each line + # + def each(rs = $/) + yield line + end + #same than each + def each_line(rs = $/) + yield line + end + + #return true if the file is empty + # + def empty? + end + + #freeze the current file + # + def freeze + end + + #return true if the file is frozen + # + def frozen + end + + #global substitution + # + #str.gsub!(pattern, replacement) => str or nil + # + #str.gsub!(pattern) {|match| block } => str or nil + # + def gsub!(pattern, replacement = nil) + end + + #return true if other is found + # + def include?(other) + end + + #return the index of substr + # + def index(substr[, pos]) + end + + #insert str at index + # + def insert(index, str) >= 1.7.1 + end + + #return the size of the file + # + def length + end + + #convert pattern to a Regexp and then call + #match on self + def match(pattern) + end + + #reverse the content of the file + # + def reverse! + end + + #return the index of the last occurrence of substr + # + def rindex(substr[, pos]) + end + + #return an array of all occurence matched by pattern + # + def scan(pattern) + end + + #iterate through the file, matching the pattern + # + def scan(pattern) + yield str + end + + #return the size of the file + # + def size + end + + #same than [] + # + def slice + end + + #delete the specified portion of the file + # + def slice! + end + + #splits into a list of strings and return this array + # + def split([sep[, limit]]) + end + + #squeezes sequences of the same characters which is included in str + # + def squeeze!([str]) + end + + #removes leading and trailing whitespace + # + def strip! + end + + #removes leading whitespace + # + def lstrip! + end + + #removes trailing whitespace + # + def rstrip! + end + + #substitution + # + #str.sub!(pattern, replacement) => str or nil + # + #str.sub!(pattern) {|match| block } => str or nil + # + # + def sub!(pattern, replacement = nil) + end + + #return a checksum + # + def sum(bits = 16) + end + + #replaces all lowercase characters to uppercase characters, and vice-versa + # + def swapcase! + end + + #translate the character from search to replace + # + def tr!(search, replace) + end + + #translate the character from search to replace, then + #squeeze sequence of the same characters + # + def tr_s!(search, replace) + end + + #replaces all lowercase characters to downcase characters + # + def upcase! + end + + +end diff --git a/docs/b.rb b/docs/b.rb deleted file mode 100755 index 0839bb3..0000000 --- a/docs/b.rb +++ /dev/null @@ -1,109 +0,0 @@ -#!/usr/bin/ruby - -def yield_or_not(primary) - text = primary.sub(/\{\s*\|([^|]+)\|[^}]*\}/, '') - if text != primary - "def #{text}yield #$1\nend" - else - "def #{text}end" - end -end - -def normalize(text) - norm = text.gsub(/\(\(\|([^|]+)\|\)\)/, '\\1') - norm.gsub!(/\(\(\{/, '') - norm.gsub!(/\}\)\)/, '') - norm.gsub!(/\(\(<([^|>]+)[^>]*>\)\)/, '\\1') - norm.gsub!(/^\s*:\s/, ' * ') - norm -end - -def intern_def(text, names, fout) - fout.puts "##{normalize(text.join('#'))}" - fout.puts yield_or_not(names[0]) - if names.size > 1 - n = names[0].chomp.sub(/\(.*/, '') - names[1 .. -1].each do |na| - nd = na.chomp.sub(/\(.*/, '') - if nd != n - fout.puts "#same than #{n}" - fout.puts yield_or_not(na) - end - end - end -end - -def output_def(text, names, keywords, fout) - if ! names.empty? - keywords.each do |k| - fout.puts k - intern_def(text, names, fout) - fout.puts "end" if k != "" - end - end -end - -def loop_file(file, fout) - text, keywords, names = [], [""], [] - comment = false - rep, indent, vide = '', -1, nil - IO.foreach(file) do |line| - if /^#\^/ =~ line - comment = ! comment - next - end - if comment - fout.puts "# #{normalize(line)}" - next - end - case line - when /^\s*$/ - vide = true - text.push line - when /^#\^/ - comment = ! comment - when /^##/ - line[0] = ?\s - fout.puts line - when /^#\s*(.+?)\s*$/ - keyword = $1 - output_def(text, names, keywords, fout) - text, names = [], [] - keywords = keyword.split(/\s*##\s*/) - if keywords.size == 1 - fout.puts keywords[0] - keywords = [""] - end - when /^#/ - when /^---/ - name = $' - if vide - output_def(text, names, keywords, fout) - text, names = [], [] - rep, indent, vide = '', -1, false - end - names.push name - else - vide = false - if line.sub!(/^(\s*): /, '* ') - indent += ($1 <=> rep) - rep = $1 - else - line.sub!(/^#{rep}/, '') - end - if indent >= 0 - line = (' ' * indent) + line - else - line.sub!(/\A\s*/, '') - end - text.push line - end - end -end - -File.open("#{ARGV[0]}.rb", "w") do |fout| - loop_file("../#{ARGV[0]}.rd", fout) - Dir['*.rd'].each do |file| - loop_file(file, fout) - end -end diff --git a/docs/mmap.rb b/docs/mmap.rb deleted file mode 100644 index 2c585a0..0000000 --- a/docs/mmap.rb +++ /dev/null @@ -1,396 +0,0 @@ -# The Mmap class implement memory-mapped file objects -# -# Most of these methods have the same syntax than the methods of String -# -# === WARNING -# === The variables $' and $` are not available with gsub! and sub! -class Mmap - include Comparable - include Enumerable - class << self - - #disable paging of all pages mapped. flag can be - #Mmap::MCL_CURRENT or Mmap::MCL_FUTURE - # - def lockall(flag) - end - - #create a new Mmap object - # - #* file - # - # Pathname of the file, if nil is given an anonymous map - # is created Mmanp::MAP_ANON - # - #* mode - # - # Mode to open the file, it can be "r", "w", "rw", "a" - # - #* protection - # - # specify the nature of the mapping - # - # * Mmap::MAP_SHARED - # - # Creates a mapping that's shared with all other processes - # mapping the same areas of the file. - # The default value is Mmap::MAP_SHARED - # - # * Mmap::MAP_PRIVATE - # - # Creates a private copy-on-write mapping, so changes to the - # contents of the mmap object will be private to this process - # - #* options - # - # Hash. If one of the options length or offset - # is specified it will not possible to modify the size of - # the mapped file. - # - # length:: maps length bytes from the file - # - # offset:: the mapping begin at offset - # - # advice:: the type of the access (see #madvise) - # - # - def new(file, mode = "r", protection = Mmap::MAP_SHARED, options = {}) - end - - #reenable paging - # - def unlockall - end - end - - #add count bytes to the file (i.e. pre-extend the file) - # - def extend(count) - end - - #advice can have the value Mmap::MADV_NORMAL, - #Mmap::MADV_RANDOM, Mmap::MADV_SEQUENTIAL, - #Mmap::MADV_WILLNEED, Mmap::MADV_DONTNEED - # - def madvise(advice) - end - - #change the mode, value must be "r", "w" or "rw" - # - def mprotect(mode) - end - - #disable paging - # - def mlock - end - - #flush the file - # - def msync - end - #same than msync - def flush - end - - #reenable paging - # - def munlock - end - - #terminate the association - def munmap - end - # - #=== Other methods with the same syntax than for the class String - # - # - - #comparison - # - def ==(other) - end - - #comparison - # - def >(other) - end - - #comparison - # - def >=(other) - end - - #comparison - # - def <(other) - end - - #comparison - # - def <=(other) - end - - #used for case comparison - # - def ===(other) - end - - #append other to self - # - def <<(other) - end - - #return an index of the match - # - def =~(other) - end - - #Element reference - with the following syntax - # - #self[nth] - # - #retrieve the nth character - # - #self[start..last] - # - #return a substring from start to last - # - #self[start, length] - # - #return a substring of lenght characters from start - # - def [](args) - end - - - # Element assignement - with the following syntax - # - # self[nth] = val - # - # change the nth character with val - # - # self[start..last] = val - # - # change substring from start to last with val - # - # self[start, len] = val - # - # replace length characters from start with val. - # - def []=(args) - end - - #comparison : return -1, 0, 1 - # - def self <=> other - end - - # only with ruby >= 1.7.1 - def casecmp(other) - end - - #append the contents of other - # - def concat(other) - end - - #change the first character to uppercase letter - # - def capitalize! - end - - #chop off the last character - # - def chop! - end - - #chop off the line ending character, specified by rs - # - def chomp!(rs = $/) - end - - #each parameter defines a set of character to count - # - def count(o1 [, o2, ...]) - end - - #crypt with salt - # - def crypt(salt) - end - - #delete every characters included in str - # - def delete!(str) - end - - #change all uppercase character to lowercase character - # - def downcase! - end - - #iterate on each byte - # - def each_byte - yield char - end - - #iterate on each line - # - def each(rs = $/) - yield line - end - #same than each - def each_line(rs = $/) - yield line - end - - #return true if the file is empty - # - def empty? - end - - #freeze the current file - # - def freeze - end - - #return true if the file is frozen - # - def frozen - end - - #global substitution - # - #str.gsub!(pattern, replacement) => str or nil - # - #str.gsub!(pattern) {|match| block } => str or nil - # - def gsub!(pattern, replacement = nil) - end - - #return true if other is found - # - def include?(other) - end - - #return the index of substr - # - def index(substr[, pos]) - end - - #insert str at index - # - def insert(index, str) >= 1.7.1 - end - - #return the size of the file - # - def length - end - - #convert pattern to a Regexp and then call - #match on self - def match(pattern) - end - - #reverse the content of the file - # - def reverse! - end - - #return the index of the last occurrence of substr - # - def rindex(substr[, pos]) - end - - #return an array of all occurence matched by pattern - # - def scan(pattern) - end - - #iterate through the file, matching the pattern - # - def scan(pattern) - yield str - end - - #return the size of the file - # - def size - end - - #same than [] - # - def slice - end - - #delete the specified portion of the file - # - def slice! - end - - #splits into a list of strings and return this array - # - def split([sep[, limit]]) - end - - #squeezes sequences of the same characters which is included in str - # - def squeeze!([str]) - end - - #removes leading and trailing whitespace - # - def strip! - end - - #removes leading whitespace - # - def lstrip! - end - - #removes trailing whitespace - # - def rstrip! - end - - #substitution - # - #str.sub!(pattern, replacement) => str or nil - # - #str.sub!(pattern) {|match| block } => str or nil - # - # - def sub!(pattern, replacement = nil) - end - - #return a checksum - # - def sum(bits = 16) - end - - #replaces all lowercase characters to uppercase characters, and vice-versa - # - def swapcase! - end - - #translate the character from search to replace - # - def tr!(search, replace) - end - - #translate the character from search to replace, then - #squeeze sequence of the same characters - # - def tr_s!(search, replace) - end - - #replaces all lowercase characters to downcase characters - # - def upcase! - end - - -end diff --git a/ext/runtest.rb b/ext/runtest.rb new file mode 100644 index 0000000..5ad822c --- /dev/null +++ b/ext/runtest.rb @@ -0,0 +1,11 @@ +#!/usr/bin/ruby +#-- +# don't use it's for gem +$LOAD_PATH.each do |p| + if p.gsub!(%r{/ext\Z}, '/test') && + File.exists?(p + '/mmapt.rb') + load(p + '/mmapt.rb') + break + end +end + diff --git a/extconf.rb b/extconf.rb index 1a5f9ef..d7a7693 100644 --- a/extconf.rb +++ b/extconf.rb @@ -26,10 +26,10 @@ create_makefile "mmap" begin make = open("Makefile", "a") make.puts "\ntest: $(DLLIB)" - Dir.foreach('tests') do |x| + Dir.foreach('test') do |x| next if /^\./ =~ x || /(_\.rb|~)$/ =~ x next if FileTest.directory?(x) - make.print "\truby tests/#{x}\n" + make.print "\truby test/#{x}\n" end if unknown make.print <<-EOT @@ -47,22 +47,22 @@ EOT EOT make.print "HTML = mmap.html" - docs = Dir['docs/*.rd'] - docs.each {|x| make.print " \\\n\t#{x.sub(/\.rd$/, '.html')}" } - make.print "\n\nRDOC = docs/mmap.rb" + doc = Dir['doc/*.rd'] + doc.each {|x| make.print " \\\n\t#{x.sub(/\.rd$/, '.html')}" } + make.print "\n\nRDOC = doc/mmap.rb" make.puts make.print <<-EOF -rdoc: docs/doc/index.html +rdoc: doc/doc/index.html -docs/doc/index.html: $(RDOC) -\t@-(cd docs; rdoc mmap.rb) +doc/doc/index.html: $(RDOC) +\t@-(cd doc; rdoc mmap.rb) -ri: docs/mmap.rb -\t@-(cd docs; rdoc -r mmap.rb) +ri: doc/mmap.rb +\t@-(cd doc; rdoc -r mmap.rb) -ri-site: docs/mmap.rb -\t@-(cd docs; rdoc -R mmap.rb) +ri-site: doc/mmap.rb +\t@-(cd doc; rdoc -R mmap.rb) rd2: html diff --git a/mmap.c b/mmap.c index 70fe833..712b5dd 100644 --- a/mmap.c +++ b/mmap.c @@ -273,7 +273,7 @@ mm_extend(obj, a) if (len > 0) { mm_expandf(t_mm, t_mm->len + len); } - return INT2NUM(t_mm->len); + return UINT2NUM(t_mm->len); } static VALUE @@ -290,7 +290,7 @@ mm_i_options(arg, obj) key = rb_obj_as_string(key); options = StringValuePtr(key); if (strcmp(options, "length") == 0) { - t_mm->len = NUM2INT(value); + t_mm->len = NUM2UINT(value); if (t_mm->len <= 0) { rb_raise(rb_eArgError, "Invalid value for length %d", t_mm->len); } @@ -406,8 +406,15 @@ mm_init(argc, argv, obj) if (NIL_P(vmode)) { mode = "r"; } - else if (TYPE(vmode) == T_ARRAY && RARRAY(vmode)->len >= 2) { - VALUE tmp = RARRAY(vmode)->ptr[0]; + else if (rb_respond_to(vmode, rb_intern("to_ary"))) { + VALUE tmp; + + vmode = rb_convert_type(vmode, T_ARRAY, "Array", "to_ary"); + if (RARRAY(vmode)->len != 2) { + rb_raise(rb_eArgError, "Invalid length %d (expected 2)", + RARRAY(vmode)->len); + } + tmp = RARRAY(vmode)->ptr[0]; mode = StringValuePtr(tmp); perm = NUM2INT(RARRAY(vmode)->ptr[1]); } @@ -420,10 +427,7 @@ mm_init(argc, argv, obj) } else if (strcmp(mode, "w") == 0) { smode = O_RDWR | O_TRUNC; - pmode = PROT_WRITE; - if (!NIL_P(fdv)) { - pmode |= PROT_READ; - } + pmode = PROT_READ | PROT_WRITE; } else if (strcmp(mode, "rw") == 0 || strcmp(mode, "wr") == 0) { smode = O_RDWR; @@ -1281,11 +1285,16 @@ mm_equal(a, b) VALUE a, b; { VALUE result; - + mm_mmap *t_mm, *u_mm; + if (a == b) return Qtrue; if (TYPE(b) != T_DATA || RDATA(b)->dfree != (RUBY_DATA_FUNC)mm_free) return Qfalse; + GetMmap(a, t_mm, 0); + GetMmap(b, u_mm, 0); + if (t_mm->real != u_mm->real) + return Qfalse; a = mm_str(a, MM_ORIGIN); b = mm_str(b, MM_ORIGIN); result = rb_funcall2(a, rb_intern("=="), 1, &b); @@ -1299,11 +1308,16 @@ mm_eql(a, b) VALUE a, b; { VALUE result; + mm_mmap *t_mm, *u_mm; if (a == b) return Qtrue; if (TYPE(b) != T_DATA || RDATA(b)->dfree != (RUBY_DATA_FUNC)mm_free) return Qfalse; + GetMmap(a, t_mm, 0); + GetMmap(b, u_mm, 0); + if (t_mm->real != u_mm->real) + return Qfalse; a = mm_str(a, MM_ORIGIN); b = mm_str(b, MM_ORIGIN); result = rb_funcall2(a, rb_intern("eql?"), 1, &b); @@ -1332,7 +1346,7 @@ mm_size(a) mm_mmap *t_mm; GetMmap(a, t_mm, 0); - return INT2NUM(t_mm->real); + return UINT2NUM(t_mm->real); } static VALUE @@ -1722,9 +1736,10 @@ Init_mmap() #ifdef MAP_NOSYNC rb_define_const(mm_cMap, "MAP_NOSYNC", INT2FIX(MAP_NOSYNC)); #endif +#ifdef MCL_CURRENT rb_define_const(mm_cMap, "MCL_CURRENT", INT2FIX(MCL_CURRENT)); rb_define_const(mm_cMap, "MCL_FUTURE", INT2FIX(MCL_FUTURE)); - +#endif rb_include_module(mm_cMap, rb_mComparable); rb_include_module(mm_cMap, rb_mEnumerable); diff --git a/test/mmapt.rb b/test/mmapt.rb new file mode 100644 index 0000000..80c64c2 --- /dev/null +++ b/test/mmapt.rb @@ -0,0 +1,380 @@ +#!/usr/bin/ruby +$LOAD_PATH.unshift *%w{.. . test} +require 'mmap' +require 'ftools' + +$pathmm = $LOAD_PATH.find {|p| File.exist?(p + "/mmap.c") } +unless $pathmm + $LOAD_PATH.each do |p| + p p + if p.gsub!(%r{/test\Z}, '/') && + File.exists?(p + '/mmap.c') + $pathmm = p + break + end + end +end +raise "unable to find mmap.c" unless $pathmm + +load "#{$pathmm}/test/runit_.rb" + +$mmap, $str = nil, nil + +Inh = defined?(RUNIT) ? RUNIT : Test::Unit + +$pathmm = $LOAD_PATH.find {|p| File.exist?(p + "/mmap.c") } +raise "unable to find mmap.c" unless $pathmm + +Dir.mkdir("#{$pathmm}/tmp") unless FileTest.directory?("#{$pathmm}/tmp") + +Dir["#{$pathmm}/tmp/*"].each do |f| + File.unlink(f) if FileTest.file?(f) +end + +class TestMmap < Inh::TestCase + + def internal_read + File.readlines("#{$pathmm}/tmp/mmap", nil)[0] + end + + def internal_init(io = false) + $mmap.unmap if $mmap + file = "#{$pathmm}/mmap.c" + File.syscopy file, "#{$pathmm}/tmp/mmap" + $str = internal_read + if io + assert_kind_of(Mmap, $mmap = Mmap.new(File.new("#{$pathmm}/tmp/mmap", "r+"), "rw"), "") + else + assert_kind_of(Mmap, $mmap = Mmap.new("#{$pathmm}/tmp/mmap", "rw"), "") + end + end + + def test_00_init + internal_init + assert_equal($mmap.length, $str.length, "") + end + + def test_01_aref + max = $str.size * 2 + 72.times do + ran1 = rand(max) + assert_equal($mmap[ran1], $str[ran1], ""); + assert_equal($mmap[-ran1], $str[-ran1], ""); + ran2 = rand(max) + assert_equal($mmap[ran1, ran2], $str[ran1, ran2], ""); + assert_equal($mmap[-ran1, ran2], $str[-ran1, ran2], ""); + assert_equal($mmap[ran1, -ran2], $str[ran1, -ran2], ""); + assert_equal($mmap[-ran1, -ran2], $str[-ran1, -ran2], ""); + assert_equal($mmap[ran1 .. ran2], $str[ran1 .. ran2], ""); + assert_equal($mmap[-ran1 .. ran2], $str[-ran1 .. ran2], ""); + assert_equal($mmap[ran1 .. -ran2], $str[ran1 .. -ran2], ""); + assert_equal($mmap[-ran1 .. -ran2], $str[-ran1 .. -ran2], ""); + end + assert_equal($mmap[/random/], $str[/random/], "") + assert_equal($mmap[/real/], $str[/real/], "") + assert_equal($mmap[/none/], $str[/none/], "") + end + + def internal_aset(a, b = nil, c = true) + access = if b + repl = '' + rand(12).times do + repl << (65 + rand(25)) + end + if c + "[a, b] = '#{repl}'" + else + "[a .. b] = '#{repl}'" + end + else + "[a] = #{(65 + rand(25))}" + end + begin + eval "$str#{access}" + rescue IndexError, RangeError + begin + eval "$mmap#{access}" + rescue IndexError, RangeError + else + flunk("*must* fail with IndexError") + end + else + eval "$mmap#{access}" + end + assert_equal($mmap.to_str, $str, "") + end + + def test_02_aset + $mmap[/...../] = "change it" + $str[/...../] = "change it" + assert_equal($mmap.to_str, $str, "aset regexp") + $mmap["ge i"] = "change it" + $str["ge i"] = "change it" + assert_equal($mmap.to_str, $str, "aset regexp") + max = $str.size * 2 + 72.times do + ran1 = rand(max) + internal_aset(ran1) + internal_aset(-ran1) + ran2 = rand(max) + internal_aset(ran1, ran2) + internal_aset(ran1, -ran2) + internal_aset(-ran1, ran2) + internal_aset(-ran1, -ran2) + internal_aset(ran1, ran2, false) + internal_aset(ran1, -ran2, false) + internal_aset(-ran1, ran2, false) + internal_aset(-ran1, -ran2, false) + end + internal_init + end + + def internal_slice(a, b = nil, c = true) + access = if b + if c + ".slice!(a, b)" + else + ".slice!(a .. b)" + end + else + ".slice!(a)" + end + begin + eval "$str#{access}" + rescue IndexError, RangeError + begin + eval "$mmap#{access}" + rescue IndexError, RangeError + else + flunk("*must* fail with IndexError") + end + else + eval "$mmap#{access}" + end + assert_equal($mmap.to_str, $str, "") + end + + def test_03_slice + max = $str.size * 2 + 72.times do + ran1 = rand(max) + internal_slice(ran1) + internal_slice(-ran1) + ran2 = rand(max) + internal_slice(ran1, ran2) + internal_slice(ran1, -ran2) + internal_slice(-ran1, ran2) + internal_slice(-ran1, -ran2) + internal_slice(ran1, ran2, false) + internal_slice(ran1, -ran2, false) + internal_slice(-ran1, ran2, false) + internal_slice(-ran1, -ran2, false) + end + internal_init + end + + def test_04_reg + assert_equal($mmap.scan(/include/), $str.scan(/include/), "") + assert_equal($mmap.index("rb_raise"), $str.index("rb_raise"), "") + assert_equal($mmap.rindex("rb_raise"), $str.rindex("rb_raise"), "") + assert_equal($mmap.index(/rb_raise/), $str.index(/rb_raise/), "") + assert_equal($mmap.rindex(/rb_raise/), $str.rindex(/rb_raise/), "") + ('a' .. 'z').each do |i| + assert_equal($mmap.index(i), $str.index(i), "") + assert_equal($mmap.rindex(i), $str.rindex(i), "") + assert_equal($mmap.index(i), $str.index(/#{i}/), "") + assert_equal($mmap.rindex(i), $str.rindex(/#{i}/), "") + end + $mmap.sub!(/GetMmap/, 'XXXX'); $str.sub!(/GetMmap/, 'XXXX') + assert_equal($mmap.to_str, $str, "") + $mmap.gsub!(/GetMmap/, 'XXXX'); $str.gsub!(/GetMmap/, 'XXXX') + assert_equal($mmap.to_str, $str, "") + $mmap.gsub!(/YYYY/, 'XXXX'); $str.gsub!(/YYYY/, 'XXXX') + assert_equal($mmap.to_str, $str, "") + assert_equal($mmap.split(/\w+/), $str.split(/\w+/), "") + assert_equal($mmap.split(/\W+/), $str.split(/\W+/), "") + assert_equal($mmap.crypt("abc"), $str.crypt("abc"), "") + internal_init + end + + def internal_modify idmod, *args + if res = $str.method(idmod)[*args] + assert_equal($mmap.method(idmod)[*args].to_str, res, "<#{idmod}>") + else + assert_equal($mmap.method(idmod)[*args], res, "<#{idmod}>") + end + end + + def test_05_modify + internal_modify(:reverse!) + internal_modify(:upcase!) + internal_modify(:downcase!) + internal_modify(:capitalize!) + internal_modify(:swapcase!) + internal_modify(:strip!) + internal_modify(:chop!) + internal_modify(:chomp!) + internal_modify(:squeeze!) + internal_modify(:tr!, 'abcdefghi', '123456789') + internal_modify(:tr_s!, 'jklmnopqr', '123456789') + internal_modify(:delete!, 'A-Z') + end + + def test_06_iterate + internal_init + mmap = []; $mmap.each {|l| mmap << l} + str = []; $str.each {|l| str << l} + assert_equal(mmap, str, "") + mmap = []; $mmap.each_byte {|l| mmap << l} + str = []; $str.each_byte {|l| str << l} + assert_equal(mmap, str, "") + end + + def test_07_concat + internal_init + [$mmap, $str].each {|l| l << "bc"; l << 12; l << "ab"} + assert_equal($mmap.to_str, $str, "<<") + assert_raises(TypeError) { $mmap << 456 } + end + + def test_08_extend + $mmap.extend(4096) + assert_equal($mmap.to_str, $str, "extend") + if $str.respond_to?(:insert) + 10.times do + pos = rand($mmap.size) + str = "XX" * rand(66) + $str.insert(pos, str) + $mmap.insert(pos, str) + assert_equal($mmap.to_str, $str, "insert") + end + end + end + + def test_09_msync + 3.times do |i| + [$mmap, $str].each {|l| l << "x" * 4096 } + str = internal_read + if str != $mmap.to_str + $mmap.msync + assert_equal($mmap.to_str, internal_read, "msync") + break + end + end + end + + def test_10_protect + assert_equal($mmap, $mmap.protect("w"), "protect") + assert_equal("a", $mmap[12] = "a", "affect") + $str[12] = "a" + assert_equal($mmap.to_str, $str, "protect") + assert_raises(TypeError) { $mmap << "a" } + assert_equal($mmap, $mmap.protect("r"), "protect") + assert_raises(TypeError) { $mmap[12] = "a" } + assert_raises(TypeError) { $mmap.protect("rw") } + end + + def test_11_anonymous + if defined?(Mmap::MAP_ANONYMOUS) + assert_kind_of(Mmap, $mmap = + Mmap.new(nil, "length" => 8192, "offset" => 12, + "increment" => 1024, "initialize" => " ")) + $str = " " * 8192 + 1024.times do + pos = rand(8192) + $mmap[pos] = $str[pos] = 32 + rand(64) + end + assert_equal($mmap.to_str, $str, "insert anonymous") + assert_raises(IndexError) { $mmap[12345] = "a" } + assert_raises(TypeError) { $mmap << "a" } + end + end + + def test_12_fileno + internal_init(true) + test_01_aref + $mmap[12] = "3"; $str[12] = "3" + assert_equal($mmap.to_str, $str, "insert io") + assert_equal(0, $mmap <=> $str, "cmp") + assert_raises(TypeError) { $mmap[12] = "ab" } + $mmap.freeze + if $str.respond_to?(:match) + assert_equal($str.match("rb_match_busy").offset(0), + $mmap.match("rb_match_busy").offset(0), "match") + assert_equal($str.match(/rb_../).offset(0), + $mmap.match(/rb_../).offset(0), "match") + assert_equal($str.match("rb_match_buzy"), + $mmap.match("rb_match_buzy"), "no match") + assert_equal($str =~ /rb_match_busy/, + $mmap =~ /rb_match_busy/, "match") + assert_equal($str =~ /rb_match_buzy/, + $mmap =~ /rb_match_buzy/, "no match") + end + assert_raises(TypeError) { $mmap[12] = "a" } + end + + def test_13_div + string = "azertyuiopqsdfghjklm" + assert_kind_of(Mmap, m0 = Mmap.new("#{$pathmm}/tmp/aa", "a"), "new a") + File.open("#{$pathmm}/tmp/bb", "w") {|f| f.puts "aaa" } + assert_kind_of(Mmap, m1 = Mmap.new("#{$pathmm}/tmp/bb", "w"), "new a") + assert_equal(true, m0.empty?, "empty") + assert_equal(true, m1.empty?, "empty") + assert_equal(m0, m0 << string, "<<") + assert_equal(m1, m1 << string, "<<") + assert_equal(false, m0.empty?, "empty") + assert_equal(false, m1.empty?, "empty") + assert_equal(true, m0 == m1, "==") + if string.respond_to?(:casecmp) + assert_equal(0, m0.casecmp(string.upcase), "casecmp") + assert_equal(0, m0.casecmp(m1), "casecmp") + end + assert_equal(true, m0 === m1, "===") + assert_equal(false, m0 === string, "===") + assert_equal(true, m0.eql?(m1), ".eql?") + assert_equal(true, m1.eql?(m0), ".eql?") + assert_equal(false, m1.eql?(string), ".eql?") + assert_equal(m0.hash, m1.hash, "hash") + assert_equal(true, m0.include?("azert"), "include") + assert_equal(false, m1.include?("aqert"), "include") + i = 0 + m0.scan(/./) {|c| assert_equal(c, string[i,1], "scan"); i += 1} + assert_nil(m0.munmap, "munmap") + assert_nil(m1.munmap, "munmap") + end + + def test_14_other + if File.exist?("#{$pathmm}/tmp/aa") + string = "azertyuiopqsdfghjklm" + assert_kind_of(Mmap, m0 = Mmap.new("#{$pathmm}/tmp/aa", "r"), "new r") + assert_equal(string, m0.to_str, "content") + assert_raises(TypeError) { m0[0] = 12 } + assert_raises(TypeError) { m0 << 12 } + assert_nil(m0.munmap, "munmap") + if defined?(Mmap::MAP_ANONYMOUS) + assert_raises(ArgumentError) { Mmap.new(nil, "w") } + assert_kind_of(Mmap, m0 = Mmap.new(nil, 12), "new w") + assert_equal(false, m0.empty?, "empty") + assert_equal("a", m0[0] = "a", "set") + assert_raises(TypeError) { m0 << 12 } + if defined?(Mmap::MADV_DONTNEED) + assert_nil(m0.advise(Mmap::MADV_DONTNEED), "advise") + assert_equal("a", m0[0,1], "get") + end + assert_equal(m0, m0.sub!(/./) { "y" }, "sub") + assert_equal(m0, m0.gsub!(/./) { "x" }, "gsub") + assert_equal("x" * 12, m0.to_str, "retrieve") + assert_equal("ab", m0[1..2] = "ab", "range") + assert_raises(TypeError) { m0[1..2] = "abc" } + assert_raises(ArgumentError) { m0.lock } + assert_raises(ArgumentError) { Mmap::lockall(0) } + assert_nil(m0.munmap, "munmap") + end + end + end +end + +if defined?(RUNIT) + RUNIT::CUI::TestRunner.run(TestMmap.suite) +end + diff --git a/test/runit_.rb b/test/runit_.rb new file mode 100644 index 0000000..639c149 --- /dev/null +++ b/test/runit_.rb @@ -0,0 +1,45 @@ +#:nodoc:all +begin + require 'test/unit' +rescue LoadError + require 'runit/testcase' + require 'runit/cui/testrunner' + + module RUNIT + module Assert + def assert_raises(error, message = nil) + begin + yield + rescue error + assert(true, message) + rescue + assert_fail("must fail with #{error} : #{string}") + else + assert_fail("*must* fail : #{string}") + end + end + def flunk(message = "") + assert_fail(message) + end + end + end +end + + +if RUBY_VERSION > "1.7" + class Array + alias indices select + end + class Hash + alias indexes select + end + module BDB + class Common + alias indexes select + end + + class Recnum + alias indices select + end + end +end diff --git a/tests/mmapt.rb b/tests/mmapt.rb deleted file mode 100644 index 6b81505..0000000 --- a/tests/mmapt.rb +++ /dev/null @@ -1,362 +0,0 @@ -#!/usr/bin/ruby -$LOAD_PATH.unshift *%w{.. . tests} -require 'mmap' -require 'ftools' -require 'runit_' - -$mmap, $str = nil, nil - -Inh = defined?(RUNIT) ? RUNIT : Test::Unit - -Dir["tmp/*"].each do |f| - File.unlink(f) if FileTest.file?(f) -end - -class TestMmap < Inh::TestCase - - def internal_read - File.readlines("tmp/mmap", nil)[0] - end - - def internal_init(io = false) - $mmap.unmap if $mmap - file = "mmap.c" - file = "../mmap.c" unless File.exist? file - File.syscopy file, "tmp/mmap" - $str = internal_read - if io - assert_kind_of(Mmap, $mmap = Mmap.new(File.new("tmp/mmap", "r+"), "rw"), "") - else - assert_kind_of(Mmap, $mmap = Mmap.new("tmp/mmap", "rw"), "") - end - end - - def test_00_init - internal_init - assert_equal($mmap.length, $str.length, "") - end - - def test_01_aref - max = $str.size * 2 - 72.times do - ran1 = rand(max) - assert_equal($mmap[ran1], $str[ran1], ""); - assert_equal($mmap[-ran1], $str[-ran1], ""); - ran2 = rand(max) - assert_equal($mmap[ran1, ran2], $str[ran1, ran2], ""); - assert_equal($mmap[-ran1, ran2], $str[-ran1, ran2], ""); - assert_equal($mmap[ran1, -ran2], $str[ran1, -ran2], ""); - assert_equal($mmap[-ran1, -ran2], $str[-ran1, -ran2], ""); - assert_equal($mmap[ran1 .. ran2], $str[ran1 .. ran2], ""); - assert_equal($mmap[-ran1 .. ran2], $str[-ran1 .. ran2], ""); - assert_equal($mmap[ran1 .. -ran2], $str[ran1 .. -ran2], ""); - assert_equal($mmap[-ran1 .. -ran2], $str[-ran1 .. -ran2], ""); - end - assert_equal($mmap[/random/], $str[/random/], "") - assert_equal($mmap[/real/], $str[/real/], "") - assert_equal($mmap[/none/], $str[/none/], "") - end - - def internal_aset(a, b = nil, c = true) - access = if b - repl = '' - rand(12).times do - repl << (65 + rand(25)) - end - if c - "[a, b] = '#{repl}'" - else - "[a .. b] = '#{repl}'" - end - else - "[a] = #{(65 + rand(25))}" - end - begin - eval "$str#{access}" - rescue IndexError, RangeError - begin - eval "$mmap#{access}" - rescue IndexError, RangeError - else - flunk("*must* fail with IndexError") - end - else - eval "$mmap#{access}" - end - assert_equal($mmap.to_str, $str, "") - end - - def test_02_aset - $mmap[/...../] = "change it" - $str[/...../] = "change it" - assert_equal($mmap.to_str, $str, "aset regexp") - $mmap["ge i"] = "change it" - $str["ge i"] = "change it" - assert_equal($mmap.to_str, $str, "aset regexp") - max = $str.size * 2 - 72.times do - ran1 = rand(max) - internal_aset(ran1) - internal_aset(-ran1) - ran2 = rand(max) - internal_aset(ran1, ran2) - internal_aset(ran1, -ran2) - internal_aset(-ran1, ran2) - internal_aset(-ran1, -ran2) - internal_aset(ran1, ran2, false) - internal_aset(ran1, -ran2, false) - internal_aset(-ran1, ran2, false) - internal_aset(-ran1, -ran2, false) - end - internal_init - end - - def internal_slice(a, b = nil, c = true) - access = if b - if c - ".slice!(a, b)" - else - ".slice!(a .. b)" - end - else - ".slice!(a)" - end - begin - eval "$str#{access}" - rescue IndexError, RangeError - begin - eval "$mmap#{access}" - rescue IndexError, RangeError - else - flunk("*must* fail with IndexError") - end - else - eval "$mmap#{access}" - end - assert_equal($mmap.to_str, $str, "") - end - - def test_03_slice - max = $str.size * 2 - 72.times do - ran1 = rand(max) - internal_slice(ran1) - internal_slice(-ran1) - ran2 = rand(max) - internal_slice(ran1, ran2) - internal_slice(ran1, -ran2) - internal_slice(-ran1, ran2) - internal_slice(-ran1, -ran2) - internal_slice(ran1, ran2, false) - internal_slice(ran1, -ran2, false) - internal_slice(-ran1, ran2, false) - internal_slice(-ran1, -ran2, false) - end - internal_init - end - - def test_04_reg - assert_equal($mmap.scan(/include/), $str.scan(/include/), "") - assert_equal($mmap.index("rb_raise"), $str.index("rb_raise"), "") - assert_equal($mmap.rindex("rb_raise"), $str.rindex("rb_raise"), "") - assert_equal($mmap.index(/rb_raise/), $str.index(/rb_raise/), "") - assert_equal($mmap.rindex(/rb_raise/), $str.rindex(/rb_raise/), "") - ('a' .. 'z').each do |i| - assert_equal($mmap.index(i), $str.index(i), "") - assert_equal($mmap.rindex(i), $str.rindex(i), "") - assert_equal($mmap.index(i), $str.index(/#{i}/), "") - assert_equal($mmap.rindex(i), $str.rindex(/#{i}/), "") - end - $mmap.sub!(/GetMmap/, 'XXXX'); $str.sub!(/GetMmap/, 'XXXX') - assert_equal($mmap.to_str, $str, "") - $mmap.gsub!(/GetMmap/, 'XXXX'); $str.gsub!(/GetMmap/, 'XXXX') - assert_equal($mmap.to_str, $str, "") - $mmap.gsub!(/YYYY/, 'XXXX'); $str.gsub!(/YYYY/, 'XXXX') - assert_equal($mmap.to_str, $str, "") - assert_equal($mmap.split(/\w+/), $str.split(/\w+/), "") - assert_equal($mmap.split(/\W+/), $str.split(/\W+/), "") - assert_equal($mmap.crypt("abc"), $str.crypt("abc"), "") - internal_init - end - - def internal_modify idmod, *args - if res = $str.method(idmod)[*args] - assert_equal($mmap.method(idmod)[*args].to_str, res, "<#{idmod}>") - else - assert_equal($mmap.method(idmod)[*args], res, "<#{idmod}>") - end - end - - def test_05_modify - internal_modify(:reverse!) - internal_modify(:upcase!) - internal_modify(:downcase!) - internal_modify(:capitalize!) - internal_modify(:swapcase!) - internal_modify(:strip!) - internal_modify(:chop!) - internal_modify(:chomp!) - internal_modify(:squeeze!) - internal_modify(:tr!, 'abcdefghi', '123456789') - internal_modify(:tr_s!, 'jklmnopqr', '123456789') - internal_modify(:delete!, 'A-Z') - end - - def test_06_iterate - internal_init - mmap = []; $mmap.each {|l| mmap << l} - str = []; $str.each {|l| str << l} - assert_equal(mmap, str, "") - mmap = []; $mmap.each_byte {|l| mmap << l} - str = []; $str.each_byte {|l| str << l} - assert_equal(mmap, str, "") - end - - def test_07_concat - internal_init - [$mmap, $str].each {|l| l << "bc"; l << 12; l << "ab"} - assert_equal($mmap.to_str, $str, "<<") - assert_raises(TypeError) { $mmap << 456 } - end - - def test_08_extend - $mmap.extend(4096) - assert_equal($mmap.to_str, $str, "extend") - if $str.respond_to?(:insert) - 10.times do - pos = rand($mmap.size) - str = "XX" * rand(66) - $str.insert(pos, str) - $mmap.insert(pos, str) - assert_equal($mmap.to_str, $str, "insert") - end - end - end - - def test_09_msync - 3.times do |i| - [$mmap, $str].each {|l| l << "x" * 4096 } - str = internal_read - if str != $mmap.to_str - $mmap.msync - assert_equal($mmap.to_str, internal_read, "msync") - break - end - end - end - - def test_10_protect - assert_equal($mmap, $mmap.protect("w"), "protect") - assert_equal("a", $mmap[12] = "a", "affect") - $str[12] = "a" - assert_equal($mmap.to_str, $str, "protect") - assert_raises(TypeError) { $mmap << "a" } - assert_equal($mmap, $mmap.protect("r"), "protect") - assert_raises(TypeError) { $mmap[12] = "a" } - assert_raises(TypeError) { $mmap.protect("rw") } - end - - def test_11_anonymous - if defined?(Mmap::MAP_ANONYMOUS) - assert_kind_of(Mmap, $mmap = - Mmap.new(nil, "length" => 8192, "offset" => 12, - "increment" => 1024, "initialize" => " ")) - $str = " " * 8192 - 1024.times do - pos = rand(8192) - $mmap[pos] = $str[pos] = 32 + rand(64) - end - assert_equal($mmap.to_str, $str, "insert anonymous") - assert_raises(IndexError) { $mmap[12345] = "a" } - assert_raises(TypeError) { $mmap << "a" } - end - end - - def test_12_fileno - internal_init(true) - test_01_aref - $mmap[12] = "3"; $str[12] = "3" - assert_equal($mmap.to_str, $str, "insert io") - assert_equal(0, $mmap <=> $str, "cmp") - assert_raises(TypeError) { $mmap[12] = "ab" } - $mmap.freeze - if $str.respond_to?(:match) - assert_equal($str.match("rb_match_busy").offset(0), - $mmap.match("rb_match_busy").offset(0), "match") - assert_equal($str.match(/rb_../).offset(0), - $mmap.match(/rb_../).offset(0), "match") - assert_equal($str.match("rb_match_buzy"), - $mmap.match("rb_match_buzy"), "no match") - assert_equal($str =~ /rb_match_busy/, - $mmap =~ /rb_match_busy/, "match") - assert_equal($str =~ /rb_match_buzy/, - $mmap =~ /rb_match_buzy/, "no match") - end - assert_raises(TypeError) { $mmap[12] = "a" } - end - - def test_13_div - string = "azertyuiopqsdfghjklm" - assert_kind_of(Mmap, m0 = Mmap.new("tmp/aa", "a"), "new a") - File.open("tmp/bb", "w") {|f| f.puts "aaa" } - assert_kind_of(Mmap, m1 = Mmap.new("tmp/bb", "w"), "new a") - assert_equal(true, m0.empty?, "empty") - assert_equal(true, m1.empty?, "empty") - assert_equal(m0, m0 << string, "<<") - assert_equal(m1, m1 << string, "<<") - assert_equal(false, m0.empty?, "empty") - assert_equal(false, m1.empty?, "empty") - assert_equal(true, m0 == m1, "==") - if string.respond_to?(:casecmp) - assert_equal(0, m0.casecmp(string.upcase), "casecmp") - assert_equal(0, m0.casecmp(m1), "casecmp") - end - assert_equal(true, m0 === m1, "===") - assert_equal(false, m0 === string, "===") - assert_equal(true, m0.eql?(m1), ".eql?") - assert_equal(true, m1.eql?(m0), ".eql?") - assert_equal(false, m1.eql?(string), ".eql?") - assert_equal(m0.hash, m1.hash, "hash") - assert_equal(true, m0.include?("azert"), "include") - assert_equal(false, m1.include?("aqert"), "include") - i = 0 - m0.scan(/./) {|c| assert_equal(c, string[i,1], "scan"); i += 1} - assert_nil(m0.munmap, "munmap") - assert_nil(m1.munmap, "munmap") - end - - def test_14_other - if File.exist?("tmp/aa") - string = "azertyuiopqsdfghjklm" - assert_kind_of(Mmap, m0 = Mmap.new("tmp/aa", "r"), "new r") - assert_equal(string, m0.to_str, "content") - assert_raises(TypeError) { m0[0] = 12 } - assert_raises(TypeError) { m0 << 12 } - assert_nil(m0.munmap, "munmap") - if defined?(Mmap::MAP_ANONYMOUS) - assert_raises(ArgumentError) { Mmap.new(nil, "w") } - assert_kind_of(Mmap, m0 = Mmap.new(nil, 12), "new w") - assert_equal(false, m0.empty?, "empty") - assert_equal("a", m0[0] = "a", "set") - assert_raises(TypeError) { m0 << 12 } - if defined?(Mmap::MADV_DONTNEED) - assert_nil(m0.advise(Mmap::MADV_DONTNEED), "advise") - assert_equal("a", m0[0,1], "get") - end - assert_equal(m0, m0.sub!(/./) { "y" }, "sub") - assert_equal(m0, m0.gsub!(/./) { "x" }, "gsub") - assert_equal("x" * 12, m0.to_str, "retrieve") - assert_equal("ab", m0[1..2] = "ab", "range") - assert_raises(TypeError) { m0[1..2] = "abc" } - assert_raises(ArgumentError) { m0.lock } - assert_raises(ArgumentError) { Mmap::lockall(0) } - assert_nil(m0.munmap, "munmap") - end - end - end -end - -if defined?(RUNIT) - RUNIT::CUI::TestRunner.run(TestMmap.suite) -end - diff --git a/tests/runit_.rb b/tests/runit_.rb deleted file mode 100644 index 0479592..0000000 --- a/tests/runit_.rb +++ /dev/null @@ -1,44 +0,0 @@ -begin - require 'test/unit' -rescue LoadError - require 'runit/testcase' - require 'runit/cui/testrunner' - - module RUNIT - module Assert - def assert_raises(error, message = nil) - begin - yield - rescue error - assert(true, message) - rescue - assert_fail("must fail with #{error} : #{string}") - else - assert_fail("*must* fail : #{string}") - end - end - def flunk(message = "") - assert_fail(message) - end - end - end -end - - -if RUBY_VERSION > "1.7" - class Array - alias indices select - end - class Hash - alias indexes select - end - module BDB - class Common - alias indexes select - end - - class Recnum - alias indices select - end - end -end -- cgit v1.1-2-g2b99